mouse_host_app.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. //--------------------------------------------------------------------+
  27. // INCLUDE
  28. //--------------------------------------------------------------------+
  29. #include "mouse_host_app.h"
  30. #include "app_os_prio.h"
  31. #if CFG_TUH_HID_MOUSE
  32. //--------------------------------------------------------------------+
  33. // MACRO CONSTANT TYPEDEF
  34. //--------------------------------------------------------------------+
  35. #define QUEUE_MOUSE_REPORT_DEPTH 4
  36. //--------------------------------------------------------------------+
  37. // INTERNAL OBJECT & FUNCTION DECLARATION
  38. //--------------------------------------------------------------------+
  39. static osal_queue_t queue_mouse_hdl;
  40. CFG_TUSB_MEM_SECTION static hid_mouse_report_t usb_mouse_report;
  41. static inline void process_mouse_report(hid_mouse_report_t const * p_report);
  42. //--------------------------------------------------------------------+
  43. // tinyusb callbacks
  44. //--------------------------------------------------------------------+
  45. void tuh_hid_mouse_mounted_cb(uint8_t dev_addr)
  46. {
  47. // application set-up
  48. printf("\na Mouse device (address %d) is mounted\n", dev_addr);
  49. osal_queue_flush(queue_mouse_hdl);
  50. (void) tuh_hid_mouse_get_report(dev_addr, (uint8_t*) &usb_mouse_report); // first report
  51. }
  52. void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr)
  53. {
  54. // application tear-down
  55. printf("\na Mouse device (address %d) is unmounted\n", dev_addr);
  56. }
  57. // invoked ISR context
  58. void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event)
  59. {
  60. switch(event)
  61. {
  62. case XFER_RESULT_SUCCESS:
  63. osal_queue_send(queue_mouse_hdl, &usb_mouse_report);
  64. (void) tuh_hid_mouse_get_report(dev_addr, (uint8_t*) &usb_mouse_report);
  65. break;
  66. case XFER_RESULT_FAILED:
  67. (void) tuh_hid_mouse_get_report(dev_addr, (uint8_t*) &usb_mouse_report); // ignore & continue
  68. break;
  69. default :
  70. break;
  71. }
  72. }
  73. //--------------------------------------------------------------------+
  74. // APPLICATION CODE
  75. // NOTICE: MOUSE REPORT IS NOT CORRECT UNTIL A DECENT HID PARSER IS
  76. // IMPLEMENTED, MEANWHILE IT CAN MISS DISPLAY BUTTONS OR X,Y etc
  77. //--------------------------------------------------------------------+
  78. void mouse_host_app_init(void)
  79. {
  80. tu_memclr(&usb_mouse_report, sizeof(hid_mouse_report_t));
  81. queue_mouse_hdl = osal_queue_create( QUEUE_MOUSE_REPORT_DEPTH, sizeof(hid_mouse_report_t) );
  82. TU_ASSERT( queue_mouse_hdl, VOID_RETURN);
  83. TU_VERIFY( osal_task_create(mouse_host_app_task, "mouse", 128, NULL, MOUSE_APP_TASK_PRIO), );
  84. }
  85. //------------- main task -------------//
  86. void mouse_host_app_task(void* param)
  87. {
  88. (void) param;
  89. OSAL_TASK_BEGIN
  90. tusb_error_t error;
  91. hid_mouse_report_t mouse_report;
  92. osal_queue_receive(queue_mouse_hdl, &mouse_report, OSAL_TIMEOUT_WAIT_FOREVER, &error);
  93. (void) error; // suppress compiler's warnings
  94. process_mouse_report(&mouse_report);
  95. OSAL_TASK_END
  96. }
  97. //--------------------------------------------------------------------+
  98. // HELPER
  99. //--------------------------------------------------------------------+
  100. void cursor_movement(int8_t x, int8_t y, int8_t wheel)
  101. {
  102. //------------- X -------------//
  103. if ( x < 0)
  104. {
  105. printf(ANSI_CURSOR_BACKWARD(%d), (-x)); // move left
  106. }else if ( x > 0)
  107. {
  108. printf(ANSI_CURSOR_FORWARD(%d), x); // move right
  109. }else { }
  110. //------------- Y -------------//
  111. if ( y < 0)
  112. {
  113. printf(ANSI_CURSOR_UP(%d), (-y)); // move up
  114. }else if ( y > 0)
  115. {
  116. printf(ANSI_CURSOR_DOWN(%d), y); // move down
  117. }else { }
  118. //------------- wheel -------------//
  119. if (wheel < 0)
  120. {
  121. printf(ANSI_SCROLL_UP(%d), (-wheel)); // scroll up
  122. }else if (wheel > 0)
  123. {
  124. printf(ANSI_SCROLL_DOWN(%d), wheel); // scroll down
  125. }else { }
  126. }
  127. static inline void process_mouse_report(hid_mouse_report_t const * p_report)
  128. {
  129. static hid_mouse_report_t prev_report = { 0, 0, 0, 0 };
  130. //------------- button state -------------//
  131. uint8_t button_changed_mask = p_report->buttons ^ prev_report.buttons;
  132. if ( button_changed_mask & p_report->buttons)
  133. {
  134. printf(" %c%c%c ",
  135. p_report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-',
  136. p_report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-',
  137. p_report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-');
  138. }
  139. //------------- cursor movement -------------//
  140. cursor_movement(p_report->x, p_report->y, p_report->wheel);
  141. }
  142. #endif