zeitraffer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. notification_message(notifications, &sequence_click);
  99. //view_port_update(view_port);
  100. }
  101. if(event.input.key == InputKeyLeft) {
  102. Count--;
  103. notification_message(notifications, &sequence_click);
  104. //view_port_update(view_port);
  105. }
  106. if(event.input.key == InputKeyUp) {
  107. Time++;
  108. notification_message(notifications, &sequence_click);
  109. //view_port_update(view_port);
  110. }
  111. if(event.input.key == InputKeyDown) {
  112. Time--;
  113. notification_message(notifications, &sequence_click);
  114. //view_port_update(view_port);
  115. }
  116. if(event.input.key == InputKeyOk) {
  117. if(furi_timer_is_running(timer)) {
  118. notification_message(notifications, &sequence_error);
  119. furi_timer_stop(timer);
  120. }
  121. else {
  122. furi_timer_start(timer, 1000);
  123. if (WorkCount == 0) WorkCount = Count;
  124. if (WorkTime == 0) WorkTime = 3;
  125. if (Count == 0) {InfiniteShot = true; WorkCount = 1;} else InfiniteShot = false;
  126. notification_message(notifications, &sequence_success);
  127. }
  128. }
  129. }
  130. if(event.input.type == InputTypeLong) {
  131. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  132. if(event.input.key == InputKeyBack) {
  133. notification_message(notifications, &sequence_click);
  134. break;
  135. }
  136. if(event.input.key == InputKeyOk) {
  137. furi_timer_start(timer, 1000);
  138. WorkCount = Count;
  139. WorkTime = 3;
  140. if (Count == 0) {InfiniteShot = true; WorkCount = 1;} else InfiniteShot = false;
  141. notification_message(notifications, &sequence_success);
  142. }
  143. }
  144. if(event.input.type == InputTypeRepeat) {
  145. if(event.input.key == InputKeyRight) {
  146. Count = Count+10;
  147. //view_port_update(view_port);
  148. }
  149. if(event.input.key == InputKeyLeft) {
  150. Count = Count-10;
  151. //view_port_update(view_port);
  152. }
  153. if(event.input.key == InputKeyUp) {
  154. Time = Time+10;
  155. //view_port_update(view_port);
  156. }
  157. if(event.input.key == InputKeyDown) {
  158. Time = Time-10;
  159. //view_port_update(view_port);
  160. }
  161. }
  162. // Наше событие — это сработавший таймер
  163. } else if(event.type == EventTypeTick) {
  164. // Отправляем нотификацию мигания синим светодиодом
  165. WorkTime--;
  166. notification_message(notifications, &sequence_blink_blue_100);
  167. if( WorkTime < 1 ) {
  168. WorkCount--;
  169. view_port_update(view_port);
  170. notification_message(notifications, &sequence_click);
  171. gpio_item_set_all_pins(true);
  172. //gpio_item_set_pin(6, true);
  173. furi_delay_ms(200);
  174. //gpio_item_set_pin(6, false);
  175. gpio_item_set_all_pins(false);
  176. if (InfiniteShot) WorkCount++;
  177. WorkTime = Time;
  178. view_port_update(view_port);
  179. }
  180. if( WorkCount < 1 ) {
  181. gpio_item_set_all_pins(false);
  182. furi_timer_stop(timer);
  183. notification_message(notifications, &sequence_audiovisual_alert);
  184. WorkTime = 3;
  185. WorkCount = 0;
  186. }
  187. }
  188. if (Time < 1) Time = 1;
  189. if (Count < 0) Count = 0;
  190. }
  191. // Очищаем таймер
  192. furi_timer_free(timer);
  193. // Специальная очистка памяти, занимаемой очередью
  194. furi_message_queue_free(event_queue);
  195. // Чистим созданные объекты, связанные с интерфейсом
  196. gui_remove_view_port(gui, view_port);
  197. view_port_free(view_port);
  198. furi_record_close(RECORD_GUI);
  199. // Очищаем нотификации
  200. furi_record_close(RECORD_NOTIFICATION);
  201. return 0;
  202. }