zeitraffer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <notification/notification_messages.h>
  6. #include <notification/notification_messages_notes.h>
  7. int Time = 10;
  8. int Count = 10;
  9. int WorkTime = 0;
  10. int WorkCount = 0;
  11. bool InfiniteShot = false;
  12. typedef enum {
  13. EventTypeTick,
  14. EventTypeInput,
  15. } EventType;
  16. typedef struct {
  17. EventType type;
  18. InputEvent input;
  19. } ZeitrafferEvent;
  20. static void draw_callback(Canvas* canvas, void* ctx) {
  21. UNUSED(ctx);
  22. char temp_str[36];
  23. canvas_clear(canvas);
  24. canvas_set_font(canvas, FontPrimary);
  25. snprintf(temp_str,sizeof(temp_str),"Set: %i frames, %i sec",Count,Time);
  26. canvas_draw_str(canvas, 3, 20, temp_str);
  27. snprintf(temp_str,sizeof(temp_str),"Left: %i frames, %i sec",WorkCount,WorkTime);
  28. canvas_draw_str(canvas, 3, 45, temp_str);
  29. }
  30. static void input_callback(InputEvent* input_event, void* ctx) {
  31. // Проверяем, что контекст не нулевой
  32. furi_assert(ctx);
  33. FuriMessageQueue* event_queue = ctx;
  34. ZeitrafferEvent event = {.type = EventTypeInput, .input = *input_event};
  35. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  36. }
  37. static void timer_callback(FuriMessageQueue* event_queue) {
  38. // Проверяем, что контекст не нулевой
  39. furi_assert(event_queue);
  40. ZeitrafferEvent event = {.type = EventTypeTick};
  41. furi_message_queue_put(event_queue, &event, 0);
  42. }
  43. int32_t zeitraffer_app(void* p) {
  44. UNUSED(p);
  45. // Текущее событие типа кастомного типа ZeitrafferEvent
  46. ZeitrafferEvent event;
  47. // Очередь событий на 8 элементов размера ZeitrafferEvent
  48. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ZeitrafferEvent));
  49. // Создаем новый view port
  50. ViewPort* view_port = view_port_alloc();
  51. // Создаем callback отрисовки, без контекста
  52. view_port_draw_callback_set(view_port, draw_callback, NULL);
  53. // Создаем callback нажатий на клавиши, в качестве контекста передаем
  54. // нашу очередь сообщений, чтоб запихивать в неё эти события
  55. view_port_input_callback_set(view_port, input_callback, event_queue);
  56. // Создаем GUI приложения
  57. Gui* gui = furi_record_open(RECORD_GUI);
  58. // Подключаем view port к GUI в полноэкранном режиме
  59. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  60. // Создаем периодический таймер с коллбэком, куда в качестве
  61. // контекста будет передаваться наша очередь событий
  62. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  63. // Запускаем таймер
  64. //furi_timer_start(timer, 1500);
  65. // Включаем нотификации
  66. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  67. // Бесконечный цикл обработки очереди событий
  68. while(1) {
  69. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  70. // и проверяем, что у нас получилось это сделать
  71. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  72. // Наше событие — это нажатие кнопки
  73. if(event.type == EventTypeInput) {
  74. if(event.input.type == InputTypeShort) {
  75. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  76. if(event.input.key == InputKeyBack) {
  77. if(furi_timer_is_running(timer)) {
  78. notification_message(notifications, &sequence_error);
  79. }
  80. else {
  81. WorkCount = Count;
  82. WorkTime = 3;
  83. if (Count == 0) {InfiniteShot = true; WorkCount = 1;} else InfiniteShot = false;
  84. notification_message(notifications, &sequence_success);
  85. }
  86. // break;
  87. }
  88. if(event.input.key == InputKeyRight) {
  89. Count++;
  90. //view_port_update(view_port);
  91. }
  92. if(event.input.key == InputKeyLeft) {
  93. Count--;
  94. //view_port_update(view_port);
  95. }
  96. if(event.input.key == InputKeyUp) {
  97. Time++;
  98. //view_port_update(view_port);
  99. }
  100. if(event.input.key == InputKeyDown) {
  101. Time--;
  102. //view_port_update(view_port);
  103. }
  104. if(event.input.key == InputKeyOk) {
  105. if(furi_timer_is_running(timer)) {
  106. notification_message(notifications, &sequence_error);
  107. furi_timer_stop(timer);
  108. }
  109. else {
  110. furi_timer_start(timer, 1000);
  111. if (WorkCount == 0) WorkCount = Count;
  112. if (WorkTime == 0) WorkTime = 3;
  113. if (Count == 0) {InfiniteShot = true; WorkCount = 1;} else InfiniteShot = false;
  114. notification_message(notifications, &sequence_success);
  115. }
  116. }
  117. }
  118. if(event.input.type == InputTypeLong) {
  119. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  120. if(event.input.key == InputKeyBack) {
  121. // notification_message(notifications, &sequence_audiovisual_alert);
  122. break;
  123. }
  124. if(event.input.key == InputKeyOk) {
  125. furi_timer_start(timer, 1000);
  126. WorkCount = Count;
  127. WorkTime = 3;
  128. if (Count == 0) {InfiniteShot = true; WorkCount = 1;} else InfiniteShot = false;
  129. notification_message(notifications, &sequence_success);
  130. }
  131. }
  132. if(event.input.type == InputTypeRepeat) {
  133. if(event.input.key == InputKeyRight) {
  134. Count = Count+10;
  135. //view_port_update(view_port);
  136. }
  137. if(event.input.key == InputKeyLeft) {
  138. Count = Count-10;
  139. //view_port_update(view_port);
  140. }
  141. if(event.input.key == InputKeyUp) {
  142. Time = Time+10;
  143. //view_port_update(view_port);
  144. }
  145. if(event.input.key == InputKeyDown) {
  146. Time = Time-10;
  147. //view_port_update(view_port);
  148. }
  149. }
  150. // Наше событие — это сработавший таймер
  151. } else if(event.type == EventTypeTick) {
  152. // Отправляем нотификацию мигания синим светодиодом
  153. WorkTime--;
  154. notification_message(notifications, &sequence_blink_blue_100);
  155. if( WorkTime < 1 ) {
  156. WorkCount--;
  157. if (InfiniteShot) WorkCount++;
  158. notification_message(notifications, message_display_backlight_on);
  159. WorkTime = Time;
  160. }
  161. if( WorkCount < 1 ) {
  162. furi_timer_stop(timer);
  163. notification_message(notifications, &sequence_audiovisual_alert);
  164. WorkTime = 3;
  165. WorkCount = 0;
  166. }
  167. }
  168. if (Time < 1) Time = 1;
  169. if (Count < 0) Count = 0;
  170. }
  171. // Очищаем таймер
  172. furi_timer_free(timer);
  173. // Специальная очистка памяти, занимаемой очередью
  174. furi_message_queue_free(event_queue);
  175. // Чистим созданные объекты, связанные с интерфейсом
  176. gui_remove_view_port(gui, view_port);
  177. view_port_free(view_port);
  178. furi_record_close(RECORD_GUI);
  179. // Очищаем нотификации
  180. furi_record_close(RECORD_NOTIFICATION);
  181. return 0;
  182. }