zeitraffer.c 7.8 KB

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