intervalometer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 <sony_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,
  159. "%02d:%02d:%02d:%03d",
  160. *line_value / 3600000,
  161. (*line_value / 60000) % 60,
  162. (*line_value / 1000) % 60,
  163. *line_value % 1000);
  164. canvas_set_font(canvas, FontKeyboard);
  165. canvas_draw_str_aligned(
  166. canvas, 124, text_y, AlignRight, AlignCenter, 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, furi_string_get_cstr(temp_str));
  203. // TODO(luna) 0 values are actually more special for shot count and burst count.
  204. // former being infinite, latter being uh.. nothing? not allowed?.. review this logic later.
  205. if(*line_value > 0) {
  206. canvas_draw_str_aligned(
  207. canvas, VALUE_X - VALUE_W / 2, text_y, AlignCenter, AlignCenter, "<");
  208. }
  209. canvas_draw_str_aligned(
  210. canvas, VALUE_X + VALUE_W / 2, text_y, AlignCenter, AlignCenter, ">");
  211. break;
  212. }
  213. }
  214. furi_string_free(temp_str);
  215. }
  216. static void
  217. 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. } else if(new_line >= (view->scroll_pos + 3)) {
  257. view->scroll_pos += dy;
  258. }
  259. // Do `dx` behavior
  260. switch(line_type) {
  261. case FvConfigEditTypeCount:
  262. if(*line_value + dx >= 0) {
  263. *line_value += dx;
  264. }
  265. break;
  266. case FvConfigEditTypeTimer:
  267. // no-op unless edit mode
  268. break;
  269. }
  270. } else /* edit mode */ {
  271. switch(line_type) {
  272. case FvConfigEditTypeCount:
  273. // If current line does not edit mode.. why are we in edit mode?
  274. // Reaching this would be a bug, so lets go back to normal mode.
  275. view->edit_mode = false;
  276. return;
  277. case FvConfigEditTypeTimer:
  278. switch(view->cur_index) {
  279. case 0:
  280. if(*line_value + (dy * -10) >= 0) {
  281. *line_value += (dy * -10);
  282. }
  283. break;
  284. case 1:
  285. if(*line_value + (dy * -1000) >= 0) {
  286. *line_value += (dy * -1000);
  287. }
  288. break;
  289. case 2:
  290. if(*line_value + (dy * -60000) >= 0) {
  291. *line_value += (dy * -60000);
  292. }
  293. break;
  294. case 3:
  295. if(*line_value + (dy * -3600000) >= 0) {
  296. *line_value += (dy * -3600000);
  297. }
  298. break;
  299. }
  300. view->cur_index -= dx;
  301. if(view->cur_index < 0) {
  302. view->cur_index = 0;
  303. }
  304. if(view->cur_index > 3) {
  305. view->cur_index = 3;
  306. }
  307. break;
  308. }
  309. }
  310. }
  311. static int flipvalo_config_edit_input(InputEvent* event, struct flipvalo_config_edit_view* view) {
  312. // ignore all but short and repeats
  313. if(!(event->type == InputTypeShort || event->type == InputTypeRepeat)) {
  314. return 0;
  315. }
  316. switch(event->key) {
  317. case InputKeyRight:
  318. flipvalo_config_edit_input_move_cursor(view, 1, 0);
  319. break;
  320. case InputKeyLeft:
  321. flipvalo_config_edit_input_move_cursor(view, -1, 0);
  322. break;
  323. case InputKeyUp:
  324. flipvalo_config_edit_input_move_cursor(view, 0, -1);
  325. break;
  326. case InputKeyDown:
  327. flipvalo_config_edit_input_move_cursor(view, 0, 1);
  328. break;
  329. case InputKeyOk:
  330. //TODO(luna) Check if line supports edit mode before doing this.
  331. view->edit_mode = !view->edit_mode;
  332. break;
  333. case InputKeyBack:
  334. if(view->edit_mode) {
  335. view->edit_mode = false;
  336. } else {
  337. // exit config edit view
  338. return 1;
  339. }
  340. default:
  341. break;
  342. }
  343. return 0;
  344. }
  345. // XXX(luna) back to app
  346. static void flipvalo_run_state_init(struct flipvalo_run_state* fv_run_state) {
  347. fv_run_state->burst_cur = 1;
  348. fv_run_state->shot_cur = 1;
  349. fv_run_state->tick_next = 0;
  350. fv_run_state->state = FVDone;
  351. fv_run_state->tick_next = 0;
  352. fv_run_state->tick_cur = 0;
  353. }
  354. static int sony_ir_trigger(void* ctx) {
  355. UNUSED(ctx);
  356. InfraredMessage message = {
  357. .address = 0x1E3A,
  358. .command = 0x2D,
  359. .protocol = InfraredProtocolSIRC20,
  360. };
  361. infrared_send(&message, 1);
  362. return 0;
  363. }
  364. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  365. furi_assert(event_queue);
  366. struct plugin_event event = {.type = EventTypeKey, .input = *input_event};
  367. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  368. }
  369. static inline bool flipvalo_intv_running(struct flipvalo_priv* fv_priv) {
  370. return fv_priv->run_state.state != FVDone;
  371. }
  372. static void flipvalo_intv_tick(struct flipvalo_priv* fv_priv) {
  373. struct flipvalo_config* conf = &fv_priv->config;
  374. struct flipvalo_run_state* run = &fv_priv->run_state;
  375. // check if action required
  376. if(run->tick_cur++ >= run->tick_next) {
  377. // call trigger function
  378. conf->send_trigger_fn(conf->output_config);
  379. fv_priv->gui_shutter_blink = 3;
  380. // end of burst, prepare next shot
  381. if(run->burst_cur >= conf->burst_count) {
  382. run->burst_cur = 1;
  383. run->shot_cur++;
  384. run->state = FVWaitContShot;
  385. run->tick_next = run->tick_cur + ((conf->interval_delay_msec * conf->tickrate) / 1000);
  386. } else /*continue burst */ {
  387. run->burst_cur++;
  388. run->state = FVWaitBurst;
  389. run->tick_next = run->tick_cur + ((conf->burst_delay_msec * conf->tickrate) / 1000);
  390. }
  391. }
  392. if(run->shot_cur > conf->shot_count) {
  393. run->state = FVDone;
  394. }
  395. }
  396. static void flipvalo_intv_stop(struct flipvalo_priv* fv_priv) {
  397. fv_priv->run_state.state = FVDone;
  398. }
  399. static void flipvalo_intv_start(struct flipvalo_priv* fv_priv) {
  400. // clear struct
  401. furi_assert(fv_priv);
  402. flipvalo_run_state_init(&fv_priv->run_state);
  403. fv_priv->run_state.state = FVWaitInitShot;
  404. fv_priv->run_state.tick_next =
  405. ((fv_priv->config.init_delay_msec * fv_priv->config.tickrate) / 1000);
  406. }
  407. static void timer_callback(void* ctx) {
  408. furi_assert(ctx);
  409. struct flipvalo_priv* fv_priv = ctx;
  410. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  411. if(flipvalo_intv_running(fv_priv)) {
  412. flipvalo_intv_tick(fv_priv);
  413. }
  414. furi_mutex_release(fv_priv->mutex);
  415. }
  416. static void render_callback(Canvas* const canvas, void* ctx) {
  417. furi_assert(ctx);
  418. struct flipvalo_priv* fv_priv = ctx;
  419. FuriString* temp_str = furi_string_alloc();
  420. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  421. // invert screen if blinking
  422. if(fv_priv->gui_shutter_blink > 0) {
  423. fv_priv->gui_shutter_blink--;
  424. canvas_draw_box(canvas, 0, 0, 127, 63);
  425. canvas_set_color(canvas, ColorWhite);
  426. }
  427. if(fv_priv->ui_scene == FVSceneMain) {
  428. int countdown_msec =
  429. (1000 * (fv_priv->run_state.tick_next - fv_priv->run_state.tick_cur)) /
  430. fv_priv->config.tickrate;
  431. int elapsed_msec = (1000 * fv_priv->run_state.tick_cur) / fv_priv->config.tickrate;
  432. canvas_draw_frame(canvas, 0, 0, 128, 64);
  433. // draw countdown
  434. canvas_set_font(canvas, FontPrimary);
  435. furi_string_printf(
  436. temp_str,
  437. "%02d:%02d:%02d:%03d",
  438. countdown_msec / 3600000,
  439. (countdown_msec / 60000) % 60,
  440. (countdown_msec / 1000) % 60,
  441. countdown_msec % 1000);
  442. canvas_draw_str_aligned(
  443. canvas, 64, 24, AlignCenter, AlignCenter, furi_string_get_cstr(temp_str));
  444. // draw top and bottom status bars
  445. canvas_set_font(canvas, FontSecondary);
  446. furi_string_printf(
  447. temp_str,
  448. "%02d:%02d:%02d",
  449. elapsed_msec / 3600000,
  450. (elapsed_msec / 60000) % 60,
  451. (elapsed_msec / 1000) % 60);
  452. canvas_draw_str_aligned(
  453. canvas, 4, 8, AlignLeft, AlignCenter, furi_string_get_cstr(temp_str));
  454. furi_string_printf(temp_str, "Shot: %d", fv_priv->run_state.shot_cur);
  455. canvas_draw_str_aligned(
  456. canvas, 124, 8, AlignRight, AlignCenter, furi_string_get_cstr(temp_str));
  457. elements_button_left(canvas, "Cfg");
  458. elements_button_right(canvas, "Snap");
  459. if(fv_priv->run_state.state == FVDone) {
  460. elements_button_center(canvas, "Start");
  461. } else {
  462. elements_button_center(canvas, "Stop ");
  463. }
  464. } else if(fv_priv->ui_scene == FVSceneConfig) {
  465. flipvalo_config_edit_draw(canvas, &fv_priv->config_edit_view);
  466. }
  467. furi_string_free(temp_str);
  468. furi_mutex_release(fv_priv->mutex);
  469. }
  470. static void flipvalo_config_init(struct flipvalo_config* fv_conf) {
  471. fv_conf->init_delay_msec = 2000;
  472. fv_conf->interval_delay_msec = 0;
  473. fv_conf->shot_count = 1;
  474. fv_conf->burst_count = 1;
  475. fv_conf->burst_delay_msec = 0;
  476. fv_conf->tickrate = 125;
  477. fv_conf->send_trigger_fn = sony_ir_trigger;
  478. fv_conf->output_config = NULL;
  479. }
  480. static void flipvalo_priv_init(struct flipvalo_priv* fv_priv) {
  481. flipvalo_config_init(&fv_priv->config);
  482. flipvalo_config_edit_view_init(&fv_priv->config_edit_view);
  483. flipvalo_run_state_init(&fv_priv->run_state);
  484. fv_priv->gui_shutter_blink = 0;
  485. fv_priv->timer = NULL;
  486. fv_priv->notifications = NULL;
  487. fv_priv->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  488. fv_priv->gui_shutter_blink = 0;
  489. }
  490. int32_t flipvalo_app() {
  491. int ret = 0;
  492. ViewPort* view_port = NULL;
  493. Gui* gui = NULL;
  494. FuriStatus event_status = {0};
  495. struct plugin_event event = {0};
  496. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(struct plugin_event));
  497. struct flipvalo_priv* fv_priv = malloc(sizeof(*fv_priv));
  498. flipvalo_priv_init(fv_priv);
  499. if(!fv_priv->mutex) {
  500. FURI_LOG_E("Flipvalo", "Cannot create mutex\r\n");
  501. ret = 1;
  502. goto cleanup;
  503. }
  504. view_port = view_port_alloc();
  505. view_port_draw_callback_set(view_port, render_callback, fv_priv);
  506. view_port_input_callback_set(view_port, input_callback, event_queue);
  507. fv_priv->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, fv_priv);
  508. furi_timer_start(
  509. fv_priv->timer, (uint32_t)furi_kernel_get_tick_frequency() / fv_priv->config.tickrate);
  510. gui = furi_record_open("gui");
  511. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  512. while(true) {
  513. event_status = furi_message_queue_get(event_queue, &event, 100);
  514. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  515. // catch event_status that is not Ok
  516. if(event_status == FuriStatusErrorTimeout) {
  517. // timeout, ignore
  518. goto next_event;
  519. } else if(event_status != FuriStatusOk) {
  520. FURI_LOG_E("Flipvalo", "Event Queue Error: %d\r\n", event_status);
  521. goto next_event;
  522. // TODO(luna) evaluate if we should exit here.
  523. //goto cleanup;
  524. }
  525. // handle input
  526. if(/* long press back */
  527. event.type == EventTypeKey && event.input.type == InputTypeLong &&
  528. event.input.key == InputKeyBack) {
  529. goto cleanup;
  530. }
  531. switch(fv_priv->ui_scene) {
  532. case FVSceneMain:
  533. // TODO(luna) Maybe give this a function.. look howl clean FVSceneConfig is...
  534. if(event.type == EventTypeKey) {
  535. if(event.input.type == InputTypeShort) {
  536. switch(event.input.key) {
  537. case InputKeyUp:
  538. break;
  539. case InputKeyDown:
  540. break;
  541. case InputKeyLeft:
  542. flipvalo_intv_stop(fv_priv);
  543. fv_priv->config_edit_view.config = &fv_priv->config;
  544. fv_priv->ui_scene = FVSceneConfig;
  545. break;
  546. case InputKeyRight:
  547. fv_priv->gui_shutter_blink = 3;
  548. fv_priv->config.send_trigger_fn(fv_priv->config.output_config);
  549. break;
  550. case InputKeyOk:
  551. if(flipvalo_intv_running(fv_priv)) {
  552. flipvalo_intv_stop(fv_priv);
  553. } else {
  554. flipvalo_intv_start(fv_priv);
  555. }
  556. break;
  557. case InputKeyMAX:
  558. break;
  559. case InputKeyBack:
  560. break;
  561. }
  562. }
  563. }
  564. break;
  565. case FVSceneConfig:
  566. ret = flipvalo_config_edit_input(&event.input, &fv_priv->config_edit_view);
  567. if(ret) {
  568. fv_priv->ui_scene = FVSceneMain;
  569. }
  570. break;
  571. }
  572. next_event:
  573. furi_mutex_release(fv_priv->mutex);
  574. view_port_update(view_port);
  575. }
  576. cleanup:
  577. if(view_port) {
  578. view_port_enabled_set(view_port, false);
  579. if(gui) {
  580. gui_remove_view_port(gui, view_port);
  581. furi_record_close("gui");
  582. }
  583. view_port_free(view_port);
  584. }
  585. if(event_queue) {
  586. furi_message_queue_free(event_queue);
  587. }
  588. if(fv_priv) {
  589. furi_mutex_free(fv_priv->mutex);
  590. furi_timer_free(fv_priv->timer);
  591. }
  592. free(fv_priv);
  593. return ret;
  594. }