gpio-tester.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <furi.h>
  2. #include <api-hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <notification/notification-messages.h>
  6. typedef struct {
  7. const char* name;
  8. GpioPin pin;
  9. } GpioItem;
  10. const GpioItem GPIO_PINS[] = {
  11. {"1.2: PA7", {GPIOA, GPIO_PIN_7}},
  12. {"1.3: PA6", {GPIOA, GPIO_PIN_6}},
  13. {"1.4: PA4", {GPIOA, GPIO_PIN_4}},
  14. {"1.5: PB3", {GPIOB, GPIO_PIN_3}},
  15. {"1.6: PB2", {GPIOB, GPIO_PIN_2}},
  16. {"1.7: PC3", {GPIOC, GPIO_PIN_3}},
  17. {"2.7: PC1", {GPIOC, GPIO_PIN_1}},
  18. {"2.8: PC0", {GPIOC, GPIO_PIN_0}},
  19. };
  20. typedef enum {
  21. EventTypeTick,
  22. EventTypeKey,
  23. } EventType;
  24. typedef struct {
  25. union {
  26. InputEvent input;
  27. } value;
  28. EventType type;
  29. } AppEvent;
  30. typedef struct {
  31. uint8_t gpio_index;
  32. } State;
  33. static void render_callback(Canvas* canvas, void* ctx) {
  34. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  35. canvas_clear(canvas);
  36. canvas_set_color(canvas, ColorBlack);
  37. canvas_set_font(canvas, FontPrimary);
  38. canvas_draw_str(canvas, 2, 10, "GPIO demo");
  39. canvas_set_font(canvas, FontSecondary);
  40. canvas_draw_str(canvas, 2, 25, GPIO_PINS[state->gpio_index].name);
  41. release_mutex((ValueMutex*)ctx, state);
  42. }
  43. static void input_callback(InputEvent* input_event, void* ctx) {
  44. osMessageQueueId_t event_queue = ctx;
  45. AppEvent event;
  46. event.type = EventTypeKey;
  47. event.value.input = *input_event;
  48. osMessageQueuePut(event_queue, &event, 0, 0);
  49. }
  50. int32_t app_gpio_test(void* p) {
  51. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(AppEvent), NULL);
  52. furi_check(event_queue);
  53. State _state;
  54. _state.gpio_index = 0;
  55. ValueMutex state_mutex;
  56. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  57. printf("[gpio-tester] cannot create mutex\r\n");
  58. return 255;
  59. }
  60. ViewPort* view_port = view_port_alloc();
  61. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  62. view_port_input_callback_set(view_port, input_callback, event_queue);
  63. // Open GUI and register view_port
  64. Gui* gui = furi_record_open("gui");
  65. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  66. NotificationApp* notification = furi_record_open("notification");
  67. // configure pin
  68. for(uint8_t i = 0; i < sizeof(GPIO_PINS) / sizeof(GPIO_PINS[0]); i++) {
  69. hal_gpio_init(
  70. (GpioPin*)&GPIO_PINS[i].pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  71. }
  72. AppEvent event;
  73. while(1) {
  74. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  75. State* state = (State*)acquire_mutex_block(&state_mutex);
  76. if(event_status == osOK) {
  77. if(event.type == EventTypeKey) {
  78. if(event.value.input.type == InputTypeShort &&
  79. event.value.input.key == InputKeyBack) {
  80. printf("[gpio-tester] bye!\r\n");
  81. notification_message(notification, &sequence_reset_green);
  82. furi_record_close("notification");
  83. view_port_enabled_set(view_port, false);
  84. gui_remove_view_port(gui, view_port);
  85. view_port_free(view_port);
  86. return 0;
  87. }
  88. if(event.value.input.type == InputTypeShort &&
  89. event.value.input.key == InputKeyRight) {
  90. if(state->gpio_index < (sizeof(GPIO_PINS) / sizeof(GPIO_PINS[0]) - 1)) {
  91. state->gpio_index++;
  92. }
  93. }
  94. if(event.value.input.type == InputTypeShort &&
  95. event.value.input.key == InputKeyLeft) {
  96. if(state->gpio_index > 0) {
  97. state->gpio_index--;
  98. }
  99. }
  100. if(event.value.input.key == InputKeyOk) {
  101. if(event.value.input.type == InputTypePress) {
  102. hal_gpio_write((GpioPin*)&GPIO_PINS[state->gpio_index].pin, true);
  103. notification_message(notification, &sequence_set_green_255);
  104. } else if(event.value.input.type == InputTypeRelease) {
  105. hal_gpio_write((GpioPin*)&GPIO_PINS[state->gpio_index].pin, false);
  106. notification_message(notification, &sequence_reset_green);
  107. }
  108. }
  109. }
  110. }
  111. release_mutex(&state_mutex, state);
  112. view_port_update(view_port);
  113. }
  114. return 0;
  115. }