usb_mouse.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. UsbMode usb_mode_prev = furi_hal_usb_get_config();
  35. furi_hal_usb_set_config(UsbModeHid);
  36. view_port_draw_callback_set(view_port, usb_mouse_render_callback, NULL);
  37. view_port_input_callback_set(view_port, usb_mouse_input_callback, event_queue);
  38. // Open GUI and register view_port
  39. Gui* gui = furi_record_open("gui");
  40. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  41. UsbMouseEvent event;
  42. while(1) {
  43. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  44. if(event_status == osOK) {
  45. if(event.type == EventTypeInput) {
  46. if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  47. break;
  48. }
  49. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  50. furi_hal_hid_mouse_press(HID_MOUSE_BTN_RIGHT);
  51. furi_hal_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
  52. }
  53. if(event.input.key == InputKeyOk) {
  54. if(event.input.type == InputTypePress) {
  55. furi_hal_hid_mouse_press(HID_MOUSE_BTN_LEFT);
  56. } else if(event.input.type == InputTypeRelease) {
  57. furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
  58. }
  59. }
  60. if(event.input.key == InputKeyRight) {
  61. if(event.input.type == InputTypePress) {
  62. furi_hal_hid_mouse_move(MOUSE_MOVE_SHORT, 0);
  63. } else if(event.input.type == InputTypeRepeat) {
  64. furi_hal_hid_mouse_move(MOUSE_MOVE_LONG, 0);
  65. }
  66. }
  67. if(event.input.key == InputKeyLeft) {
  68. if(event.input.type == InputTypePress) {
  69. furi_hal_hid_mouse_move(-MOUSE_MOVE_SHORT, 0);
  70. } else if(event.input.type == InputTypeRepeat) {
  71. furi_hal_hid_mouse_move(-MOUSE_MOVE_LONG, 0);
  72. }
  73. }
  74. if(event.input.key == InputKeyDown) {
  75. if(event.input.type == InputTypePress) {
  76. furi_hal_hid_mouse_move(0, MOUSE_MOVE_SHORT);
  77. } else if(event.input.type == InputTypeRepeat) {
  78. furi_hal_hid_mouse_move(0, MOUSE_MOVE_LONG);
  79. }
  80. }
  81. if(event.input.key == InputKeyUp) {
  82. if(event.input.type == InputTypePress) {
  83. furi_hal_hid_mouse_move(0, -MOUSE_MOVE_SHORT);
  84. } else if(event.input.type == InputTypeRepeat) {
  85. furi_hal_hid_mouse_move(0, -MOUSE_MOVE_LONG);
  86. }
  87. }
  88. }
  89. }
  90. view_port_update(view_port);
  91. }
  92. furi_hal_usb_set_config(usb_mode_prev);
  93. // remove & free all stuff created by app
  94. gui_remove_view_port(gui, view_port);
  95. view_port_free(view_port);
  96. osMessageQueueDelete(event_queue);
  97. return 0;
  98. }