keypad_test.c 6.2 KB

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