blink_test.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #define BLINK_COLOR_COUNT 7
  7. typedef enum {
  8. EventTypeTick,
  9. EventTypeKey,
  10. } EventType;
  11. typedef struct {
  12. EventType type;
  13. InputEvent input;
  14. } BlinkEvent;
  15. static void blink_test_update(void* ctx) {
  16. furi_assert(ctx);
  17. osMessageQueueId_t event_queue = ctx;
  18. BlinkEvent event = {.type = EventTypeTick};
  19. osMessageQueuePut(event_queue, &event, 0, 0);
  20. }
  21. static void blink_test_draw_callback(Canvas* canvas, void* ctx) {
  22. canvas_clear(canvas);
  23. canvas_set_font(canvas, FontPrimary);
  24. canvas_draw_str(canvas, 2, 10, "Blink application");
  25. }
  26. static void blink_test_input_callback(InputEvent* input_event, void* ctx) {
  27. furi_assert(ctx);
  28. osMessageQueueId_t event_queue = ctx;
  29. BlinkEvent event = {.type = EventTypeKey, .input = *input_event};
  30. osMessageQueuePut(event_queue, &event, 0, 0);
  31. }
  32. int32_t blink_test_app(void* p) {
  33. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(BlinkEvent), NULL);
  34. // Configure view port
  35. ViewPort* view_port = view_port_alloc();
  36. furi_check(view_port);
  37. view_port_draw_callback_set(view_port, blink_test_draw_callback, NULL);
  38. view_port_input_callback_set(view_port, blink_test_input_callback, event_queue);
  39. osTimerId_t timer = osTimerNew(blink_test_update, osTimerPeriodic, event_queue, NULL);
  40. osTimerStart(timer, 1000);
  41. // Register view port in GUI
  42. Gui* gui = furi_record_open("gui");
  43. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  44. NotificationApp* notifications = furi_record_open("notification");
  45. const NotificationSequence* colors[BLINK_COLOR_COUNT] = {
  46. &sequence_blink_red_100,
  47. &sequence_blink_green_100,
  48. &sequence_blink_blue_100,
  49. &sequence_blink_yellow_100,
  50. &sequence_blink_cyan_100,
  51. &sequence_blink_magenta_100,
  52. &sequence_blink_white_100,
  53. };
  54. uint8_t state = 0;
  55. BlinkEvent event;
  56. while(1) {
  57. furi_check(osMessageQueueGet(event_queue, &event, NULL, osWaitForever) == osOK);
  58. if(event.type == EventTypeKey) {
  59. if((event.input.type == InputTypeShort) && (event.input.key == InputKeyBack)) {
  60. furi_record_close("notification");
  61. view_port_enabled_set(view_port, false);
  62. gui_remove_view_port(gui, view_port);
  63. view_port_free(view_port);
  64. osMessageQueueDelete(event_queue);
  65. osTimerDelete(timer);
  66. return 0;
  67. }
  68. } else {
  69. notification_message(notifications, colors[state]);
  70. state++;
  71. if(state >= BLINK_COLOR_COUNT) {
  72. state = 0;
  73. }
  74. }
  75. }
  76. return 0;
  77. }