
#include"input_dev/key_map.hpp"
int gamepad_map(const std::pair<std::string, float> data) {
     string code = data.first;
    float value = data.second;
    int key = KEY_STOP;
    if (code == "方向水平") {
        //cout << "方向水平:" << value << endl;
        if(value<-0.5)
            key = KEY_LEFT;
        else if(value>0.5)
            key = KEY_RIGHT;
    } else if (code == "方向垂直") {
        //cout << "方向垂直:" << value << endl;
        if(value<-0.5)
            key = KEY_BACKWARD;
        else if(value>0.5)
            key = KEY_FORWARD;
    } else if (code == "左杆水平") {
        //cout << "左杆水平:" << value << endl;
    } else if (code == "左杆垂直") {
        //cout << "左杆垂直:" << value << endl;
    } else if (code == "右杆水平") {
        //cout << "右杆水平:" << value << endl;
    } else if (code == "右杆垂直") {
        //cout << "右杆垂直:" << value << endl;
    } else if (code == "左触发器") {
        //cout << "左触发器:" << value << endl;
    } else if (code == "右触发器") {
        //cout << "右触发器:" << value << endl;
    }else if (code == "START") {
        //cout << "START:" << value << endl;
        if(value>0.5)
            key = KEY_ENABLE;
    } else if (code == "BACK") {
        //cout << "BACK:" << value << endl;
        if(value>0.5)
            key = KEY_DISABLE;
    }else if (code == "LB") {
        //cout << "LB:" << value << endl;
    } else if (code == "RB") {
        //cout << "RB:" << value << endl;
    } else if (code == "A") {
        //cout << "A:" << value << endl;
        if(value>0.5)
            key = KEY_DOWN;
    } else if (code == "B") {
        //cout << "B:" << value << endl;
        if(value>0.5)
            key = KEY_GRAB;
    } else if (code == "X") {
        //cout << "X:" << value << endl;
        if(value>0.5)
            key = KEY_RELEASE;
    } else if (code == "Y") {
        //cout << "Y:" << value << endl;
        if(value>0.5)
            key = KEY_UP;
    } else {
        cout << "未识别:" << code << endl;
        key = -1; // 表示未识别的键
    }
    return key;
}



// 去除空白字符
std::string trim(const std::string& str) {
    size_t start = str.find_first_not_of(" \t\n\r。");
    if (start == std::string::npos) return "";
    size_t end = str.find_last_not_of(" \t\n\r。");
    return str.substr(start, end - start + 1);
}

std::string removeBOM(const std::string& str) {
    if (str.length() >= 3 && 
        (unsigned char)str[0] == 0xEF && 
        (unsigned char)str[1] == 0xBB && 
        (unsigned char)str[2] == 0xBF) {
        return str.substr(3);
    }
    return str;
}
int mic_map(std::string data){
    if (data.length() >= 3) {
        std::string lastWord = data.substr(data.length() - 3);
        if (lastWord == "。") {
            data.erase(data.length() - 3);
        }
    }
    int key=KEY_STOP;
    //cout<<"语音识别:";
    if (data == "停止"){
        //cout<<"停止" <<endl;
        key=KEY_STOP;
    }else if(data=="前进"){
        //cout<<"前进"<<endl;
        key=KEY_FORWARD;
    }else if(data=="后退"){
        //cout <<"后退"<<endl;
        key=KEY_BACKWARD;
    }else if(data=="左转"){
        //cout<<"左转"<<endl;
        key=KEY_LEFT;
    }else if(data=="右转"){
        //cout<<"右转"<<endl;
        key=KEY_RIGHT;
    }else if(data=="上升"){
        //cout<<"上升"<<endl;
        key=KEY_UP;
    }else if(data=="下降"){
        //cout<<"下降"<<endl;
        key=KEY_DOWN;
    }else if(data=="抓取"){
        //cout<<"抓取"<<endl;
        key=KEY_GRAB;
    }else if(data=="松开"){
        //cout<<"松开"<<endl;
        key=KEY_RELEASE;
    }else if(data=="开机"){
        //cout<<"开机"<<endl;
        key=KEY_ENABLE;
    }else if(data=="停机"){
        //cout<<"停机"<<endl;
        key=KEY_DISABLE;
    }else{
        cout<<"未识别:"<<data<<endl;
        key=-1;
    }
    return key;
}
std::string key_str(int key){
    string str="";
    switch (key)
    {
    case KEY_STOP:
        str="KEY_STOP";
        break;
    case KEY_FORWARD:
        str="KEY_FORWARD"; 
        break;
    case KEY_BACKWARD:
        str="KEY_BACKWARD";
        break;  
    case KEY_LEFT:
        str="KEY_LEFT";
        break;
    case KEY_RIGHT:
        str="KEY_RIGHT";
        break;
    case KEY_UP:
        str="KEY_UP";
        break;
    case KEY_DOWN:
        str="KEY_DOWN";
        break;
    case KEY_GRAB:  
        str="KEY_GRAB";
        break;
    case KEY_RELEASE:
        str="KEY_RELEASE";
        break;
    case KEY_ENABLE:
        str="KEY_ENABLE";
        break;
    case KEY_DISABLE:
        str="KEY_DISABLE";
        break;
    default:
        break;
    }
    return str;
}