usb_mouse.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. UNUSED(ctx);
  18. canvas_clear(canvas);
  19. canvas_set_font(canvas, FontPrimary);
  20. canvas_draw_str(canvas, 0, 10, "USB Mouse Demo");
  21. canvas_set_font(canvas, FontSecondary);
  22. canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
  23. }
  24. static void usb_mouse_input_callback(InputEvent* input_event, void* ctx) {
  25. FuriMessageQueue* event_queue = ctx;
  26. UsbMouseEvent event;
  27. event.type = EventTypeInput;
  28. event.input = *input_event;
  29. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  30. }
  31. int32_t usb_mouse_app(void* p) {
  32. UNUSED(p);
  33. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(UsbMouseEvent));
  34. furi_check(event_queue);
  35. ViewPort* view_port = view_port_alloc();
  36. FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
  37. furi_hal_usb_unlock();
  38. furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
  39. view_port_draw_callback_set(view_port, usb_mouse_render_callback, NULL);
  40. view_port_input_callback_set(view_port, usb_mouse_input_callback, event_queue);
  41. // Open GUI and register view_port
  42. Gui* gui = furi_record_open(RECORD_GUI);
  43. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  44. UsbMouseEvent event;
  45. while(1) {
  46. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  47. if(event_status == FuriStatusOk) {
  48. if(event.type == EventTypeInput) {
  49. if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  50. break;
  51. }
  52. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  53. furi_hal_hid_mouse_press(HID_MOUSE_BTN_RIGHT);
  54. furi_hal_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
  55. }
  56. if(event.input.key == InputKeyOk) {
  57. if(event.input.type == InputTypePress) {
  58. furi_hal_hid_mouse_press(HID_MOUSE_BTN_LEFT);
  59. } else if(event.input.type == InputTypeRelease) {
  60. furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
  61. }
  62. }
  63. if(event.input.key == InputKeyRight) {
  64. if(event.input.type == InputTypePress) {
  65. furi_hal_hid_mouse_move(MOUSE_MOVE_SHORT, 0);
  66. } else if(event.input.type == InputTypeRepeat) {
  67. furi_hal_hid_mouse_move(MOUSE_MOVE_LONG, 0);
  68. }
  69. }
  70. if(event.input.key == InputKeyLeft) {
  71. if(event.input.type == InputTypePress) {
  72. furi_hal_hid_mouse_move(-MOUSE_MOVE_SHORT, 0);
  73. } else if(event.input.type == InputTypeRepeat) {
  74. furi_hal_hid_mouse_move(-MOUSE_MOVE_LONG, 0);
  75. }
  76. }
  77. if(event.input.key == InputKeyDown) {
  78. if(event.input.type == InputTypePress) {
  79. furi_hal_hid_mouse_move(0, MOUSE_MOVE_SHORT);
  80. } else if(event.input.type == InputTypeRepeat) {
  81. furi_hal_hid_mouse_move(0, MOUSE_MOVE_LONG);
  82. }
  83. }
  84. if(event.input.key == InputKeyUp) {
  85. if(event.input.type == InputTypePress) {
  86. furi_hal_hid_mouse_move(0, -MOUSE_MOVE_SHORT);
  87. } else if(event.input.type == InputTypeRepeat) {
  88. furi_hal_hid_mouse_move(0, -MOUSE_MOVE_LONG);
  89. }
  90. }
  91. }
  92. }
  93. view_port_update(view_port);
  94. }
  95. furi_hal_usb_set_config(usb_mode_prev, NULL);
  96. // remove & free all stuff created by app
  97. gui_remove_view_port(gui, view_port);
  98. view_port_free(view_port);
  99. furi_message_queue_free(event_queue);
  100. return 0;
  101. }