mouse.ino 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "port.h"
  2. // #include "pipe.h"
  3. #include "mousepipe.h"
  4. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  5. #include "esp32-hal-log.h"
  6. #else
  7. #include "esp_log.h"
  8. #endif
  9. #include "parse_hid.h"
  10. #define DEVICE_ADDRESS 2
  11. USBHostMouse port(2);
  12. extern void parse_hid_report_map(uint8_t *map, size_t size);
  13. void usbh_mouse_device_ready()
  14. {
  15. port.setIdle();
  16. port.getHidReportMap();
  17. }
  18. void ctrl_pipe_cb(ext_pipe_event_msg_t event, usb_irp_t *irp)
  19. {
  20. Serial.printf("CTRL EVENT: 0x%x\n", event);
  21. if (event == 0xA206)
  22. {
  23. port.inpipe->inData();
  24. }
  25. }
  26. /**
  27. * hid report with buttons, wheel and move
  28. */
  29. static void hid_datain_cb(ext_pipe_event_msg_t event, usb_irp_t *irp)
  30. {
  31. switch (event)
  32. {
  33. case HCD_PIPE_EVENT_IRP_DONE:
  34. ESP_LOGD("Pipe cdc: ", "XFER status: %d, num bytes: %d, actual bytes: %d", irp->status, irp->num_bytes, irp->actual_num_bytes);
  35. // ESP_LOG_BUFFER_HEX("", irp->data_buffer, irp->num_bytes);
  36. ESP_LOGI("", "HID REPORT ID: %d", irp->data_buffer[0]);
  37. ESP_LOGI("", "Mouse buttons: %d", irp->data_buffer[1]);
  38. ESP_LOGI("", "X axis(raw bytes): %i", (int8_t)irp->data_buffer[2]);
  39. ESP_LOGI("", "Y axis(raw bytes): %i", (int8_t)irp->data_buffer[4]);
  40. ESP_LOGI("", "Mouse wheel: %i\n", (int8_t)irp->data_buffer[5]);
  41. // ESP_LOGI("", "Mouse buttons: %d", get_buttons(irp->data_buffer));
  42. // ESP_LOGI("", "X axis(raw bytes): %i", get_x_axis(irp->data_buffer));
  43. // ESP_LOGI("", "Y axis(raw bytes): %i", get_y_axis(irp->data_buffer));
  44. // ESP_LOGI("", "Mouse wheel: %i\n", get_wheel(irp->data_buffer));
  45. break;
  46. }
  47. }
  48. void port_cb(port_event_msg_t msg, USBHostPort *port)
  49. {
  50. Serial.printf("PORT EVENT: 0x%x\n", msg.port_event);
  51. }
  52. void setup()
  53. {
  54. Serial.begin(115200);
  55. if(port.init()){
  56. port.onPortEvent(port_cb);
  57. port.onControlEvent(ctrl_pipe_cb);
  58. port.onDataIn(hid_datain_cb);
  59. }
  60. }
  61. void loop()
  62. {
  63. delay(1000);
  64. // hid_mouse_t * mouse = get_mouse_struct();
  65. // Serial.printf("buttons first bits: %d, bits count: %d\n", mouse->buttons.bits_start + 8, (mouse->buttons.count));
  66. // Serial.printf("axes first bits: %d, bits count: %d\n", mouse->axes.bits_start + 8, (mouse->axes.count) * mouse->axes.size);
  67. // Serial.printf("wheel first bits: %d, bits count: %d\n", mouse->wheel.bits_start + 8, (mouse->wheel.count) * mouse->wheel.size);
  68. }