Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.37 KB | None | 0 0
  1. /*
  2.  * This program is free software; you can redistribute it and/or modify
  3.  * it under the terms of the GNU General Public License as published by
  4.  * the Free Software Foundation; either version 2 of the License, or
  5.  * (at your option) any later version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful,
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  * GNU General Public License for more details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License
  13.  * along with this program; if not, write to the Free Software
  14.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15.  * MA 02110-1301, USA.
  16.  *
  17.  * Copyright 2017 Benjamin Aigner <beni@asterics-foundation.org>
  18.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "freertos/FreeRTOS.h"
  25. #include "freertos/task.h"
  26. #include "freertos/event_groups.h"
  27. #include "esp_system.h"
  28. #include "esp_wifi.h"
  29. #include "esp_event_loop.h"
  30. #include "esp_log.h"
  31. #include "nvs_flash.h"
  32. #include "esp_bt.h"
  33. #include "driver/gpio.h"
  34.  
  35. #include "config.h"
  36. #include "HID_kbdmousejoystick.h"
  37.  
  38. #include "esp_gap_ble_api.h"
  39. //#include "esp_hidd_prf_api.h"
  40. /*#include "esp_bt_defs.h"
  41. #include "esp_gatts_api.h"
  42. #include "esp_gatt_defs.h"
  43. #include "esp_bt_main.h"
  44. #include "esp_bt_device.h"*/
  45. #include "driver/gpio.h"
  46. #include "driver/uart.h"
  47. //#include "hid_dev.h"
  48. #include "keyboard.h"
  49.  
  50. /** demo mouse speed */
  51. #define MOUSE_SPEED 30
  52. #define MAX_CMDLEN  100
  53.  
  54. #define EXT_UART_TAG "EXT_UART"
  55. #define CONSOLE_UART_TAG "CONSOLE_UART"
  56.  
  57.  
  58. static uint8_t keycode_modifier=0;
  59. static uint8_t keycode_deadkey_first=0;
  60. //static joystick_data_t joystick;//currently unused, no joystick implemented
  61. static config_data_t config;
  62. // joystick_command_t joystickCmd;
  63.  
  64.  
  65. void update_config()
  66. {
  67.     nvs_handle my_handle;
  68.     esp_err_t err = nvs_open("config_c", NVS_READWRITE, &my_handle);
  69.     if(err != ESP_OK) ESP_LOGE("MAIN","error opening NVS");
  70.     err = nvs_set_str(my_handle, "btname", config.bt_device_name);
  71.     if(err != ESP_OK) ESP_LOGE("MAIN","error saving NVS - bt name");
  72.     err = nvs_set_u8(my_handle, "locale", config.locale);
  73.     if(err != ESP_OK) ESP_LOGE("MAIN","error saving NVS - locale");
  74.     printf("Committing updates in NVS ... ");
  75.     err = nvs_commit(my_handle);
  76.     printf((err != ESP_OK) ? "Failed!\n" : "Done\n");
  77.     nvs_close(my_handle);
  78. }
  79.  
  80.  
  81. void sendKeyCode(uint8_t c, keyboard_action type) {
  82.     keyboard_command_t keyboardCmd;
  83.     keyboardCmd.type = type;
  84.     keyboardCmd.keycode = c;
  85.     xQueueSend(keyboard_q,(void *)&keyboardCmd, (TickType_t) 0);
  86. }
  87.  
  88. void sendKey(uint8_t c, keyboard_action type) {
  89.     uint8_t keycode;
  90.     keycode = parse_for_keycode(c,config.locale,&keycode_modifier,&keycode_deadkey_first); //send current byte to parser
  91.     if(keycode == 0)
  92.     {
  93.         ESP_LOGI(EXT_UART_TAG,"keycode is 0 for 0x%X, skipping to next byte",c);
  94.         return; //if no keycode is found,skip to next byte (might be a 16bit UTF8)
  95.     }
  96.     ESP_LOGI(EXT_UART_TAG,"keycode: %d, modifier: %d, deadkey: %d",keycode,keycode_modifier,keycode_deadkey_first);
  97.     //TODO: do deadkey sequence...
  98.         //if a keycode is found, add to keycodes for HID
  99.     sendKeyCode(keycode, type);
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. #define CMDSTATE_IDLE 0
  107. #define CMDSTATE_GET_RAW 1
  108. #define CMDSTATE_GET_ASCII 2
  109.  
  110. struct cmdBuf {
  111.     int state;
  112.     int expectedBytes;
  113.     int bufferLength;
  114.     uint8_t buf[MAX_CMDLEN];
  115. } ;
  116.  
  117. uint8_t uppercase(uint8_t c)
  118. {
  119.     if ((c>='a') && (c<='z')) return (c-'a'+'A');
  120.     return(c);
  121. }
  122.  
  123. int get_int(const char * input, int index, int * value)
  124. {
  125.   int sign=1, result=0, valid=0;
  126.  
  127.   while (input[index]==' ') index++;   // skip leading spaces
  128.   if (input[index]=='-') { sign=-1; index++;}
  129.   while ((input[index]>='0') && (input[index]<='9'))
  130.   {
  131.       result= result*10+input[index]-'0';
  132.       valid=1;
  133.       index++;
  134.   }
  135.   while (input[index]==' ') index++;  // skip trailing spaces
  136.   if (input[index]==',') index++;     // or a comma
  137.  
  138.   if (valid) { *value = result*sign; return (index);}
  139.   return(0);
  140. }
  141.  
  142. uint8_t * stringToHexBuf(uint8_t * arr,uint8_t * buf){
  143.     unsigned long int tmp2;
  144.     uint8_t tmp[2];
  145.     int cpt=0;
  146.     for(int j=0;j<5;j++){
  147.         memcpy(tmp,&arr[j*2],2);
  148.         printf("%s\n",tmp);
  149.         tmp2=strtoul(tmp,NULL,16);
  150.         buf[j]=tmp2;
  151.         printf("%d\n",tmp2);
  152.     }
  153. }
  154.  
  155.  
  156.  
  157. void processCommand(struct cmdBuf *cmdBuffer)
  158. {
  159.     //commands:
  160.     // $ID
  161.     // $PMx (0 or 1)
  162.     // $GP
  163.     // $DPx (number of paired device, starting with 0)
  164.     // $KW (+layouts)
  165.     // $KD/KU
  166.     // $KR
  167.     // $KL
  168.     // $M
  169.     // $KC
  170.  
  171.     // TBD:
  172.     // $KK (if necessary)
  173.     // joystick (everything)
  174.  
  175.     /* /!\ /!\ /!\ /!\ /!\ /!\ */
  176.     //C'EST ICI LA MERDE
  177.  
  178.     if(cmdBuffer->bufferLength < 2) return;
  179.     //easier this way than typecast in each str* function
  180.     const char *input = (const char *) cmdBuffer->buf;
  181.     int len = cmdBuffer->bufferLength;
  182.     uint8_t keycode;
  183.     const char nl = '\n';
  184.     esp_ble_bond_dev_t * btdevlist;
  185.     int counter;
  186.  
  187.  
  188.  
  189.     /**++++ commands without parameters ++++*/
  190.     //get module ID
  191.     if(strcmp(input,"ID") == 0)
  192.     {
  193.         uart_write_bytes(EX_UART_NUM, MODULE_ID, sizeof(MODULE_ID));
  194.         ESP_LOGD(EXT_UART_TAG,"sending module id (ID)");
  195.         return;
  196.     }
  197.  
  198.     //get all BT pairings
  199.     if(strcmp(input,"GP") == 0)
  200.     {
  201.         counter = esp_ble_get_bond_device_num();
  202.         char hexnum[5];
  203.  
  204.         if(counter > 0)
  205.         {
  206.             btdevlist = (esp_ble_bond_dev_t *) malloc(sizeof(esp_ble_bond_dev_t)*counter);
  207.             if(btdevlist != NULL)
  208.             {
  209.                 if(esp_ble_get_bond_device_list(&counter,btdevlist) == ESP_OK)
  210.                 {
  211.                     ESP_LOGI(EXT_UART_TAG,"bonded devices (starting with index 0):");
  212.                     ESP_LOGI(EXT_UART_TAG,"---------------------------------------");
  213.                     for(uint8_t i = 0; i<counter;i++)
  214.                     {
  215.                         //print on monitor & external uart
  216.                         esp_log_buffer_hex(EXT_UART_TAG, btdevlist[i].bd_addr, sizeof(esp_bd_addr_t));
  217.                         for (int t=0; t<sizeof(esp_bd_addr_t);t++) {
  218.                                 sprintf(hexnum,"%02X ",btdevlist[i].bd_addr[t]);
  219.                                 uart_write_bytes(EX_UART_NUM, hexnum, 3);
  220.                         }
  221.                         uart_write_bytes(EX_UART_NUM,&nl,sizeof(nl)); //newline
  222.                     }
  223.                     ESP_LOGI(EXT_UART_TAG,"---------------------------------------");
  224.                 } else ESP_LOGE(EXT_UART_TAG,"error getting device list");
  225.             } else ESP_LOGE(EXT_UART_TAG,"error allocating memory for device list");
  226.         } else ESP_LOGE(EXT_UART_TAG,"error getting bonded devices count or no devices bonded");
  227.         return;
  228.     }
  229.  
  230.     //joystick: update data (send a report)
  231.     if(strcmp(input,"JU") == 0)
  232.     {
  233.         //TBD: joystick
  234.         ESP_LOGD(EXT_UART_TAG,"TBD! joystick: send report (JU)");
  235.         return;
  236.     }
  237.  
  238.     //keyboard: release all
  239.     if(strcmp(input,"KR") == 0)
  240.     {
  241.         sendKeyCode(0, RELEASE_ALL);
  242.         ESP_LOGD(EXT_UART_TAG,"keyboard: release all (KR)");
  243.         return;
  244.     }
  245.     ESP_LOGI(EXT_UART_TAG,"input0 : %d",input[0]);
  246.     /**++++ commands with parameters ++++*/
  247.     switch(input[0])
  248.     {
  249.         case 'K': //keyboard
  250.         {
  251.  
  252.            
  253.  
  254.  
  255.             if(input[1]=='Z'){
  256.  
  257.                 ESP_LOGI(EXT_UART_TAG,"On rentre dans KZ");
  258.                 uint8_t buf[5];
  259.                 stringToHexBuf(input,2,buf);
  260.                 sendKeyCode(buf[0], PRESS);
  261.                 sendKeyCode(buf[1], PRESS);
  262.                 sendKeyCode(buf[2], PRESS);
  263.                 sendKeyCode(buf[3], PRESS);
  264.                 sendKeyCode(buf[4], PRESS);
  265.                 return;
  266.             }
  267.  
  268.             ESP_LOGI(EXT_UART_TAG,"On rentre dans K");
  269.             int param=0;
  270.             if ((input[1] == 'U') || (input[1] == 'D') || (input[1] == 'L'))
  271.             {
  272.                 if (!get_int(input,2,&param)) {
  273.                     ESP_LOGE(EXT_UART_TAG,"Keyboad command parameter needs integer format");
  274.                     break;
  275.                 }
  276.             }
  277.             //key up
  278.             if(input[1] == 'U')
  279.             {
  280.                 sendKeyCode(param, RELEASE);
  281.                 return;
  282.             }
  283.             //key down
  284.             if(input[1] == 'D')
  285.             {
  286.                 sendKeyCode(param, PRESS);
  287.                 return;
  288.             }
  289.             //keyboard, set locale
  290.             if(input[1] == 'L')
  291.             {
  292.                 if(param < LAYOUT_MAX)   {
  293.                     config.locale = param;
  294.                     update_config();
  295.                 } else ESP_LOGE(EXT_UART_TAG,"Locale number out of range");
  296.                 return;
  297.             }
  298.  
  299.             //keyboard, write
  300.             if(input[1] == 'W')
  301.             {
  302.                 ESP_LOGI(EXT_UART_TAG,"sending keyboard write, len: %d (bytes, not characters!)",len-2);
  303.                 for(uint16_t i = 2; i<len; i++)
  304.                 {
  305.                     if(input[i] == '\0')
  306.                     {
  307.                         ESP_LOGI(EXT_UART_TAG,"terminated string, ending KW");
  308.                         break;
  309.                     }
  310.                     sendKey(input[i],PRESS_RELEASE);
  311.                 }
  312.                 return;
  313.             }
  314.  
  315.             //keyboard, get keycode for unicode bytes
  316.             if(input[1] == 'C' && len == 4)
  317.             {
  318.                 keycode = get_keycode(input[2],config.locale,&keycode_modifier,&keycode_deadkey_first);
  319.                 //if the first byte is not sufficient, try with second byte.
  320.                 if(keycode == 0)
  321.                 {
  322.                     keycode = get_keycode(input[3],config.locale,&keycode_modifier,&keycode_deadkey_first);
  323.                 }
  324.                 //first the keycode + modifier are sent.
  325.                 //deadkey starting key is sent afterwards (0 if not necessary)
  326.                 uart_write_bytes(EX_UART_NUM, (char *)&keycode, sizeof(keycode));
  327.                 uart_write_bytes(EX_UART_NUM, (char *)&keycode_modifier, sizeof(keycode_modifier));
  328.                 uart_write_bytes(EX_UART_NUM, (char *)&keycode_deadkey_first, sizeof(keycode_deadkey_first));
  329.                 uart_write_bytes(EX_UART_NUM, &nl, sizeof(nl));
  330.                 return;
  331.             }
  332.             //keyboard, get unicode mapping between 2 locales
  333.             if(input[1] == 'K' && len == 6)
  334.             {
  335.                 //cpoint = get_cpoint((input[2] << 8) || input[3],input[4],input[5]);
  336.                 //uart_write_bytes(EX_UART_NUM, (char *)&cpoint, sizeof(cpoint));
  337.                 //uart_write_bytes(EX_UART_NUM, &nl, sizeof(nl));
  338.                 //TODO: is this necessary or useful anyway??
  339.                 return;
  340.             }
  341.             break;
  342.         }
  343.         case 'J': //joystick
  344.             //joystick, set X,Y,Z (each 0-1023)
  345.             if(input[1] == 'S' && len == 8) { }
  346.             //joystick, set Z rotate, slider left, slider right (each 0-1023)
  347.             if(input[1] == 'T' && len == 8) { }
  348.             //joystick, set buttons (bitmap for button nr 1-16)
  349.             if(input[1] == 'B' && len == 4) { }
  350.             //joystick, set hat (0-360°)
  351.             if(input[1] == 'H' && len == 4) { }
  352.             //TBD: joystick
  353.             ESP_LOGD(EXT_UART_TAG,"TBD! joystick");
  354.             break;
  355.  
  356.         default: //test for management commands
  357.             break;
  358.     }
  359.  
  360.     // M <buttons> <X> <Y> <wheel>
  361.     // create mouse activity
  362.     if(input[0] == 'M')
  363.     {
  364.         int index=1, value;
  365.         mouse_command_t mouseCmd;
  366.  
  367.         if ((index=get_int(input,index,&value))) mouseCmd.buttons = value; else return;
  368.         if ((index=get_int(input,index,&value))) mouseCmd.x = value; else return;
  369.         if ((index=get_int(input,index,&value))) mouseCmd.y = value; else return;
  370.         if ((index=get_int(input,index,&value))) mouseCmd.wheel = value; else return;
  371.  
  372.         xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  373.         ESP_LOGI(EXT_UART_TAG,"mouse command: %d,%d,%d,%d",mouseCmd.buttons,mouseCmd.x,mouseCmd.y,mouseCmd.wheel);
  374.         return;
  375.     }
  376.  
  377.     //DP: delete one pairing
  378.     if(input[0] == 'D' && input[1] == 'P')
  379.     {
  380.         int index_to_remove;
  381.         if (!(get_int(input,2,&index_to_remove))) {
  382.             ESP_LOGE(EXT_UART_TAG,"DP: invalid parameter, need integer");
  383.             return;
  384.         }
  385.  
  386.         counter = esp_ble_get_bond_device_num();
  387.         if(counter == 0)
  388.         {
  389.             ESP_LOGE(EXT_UART_TAG,"error deleting device, no paired devices");
  390.             return;
  391.         }
  392.  
  393.         if(index_to_remove >= counter)
  394.         {
  395.             ESP_LOGE(EXT_UART_TAG,"error deleting device, number out of range");
  396.             return;
  397.         }
  398.         if(counter >= 0)
  399.         {
  400.             btdevlist = (esp_ble_bond_dev_t *) malloc(sizeof(esp_ble_bond_dev_t)*counter);
  401.             if(btdevlist != NULL)
  402.             {
  403.                 if(esp_ble_get_bond_device_list(&counter,btdevlist) == ESP_OK)
  404.                 {
  405.                     esp_ble_remove_bond_device(btdevlist[index_to_remove].bd_addr);
  406.                 } else ESP_LOGE(EXT_UART_TAG,"error getting device list");
  407.                 free (btdevlist);
  408.             } else ESP_LOGE(EXT_UART_TAG,"error allocating memory for device list");
  409.         } else ESP_LOGE(EXT_UART_TAG,"error getting bonded devices count");
  410.         ESP_LOGE(EXT_UART_TAG,"TBD: delete pairing");
  411.         return;
  412.     }
  413.  
  414.     //PM: enable/disable advertisting/pairing
  415.     if(input[0] == 'P' && input[1] == 'M')
  416.     {
  417.         int param;
  418.         if (!(get_int(input,2,&param))) {
  419.             ESP_LOGE(EXT_UART_TAG,"PM: invalid parameter, need integer");
  420.             return;
  421.         }
  422.  
  423.         if(param == 0)
  424.         {
  425.             HID_kbdmousejoystick_deactivatePairing();
  426.             ESP_LOGI(EXT_UART_TAG,"PM: pairing deactivated");
  427.  
  428.         } else if(param == 1)
  429.         {
  430.             HID_kbdmousejoystick_activatePairing();
  431.             ESP_LOGI(EXT_UART_TAG,"PM: pairing activated");
  432.  
  433.         } else ESP_LOGE(EXT_UART_TAG,"PM: parameter error, either 0 or 1");
  434.         return;
  435.     }
  436.  
  437.     //set BT GATT advertising name
  438.     if(strncmp(input,"NAME ", 5) == 0)
  439.     {
  440.         if ((strlen(input)>6) && (strlen(input)-4<MAX_BT_DEVICENAME_LENGTH))
  441.         {
  442.             strcpy (config.bt_device_name, input+5);
  443.             update_config();
  444.             ESP_LOGI(EXT_UART_TAG,"NAME: new bt device name was stored");
  445.         }
  446.         else ESP_LOGI(EXT_UART_TAG,"NAME: given bt name is too long or too short");
  447.         return;
  448.     }
  449.  
  450.     ESP_LOGE(EXT_UART_TAG,"No command executed with: %s ; len= %d\n",input,len);
  451. }
  452.  
  453.  
  454.  
  455. void uart_parse_command (uint8_t character, struct cmdBuf * cmdBuffer)
  456. {
  457.     static uint8_t keycode = 0;
  458.  
  459.     switch (cmdBuffer->state) {
  460.  
  461.         case CMDSTATE_IDLE:
  462.                 if (character==0xfd) {
  463.                     cmdBuffer->bufferLength=0;
  464.                     cmdBuffer->expectedBytes=8;   // 8 bytes for raw report size
  465.                     cmdBuffer->state=CMDSTATE_GET_RAW;
  466.                 }
  467.                 else if (character == '$') {
  468.                     cmdBuffer->bufferLength=0;   // we will read an ASCII-command until CR or LF
  469.                     cmdBuffer->state=CMDSTATE_GET_ASCII;
  470.                 }
  471.                 else sendKey(character, PRESS_RELEASE);         // treat whatever comes in as keyboard output
  472.                 break;
  473.  
  474.         case CMDSTATE_GET_RAW:
  475.                 cmdBuffer->buf[cmdBuffer->bufferLength]=character;
  476.                 //if ((cmdBuffer->bufferLength == 1) && (character==0x00))  // we have a keyboard report: increase by 2 bytes
  477.                   // cmdBuffer->expectedBytes += 2;
  478.  
  479.                 cmdBuffer->bufferLength++;
  480.                 cmdBuffer->expectedBytes--;
  481.                 if (!cmdBuffer->expectedBytes) {
  482.                     if (cmdBuffer->buf[1] == 0x00) {   // keyboard report
  483.                         // TBD: synchonize with semaphore!
  484.                         if(HID_kbdmousejoystick_rawKeyboard(cmdBuffer->buf,8) != ESP_OK)
  485.                         {
  486.                             ESP_LOGE(EXT_UART_TAG,"Error sending raw kbd");
  487.                         } else ESP_LOGI(EXT_UART_TAG,"Keyboard sent");
  488.                     } else if (cmdBuffer->buf[1] == 0x03) {  // mouse report
  489.                         // TBD: synchonize with semaphore!
  490.                         if(HID_kbdmousejoystick_rawMouse(&(cmdBuffer->buf[2]),4) != ESP_OK)
  491.                         {
  492.                             ESP_LOGE(EXT_UART_TAG,"Error sending raw mouse");
  493.                         } else ESP_LOGI(EXT_UART_TAG,"Mouse sent");
  494.  
  495.                     }
  496.                     else ESP_LOGE(EXT_UART_TAG,"Unknown RAW HID packet");
  497.                     cmdBuffer->state=CMDSTATE_IDLE;
  498.                 }
  499.                 break;
  500.  
  501.         case CMDSTATE_GET_ASCII:
  502.                 if (character=='$')  {   //  key '$' can be created by sending "$$"
  503.                     sendKey('$', PRESS_RELEASE);
  504.                     cmdBuffer->state=CMDSTATE_IDLE;
  505.                 } else {   // collect a command string until CR or LF are received
  506.                     if ((character==0x0d) || (character==0x0a))  {
  507.                         cmdBuffer->buf[cmdBuffer->bufferLength]=0;
  508.                         ESP_LOGI(EXT_UART_TAG,"sending command to parser: %s",cmdBuffer->buf);
  509.  
  510.                         processCommand(cmdBuffer);
  511.                         cmdBuffer->state=CMDSTATE_IDLE;
  512.                     } else {
  513.                         if (cmdBuffer->bufferLength < MAX_CMDLEN-1)
  514.                           cmdBuffer->buf[cmdBuffer->bufferLength++]=character;
  515.                     }
  516.                 }
  517.                 break;
  518.         default:
  519.             cmdBuffer->state=CMDSTATE_IDLE;
  520.     }
  521. }
  522.  
  523.  
  524.  
  525. void uart_console(void *pvParameters)
  526. {
  527.     char character;
  528.     mouse_command_t mouseCmd;
  529.     keyboard_command_t keyboardCmd;
  530.  
  531.     //Install UART driver, and get the queue.
  532.     uart_driver_install(CONSOLE_UART_NUM, UART_FIFO_LEN * 2, UART_FIFO_LEN * 2, 0, NULL, 0);
  533.  
  534.     ESP_LOGI("UART","console UART processing task started");
  535.  
  536.     while(1)
  537.     {
  538.         // read single byte
  539.         uart_read_bytes(CONSOLE_UART_NUM, (uint8_t*) &character, 1, portMAX_DELAY);
  540.         // uart_parse_command(character, &cmdBuffer);
  541.  
  542.         if(HID_kbdmousejoystick_isConnected() == 0) {
  543.             ESP_LOGI(CONSOLE_UART_TAG,"Not connected, ignoring '%c'", character);
  544.         } else {
  545.             //Do not send anything if queues are uninitialized
  546.             if(mouse_q == NULL || keyboard_q == NULL || joystick_q == NULL)
  547.             {
  548.                 ESP_LOGE(CONSOLE_UART_TAG,"queues not initialized");
  549.                 continue;
  550.             }
  551.             switch (character){
  552.                 case 'a':
  553.                     mouseCmd.x = -MOUSE_SPEED;
  554.                     mouseCmd.y = 0;
  555.                     mouseCmd.buttons = 0;
  556.                     mouseCmd.wheel = 0;
  557.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  558.                     ESP_LOGI(CONSOLE_UART_TAG,"mouse: a");
  559.                     break;
  560.                 case 's':
  561.                     mouseCmd.x = 0;
  562.                     mouseCmd.y = MOUSE_SPEED;
  563.                     mouseCmd.buttons = 0;
  564.                     mouseCmd.wheel = 0;
  565.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  566.                     ESP_LOGI(CONSOLE_UART_TAG,"mouse: s");
  567.                     break;
  568.                 case 'd':
  569.                     mouseCmd.x = MOUSE_SPEED;
  570.                     mouseCmd.y = 0;
  571.                     mouseCmd.buttons = 0;
  572.                     mouseCmd.wheel = 0;
  573.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  574.                     ESP_LOGI(CONSOLE_UART_TAG,"mouse: d");
  575.                     break;
  576.                 case 'w':
  577.                     mouseCmd.x = 0;
  578.                     mouseCmd.y = -MOUSE_SPEED;
  579.                     mouseCmd.buttons = 0;
  580.                     mouseCmd.wheel = 0;
  581.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  582.                     ESP_LOGI(CONSOLE_UART_TAG,"mouse: w");
  583.                     break;
  584.                 case 'l':
  585.                     mouseCmd.x = 0;
  586.                     mouseCmd.y = 0;
  587.                     mouseCmd.buttons = 1;
  588.                     mouseCmd.wheel = 0;
  589.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  590.                     mouseCmd.x = 0;
  591.                     mouseCmd.y = 0;
  592.                     mouseCmd.buttons = 0;
  593.                     mouseCmd.wheel = 0;
  594.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  595.                     ESP_LOGI(CONSOLE_UART_TAG,"mouse: l");
  596.                     break;
  597.                 case 'r':
  598.                     mouseCmd.x = 0;
  599.                     mouseCmd.y = 0;
  600.                     mouseCmd.buttons = 2;
  601.                     mouseCmd.wheel = 0;
  602.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  603.                     mouseCmd.x = 0;
  604.                     mouseCmd.y = 0;
  605.                     mouseCmd.buttons = 0;
  606.                     mouseCmd.wheel = 0;
  607.                     xQueueSend(mouse_q,(void *)&mouseCmd, (TickType_t) 0);
  608.                     ESP_LOGI(CONSOLE_UART_TAG,"mouse: r");
  609.                     break;
  610.                 case 'q':
  611.                     ESP_LOGI(CONSOLE_UART_TAG,"received q: sending key y for test purposes");
  612.                     keyboardCmd.keycode = 28;
  613.                     keyboardCmd.type = PRESS_RELEASE;
  614.                     xQueueSend(keyboard_q,(void *)&keyboardCmd, (TickType_t) 0);
  615.                     break;
  616.                 default:
  617.                     ESP_LOGI(CONSOLE_UART_TAG,"received: %d",character);
  618.                     break;
  619.             }
  620.         }
  621.     }
  622. }
  623.  
  624.  
  625. void uart_external(void *pvParameters)
  626. {
  627.     char character;
  628.     struct cmdBuf cmdBuffer;
  629.  
  630.  
  631.     //Install UART driver, and get the queue.
  632.     esp_err_t ret = ESP_OK;
  633.     const uart_config_t uart_config = {
  634.         .baud_rate = 9600,
  635.         .data_bits = UART_DATA_8_BITS,
  636.         .parity = UART_PARITY_DISABLE,
  637.         .stop_bits = UART_STOP_BITS_1,
  638.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  639.     };
  640.  
  641.     //update UART config
  642.     ret = uart_param_config(EX_UART_NUM, &uart_config);
  643.     if(ret != ESP_OK)
  644.     {
  645.         ESP_LOGE(EXT_UART_TAG,"external UART param config failed");
  646.     }
  647.  
  648.     //set IO pins
  649.     ret = uart_set_pin(EX_UART_NUM, EX_SERIAL_TXPIN, EX_SERIAL_RXPIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  650.     if(ret != ESP_OK)
  651.     {
  652.         ESP_LOGE(EXT_UART_TAG,"external UART set pin failed");
  653.     }
  654.     uart_driver_install(EX_UART_NUM, UART_FIFO_LEN * 2, UART_FIFO_LEN * 2, 0, NULL, 0);
  655.  
  656.     ESP_LOGI(EXT_UART_TAG,"external UART processing task started");
  657.     cmdBuffer.state=CMDSTATE_IDLE;
  658.  
  659.     while(1)
  660.     {
  661.         // read single byte
  662.         uart_read_bytes(EX_UART_NUM, (uint8_t*) &character, 1, portMAX_DELAY);
  663.         uart_parse_command(character, &cmdBuffer);
  664.     }
  665. }
  666.  
  667. void blink_task(void *pvParameter)
  668. {
  669.     // Initialize GPIO pins
  670.     gpio_pad_select_gpio(INDICATOR_LED_PIN);
  671.     gpio_set_direction(INDICATOR_LED_PIN, GPIO_MODE_OUTPUT);
  672.     int blinkTime;
  673.  
  674.     while(1) {
  675.  
  676.         if (HID_kbdmousejoystick_isConnected())
  677.             blinkTime=1000;
  678.         else blinkTime=250;
  679.  
  680.  
  681.         /* Blink off (output low) */
  682.         gpio_set_level(INDICATOR_LED_PIN, 0);
  683.         vTaskDelay(blinkTime / portTICK_PERIOD_MS);
  684.         /* Blink on (output high) */
  685.         gpio_set_level(INDICATOR_LED_PIN, 1);
  686.         vTaskDelay(blinkTime / portTICK_PERIOD_MS);
  687.     }
  688. }
  689.  
  690. extern "C" void app_main()
  691. {
  692.     esp_err_t ret;
  693.  
  694.     // Initialize NVS.
  695.     ret = nvs_flash_init();
  696.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
  697.         ESP_ERROR_CHECK(nvs_flash_erase());
  698.         ret = nvs_flash_init();
  699.     }
  700.     ESP_ERROR_CHECK( ret );
  701.  
  702.     // Read config
  703.     nvs_handle my_handle;
  704.     ESP_LOGI("MAIN","loading configuration from NVS");
  705.     ret = nvs_open("config_c", NVS_READWRITE, &my_handle);
  706.     if(ret != ESP_OK) ESP_LOGE("MAIN","error opening NVS");
  707.     size_t available_size = MAX_BT_DEVICENAME_LENGTH;
  708.     strcpy(config.bt_device_name, GATTS_TAG);
  709.     nvs_get_str (my_handle, "btname", config.bt_device_name, &available_size);
  710.     if(ret != ESP_OK)
  711.     {
  712.         ESP_LOGE("MAIN","error reading NVS - bt name, setting to default");
  713.         strcpy(config.bt_device_name, GATTS_TAG);
  714.     } else ESP_LOGI("MAIN","bt device name is: %s",config.bt_device_name);
  715.  
  716.     ret = nvs_get_u8(my_handle, "locale", &config.locale);
  717.     if(ret != ESP_OK || config.locale >= LAYOUT_MAX)
  718.     {
  719.         ESP_LOGE("MAIN","error reading NVS - locale, setting to US_INTERNATIONAL");
  720.         config.locale = LAYOUT_US_INTERNATIONAL;
  721.     } else ESP_LOGI("MAIN","locale code is : %d",config.locale);
  722.     nvs_close(my_handle);
  723.  
  724.     // TBD: apply country code
  725.     // load HID country code for locale before initialising HID
  726.     // hidd_set_countrycode(get_hid_country_code(config.locale));
  727.  
  728.  
  729.     //activate mouse & keyboard BT stack (joystick is not working yet)
  730.     HID_kbdmousejoystick_init(1,1,0,0,config.bt_device_name);
  731.     ESP_LOGI("HIDD","MAIN finished...");
  732.  
  733.     esp_log_level_set("*", ESP_LOG_INFO);
  734.  
  735.  
  736.     // now start the tasks for processing UART input and indicator LED
  737.  
  738.     xTaskCreate(&uart_console,  "console", 4096, NULL, configMAX_PRIORITIES, NULL);
  739.     xTaskCreate(&uart_external, "external", 4096, NULL, configMAX_PRIORITIES, NULL);
  740.     xTaskCreate(&blink_task, "blink", 4096, NULL, configMAX_PRIORITIES, NULL);
  741.  
  742. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement