intervalometer.c 26 KB

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