intervalometer.c 26 KB

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