keypad_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. extern TIM_HandleTypeDef SPEAKER_TIM;
  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. } State;
  13. typedef enum {
  14. EventTypeTick,
  15. EventTypeKey,
  16. } EventType;
  17. typedef struct {
  18. union {
  19. InputEvent input;
  20. } value;
  21. EventType type;
  22. } AppEvent;
  23. static void reset_state(State* state) {
  24. state->left = 0;
  25. state->right = 0;
  26. state->up = 0;
  27. state->down = 0;
  28. state->ok = 0;
  29. }
  30. static void render_callback(Canvas* canvas, void* ctx) {
  31. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  32. canvas_clear(canvas);
  33. char strings[5][20];
  34. sprintf(strings[0], "Ok: %d", state->ok);
  35. sprintf(strings[1], "L: %d", state->left);
  36. sprintf(strings[2], "R: %d", state->right);
  37. sprintf(strings[3], "U: %d", state->up);
  38. sprintf(strings[4], "D: %d", state->down);
  39. canvas_set_font(canvas, FontPrimary);
  40. canvas_draw_str(canvas, 0, 10, "Keypad test");
  41. canvas_set_font(canvas, FontSecondary);
  42. canvas_draw_str(canvas, 0, 24, strings[1]);
  43. canvas_draw_str(canvas, 35, 24, strings[2]);
  44. canvas_draw_str(canvas, 0, 36, strings[3]);
  45. canvas_draw_str(canvas, 35, 36, strings[4]);
  46. canvas_draw_str(canvas, 0, 48, strings[0]);
  47. canvas_draw_circle(canvas, 100, 26, 25);
  48. if(state->press[0]) canvas_draw_disc(canvas, 118, 26, 5);
  49. if(state->press[1]) canvas_draw_disc(canvas, 82, 26, 5);
  50. if(state->press[2]) canvas_draw_disc(canvas, 100, 8, 5);
  51. if(state->press[3]) canvas_draw_disc(canvas, 100, 44, 5);
  52. if(state->press[4]) canvas_draw_disc(canvas, 100, 26, 5);
  53. canvas_draw_str(canvas, 10, 63, "[back] - reset, hold to exit");
  54. release_mutex((ValueMutex*)ctx, state);
  55. }
  56. static void input_callback(InputEvent* input_event, void* ctx) {
  57. osMessageQueueId_t event_queue = ctx;
  58. AppEvent event;
  59. event.type = EventTypeKey;
  60. event.value.input = *input_event;
  61. osMessageQueuePut(event_queue, &event, 0, 0);
  62. }
  63. int32_t keypad_test(void* p) {
  64. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(AppEvent), NULL);
  65. furi_check(event_queue);
  66. State _state = {{false, false, false, false, false}, 0, 0, 0, 0, 0};
  67. ValueMutex state_mutex;
  68. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  69. printf("[keypad_test] cannot create mutex\r\n");
  70. return 0;
  71. }
  72. ViewPort* view_port = view_port_alloc();
  73. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  74. view_port_input_callback_set(view_port, input_callback, event_queue);
  75. // Open GUI and register view_port
  76. Gui* gui = furi_record_open("gui");
  77. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  78. AppEvent event;
  79. while(1) {
  80. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  81. State* state = (State*)acquire_mutex_block(&state_mutex);
  82. if(event_status == osOK) {
  83. if(event.type == EventTypeKey) {
  84. if(event.value.input.type == InputTypeLong &&
  85. event.value.input.key == InputKeyBack) {
  86. printf("[keypad test] bye!\r\n");
  87. release_mutex(&state_mutex, state);
  88. break;
  89. }
  90. if(event.value.input.type == InputTypeShort &&
  91. event.value.input.key == InputKeyBack) {
  92. reset_state(state);
  93. }
  94. if(event.value.input.key == InputKeyRight) {
  95. if(event.value.input.type == InputTypePress) {
  96. state->press[0] = true;
  97. } else if(event.value.input.type == InputTypeRelease) {
  98. state->press[0] = false;
  99. }
  100. if(event.value.input.type == InputTypeShort) {
  101. ++state->right;
  102. }
  103. }
  104. if(event.value.input.key == InputKeyLeft) {
  105. if(event.value.input.type == InputTypePress) {
  106. state->press[1] = true;
  107. } else if(event.value.input.type == InputTypeRelease) {
  108. state->press[1] = false;
  109. }
  110. if(event.value.input.type == InputTypeShort) {
  111. ++state->left;
  112. }
  113. }
  114. if(event.value.input.key == InputKeyUp) {
  115. if(event.value.input.type == InputTypePress) {
  116. state->press[2] = true;
  117. } else if(event.value.input.type == InputTypeRelease) {
  118. state->press[2] = false;
  119. }
  120. if(event.value.input.type == InputTypeShort) {
  121. ++state->up;
  122. }
  123. }
  124. if(event.value.input.key == InputKeyDown) {
  125. if(event.value.input.type == InputTypePress) {
  126. state->press[3] = true;
  127. } else if(event.value.input.type == InputTypeRelease) {
  128. state->press[3] = false;
  129. }
  130. if(event.value.input.type == InputTypeShort) {
  131. ++state->down;
  132. }
  133. }
  134. if(event.value.input.key == InputKeyOk) {
  135. if(event.value.input.type == InputTypePress) {
  136. state->press[4] = true;
  137. } else if(event.value.input.type == InputTypeRelease) {
  138. state->press[4] = false;
  139. }
  140. if(event.value.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. }