zeitraffer.c 9.9 KB

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