zeitraffer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <notification/notification_messages.h>
  6. #include "gpio/gpio_item.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; // Режим BULB
  14. int Backlight = 0; // Подсветка: вкл/выкл/авто
  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. switch (Backlight) {
  48. case 1:
  49. canvas_draw_str(canvas, 3, 55, "Backlight: ON");
  50. break;
  51. case 2:
  52. canvas_draw_str(canvas, 3, 55, "Backlight: OFF");
  53. break;
  54. default:
  55. canvas_draw_str(canvas, 3, 55, "Backlight: AUTO");
  56. }
  57. }
  58. static void input_callback(InputEvent* input_event, void* ctx) {
  59. // Проверяем, что контекст не нулевой
  60. furi_assert(ctx);
  61. FuriMessageQueue* event_queue = ctx;
  62. ZeitrafferEvent event = {.type = EventTypeInput, .input = *input_event};
  63. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  64. }
  65. static void timer_callback(FuriMessageQueue* event_queue) {
  66. // Проверяем, что контекст не нулевой
  67. furi_assert(event_queue);
  68. ZeitrafferEvent event = {.type = EventTypeTick};
  69. furi_message_queue_put(event_queue, &event, 0);
  70. }
  71. int32_t zeitraffer_app(void* p) {
  72. UNUSED(p);
  73. // Текущее событие типа кастомного типа ZeitrafferEvent
  74. ZeitrafferEvent event;
  75. // Очередь событий на 8 элементов размера ZeitrafferEvent
  76. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ZeitrafferEvent));
  77. // Создаем новый view port
  78. ViewPort* view_port = view_port_alloc();
  79. // Создаем callback отрисовки, без контекста
  80. view_port_draw_callback_set(view_port, draw_callback, NULL);
  81. // Создаем callback нажатий на клавиши, в качестве контекста передаем
  82. // нашу очередь сообщений, чтоб запихивать в неё эти события
  83. view_port_input_callback_set(view_port, input_callback, event_queue);
  84. // Создаем GUI приложения
  85. Gui* gui = furi_record_open(RECORD_GUI);
  86. // Подключаем view port к GUI в полноэкранном режиме
  87. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  88. // Конфигурим пины
  89. gpio_item_configure_all_pins(GpioModeOutputPushPull);
  90. // Создаем периодический таймер с коллбэком, куда в качестве
  91. // контекста будет передаваться наша очередь событий
  92. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  93. // Запускаем таймер
  94. //furi_timer_start(timer, 1500);
  95. // Включаем нотификации
  96. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  97. // Бесконечный цикл обработки очереди событий
  98. while(1) {
  99. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  100. // и проверяем, что у нас получилось это сделать
  101. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  102. // Наше событие — это нажатие кнопки
  103. if(event.type == EventTypeInput) {
  104. if(event.input.type == InputTypeShort) { // Короткие нажатия
  105. if(event.input.key == InputKeyBack) {
  106. if(furi_timer_is_running(timer)) { // Если таймер запущен - нефиг мацать кнопки!
  107. notification_message(notifications, &sequence_error);
  108. }
  109. else {
  110. WorkCount = Count;
  111. WorkTime = 3;
  112. if (Count == 0) {
  113. InfiniteShot = true;
  114. WorkCount = 1;
  115. }
  116. else
  117. InfiniteShot = false;
  118. notification_message(notifications, &sequence_success);
  119. }
  120. }
  121. if(event.input.key == InputKeyRight) {
  122. if(furi_timer_is_running(timer)) {
  123. notification_message(notifications, &sequence_error);
  124. }
  125. else {
  126. Count++;
  127. notification_message(notifications, &sequence_click);
  128. }
  129. }
  130. if(event.input.key == InputKeyLeft) {
  131. if(furi_timer_is_running(timer)) {
  132. notification_message(notifications, &sequence_error);
  133. }
  134. else {
  135. Count--;
  136. notification_message(notifications, &sequence_click);
  137. }
  138. }
  139. if(event.input.key == InputKeyUp) {
  140. if(furi_timer_is_running(timer)) {
  141. notification_message(notifications, &sequence_error);
  142. }
  143. else {
  144. Time++;
  145. notification_message(notifications, &sequence_click);
  146. }
  147. }
  148. if(event.input.key == InputKeyDown) {
  149. if(furi_timer_is_running(timer)) {
  150. notification_message(notifications, &sequence_error);
  151. }
  152. else {
  153. Time--;
  154. notification_message(notifications, &sequence_click);
  155. }
  156. }
  157. if(event.input.key == InputKeyOk) {
  158. if(furi_timer_is_running(timer)) {
  159. notification_message(notifications, &sequence_click);
  160. furi_timer_stop(timer);
  161. }
  162. else {
  163. furi_timer_start(timer, 1000);
  164. if (WorkCount == 0)
  165. WorkCount = Count;
  166. if (WorkTime == 0)
  167. WorkTime = 3;
  168. if (Count == 0) {
  169. InfiniteShot = true;
  170. WorkCount = 1;
  171. }
  172. else
  173. InfiniteShot = false;
  174. if (Count == -1) {
  175. gpio_item_set_pin(4, true);
  176. gpio_item_set_pin(5, true);
  177. Bulb = true;
  178. WorkCount = 1;
  179. WorkTime = Time;
  180. }
  181. else
  182. Bulb = false;
  183. notification_message(notifications, &sequence_success);
  184. }
  185. }
  186. }
  187. if(event.input.type == InputTypeLong) { // Длинные нажатия
  188. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  189. if(event.input.key == InputKeyBack) {
  190. if(furi_timer_is_running(timer)) { // А если работает таймер - не выходим :D
  191. notification_message(notifications, &sequence_error);
  192. }
  193. else {
  194. notification_message(notifications, &sequence_click);
  195. gpio_item_set_all_pins(false);
  196. furi_timer_stop(timer);
  197. notification_message(notifications, &sequence_display_backlight_enforce_auto);
  198. break;
  199. }
  200. }
  201. if(event.input.key == InputKeyOk) {
  202. // Нам ваша подсветка и нахой не нужна! Или нужна?
  203. Backlight++;
  204. if (Backlight > 2) Backlight = 0;
  205. }
  206. }
  207. if(event.input.type == InputTypeRepeat) { // Зажатые кнопки
  208. if(event.input.key == InputKeyRight) {
  209. if(furi_timer_is_running(timer)) {
  210. notification_message(notifications, &sequence_error);
  211. }
  212. else {
  213. Count = Count+10;
  214. }
  215. }
  216. if(event.input.key == InputKeyLeft) {
  217. if(furi_timer_is_running(timer)) {
  218. notification_message(notifications, &sequence_error);
  219. }
  220. else {
  221. Count = Count-10;
  222. }
  223. }
  224. if(event.input.key == InputKeyUp) {
  225. if(furi_timer_is_running(timer)) {
  226. notification_message(notifications, &sequence_error);
  227. }
  228. else {
  229. Time = Time+10;
  230. }
  231. }
  232. if(event.input.key == InputKeyDown) {
  233. if(furi_timer_is_running(timer)) {
  234. notification_message(notifications, &sequence_error);
  235. }
  236. else {
  237. Time = Time-10;
  238. }
  239. }
  240. }
  241. }
  242. // Наше событие — это сработавший таймер
  243. else if(event.type == EventTypeTick) {
  244. WorkTime--;
  245. if( WorkTime < 1 ) { // фоткаем
  246. notification_message(notifications, &sequence_blink_white_100);
  247. if (Bulb) {
  248. gpio_item_set_all_pins(false); WorkCount = 0;
  249. }
  250. else {
  251. WorkCount--;
  252. view_port_update(view_port);
  253. notification_message(notifications, &sequence_click);
  254. // Дрыгаем ногами
  255. //gpio_item_set_all_pins(true);
  256. gpio_item_set_pin(4, true);
  257. gpio_item_set_pin(5, true);
  258. furi_delay_ms(400); // На короткие нажатия фотик плохо реагирует
  259. gpio_item_set_pin(4, false);
  260. gpio_item_set_pin(5, false);
  261. //gpio_item_set_all_pins(false);
  262. if (InfiniteShot) WorkCount++;
  263. WorkTime = Time;
  264. view_port_update(view_port);
  265. }
  266. }
  267. else {
  268. // Отправляем нотификацию мигания синим светодиодом
  269. notification_message(notifications, &sequence_blink_blue_100);
  270. }
  271. if( WorkCount < 1 ) { // закончили
  272. gpio_item_set_all_pins(false);
  273. furi_timer_stop(timer);
  274. notification_message(notifications, &sequence_audiovisual_alert);
  275. WorkTime = 3;
  276. WorkCount = 0;
  277. }
  278. switch (Backlight) { // чо по подсветке?
  279. case 1:
  280. notification_message(notifications, &sequence_display_backlight_on);
  281. break;
  282. case 2:
  283. notification_message(notifications, &sequence_display_backlight_off);
  284. break;
  285. default:
  286. notification_message(notifications, &sequence_display_backlight_enforce_auto);
  287. }
  288. }
  289. if (Time < 1) Time = 1; // Не даём открутить таймер меньше единицы
  290. if (Count < -1) Count = 0; // А тут даём, бо 0 кадров это бесконечная съёмка, а -1 кадров - BULB
  291. }
  292. // Очищаем таймер
  293. furi_timer_free(timer);
  294. // Специальная очистка памяти, занимаемой очередью
  295. furi_message_queue_free(event_queue);
  296. // Чистим созданные объекты, связанные с интерфейсом
  297. gui_remove_view_port(gui, view_port);
  298. view_port_free(view_port);
  299. furi_record_close(RECORD_GUI);
  300. // Очищаем нотификации
  301. furi_record_close(RECORD_NOTIFICATION);
  302. return 0;
  303. }