zeitraffer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "zeitraffer_icons.h"
  9. #include <assets_icons.h>
  10. #define CONFIG_FILE_PATH APP_DATA_PATH("timelapse.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(void* ctx) {
  93. FuriMessageQueue* event_queue = ctx;
  94. // Проверяем, что контекст не нулевой
  95. furi_assert(event_queue);
  96. ZeitrafferEvent event = {.type = EventTypeTick};
  97. furi_message_queue_put(event_queue, &event, 0);
  98. }
  99. int32_t zeitraffer_app(void* p) {
  100. UNUSED(p);
  101. // Текущее событие типа кастомного типа ZeitrafferEvent
  102. ZeitrafferEvent event;
  103. // Очередь событий на 8 элементов размера ZeitrafferEvent
  104. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ZeitrafferEvent));
  105. // Создаем новый view port
  106. ViewPort* view_port = view_port_alloc();
  107. // Создаем callback отрисовки, без контекста
  108. view_port_draw_callback_set(view_port, draw_callback, NULL);
  109. // Создаем callback нажатий на клавиши, в качестве контекста передаем
  110. // нашу очередь сообщений, чтоб запихивать в неё эти события
  111. view_port_input_callback_set(view_port, input_callback, event_queue);
  112. // Создаем GUI приложения
  113. Gui* gui = furi_record_open(RECORD_GUI);
  114. // Подключаем view port к GUI в полноэкранном режиме
  115. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  116. // Конфигурим пины
  117. gpio_item_configure_all_pins(GpioModeOutputPushPull);
  118. // Создаем периодический таймер с коллбэком, куда в качестве
  119. // контекста будет передаваться наша очередь событий
  120. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  121. // Запускаем таймер
  122. //furi_timer_start(timer, 1500);
  123. // Включаем нотификации
  124. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  125. Storage* storage = furi_record_open(RECORD_STORAGE);
  126. // Загружаем настройки
  127. FlipperFormat* load = flipper_format_file_alloc(storage);
  128. do {
  129. if(!flipper_format_file_open_existing(load, CONFIG_FILE_PATH)) {
  130. notification_message(notifications, &sequence_error);
  131. break;
  132. }
  133. if(!flipper_format_read_int32(load, "Time", &Time, 1)) {
  134. notification_message(notifications, &sequence_error);
  135. break;
  136. }
  137. if(!flipper_format_read_int32(load, "Count", &Count, 1)) {
  138. notification_message(notifications, &sequence_error);
  139. break;
  140. }
  141. if(!flipper_format_read_int32(load, "Backlight", &Backlight, 1)) {
  142. notification_message(notifications, &sequence_error);
  143. break;
  144. }
  145. if(!flipper_format_read_int32(load, "Delay", &Delay, 1)) {
  146. notification_message(notifications, &sequence_error);
  147. break;
  148. }
  149. notification_message(notifications, &sequence_success);
  150. } while(0);
  151. flipper_format_free(load);
  152. // Бесконечный цикл обработки очереди событий
  153. while(1) {
  154. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  155. // и проверяем, что у нас получилось это сделать
  156. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  157. // Наше событие — это нажатие кнопки
  158. if(event.type == EventTypeInput) {
  159. if(event.input.type == InputTypeShort) { // Короткие нажатия
  160. if(event.input.key == InputKeyBack) {
  161. if(Work) { // Если таймер запущен - нефиг мацать кнопки!
  162. notification_message(notifications, &sequence_error);
  163. } else {
  164. WorkCount = Count;
  165. WorkTime = 3;
  166. if(Count == 0) {
  167. InfiniteShot = true;
  168. WorkCount = 1;
  169. } else
  170. InfiniteShot = false;
  171. notification_message(notifications, &sequence_success);
  172. }
  173. }
  174. if(event.input.key == InputKeyRight) {
  175. if(furi_timer_is_running(timer)) {
  176. notification_message(notifications, &sequence_error);
  177. } else {
  178. Count++;
  179. notification_message(notifications, &sequence_click);
  180. }
  181. }
  182. if(event.input.key == InputKeyLeft) {
  183. if(furi_timer_is_running(timer)) {
  184. notification_message(notifications, &sequence_error);
  185. } else {
  186. Count--;
  187. notification_message(notifications, &sequence_click);
  188. }
  189. }
  190. if(event.input.key == InputKeyUp) {
  191. if(furi_timer_is_running(timer)) {
  192. notification_message(notifications, &sequence_error);
  193. } else {
  194. Time++;
  195. notification_message(notifications, &sequence_click);
  196. }
  197. }
  198. if(event.input.key == InputKeyDown) {
  199. if(furi_timer_is_running(timer)) {
  200. notification_message(notifications, &sequence_error);
  201. } else {
  202. Time--;
  203. notification_message(notifications, &sequence_click);
  204. }
  205. }
  206. if(event.input.key == InputKeyOk) {
  207. if(furi_timer_is_running(timer)) {
  208. notification_message(notifications, &sequence_click);
  209. furi_timer_stop(timer);
  210. Work = false;
  211. } else {
  212. furi_timer_start(timer, 1000);
  213. Work = true;
  214. if(WorkCount == 0) WorkCount = Count;
  215. if(WorkTime == 0) WorkTime = Delay;
  216. if(Count == 1) WorkTime = Time;
  217. if(Count == 0) {
  218. InfiniteShot = true;
  219. WorkCount = 1;
  220. } else
  221. InfiniteShot = false;
  222. if(Count == -1) {
  223. gpio_item_set_pin(4, true);
  224. gpio_item_set_pin(5, true);
  225. Bulb = true;
  226. WorkCount = 1;
  227. WorkTime = Time;
  228. } else
  229. Bulb = false;
  230. notification_message(notifications, &sequence_success);
  231. }
  232. }
  233. }
  234. if(event.input.type == InputTypeLong) { // Длинные нажатия
  235. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  236. if(event.input.key == InputKeyBack) {
  237. if(furi_timer_is_running(timer)) { // А если работает таймер - не выходим :D
  238. notification_message(notifications, &sequence_error);
  239. } else {
  240. notification_message(notifications, &sequence_click);
  241. gpio_item_set_all_pins(false);
  242. furi_timer_stop(timer);
  243. notification_message(
  244. notifications, &sequence_display_backlight_enforce_auto);
  245. break;
  246. }
  247. }
  248. if(event.input.key == InputKeyOk) {
  249. // Нам ваша подсветка и нахой не нужна! Или нужна?
  250. Backlight++;
  251. if(Backlight > 2) Backlight = 0;
  252. }
  253. }
  254. if(event.input.type == InputTypeRepeat) { // Зажатые кнопки
  255. if(event.input.key == InputKeyRight) {
  256. if(furi_timer_is_running(timer)) {
  257. notification_message(notifications, &sequence_error);
  258. } else {
  259. Count = Count + 10;
  260. }
  261. }
  262. if(event.input.key == InputKeyLeft) {
  263. if(furi_timer_is_running(timer)) {
  264. notification_message(notifications, &sequence_error);
  265. } else {
  266. Count = Count - 10;
  267. }
  268. }
  269. if(event.input.key == InputKeyUp) {
  270. if(furi_timer_is_running(timer)) {
  271. notification_message(notifications, &sequence_error);
  272. } else {
  273. Time = Time + 10;
  274. }
  275. }
  276. if(event.input.key == InputKeyDown) {
  277. if(furi_timer_is_running(timer)) {
  278. notification_message(notifications, &sequence_error);
  279. } else {
  280. Time = Time - 10;
  281. }
  282. }
  283. }
  284. view_port_update(view_port);
  285. }
  286. // Наше событие — это сработавший таймер
  287. else if(event.type == EventTypeTick) {
  288. WorkTime--;
  289. if(WorkTime < 1) { // фоткаем
  290. notification_message(notifications, &sequence_blink_white_100);
  291. if(Bulb) {
  292. gpio_item_set_all_pins(false);
  293. WorkCount = 0;
  294. } else {
  295. WorkCount--;
  296. view_port_update(view_port);
  297. notification_message(notifications, &sequence_click);
  298. // Дрыгаем ногами
  299. //gpio_item_set_all_pins(true);
  300. gpio_item_set_pin(4, true);
  301. gpio_item_set_pin(5, true);
  302. furi_delay_ms(400); // На короткие нажатия фотик плохо реагирует
  303. gpio_item_set_pin(4, false);
  304. gpio_item_set_pin(5, false);
  305. //gpio_item_set_all_pins(false);
  306. if(InfiniteShot) WorkCount++;
  307. WorkTime = Time;
  308. view_port_update(view_port);
  309. }
  310. } else {
  311. // Отправляем нотификацию мигания синим светодиодом
  312. notification_message(notifications, &sequence_blink_blue_100);
  313. }
  314. if(WorkCount < 1) { // закончили
  315. Work = false;
  316. gpio_item_set_all_pins(false);
  317. furi_timer_stop(timer);
  318. notification_message(notifications, &sequence_audiovisual_alert);
  319. WorkTime = 3;
  320. WorkCount = 0;
  321. }
  322. switch(Backlight) { // чо по подсветке?
  323. case 1:
  324. notification_message(notifications, &sequence_display_backlight_on);
  325. break;
  326. case 2:
  327. notification_message(notifications, &sequence_display_backlight_off);
  328. break;
  329. default:
  330. notification_message(notifications, &sequence_display_backlight_enforce_auto);
  331. }
  332. view_port_update(view_port);
  333. }
  334. if(Time < 1) Time = 1; // Не даём открутить таймер меньше единицы
  335. if(Count < -1)
  336. Count = 0; // А тут даём, бо 0 кадров это бесконечная съёмка, а -1 кадров - BULB
  337. }
  338. // Схороняем настройки
  339. FlipperFormat* save = flipper_format_file_alloc(storage);
  340. do {
  341. if(!flipper_format_file_open_always(save, CONFIG_FILE_PATH)) {
  342. notification_message(notifications, &sequence_error);
  343. break;
  344. }
  345. if(!flipper_format_write_header_cstr(save, "Zeitraffer", 1)) {
  346. notification_message(notifications, &sequence_error);
  347. break;
  348. }
  349. if(!flipper_format_write_comment_cstr(
  350. save,
  351. "Zeitraffer app settings: n of frames, interval time, backlight type, Delay")) {
  352. notification_message(notifications, &sequence_error);
  353. break;
  354. }
  355. if(!flipper_format_write_int32(save, "Time", &Time, 1)) {
  356. notification_message(notifications, &sequence_error);
  357. break;
  358. }
  359. if(!flipper_format_write_int32(save, "Count", &Count, 1)) {
  360. notification_message(notifications, &sequence_error);
  361. break;
  362. }
  363. if(!flipper_format_write_int32(save, "Backlight", &Backlight, 1)) {
  364. notification_message(notifications, &sequence_error);
  365. break;
  366. }
  367. if(!flipper_format_write_int32(save, "Delay", &Delay, 1)) {
  368. notification_message(notifications, &sequence_error);
  369. break;
  370. }
  371. } while(0);
  372. flipper_format_free(save);
  373. furi_record_close(RECORD_STORAGE);
  374. // Очищаем таймер
  375. furi_timer_free(timer);
  376. // Специальная очистка памяти, занимаемой очередью
  377. furi_message_queue_free(event_queue);
  378. // Чистим созданные объекты, связанные с интерфейсом
  379. gui_remove_view_port(gui, view_port);
  380. view_port_free(view_port);
  381. furi_record_close(RECORD_GUI);
  382. // Очищаем нотификации
  383. furi_record_close(RECORD_NOTIFICATION);
  384. return 0;
  385. }