intervalometer.c 26 KB

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