zeitraffer.c 9.5 KB

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