gpio-tester.c 4.1 KB

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