zeitraffer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <flipper_format/flipper_format.h>
  7. #include "gpio_item.h"
  8. #include "gpio_timelapse_icons.h"
  9. #define CONFIG_FILE_DIRECTORY_PATH "/ext/apps_data/zeitraffer"
  10. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/zeitraffer.conf"
  11. // Часть кода покрадена из https://github.com/zmactep/flipperzero-hello-world
  12. int32_t Time = 10; // Таймер
  13. int32_t Count = 10; // Количество кадров
  14. int32_t WorkTime = 0; // Счётчик таймера
  15. int32_t WorkCount = 0; // Счётчик кадров
  16. bool InfiniteShot = false; // Бесконечная съёмка
  17. bool Bulb = false; // Режим BULB
  18. int32_t Backlight = 0; // Подсветка: вкл/выкл/авто
  19. int32_t Delay = 3; // Задержка на отскочить
  20. bool Work = false;
  21. const NotificationSequence sequence_click = {
  22. &message_note_c7,
  23. &message_delay_50,
  24. &message_sound_off,
  25. NULL,
  26. };
  27. typedef enum {
  28. EventTypeTick,
  29. EventTypeInput,
  30. } EventType;
  31. typedef struct {
  32. EventType type;
  33. InputEvent input;
  34. } ZeitrafferEvent;
  35. static void draw_callback(Canvas* canvas, void* ctx) {
  36. UNUSED(ctx);
  37. char temp_str[36];
  38. canvas_clear(canvas);
  39. canvas_set_font(canvas, FontPrimary);
  40. switch(Count) {
  41. case -1:
  42. snprintf(temp_str, sizeof(temp_str), "Set: BULB %li sec", Time);
  43. break;
  44. case 0:
  45. snprintf(temp_str, sizeof(temp_str), "Set: infinite, %li sec", Time);
  46. break;
  47. default:
  48. snprintf(temp_str, sizeof(temp_str), "Set: %li frames, %li sec", Count, Time);
  49. }
  50. canvas_draw_str(canvas, 3, 15, temp_str);
  51. snprintf(temp_str, sizeof(temp_str), "Left: %li frames, %li sec", WorkCount, WorkTime);
  52. canvas_draw_str(canvas, 3, 35, temp_str);
  53. switch(Backlight) {
  54. case 1:
  55. canvas_draw_str(canvas, 13, 55, "ON");
  56. break;
  57. case 2:
  58. canvas_draw_str(canvas, 13, 55, "OFF");
  59. break;
  60. default:
  61. canvas_draw_str(canvas, 13, 55, "AUTO");
  62. }
  63. if(Work) {
  64. canvas_draw_icon(canvas, 85, 41, &I_ButtonUpHollow_7x4);
  65. canvas_draw_icon(canvas, 85, 57, &I_ButtonDownHollow_7x4);
  66. canvas_draw_icon(canvas, 59, 48, &I_ButtonLeftHollow_4x7);
  67. canvas_draw_icon(canvas, 72, 48, &I_ButtonRightHollow_4x7);
  68. } else {
  69. canvas_draw_icon(canvas, 85, 41, &I_ButtonUp_7x4);
  70. canvas_draw_icon(canvas, 85, 57, &I_ButtonDown_7x4);
  71. canvas_draw_icon(canvas, 59, 48, &I_ButtonLeft_4x7);
  72. canvas_draw_icon(canvas, 72, 48, &I_ButtonRight_4x7);
  73. }
  74. canvas_draw_icon(canvas, 3, 48, &I_Pin_star_7x7);
  75. canvas_set_font(canvas, FontPrimary);
  76. canvas_draw_str(canvas, 65, 55, "F");
  77. canvas_set_font(canvas, FontPrimary);
  78. canvas_draw_str(canvas, 85, 55, "S");
  79. //canvas_draw_icon(canvas, 59, 48, &I_ButtonLeft_4x7);
  80. //canvas_draw_icon(canvas, 72, 48, &I_ButtonRight_4x7);
  81. if(Work) {
  82. canvas_draw_icon(canvas, 106, 46, &I_loading_10px);
  83. }
  84. }
  85. static void input_callback(InputEvent* input_event, void* ctx) {
  86. // Проверяем, что контекст не нулевой
  87. furi_assert(ctx);
  88. FuriMessageQueue* event_queue = ctx;
  89. ZeitrafferEvent event = {.type = EventTypeInput, .input = *input_event};
  90. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  91. }
  92. static void timer_callback(FuriMessageQueue* event_queue) {
  93. // Проверяем, что контекст не нулевой
  94. furi_assert(event_queue);
  95. ZeitrafferEvent event = {.type = EventTypeTick};
  96. furi_message_queue_put(event_queue, &event, 0);
  97. }
  98. int32_t zeitraffer_app(void* p) {
  99. UNUSED(p);
  100. // Текущее событие типа кастомного типа ZeitrafferEvent
  101. ZeitrafferEvent event;
  102. // Очередь событий на 8 элементов размера ZeitrafferEvent
  103. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ZeitrafferEvent));
  104. // Создаем новый view port
  105. ViewPort* view_port = view_port_alloc();
  106. // Создаем callback отрисовки, без контекста
  107. view_port_draw_callback_set(view_port, draw_callback, NULL);
  108. // Создаем callback нажатий на клавиши, в качестве контекста передаем
  109. // нашу очередь сообщений, чтоб запихивать в неё эти события
  110. view_port_input_callback_set(view_port, input_callback, event_queue);
  111. // Создаем GUI приложения
  112. Gui* gui = furi_record_open(RECORD_GUI);
  113. // Подключаем view port к GUI в полноэкранном режиме
  114. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  115. // Конфигурим пины
  116. gpio_item_configure_all_pins(GpioModeOutputPushPull);
  117. // Создаем периодический таймер с коллбэком, куда в качестве
  118. // контекста будет передаваться наша очередь событий
  119. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  120. // Запускаем таймер
  121. //furi_timer_start(timer, 1500);
  122. // Включаем нотификации
  123. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  124. Storage* storage = furi_record_open(RECORD_STORAGE);
  125. // Загружаем настройки
  126. FlipperFormat* load = flipper_format_file_alloc(storage);
  127. do {
  128. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  129. notification_message(notifications, &sequence_error);
  130. break;
  131. }
  132. if(!flipper_format_file_open_existing(load, CONFIG_FILE_PATH)) {
  133. notification_message(notifications, &sequence_error);
  134. break;
  135. }
  136. if(!flipper_format_read_int32(load, "Time", &Time, 1)) {
  137. notification_message(notifications, &sequence_error);
  138. break;
  139. }
  140. if(!flipper_format_read_int32(load, "Count", &Count, 1)) {
  141. notification_message(notifications, &sequence_error);
  142. break;
  143. }
  144. if(!flipper_format_read_int32(load, "Backlight", &Backlight, 1)) {
  145. notification_message(notifications, &sequence_error);
  146. break;
  147. }
  148. if(!flipper_format_read_int32(load, "Delay", &Delay, 1)) {
  149. notification_message(notifications, &sequence_error);
  150. break;
  151. }
  152. notification_message(notifications, &sequence_success);
  153. } while(0);
  154. flipper_format_free(load);
  155. // Бесконечный цикл обработки очереди событий
  156. while(1) {
  157. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  158. // и проверяем, что у нас получилось это сделать
  159. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  160. // Наше событие — это нажатие кнопки
  161. if(event.type == EventTypeInput) {
  162. if(event.input.type == InputTypeShort) { // Короткие нажатия
  163. if(event.input.key == InputKeyBack) {
  164. if(Work) { // Если таймер запущен - нефиг мацать кнопки!
  165. notification_message(notifications, &sequence_error);
  166. } else {
  167. WorkCount = Count;
  168. WorkTime = 3;
  169. if(Count == 0) {
  170. InfiniteShot = true;
  171. WorkCount = 1;
  172. } else
  173. InfiniteShot = false;
  174. notification_message(notifications, &sequence_success);
  175. }
  176. }
  177. if(event.input.key == InputKeyRight) {
  178. if(furi_timer_is_running(timer)) {
  179. notification_message(notifications, &sequence_error);
  180. } else {
  181. Count++;
  182. notification_message(notifications, &sequence_click);
  183. }
  184. }
  185. if(event.input.key == InputKeyLeft) {
  186. if(furi_timer_is_running(timer)) {
  187. notification_message(notifications, &sequence_error);
  188. } else {
  189. Count--;
  190. notification_message(notifications, &sequence_click);
  191. }
  192. }
  193. if(event.input.key == InputKeyUp) {
  194. if(furi_timer_is_running(timer)) {
  195. notification_message(notifications, &sequence_error);
  196. } else {
  197. Time++;
  198. notification_message(notifications, &sequence_click);
  199. }
  200. }
  201. if(event.input.key == InputKeyDown) {
  202. if(furi_timer_is_running(timer)) {
  203. notification_message(notifications, &sequence_error);
  204. } else {
  205. Time--;
  206. notification_message(notifications, &sequence_click);
  207. }
  208. }
  209. if(event.input.key == InputKeyOk) {
  210. if(furi_timer_is_running(timer)) {
  211. notification_message(notifications, &sequence_click);
  212. furi_timer_stop(timer);
  213. Work = false;
  214. } else {
  215. furi_timer_start(timer, 1000);
  216. Work = true;
  217. if(WorkCount == 0) WorkCount = Count;
  218. if(WorkTime == 0) WorkTime = Delay;
  219. if(Count == 1) WorkTime = Time;
  220. if(Count == 0) {
  221. InfiniteShot = true;
  222. WorkCount = 1;
  223. } else
  224. InfiniteShot = false;
  225. if(Count == -1) {
  226. gpio_item_set_pin(4, true);
  227. gpio_item_set_pin(5, true);
  228. Bulb = true;
  229. WorkCount = 1;
  230. WorkTime = Time;
  231. } else
  232. Bulb = false;
  233. notification_message(notifications, &sequence_success);
  234. }
  235. }
  236. }
  237. if(event.input.type == InputTypeLong) { // Длинные нажатия
  238. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  239. if(event.input.key == InputKeyBack) {
  240. if(furi_timer_is_running(timer)) { // А если работает таймер - не выходим :D
  241. notification_message(notifications, &sequence_error);
  242. } else {
  243. notification_message(notifications, &sequence_click);
  244. gpio_item_set_all_pins(false);
  245. furi_timer_stop(timer);
  246. notification_message(
  247. notifications, &sequence_display_backlight_enforce_auto);
  248. break;
  249. }
  250. }
  251. if(event.input.key == InputKeyOk) {
  252. // Нам ваша подсветка и нахой не нужна! Или нужна?
  253. Backlight++;
  254. if(Backlight > 2) Backlight = 0;
  255. }
  256. }
  257. if(event.input.type == InputTypeRepeat) { // Зажатые кнопки
  258. if(event.input.key == InputKeyRight) {
  259. if(furi_timer_is_running(timer)) {
  260. notification_message(notifications, &sequence_error);
  261. } else {
  262. Count = Count + 10;
  263. }
  264. }
  265. if(event.input.key == InputKeyLeft) {
  266. if(furi_timer_is_running(timer)) {
  267. notification_message(notifications, &sequence_error);
  268. } else {
  269. Count = Count - 10;
  270. }
  271. }
  272. if(event.input.key == InputKeyUp) {
  273. if(furi_timer_is_running(timer)) {
  274. notification_message(notifications, &sequence_error);
  275. } else {
  276. Time = Time + 10;
  277. }
  278. }
  279. if(event.input.key == InputKeyDown) {
  280. if(furi_timer_is_running(timer)) {
  281. notification_message(notifications, &sequence_error);
  282. } else {
  283. Time = Time - 10;
  284. }
  285. }
  286. }
  287. view_port_update(view_port);
  288. }
  289. // Наше событие — это сработавший таймер
  290. else if(event.type == EventTypeTick) {
  291. WorkTime--;
  292. if(WorkTime < 1) { // фоткаем
  293. notification_message(notifications, &sequence_blink_white_100);
  294. if(Bulb) {
  295. gpio_item_set_all_pins(false);
  296. WorkCount = 0;
  297. } else {
  298. WorkCount--;
  299. view_port_update(view_port);
  300. notification_message(notifications, &sequence_click);
  301. // Дрыгаем ногами
  302. //gpio_item_set_all_pins(true);
  303. gpio_item_set_pin(4, true);
  304. gpio_item_set_pin(5, true);
  305. furi_delay_ms(400); // На короткие нажатия фотик плохо реагирует
  306. gpio_item_set_pin(4, false);
  307. gpio_item_set_pin(5, false);
  308. //gpio_item_set_all_pins(false);
  309. if(InfiniteShot) WorkCount++;
  310. WorkTime = Time;
  311. view_port_update(view_port);
  312. }
  313. } else {
  314. // Отправляем нотификацию мигания синим светодиодом
  315. notification_message(notifications, &sequence_blink_blue_100);
  316. }
  317. if(WorkCount < 1) { // закончили
  318. Work = false;
  319. gpio_item_set_all_pins(false);
  320. furi_timer_stop(timer);
  321. notification_message(notifications, &sequence_audiovisual_alert);
  322. WorkTime = 3;
  323. WorkCount = 0;
  324. }
  325. switch(Backlight) { // чо по подсветке?
  326. case 1:
  327. notification_message(notifications, &sequence_display_backlight_on);
  328. break;
  329. case 2:
  330. notification_message(notifications, &sequence_display_backlight_off);
  331. break;
  332. default:
  333. notification_message(notifications, &sequence_display_backlight_enforce_auto);
  334. }
  335. view_port_update(view_port);
  336. }
  337. if(Time < 1) Time = 1; // Не даём открутить таймер меньше единицы
  338. if(Count < -1)
  339. Count = 0; // А тут даём, бо 0 кадров это бесконечная съёмка, а -1 кадров - BULB
  340. }
  341. // Схороняем настройки
  342. FlipperFormat* save = flipper_format_file_alloc(storage);
  343. do {
  344. if(!flipper_format_file_open_always(save, CONFIG_FILE_PATH)) {
  345. notification_message(notifications, &sequence_error);
  346. break;
  347. }
  348. if(!flipper_format_write_header_cstr(save, "Zeitraffer", 1)) {
  349. notification_message(notifications, &sequence_error);
  350. break;
  351. }
  352. if(!flipper_format_write_comment_cstr(
  353. save,
  354. "Zeitraffer app settings: n of frames, interval time, backlight type, Delay")) {
  355. notification_message(notifications, &sequence_error);
  356. break;
  357. }
  358. if(!flipper_format_write_int32(save, "Time", &Time, 1)) {
  359. notification_message(notifications, &sequence_error);
  360. break;
  361. }
  362. if(!flipper_format_write_int32(save, "Count", &Count, 1)) {
  363. notification_message(notifications, &sequence_error);
  364. break;
  365. }
  366. if(!flipper_format_write_int32(save, "Backlight", &Backlight, 1)) {
  367. notification_message(notifications, &sequence_error);
  368. break;
  369. }
  370. if(!flipper_format_write_int32(save, "Delay", &Delay, 1)) {
  371. notification_message(notifications, &sequence_error);
  372. break;
  373. }
  374. } while(0);
  375. flipper_format_free(save);
  376. furi_record_close(RECORD_STORAGE);
  377. // Очищаем таймер
  378. furi_timer_free(timer);
  379. // Специальная очистка памяти, занимаемой очередью
  380. furi_message_queue_free(event_queue);
  381. // Чистим созданные объекты, связанные с интерфейсом
  382. gui_remove_view_port(gui, view_port);
  383. view_port_free(view_port);
  384. furi_record_close(RECORD_GUI);
  385. // Очищаем нотификации
  386. furi_record_close(RECORD_NOTIFICATION);
  387. return 0;
  388. }