blink_test.c 2.8 KB

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