zeitraffer.c 9.6 KB

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