intervalometer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. // An intervalometer application written for the Flipper Zero
  2. //
  3. // author: nitepone <sierra>
  4. #include "intervalometer.h"
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <furi.h>
  8. #include <furi_hal.h>
  9. #include <core/string.h>
  10. #include <gui/gui.h>
  11. #include <gui/elements.h>
  12. #include <gui/icon.h>
  13. #include <infrared_transmit.h>
  14. #include <input/input.h>
  15. #include <notification/notification.h>
  16. #include <notification/notification_messages.h>
  17. #include <intervalometer_icons.h>
  18. // app ui scenes
  19. enum flipvalo_ui_scene {
  20. FVSceneMain,
  21. FVSceneConfig,
  22. };
  23. // run config for intervalometer
  24. struct flipvalo_config {
  25. int init_delay_msec; // initial delay to start capture
  26. int interval_delay_msec; // time between shots
  27. int shot_count; // total shots in run
  28. int burst_count; // number of triggers in a shot
  29. int burst_delay_msec; // time between triggers in a shot
  30. int tickrate; // tick rate in "ticks per second"
  31. // camera control functions.
  32. // a bit overkill atm, but this will allow us to drop in support
  33. // for other cameras, bluetooth, and more adv. functions later.
  34. int (*send_trigger_fn)(void* output_config);
  35. void* output_config;
  36. };
  37. // run time states for intervalometer
  38. enum flipvalo_state {
  39. FVDone = 0, // done, 0 so it is default if state struct is zeroed
  40. FVWaitInitShot, // waiting for first shot
  41. FVWaitContShot, // waiting between "bursts" or "shots"
  42. FVWaitBurst, // waiting between shots in a "burst"
  43. };
  44. // run time data for intervalometer
  45. // (this can be safely cleared between runs of the intervalometer)
  46. struct flipvalo_run_state {
  47. enum flipvalo_state state; // current state of the run
  48. int tick_cur; // current tick count
  49. int tick_next; // tick when next action will occur
  50. int shot_cur; // current shot
  51. int burst_cur; // current trigger in a burst
  52. };
  53. enum flipvalo_config_edit_lines {
  54. FvConfigEditInitDelay,
  55. FvConfigEditMIN = FvConfigEditInitDelay,
  56. FvConfigEditShotCount,
  57. FvConfigEditShotDelay,
  58. FvConfigEditBurstCount,
  59. FvConfigEditBurstDelay,
  60. FvConfigEditMAX = FvConfigEditBurstDelay,
  61. };
  62. struct flipvalo_config_edit_view {
  63. // the `config` that is under edit
  64. struct flipvalo_config* config;
  65. // the `cur_index` of the selection
  66. // (e.g. editing the 3rd value of a number)
  67. int cur_index;
  68. // the `cur_line` of the selection
  69. enum flipvalo_config_edit_lines cur_line;
  70. // the current line that is at the top of the scrolled view
  71. enum flipvalo_config_edit_lines scroll_pos;
  72. // are we editing the selection?
  73. // (this is really only needed for number fields)
  74. bool edit_mode;
  75. };
  76. // private data of app
  77. struct flipvalo_priv {
  78. struct flipvalo_config config;
  79. struct flipvalo_config_edit_view config_edit_view;
  80. struct flipvalo_run_state run_state;
  81. enum flipvalo_ui_scene ui_scene;
  82. int gui_shutter_blink;
  83. FuriTimer* timer;
  84. NotificationApp* notifications;
  85. FuriMutex* mutex;
  86. };
  87. enum event_type {
  88. EventTypeTick,
  89. EventTypeKey,
  90. };
  91. struct plugin_event {
  92. enum event_type type;
  93. InputEvent input;
  94. };
  95. // XXX(luna) settings experimental ui kludge
  96. enum flipvalo_config_edit_line_type {
  97. FvConfigEditTypeTimer,
  98. FvConfigEditTypeCount,
  99. };
  100. static void flipvalo_config_edit_view_init(struct flipvalo_config_edit_view* view) {
  101. view->config = NULL;
  102. view->cur_index = 0;
  103. view->cur_line = 0;
  104. view->scroll_pos = 0;
  105. view->edit_mode = false;
  106. }
  107. #define ITEM_H 64 / 3
  108. #define ITEM_W 128
  109. #define VALUE_X 100
  110. #define VALUE_W 45
  111. static void flipvalo_config_edit_draw(Canvas* canvas, struct flipvalo_config_edit_view* view) {
  112. int* line_value;
  113. char* line_label = NULL;
  114. FuriString* temp_str = furi_string_alloc();
  115. enum flipvalo_config_edit_line_type line_type;
  116. enum flipvalo_config_edit_lines selected_line;
  117. for (size_t line = 0; line < 3; line++) {
  118. selected_line = view->scroll_pos + line;
  119. switch (selected_line) {
  120. case FvConfigEditInitDelay:
  121. line_value = &view->config->init_delay_msec;
  122. line_type = FvConfigEditTypeTimer;
  123. line_label = "Init Time";
  124. break;
  125. case FvConfigEditShotDelay:
  126. line_value = &view->config->interval_delay_msec;
  127. line_type = FvConfigEditTypeTimer;
  128. line_label = "Seq Time";
  129. break;
  130. case FvConfigEditShotCount:
  131. line_value = &view->config->shot_count;
  132. line_type = FvConfigEditTypeCount;
  133. line_label = "Seq Count";
  134. break;
  135. case FvConfigEditBurstDelay:
  136. line_value = &view->config->burst_delay_msec;
  137. line_type = FvConfigEditTypeTimer;
  138. line_label = "Brst Time";
  139. break;
  140. case FvConfigEditBurstCount:
  141. line_value = &view->config->burst_count;
  142. line_type = FvConfigEditTypeCount;
  143. line_label = "Brst Count";
  144. break;
  145. default:
  146. continue;
  147. };
  148. canvas_set_color(canvas, ColorBlack);
  149. if ((selected_line) == view->cur_line) {
  150. elements_slightly_rounded_box(canvas, 0, ITEM_H * line + 1, ITEM_W, ITEM_H - 1);
  151. canvas_set_color(canvas, ColorWhite);
  152. }
  153. uint8_t text_y = ITEM_H * line + ITEM_H / 2 + 2;
  154. canvas_draw_str_aligned(canvas, 6, text_y, AlignLeft, AlignCenter, line_label);
  155. switch(line_type) {
  156. case FvConfigEditTypeTimer:
  157. furi_string_printf(
  158. temp_str, "%02d:%02d:%02d:%03d",
  159. *line_value / 3600000,
  160. (*line_value / 60000) % 60,
  161. (*line_value / 1000) % 60,
  162. *line_value % 1000);
  163. canvas_set_font(canvas, FontKeyboard);
  164. canvas_draw_str_aligned(
  165. canvas, 124, text_y, AlignRight, AlignCenter,
  166. furi_string_get_cstr(temp_str));
  167. canvas_set_font(canvas, FontSecondary);
  168. if (view->edit_mode && view->cur_line == selected_line) {
  169. switch (view->cur_index) {
  170. case 0:
  171. canvas_draw_icon(canvas, 117, text_y - 9, &I_ArrowUp_3x5);
  172. canvas_draw_icon(canvas, 117, text_y + 5, &I_ArrowDown_3x5);
  173. canvas_draw_icon(canvas, 112, text_y - 9, &I_ArrowUp_3x5);
  174. canvas_draw_icon(canvas, 112, text_y + 5, &I_ArrowDown_3x5);
  175. canvas_draw_icon(canvas, 107, text_y - 9, &I_ArrowUp_3x5);
  176. canvas_draw_icon(canvas, 107, text_y + 5, &I_ArrowDown_3x5);
  177. break;
  178. case 1:
  179. canvas_draw_icon(canvas, 93, text_y - 9, &I_ArrowUp_3x5);
  180. canvas_draw_icon(canvas, 93, text_y + 5, &I_ArrowDown_3x5);
  181. canvas_draw_icon(canvas, 89, text_y - 9, &I_ArrowUp_3x5);
  182. canvas_draw_icon(canvas, 89, text_y + 5, &I_ArrowDown_3x5);
  183. break;
  184. case 2:
  185. canvas_draw_icon(canvas, 75, text_y - 9, &I_ArrowUp_3x5);
  186. canvas_draw_icon(canvas, 75, text_y + 5, &I_ArrowDown_3x5);
  187. canvas_draw_icon(canvas, 71, text_y - 9, &I_ArrowUp_3x5);
  188. canvas_draw_icon(canvas, 71, text_y + 5, &I_ArrowDown_3x5);
  189. break;
  190. case 3:
  191. canvas_draw_icon(canvas, 57, text_y - 9, &I_ArrowUp_3x5);
  192. canvas_draw_icon(canvas, 57, text_y + 5, &I_ArrowDown_3x5);
  193. canvas_draw_icon(canvas, 53, text_y - 9, &I_ArrowUp_3x5);
  194. canvas_draw_icon(canvas, 53, text_y + 5, &I_ArrowDown_3x5);
  195. break;
  196. }
  197. }
  198. break;
  199. case FvConfigEditTypeCount:
  200. furi_string_printf(temp_str, "%d", *line_value);
  201. canvas_draw_str_aligned(
  202. canvas, VALUE_X, text_y, AlignCenter, AlignCenter,
  203. furi_string_get_cstr(temp_str));
  204. // TODO(luna) 0 values are actually more special for shot count and burst count.
  205. // former being infinite, latter being uh.. nothing? not allowed?.. review this logic later.
  206. if(*line_value > 0) {
  207. canvas_draw_str_aligned(
  208. canvas, VALUE_X - VALUE_W / 2, text_y, AlignCenter, AlignCenter, "<");
  209. }
  210. canvas_draw_str_aligned(
  211. canvas, VALUE_X + VALUE_W / 2, text_y, AlignCenter, AlignCenter, ">");
  212. break;
  213. }
  214. }
  215. furi_string_free(temp_str);
  216. }
  217. static void flipvalo_config_edit_input_move_cursor(struct flipvalo_config_edit_view* view, int dx, int dy) {
  218. enum flipvalo_config_edit_lines new_line = 0;
  219. int* line_value;
  220. enum flipvalo_config_edit_line_type line_type;
  221. switch (view->cur_line) {
  222. case FvConfigEditInitDelay:
  223. line_value = &view->config->init_delay_msec;
  224. line_type = FvConfigEditTypeTimer;
  225. break;
  226. case FvConfigEditShotDelay:
  227. line_value = &view->config->interval_delay_msec;
  228. line_type = FvConfigEditTypeTimer;
  229. break;
  230. case FvConfigEditShotCount:
  231. line_value = &view->config->shot_count;
  232. line_type = FvConfigEditTypeCount;
  233. break;
  234. case FvConfigEditBurstDelay:
  235. line_value = &view->config->burst_delay_msec;
  236. line_type = FvConfigEditTypeTimer;
  237. break;
  238. case FvConfigEditBurstCount:
  239. line_value = &view->config->burst_count;
  240. line_type = FvConfigEditTypeCount;
  241. break;
  242. default:
  243. return;
  244. };
  245. if (!view->edit_mode) {
  246. // Do `dy` behaviors
  247. new_line = view->cur_line + dy;
  248. if (new_line > FvConfigEditMAX) {
  249. // Out of bound cursor. No-op.
  250. return;
  251. }
  252. view->cur_line = new_line;
  253. // Handle moving scroll position.
  254. if (new_line < view->scroll_pos) {
  255. view->scroll_pos = new_line;
  256. }
  257. else if (new_line >= (view->scroll_pos + 3)) {
  258. view->scroll_pos+=dy;
  259. }
  260. // Do `dx` behavior
  261. switch (line_type) {
  262. case FvConfigEditTypeCount:
  263. if (*line_value + dx >= 0) {
  264. *line_value += dx;
  265. }
  266. break;
  267. case FvConfigEditTypeTimer:
  268. // no-op unless edit mode
  269. break;
  270. }
  271. }
  272. else /* edit mode */ {
  273. switch (line_type) {
  274. case FvConfigEditTypeCount:
  275. // If current line does not edit mode.. why are we in edit mode?
  276. // Reaching this would be a bug, so lets go back to normal mode.
  277. view->edit_mode = false;
  278. return;
  279. case FvConfigEditTypeTimer:
  280. switch (view->cur_index) {
  281. case 0:
  282. if (*line_value + (dy * -10) >= 0) {
  283. *line_value += (dy * -10);
  284. }
  285. break;
  286. case 1:
  287. if (*line_value + (dy * -1000) >= 0) {
  288. *line_value += (dy * -1000);
  289. }
  290. break;
  291. case 2:
  292. if (*line_value + (dy * -60000) >= 0) {
  293. *line_value += (dy * -60000);
  294. }
  295. break;
  296. case 3:
  297. if (*line_value + (dy * -3600000) >= 0) {
  298. *line_value += (dy * -3600000);
  299. }
  300. break;
  301. }
  302. view->cur_index -= dx;
  303. if (view->cur_index < 0) {
  304. view->cur_index = 0;
  305. }
  306. if (view->cur_index > 3) {
  307. view->cur_index = 3;
  308. }
  309. break;
  310. }
  311. }
  312. }
  313. static int flipvalo_config_edit_input(InputEvent* event, struct flipvalo_config_edit_view* view) {
  314. // ignore all but short and repeats
  315. if (!(event->type == InputTypeShort || event->type == InputTypeRepeat)) {
  316. return 0;
  317. }
  318. switch (event->key) {
  319. case InputKeyRight:
  320. flipvalo_config_edit_input_move_cursor(view, 1, 0);
  321. break;
  322. case InputKeyLeft:
  323. flipvalo_config_edit_input_move_cursor(view, -1, 0);
  324. break;
  325. case InputKeyUp:
  326. flipvalo_config_edit_input_move_cursor(view, 0, -1);
  327. break;
  328. case InputKeyDown:
  329. flipvalo_config_edit_input_move_cursor(view, 0, 1);
  330. break;
  331. case InputKeyOk:
  332. //TODO(luna) Check if line supports edit mode before doing this.
  333. view->edit_mode = !view->edit_mode;
  334. break;
  335. case InputKeyBack:
  336. if (view->edit_mode) {
  337. view->edit_mode = false;
  338. } else {
  339. // exit config edit view
  340. return 1;
  341. }
  342. default:
  343. break;
  344. }
  345. return 0;
  346. }
  347. // XXX(luna) back to app
  348. static void flipvalo_run_state_init(struct flipvalo_run_state* fv_run_state) {
  349. fv_run_state->burst_cur = 1;
  350. fv_run_state->shot_cur = 1;
  351. fv_run_state->tick_next = 0;
  352. fv_run_state->state = FVDone;
  353. fv_run_state->tick_next = 0;
  354. fv_run_state->tick_cur = 0;
  355. }
  356. static int sony_ir_trigger(void* ctx) {
  357. UNUSED(ctx);
  358. InfraredMessage message = {
  359. .address = 0x1E3A,
  360. .command = 0x2D,
  361. .protocol = InfraredProtocolSIRC20,
  362. };
  363. infrared_send(&message, 1);
  364. return 0;
  365. }
  366. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  367. furi_assert(event_queue);
  368. struct plugin_event event = {.type = EventTypeKey, .input = *input_event };
  369. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  370. }
  371. static inline bool flipvalo_intv_running(struct flipvalo_priv* fv_priv) {
  372. return fv_priv->run_state.state != FVDone;
  373. }
  374. static void flipvalo_intv_tick(struct flipvalo_priv* fv_priv) {
  375. struct flipvalo_config* conf = &fv_priv->config;
  376. struct flipvalo_run_state* run = &fv_priv->run_state;
  377. // check if action required
  378. if (run->tick_cur++ >= run->tick_next) {
  379. // call trigger function
  380. conf->send_trigger_fn(conf->output_config);
  381. fv_priv->gui_shutter_blink = 3;
  382. // end of burst, prepare next shot
  383. if (run->burst_cur >= conf->burst_count) {
  384. run->burst_cur = 1;
  385. run->shot_cur++;
  386. run->state = FVWaitContShot;
  387. run->tick_next = run->tick_cur + ((conf->interval_delay_msec * conf->tickrate) / 1000);
  388. }
  389. else /*continue burst */ {
  390. run->burst_cur++;
  391. run->state = FVWaitBurst;
  392. run->tick_next = run->tick_cur + ((conf->burst_delay_msec * conf->tickrate) / 1000);
  393. }
  394. }
  395. if (run->shot_cur > conf->shot_count) {
  396. run->state = FVDone;
  397. }
  398. }
  399. static void flipvalo_intv_stop(struct flipvalo_priv* fv_priv) {
  400. fv_priv->run_state.state = FVDone;
  401. }
  402. static void flipvalo_intv_start(struct flipvalo_priv* fv_priv) {
  403. // clear struct
  404. furi_assert(fv_priv);
  405. flipvalo_run_state_init(&fv_priv->run_state);
  406. fv_priv->run_state.state = FVWaitInitShot;
  407. fv_priv->run_state.tick_next =
  408. ((fv_priv->config.init_delay_msec * fv_priv->config.tickrate) / 1000);
  409. }
  410. static void timer_callback(void* ctx) {
  411. furi_assert(ctx);
  412. struct flipvalo_priv* fv_priv = ctx;
  413. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  414. if (flipvalo_intv_running(fv_priv)) {
  415. flipvalo_intv_tick(fv_priv);
  416. }
  417. furi_mutex_release(fv_priv->mutex);
  418. }
  419. static void render_callback(Canvas* const canvas, void* ctx) {
  420. furi_assert(ctx);
  421. struct flipvalo_priv* fv_priv = ctx;
  422. FuriString* temp_str = furi_string_alloc();
  423. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  424. // invert screen if blinking
  425. if (fv_priv->gui_shutter_blink > 0) {
  426. fv_priv->gui_shutter_blink--;
  427. canvas_draw_box(canvas, 0, 0, 127, 63);
  428. canvas_set_color(canvas, ColorWhite);
  429. }
  430. if (fv_priv->ui_scene == FVSceneMain) {
  431. int countdown_msec = (1000 * (fv_priv->run_state.tick_next
  432. - fv_priv->run_state.tick_cur))
  433. / fv_priv->config.tickrate;
  434. int elapsed_msec = (1000 * fv_priv->run_state.tick_cur)
  435. / fv_priv->config.tickrate;
  436. canvas_draw_frame(canvas, 0, 0, 128, 64);
  437. // draw countdown
  438. canvas_set_font(canvas, FontPrimary);
  439. furi_string_printf(
  440. temp_str, "%02d:%02d:%02d:%03d",
  441. countdown_msec / 3600000,
  442. (countdown_msec / 60000) % 60,
  443. (countdown_msec / 1000) % 60,
  444. countdown_msec % 1000);
  445. canvas_draw_str_aligned(
  446. canvas, 64, 24,
  447. AlignCenter, AlignCenter,
  448. furi_string_get_cstr(temp_str));
  449. // draw top and bottom status bars
  450. canvas_set_font(canvas, FontSecondary);
  451. furi_string_printf(
  452. temp_str, "%02d:%02d:%02d",
  453. elapsed_msec / 3600000,
  454. (elapsed_msec / 60000) % 60,
  455. (elapsed_msec / 1000) % 60);
  456. canvas_draw_str_aligned(
  457. canvas, 4, 8,
  458. AlignLeft, AlignCenter,
  459. furi_string_get_cstr(temp_str));
  460. furi_string_printf(temp_str, "Shot: %d", fv_priv->run_state.shot_cur);
  461. canvas_draw_str_aligned(
  462. canvas, 124, 8,
  463. AlignRight, AlignCenter,
  464. furi_string_get_cstr(temp_str));
  465. elements_button_left(canvas, "Cfg");
  466. elements_button_right(canvas, "Snap");
  467. if (fv_priv->run_state.state == FVDone) {
  468. elements_button_center(canvas, "Start");
  469. } else {
  470. elements_button_center(canvas, "Stop ");
  471. }
  472. }
  473. else if (fv_priv->ui_scene == FVSceneConfig) {
  474. flipvalo_config_edit_draw(canvas, &fv_priv->config_edit_view);
  475. }
  476. furi_string_free(temp_str);
  477. furi_mutex_release(fv_priv->mutex);
  478. }
  479. static void flipvalo_config_init(struct flipvalo_config* fv_conf) {
  480. fv_conf->init_delay_msec = 2000;
  481. fv_conf->interval_delay_msec = 0;
  482. fv_conf->shot_count = 1;
  483. fv_conf->burst_count = 1;
  484. fv_conf->burst_delay_msec = 0;
  485. fv_conf->tickrate = 125;
  486. fv_conf->send_trigger_fn = sony_ir_trigger;
  487. fv_conf->output_config = NULL;
  488. }
  489. static void flipvalo_priv_init(struct flipvalo_priv* fv_priv) {
  490. flipvalo_config_init(&fv_priv->config);
  491. flipvalo_config_edit_view_init(&fv_priv->config_edit_view);
  492. flipvalo_run_state_init(&fv_priv->run_state);
  493. fv_priv->gui_shutter_blink = 0;
  494. fv_priv->timer = NULL;
  495. fv_priv->notifications = NULL;
  496. fv_priv->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  497. fv_priv->gui_shutter_blink = 0;
  498. }
  499. int32_t flipvalo_app() {
  500. int ret = 0;
  501. ViewPort* view_port = NULL;
  502. Gui* gui = NULL;
  503. FuriStatus event_status = {0};
  504. struct plugin_event event = {0};
  505. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(struct plugin_event));
  506. struct flipvalo_priv* fv_priv = malloc(sizeof(*fv_priv));
  507. flipvalo_priv_init(fv_priv);
  508. if (!fv_priv->mutex) {
  509. FURI_LOG_E("Flipvalo", "Cannot create mutex\r\n");
  510. ret = 1;
  511. goto cleanup;
  512. }
  513. view_port = view_port_alloc();
  514. view_port_draw_callback_set(view_port, render_callback, fv_priv);
  515. view_port_input_callback_set(view_port, input_callback, event_queue);
  516. fv_priv->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, fv_priv);
  517. furi_timer_start(
  518. fv_priv->timer,
  519. (uint32_t) furi_kernel_get_tick_frequency() / fv_priv->config.tickrate);
  520. gui = furi_record_open("gui");
  521. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  522. while (true) {
  523. event_status = furi_message_queue_get(event_queue, &event, 100);
  524. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  525. // catch event_status that is not Ok
  526. if (event_status == FuriStatusErrorTimeout) {
  527. // timeout, ignore
  528. goto next_event;
  529. }
  530. else if (event_status != FuriStatusOk) {
  531. FURI_LOG_E("Flipvalo", "Event Queue Error: %d\r\n", event_status);
  532. goto next_event;
  533. // TODO(luna) evaluate if we should exit here.
  534. //goto cleanup;
  535. }
  536. // handle input
  537. if ( /* long press back */
  538. event.type == EventTypeKey
  539. && event.input.type == InputTypeLong
  540. && event.input.key == InputKeyBack
  541. ) {
  542. goto cleanup;
  543. }
  544. switch (fv_priv->ui_scene) {
  545. case FVSceneMain:
  546. // TODO(luna) Maybe give this a function.. look howl clean FVSceneConfig is...
  547. if (event.type == EventTypeKey) {
  548. if (event.input.type == InputTypeShort) {
  549. switch (event.input.key) {
  550. case InputKeyUp:
  551. break;
  552. case InputKeyDown:
  553. break;
  554. case InputKeyLeft:
  555. flipvalo_intv_stop(fv_priv);
  556. fv_priv->config_edit_view.config = &fv_priv->config;
  557. fv_priv->ui_scene = FVSceneConfig;
  558. break;
  559. case InputKeyRight:
  560. fv_priv->gui_shutter_blink = 3;
  561. fv_priv->config.send_trigger_fn(fv_priv->config.output_config);
  562. break;
  563. case InputKeyOk:
  564. if (flipvalo_intv_running(fv_priv)) {
  565. flipvalo_intv_stop(fv_priv);
  566. } else {
  567. flipvalo_intv_start(fv_priv);
  568. }
  569. break;
  570. case InputKeyMAX:
  571. break;
  572. case InputKeyBack:
  573. break;
  574. }
  575. }
  576. }
  577. break;
  578. case FVSceneConfig:
  579. ret = flipvalo_config_edit_input(&event.input, &fv_priv->config_edit_view);
  580. if (ret) {
  581. fv_priv->ui_scene = FVSceneMain;
  582. }
  583. break;
  584. }
  585. next_event:
  586. furi_mutex_release(fv_priv->mutex);
  587. }
  588. cleanup:
  589. if (view_port) {
  590. view_port_enabled_set(view_port, false);
  591. if (gui) {
  592. gui_remove_view_port(gui, view_port);
  593. furi_record_close("gui");
  594. }
  595. view_port_free(view_port);
  596. }
  597. if (event_queue) {
  598. furi_message_queue_free(event_queue);
  599. }
  600. if (fv_priv) {
  601. furi_mutex_free(fv_priv->mutex);
  602. furi_timer_free(fv_priv->timer);
  603. }
  604. free(fv_priv);
  605. return ret;
  606. }