zeitraffer.c 7.5 KB

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