keypad_test.c 6.2 KB

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