usb_mouse.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #define MOUSE_MOVE_SHORT 5
  6. #define MOUSE_MOVE_LONG 20
  7. typedef enum {
  8. EventTypeInput,
  9. } EventType;
  10. typedef struct {
  11. union {
  12. InputEvent input;
  13. };
  14. EventType type;
  15. } UsbMouseEvent;
  16. static void usb_mouse_render_callback(Canvas* canvas, void* ctx) {
  17. canvas_clear(canvas);
  18. canvas_set_font(canvas, FontPrimary);
  19. canvas_draw_str(canvas, 0, 10, "USB Mouse Demo");
  20. canvas_set_font(canvas, FontSecondary);
  21. canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
  22. }
  23. static void usb_mouse_input_callback(InputEvent* input_event, void* ctx) {
  24. osMessageQueueId_t event_queue = ctx;
  25. UsbMouseEvent event;
  26. event.type = EventTypeInput;
  27. event.input = *input_event;
  28. osMessageQueuePut(event_queue, &event, 0, osWaitForever);
  29. }
  30. int32_t usb_mouse_app(void* p) {
  31. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(UsbMouseEvent), NULL);
  32. furi_check(event_queue);
  33. ViewPort* view_port = view_port_alloc();
  34. FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
  35. furi_hal_usb_unlock();
  36. furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
  37. view_port_draw_callback_set(view_port, usb_mouse_render_callback, NULL);
  38. view_port_input_callback_set(view_port, usb_mouse_input_callback, event_queue);
  39. // Open GUI and register view_port
  40. Gui* gui = furi_record_open("gui");
  41. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  42. UsbMouseEvent event;
  43. while(1) {
  44. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  45. if(event_status == osOK) {
  46. if(event.type == EventTypeInput) {
  47. if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  48. break;
  49. }
  50. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  51. furi_hal_hid_mouse_press(HID_MOUSE_BTN_RIGHT);
  52. furi_hal_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
  53. }
  54. if(event.input.key == InputKeyOk) {
  55. if(event.input.type == InputTypePress) {
  56. furi_hal_hid_mouse_press(HID_MOUSE_BTN_LEFT);
  57. } else if(event.input.type == InputTypeRelease) {
  58. furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
  59. }
  60. }
  61. if(event.input.key == InputKeyRight) {
  62. if(event.input.type == InputTypePress) {
  63. furi_hal_hid_mouse_move(MOUSE_MOVE_SHORT, 0);
  64. } else if(event.input.type == InputTypeRepeat) {
  65. furi_hal_hid_mouse_move(MOUSE_MOVE_LONG, 0);
  66. }
  67. }
  68. if(event.input.key == InputKeyLeft) {
  69. if(event.input.type == InputTypePress) {
  70. furi_hal_hid_mouse_move(-MOUSE_MOVE_SHORT, 0);
  71. } else if(event.input.type == InputTypeRepeat) {
  72. furi_hal_hid_mouse_move(-MOUSE_MOVE_LONG, 0);
  73. }
  74. }
  75. if(event.input.key == InputKeyDown) {
  76. if(event.input.type == InputTypePress) {
  77. furi_hal_hid_mouse_move(0, MOUSE_MOVE_SHORT);
  78. } else if(event.input.type == InputTypeRepeat) {
  79. furi_hal_hid_mouse_move(0, MOUSE_MOVE_LONG);
  80. }
  81. }
  82. if(event.input.key == InputKeyUp) {
  83. if(event.input.type == InputTypePress) {
  84. furi_hal_hid_mouse_move(0, -MOUSE_MOVE_SHORT);
  85. } else if(event.input.type == InputTypeRepeat) {
  86. furi_hal_hid_mouse_move(0, -MOUSE_MOVE_LONG);
  87. }
  88. }
  89. }
  90. }
  91. view_port_update(view_port);
  92. }
  93. furi_hal_usb_set_config(usb_mode_prev, NULL);
  94. // remove & free all stuff created by app
  95. gui_remove_view_port(gui, view_port);
  96. view_port_free(view_port);
  97. osMessageQueueDelete(event_queue);
  98. return 0;
  99. }