bt_hid_keynote.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "bt_hid_keynote.h"
  2. #include <furi.h>
  3. #include <furi_hal_bt_hid.h>
  4. #include <furi_hal_usb_hid.h>
  5. #include <gui/elements.h>
  6. #include "bt_hid_icons.h"
  7. struct BtHidKeynote {
  8. View* view;
  9. };
  10. typedef struct {
  11. bool left_pressed;
  12. bool up_pressed;
  13. bool right_pressed;
  14. bool down_pressed;
  15. bool ok_pressed;
  16. bool back_pressed;
  17. bool connected;
  18. } BtHidKeynoteModel;
  19. static void bt_hid_keynote_draw_arrow(Canvas* canvas, uint8_t x, uint8_t y, CanvasDirection dir) {
  20. canvas_draw_triangle(canvas, x, y, 5, 3, dir);
  21. if(dir == CanvasDirectionBottomToTop) {
  22. canvas_draw_line(canvas, x, y + 6, x, y - 1);
  23. } else if(dir == CanvasDirectionTopToBottom) {
  24. canvas_draw_line(canvas, x, y - 6, x, y + 1);
  25. } else if(dir == CanvasDirectionRightToLeft) {
  26. canvas_draw_line(canvas, x + 6, y, x - 1, y);
  27. } else if(dir == CanvasDirectionLeftToRight) {
  28. canvas_draw_line(canvas, x - 6, y, x + 1, y);
  29. }
  30. }
  31. static void bt_hid_keynote_draw_callback(Canvas* canvas, void* context) {
  32. furi_assert(context);
  33. BtHidKeynoteModel* model = context;
  34. // Header
  35. if(model->connected) {
  36. canvas_draw_icon(canvas, 0, 0, &I_Ble_connected_15x15);
  37. } else {
  38. canvas_draw_icon(canvas, 0, 0, &I_Ble_disconnected_15x15);
  39. }
  40. canvas_set_font(canvas, FontPrimary);
  41. elements_multiline_text_aligned(canvas, 17, 3, AlignLeft, AlignTop, "Keynote");
  42. canvas_draw_icon(canvas, 68, 2, &I_Pin_back_arrow_10x8);
  43. canvas_set_font(canvas, FontSecondary);
  44. elements_multiline_text_aligned(canvas, 127, 3, AlignRight, AlignTop, "Hold to exit");
  45. // Up
  46. canvas_draw_icon(canvas, 21, 24, &I_Button_18x18);
  47. if(model->up_pressed) {
  48. elements_slightly_rounded_box(canvas, 24, 26, 13, 13);
  49. canvas_set_color(canvas, ColorWhite);
  50. }
  51. bt_hid_keynote_draw_arrow(canvas, 30, 30, CanvasDirectionBottomToTop);
  52. canvas_set_color(canvas, ColorBlack);
  53. // Down
  54. canvas_draw_icon(canvas, 21, 45, &I_Button_18x18);
  55. if(model->down_pressed) {
  56. elements_slightly_rounded_box(canvas, 24, 47, 13, 13);
  57. canvas_set_color(canvas, ColorWhite);
  58. }
  59. bt_hid_keynote_draw_arrow(canvas, 30, 55, CanvasDirectionTopToBottom);
  60. canvas_set_color(canvas, ColorBlack);
  61. // Left
  62. canvas_draw_icon(canvas, 0, 45, &I_Button_18x18);
  63. if(model->left_pressed) {
  64. elements_slightly_rounded_box(canvas, 3, 47, 13, 13);
  65. canvas_set_color(canvas, ColorWhite);
  66. }
  67. bt_hid_keynote_draw_arrow(canvas, 7, 53, CanvasDirectionRightToLeft);
  68. canvas_set_color(canvas, ColorBlack);
  69. // Right
  70. canvas_draw_icon(canvas, 42, 45, &I_Button_18x18);
  71. if(model->right_pressed) {
  72. elements_slightly_rounded_box(canvas, 45, 47, 13, 13);
  73. canvas_set_color(canvas, ColorWhite);
  74. }
  75. bt_hid_keynote_draw_arrow(canvas, 53, 53, CanvasDirectionLeftToRight);
  76. canvas_set_color(canvas, ColorBlack);
  77. // Ok
  78. canvas_draw_icon(canvas, 63, 25, &I_Space_65x18);
  79. if(model->ok_pressed) {
  80. elements_slightly_rounded_box(canvas, 66, 27, 60, 13);
  81. canvas_set_color(canvas, ColorWhite);
  82. }
  83. canvas_draw_icon(canvas, 74, 29, &I_Ok_btn_9x9);
  84. elements_multiline_text_aligned(canvas, 91, 36, AlignLeft, AlignBottom, "Space");
  85. canvas_set_color(canvas, ColorBlack);
  86. // Back
  87. canvas_draw_icon(canvas, 63, 45, &I_Space_65x18);
  88. if(model->back_pressed) {
  89. elements_slightly_rounded_box(canvas, 66, 47, 60, 13);
  90. canvas_set_color(canvas, ColorWhite);
  91. }
  92. canvas_draw_icon(canvas, 74, 49, &I_Pin_back_arrow_10x8);
  93. elements_multiline_text_aligned(canvas, 91, 57, AlignLeft, AlignBottom, "Back");
  94. }
  95. static void bt_hid_keynote_process(BtHidKeynote* bt_hid_keynote, InputEvent* event) {
  96. with_view_model(
  97. bt_hid_keynote->view,
  98. BtHidKeynoteModel * model,
  99. {
  100. if(event->type == InputTypePress) {
  101. if(event->key == InputKeyUp) {
  102. model->up_pressed = true;
  103. furi_hal_bt_hid_kb_press(HID_KEYBOARD_UP_ARROW);
  104. } else if(event->key == InputKeyDown) {
  105. model->down_pressed = true;
  106. furi_hal_bt_hid_kb_press(HID_KEYBOARD_DOWN_ARROW);
  107. } else if(event->key == InputKeyLeft) {
  108. model->left_pressed = true;
  109. furi_hal_bt_hid_kb_press(HID_KEYBOARD_LEFT_ARROW);
  110. } else if(event->key == InputKeyRight) {
  111. model->right_pressed = true;
  112. furi_hal_bt_hid_kb_press(HID_KEYBOARD_RIGHT_ARROW);
  113. } else if(event->key == InputKeyOk) {
  114. model->ok_pressed = true;
  115. furi_hal_bt_hid_kb_press(HID_KEYBOARD_SPACEBAR);
  116. } else if(event->key == InputKeyBack) {
  117. model->back_pressed = true;
  118. }
  119. } else if(event->type == InputTypeRelease) {
  120. if(event->key == InputKeyUp) {
  121. model->up_pressed = false;
  122. furi_hal_bt_hid_kb_release(HID_KEYBOARD_UP_ARROW);
  123. } else if(event->key == InputKeyDown) {
  124. model->down_pressed = false;
  125. furi_hal_bt_hid_kb_release(HID_KEYBOARD_DOWN_ARROW);
  126. } else if(event->key == InputKeyLeft) {
  127. model->left_pressed = false;
  128. furi_hal_bt_hid_kb_release(HID_KEYBOARD_LEFT_ARROW);
  129. } else if(event->key == InputKeyRight) {
  130. model->right_pressed = false;
  131. furi_hal_bt_hid_kb_release(HID_KEYBOARD_RIGHT_ARROW);
  132. } else if(event->key == InputKeyOk) {
  133. model->ok_pressed = false;
  134. furi_hal_bt_hid_kb_release(HID_KEYBOARD_SPACEBAR);
  135. } else if(event->key == InputKeyBack) {
  136. model->back_pressed = false;
  137. }
  138. } else if(event->type == InputTypeShort) {
  139. if(event->key == InputKeyBack) {
  140. furi_hal_bt_hid_kb_press(HID_KEYBOARD_DELETE);
  141. furi_hal_bt_hid_kb_release(HID_KEYBOARD_DELETE);
  142. furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_AC_BACK);
  143. furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_AC_BACK);
  144. }
  145. }
  146. },
  147. true);
  148. }
  149. static bool bt_hid_keynote_input_callback(InputEvent* event, void* context) {
  150. furi_assert(context);
  151. BtHidKeynote* bt_hid_keynote = context;
  152. bool consumed = false;
  153. if(event->type == InputTypeLong && event->key == InputKeyBack) {
  154. furi_hal_bt_hid_kb_release_all();
  155. } else {
  156. bt_hid_keynote_process(bt_hid_keynote, event);
  157. consumed = true;
  158. }
  159. return consumed;
  160. }
  161. BtHidKeynote* bt_hid_keynote_alloc() {
  162. BtHidKeynote* bt_hid_keynote = malloc(sizeof(BtHidKeynote));
  163. bt_hid_keynote->view = view_alloc();
  164. view_set_context(bt_hid_keynote->view, bt_hid_keynote);
  165. view_allocate_model(bt_hid_keynote->view, ViewModelTypeLocking, sizeof(BtHidKeynoteModel));
  166. view_set_draw_callback(bt_hid_keynote->view, bt_hid_keynote_draw_callback);
  167. view_set_input_callback(bt_hid_keynote->view, bt_hid_keynote_input_callback);
  168. return bt_hid_keynote;
  169. }
  170. void bt_hid_keynote_free(BtHidKeynote* bt_hid_keynote) {
  171. furi_assert(bt_hid_keynote);
  172. view_free(bt_hid_keynote->view);
  173. free(bt_hid_keynote);
  174. }
  175. View* bt_hid_keynote_get_view(BtHidKeynote* bt_hid_keynote) {
  176. furi_assert(bt_hid_keynote);
  177. return bt_hid_keynote->view;
  178. }
  179. void bt_hid_keynote_set_connected_status(BtHidKeynote* bt_hid_keynote, bool connected) {
  180. furi_assert(bt_hid_keynote);
  181. with_view_model(
  182. bt_hid_keynote->view, BtHidKeynoteModel * model, { model->connected = connected; }, true);
  183. }