keypad_test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. typedef struct {
  5. bool press[5];
  6. uint16_t up;
  7. uint16_t down;
  8. uint16_t left;
  9. uint16_t right;
  10. uint16_t ok;
  11. } KeypadTestState;
  12. typedef enum {
  13. EventTypeInput,
  14. } EventType;
  15. typedef struct {
  16. union {
  17. InputEvent input;
  18. };
  19. EventType type;
  20. } KeypadTestEvent;
  21. static const char* keypad_test_get_key_name(InputKey key) {
  22. switch(key) {
  23. case InputKeyOk:
  24. return "Ok";
  25. case InputKeyBack:
  26. return "Back";
  27. case InputKeyLeft:
  28. return "Left";
  29. case InputKeyRight:
  30. return "Right";
  31. case InputKeyUp:
  32. return "Up";
  33. case InputKeyDown:
  34. return "Down";
  35. default:
  36. return "Unknown";
  37. }
  38. }
  39. static const char* keypad_test_get_type_name(InputType type) {
  40. switch(type) {
  41. case InputTypePress:
  42. return "Press";
  43. case InputTypeRelease:
  44. return "Release";
  45. case InputTypeShort:
  46. return "Short";
  47. case InputTypeLong:
  48. return "Long";
  49. case InputTypeRepeat:
  50. return "Repeat";
  51. default:
  52. return "Unknown";
  53. }
  54. }
  55. static void keypad_test_reset_state(KeypadTestState* state) {
  56. state->left = 0;
  57. state->right = 0;
  58. state->up = 0;
  59. state->down = 0;
  60. state->ok = 0;
  61. }
  62. static void keypad_test_render_callback(Canvas* canvas, void* ctx) {
  63. KeypadTestState* state = (KeypadTestState*)acquire_mutex((ValueMutex*)ctx, 25);
  64. canvas_clear(canvas);
  65. char strings[5][20];
  66. sprintf(strings[0], "Ok: %d", state->ok);
  67. sprintf(strings[1], "L: %d", state->left);
  68. sprintf(strings[2], "R: %d", state->right);
  69. sprintf(strings[3], "U: %d", state->up);
  70. sprintf(strings[4], "D: %d", state->down);
  71. canvas_set_font(canvas, FontPrimary);
  72. canvas_draw_str(canvas, 0, 10, "Keypad test");
  73. canvas_set_font(canvas, FontSecondary);
  74. canvas_draw_str(canvas, 0, 24, strings[1]);
  75. canvas_draw_str(canvas, 35, 24, strings[2]);
  76. canvas_draw_str(canvas, 0, 36, strings[3]);
  77. canvas_draw_str(canvas, 35, 36, strings[4]);
  78. canvas_draw_str(canvas, 0, 48, strings[0]);
  79. canvas_draw_circle(canvas, 100, 26, 25);
  80. if(state->press[0]) canvas_draw_disc(canvas, 118, 26, 5);
  81. if(state->press[1]) canvas_draw_disc(canvas, 82, 26, 5);
  82. if(state->press[2]) canvas_draw_disc(canvas, 100, 8, 5);
  83. if(state->press[3]) canvas_draw_disc(canvas, 100, 44, 5);
  84. if(state->press[4]) canvas_draw_disc(canvas, 100, 26, 5);
  85. canvas_draw_str(canvas, 10, 63, "[back] - reset, hold to exit");
  86. release_mutex((ValueMutex*)ctx, state);
  87. }
  88. static void keypad_test_input_callback(InputEvent* input_event, void* ctx) {
  89. osMessageQueueId_t event_queue = ctx;
  90. KeypadTestEvent event;
  91. event.type = EventTypeInput;
  92. event.input = *input_event;
  93. osMessageQueuePut(event_queue, &event, 0, osWaitForever);
  94. }
  95. int32_t keypad_test_app(void* p) {
  96. osMessageQueueId_t event_queue = osMessageQueueNew(32, sizeof(KeypadTestEvent), NULL);
  97. furi_check(event_queue);
  98. KeypadTestState _state = {{false, false, false, false, false}, 0, 0, 0, 0, 0};
  99. ValueMutex state_mutex;
  100. if(!init_mutex(&state_mutex, &_state, sizeof(KeypadTestState))) {
  101. FURI_LOG_E("KeypadTest", "cannot create mutex");
  102. return 0;
  103. }
  104. ViewPort* view_port = view_port_alloc();
  105. view_port_draw_callback_set(view_port, keypad_test_render_callback, &state_mutex);
  106. view_port_input_callback_set(view_port, keypad_test_input_callback, event_queue);
  107. // Open GUI and register view_port
  108. Gui* gui = furi_record_open("gui");
  109. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  110. KeypadTestEvent event;
  111. while(1) {
  112. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  113. KeypadTestState* state = (KeypadTestState*)acquire_mutex_block(&state_mutex);
  114. if(event_status == osOK) {
  115. if(event.type == EventTypeInput) {
  116. FURI_LOG_I(
  117. "KeypadTest",
  118. "key: %s type: %s",
  119. keypad_test_get_key_name(event.input.key),
  120. keypad_test_get_type_name(event.input.type));
  121. if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  122. release_mutex(&state_mutex, state);
  123. break;
  124. }
  125. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  126. keypad_test_reset_state(state);
  127. }
  128. if(event.input.key == InputKeyRight) {
  129. if(event.input.type == InputTypePress) {
  130. state->press[0] = true;
  131. } else if(event.input.type == InputTypeRelease) {
  132. state->press[0] = false;
  133. }
  134. if(event.input.type == InputTypeShort) {
  135. ++state->right;
  136. }
  137. }
  138. if(event.input.key == InputKeyLeft) {
  139. if(event.input.type == InputTypePress) {
  140. state->press[1] = true;
  141. } else if(event.input.type == InputTypeRelease) {
  142. state->press[1] = false;
  143. }
  144. if(event.input.type == InputTypeShort) {
  145. ++state->left;
  146. }
  147. }
  148. if(event.input.key == InputKeyUp) {
  149. if(event.input.type == InputTypePress) {
  150. state->press[2] = true;
  151. } else if(event.input.type == InputTypeRelease) {
  152. state->press[2] = false;
  153. }
  154. if(event.input.type == InputTypeShort) {
  155. ++state->up;
  156. }
  157. }
  158. if(event.input.key == InputKeyDown) {
  159. if(event.input.type == InputTypePress) {
  160. state->press[3] = true;
  161. } else if(event.input.type == InputTypeRelease) {
  162. state->press[3] = false;
  163. }
  164. if(event.input.type == InputTypeShort) {
  165. ++state->down;
  166. }
  167. }
  168. if(event.input.key == InputKeyOk) {
  169. if(event.input.type == InputTypePress) {
  170. state->press[4] = true;
  171. } else if(event.input.type == InputTypeRelease) {
  172. state->press[4] = false;
  173. }
  174. if(event.input.type == InputTypeShort) {
  175. ++state->ok;
  176. }
  177. }
  178. }
  179. }
  180. view_port_update(view_port);
  181. release_mutex(&state_mutex, state);
  182. }
  183. // remove & free all stuff created by app
  184. gui_remove_view_port(gui, view_port);
  185. view_port_free(view_port);
  186. osMessageQueueDelete(event_queue);
  187. delete_mutex(&state_mutex);
  188. return 0;
  189. }