intervalometer.c 27 KB

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