blink_test.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. canvas_clear(canvas);
  24. canvas_set_font(canvas, FontPrimary);
  25. canvas_draw_str(canvas, 2, 10, "Blink application");
  26. }
  27. static void blink_test_input_callback(InputEvent* input_event, void* ctx) {
  28. furi_assert(ctx);
  29. osMessageQueueId_t event_queue = ctx;
  30. BlinkEvent event = {.type = BlinkEventTypeInput, .input = *input_event};
  31. osMessageQueuePut(event_queue, &event, 0, osWaitForever);
  32. }
  33. int32_t blink_test_app(void* p) {
  34. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(BlinkEvent), NULL);
  35. // Configure view port
  36. ViewPort* view_port = view_port_alloc();
  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, osKernelGetTickFreq());
  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 == BlinkEventTypeInput) {
  59. if((event.input.type == InputTypeShort) && (event.input.key == InputKeyBack)) {
  60. break;
  61. }
  62. } else {
  63. notification_message(notifications, colors[state]);
  64. state++;
  65. if(state >= BLINK_COLOR_COUNT) {
  66. state = 0;
  67. }
  68. }
  69. }
  70. osTimerDelete(timer);
  71. gui_remove_view_port(gui, view_port);
  72. view_port_free(view_port);
  73. osMessageQueueDelete(event_queue);
  74. furi_record_close("notification");
  75. furi_record_close("gui");
  76. return 0;
  77. }