intervalometer.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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, FuriMessageQueue* event_queue) {
  438. furi_assert(event_queue);
  439. struct plugin_event event = {.type = EventTypeKey, .input = *input_event};
  440. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  441. }
  442. static inline bool flipvalo_intv_running(struct flipvalo_priv* fv_priv) {
  443. return fv_priv->run_state.state != FVDone;
  444. }
  445. static void flipvalo_intv_tick(struct flipvalo_priv* fv_priv) {
  446. struct flipvalo_config* conf = &fv_priv->config;
  447. struct flipvalo_run_state* run = &fv_priv->run_state;
  448. // check if action required
  449. if(run->tick_cur++ >= run->tick_next) {
  450. // call trigger function
  451. flipvalo_get_trigger(conf->trigger)->send(conf->output_config);
  452. fv_priv->gui_shutter_blink = 3;
  453. // end of burst, prepare next shot
  454. if(run->burst_cur >= conf->burst_count) {
  455. run->burst_cur = 1;
  456. run->shot_cur++;
  457. run->state = FVWaitContShot;
  458. run->tick_next = run->tick_cur + ((conf->interval_delay_msec * conf->tickrate) / 1000);
  459. } else /*continue burst */ {
  460. run->burst_cur++;
  461. run->state = FVWaitBurst;
  462. run->tick_next = run->tick_cur + ((conf->burst_delay_msec * conf->tickrate) / 1000);
  463. }
  464. }
  465. if(run->shot_cur > conf->shot_count) {
  466. run->state = FVDone;
  467. }
  468. }
  469. static void flipvalo_intv_stop(struct flipvalo_priv* fv_priv) {
  470. fv_priv->run_state.state = FVDone;
  471. }
  472. static void flipvalo_intv_start(struct flipvalo_priv* fv_priv) {
  473. // clear struct
  474. furi_assert(fv_priv);
  475. flipvalo_run_state_init(&fv_priv->run_state);
  476. fv_priv->run_state.state = FVWaitInitShot;
  477. fv_priv->run_state.tick_next =
  478. ((fv_priv->config.init_delay_msec * fv_priv->config.tickrate) / 1000);
  479. }
  480. static void timer_callback(void* ctx) {
  481. furi_assert(ctx);
  482. struct flipvalo_priv* fv_priv = ctx;
  483. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  484. if(flipvalo_intv_running(fv_priv)) {
  485. flipvalo_intv_tick(fv_priv);
  486. }
  487. furi_mutex_release(fv_priv->mutex);
  488. }
  489. static void render_callback(Canvas* const canvas, void* ctx) {
  490. furi_assert(ctx);
  491. struct flipvalo_priv* fv_priv = ctx;
  492. FuriString* temp_str = furi_string_alloc();
  493. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  494. // invert screen if blinking
  495. if(fv_priv->gui_shutter_blink > 0) {
  496. fv_priv->gui_shutter_blink--;
  497. canvas_draw_box(canvas, 0, 0, 127, 63);
  498. canvas_set_color(canvas, ColorWhite);
  499. }
  500. if(fv_priv->ui_scene == FVSceneMain) {
  501. int countdown_msec =
  502. (1000 * (fv_priv->run_state.tick_next - fv_priv->run_state.tick_cur)) /
  503. fv_priv->config.tickrate;
  504. int elapsed_msec = (1000 * fv_priv->run_state.tick_cur) / fv_priv->config.tickrate;
  505. canvas_draw_frame(canvas, 0, 0, 128, 64);
  506. // draw countdown
  507. canvas_set_font(canvas, FontPrimary);
  508. furi_string_printf(
  509. temp_str,
  510. "%02d:%02d:%02d:%03d",
  511. countdown_msec / 3600000,
  512. (countdown_msec / 60000) % 60,
  513. (countdown_msec / 1000) % 60,
  514. countdown_msec % 1000);
  515. canvas_draw_str_aligned(
  516. canvas, 64, 24, AlignCenter, AlignCenter, furi_string_get_cstr(temp_str));
  517. // draw top and bottom status bars
  518. canvas_set_font(canvas, FontSecondary);
  519. furi_string_printf(
  520. temp_str,
  521. "%02d:%02d:%02d",
  522. elapsed_msec / 3600000,
  523. (elapsed_msec / 60000) % 60,
  524. (elapsed_msec / 1000) % 60);
  525. canvas_draw_str_aligned(
  526. canvas, 4, 8, AlignLeft, AlignCenter, furi_string_get_cstr(temp_str));
  527. furi_string_printf(temp_str, "Shot: %d", fv_priv->run_state.shot_cur);
  528. canvas_draw_str_aligned(
  529. canvas, 124, 8, AlignRight, AlignCenter, furi_string_get_cstr(temp_str));
  530. elements_button_left(canvas, "Cfg");
  531. elements_button_right(canvas, "Snap");
  532. if(fv_priv->run_state.state == FVDone) {
  533. elements_button_center(canvas, "Start");
  534. } else {
  535. elements_button_center(canvas, "Stop ");
  536. }
  537. } else if(fv_priv->ui_scene == FVSceneConfig) {
  538. flipvalo_config_edit_draw(canvas, &fv_priv->config_edit_view);
  539. }
  540. furi_string_free(temp_str);
  541. furi_mutex_release(fv_priv->mutex);
  542. }
  543. static void flipvalo_config_init(struct flipvalo_config* fv_conf) {
  544. fv_conf->init_delay_msec = 2000;
  545. fv_conf->interval_delay_msec = 0;
  546. fv_conf->shot_count = 1;
  547. fv_conf->burst_count = 1;
  548. fv_conf->burst_delay_msec = 0;
  549. fv_conf->tickrate = 125;
  550. fv_conf->trigger = FvTrigSony;
  551. fv_conf->output_config = NULL;
  552. }
  553. static void flipvalo_priv_init(struct flipvalo_priv* fv_priv) {
  554. flipvalo_config_init(&fv_priv->config);
  555. flipvalo_config_edit_view_init(&fv_priv->config_edit_view);
  556. flipvalo_run_state_init(&fv_priv->run_state);
  557. fv_priv->gui_shutter_blink = 0;
  558. fv_priv->timer = NULL;
  559. fv_priv->notifications = NULL;
  560. fv_priv->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  561. fv_priv->gui_shutter_blink = 0;
  562. }
  563. int32_t flipvalo_app() {
  564. int ret = 0;
  565. ViewPort* view_port = NULL;
  566. Gui* gui = NULL;
  567. FuriStatus event_status = {0};
  568. struct plugin_event event = {0};
  569. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(struct plugin_event));
  570. struct flipvalo_priv* fv_priv = malloc(sizeof(*fv_priv));
  571. flipvalo_priv_init(fv_priv);
  572. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  573. InfraredSettings settings = {0};
  574. saved_struct_load(
  575. INFRARED_SETTINGS_PATH,
  576. &settings,
  577. sizeof(InfraredSettings),
  578. INFRARED_SETTINGS_MAGIC,
  579. INFRARED_SETTINGS_VERSION);
  580. if(settings.tx_pin < FuriHalInfraredTxPinMax) {
  581. furi_hal_infrared_set_tx_output(settings.tx_pin);
  582. if(settings.otg_enabled != otg_was_enabled) {
  583. if(settings.otg_enabled) {
  584. furi_hal_power_enable_otg();
  585. } else {
  586. furi_hal_power_disable_otg();
  587. }
  588. }
  589. } else {
  590. FuriHalInfraredTxPin tx_pin_detected = furi_hal_infrared_detect_tx_output();
  591. furi_hal_infrared_set_tx_output(tx_pin_detected);
  592. if(tx_pin_detected != FuriHalInfraredTxPinInternal) {
  593. furi_hal_power_enable_otg();
  594. }
  595. }
  596. if(!fv_priv->mutex) {
  597. FURI_LOG_E("Flipvalo", "Cannot create mutex\r\n");
  598. ret = 1;
  599. goto cleanup;
  600. }
  601. view_port = view_port_alloc();
  602. view_port_draw_callback_set(view_port, render_callback, fv_priv);
  603. view_port_input_callback_set(view_port, input_callback, event_queue);
  604. fv_priv->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, fv_priv);
  605. furi_timer_start(
  606. fv_priv->timer, (uint32_t)furi_kernel_get_tick_frequency() / fv_priv->config.tickrate);
  607. gui = furi_record_open("gui");
  608. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  609. while(true) {
  610. event_status = furi_message_queue_get(event_queue, &event, 100);
  611. furi_mutex_acquire(fv_priv->mutex, FuriWaitForever);
  612. // catch event_status that is not Ok
  613. if(event_status == FuriStatusErrorTimeout) {
  614. // timeout, ignore
  615. goto next_event;
  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 && event.input.type == InputTypeLong &&
  625. event.input.key == InputKeyBack) {
  626. goto cleanup;
  627. }
  628. switch(fv_priv->ui_scene) {
  629. case FVSceneMain:
  630. // TODO(luna) Maybe give this a function.. look howl clean FVSceneConfig is...
  631. if(event.type == EventTypeKey) {
  632. if(event.input.type == InputTypeShort || event.input.type == InputTypeLong) {
  633. switch(event.input.key) {
  634. case InputKeyUp:
  635. break;
  636. case InputKeyDown:
  637. break;
  638. case InputKeyLeft:
  639. flipvalo_intv_stop(fv_priv);
  640. fv_priv->config_edit_view.config = &fv_priv->config;
  641. fv_priv->ui_scene = FVSceneConfig;
  642. break;
  643. case InputKeyRight:
  644. fv_priv->gui_shutter_blink = 3;
  645. flipvalo_get_trigger(fv_priv->config.trigger)
  646. ->send(fv_priv->config.output_config);
  647. break;
  648. case InputKeyOk:
  649. if(flipvalo_intv_running(fv_priv)) {
  650. flipvalo_intv_stop(fv_priv);
  651. } else {
  652. flipvalo_intv_start(fv_priv);
  653. }
  654. break;
  655. case InputKeyMAX:
  656. break;
  657. case InputKeyBack:
  658. break;
  659. }
  660. }
  661. }
  662. break;
  663. case FVSceneConfig:
  664. ret = flipvalo_config_edit_input(&event.input, &fv_priv->config_edit_view);
  665. if(ret) {
  666. fv_priv->ui_scene = FVSceneMain;
  667. }
  668. break;
  669. }
  670. next_event:
  671. furi_mutex_release(fv_priv->mutex);
  672. view_port_update(view_port);
  673. }
  674. cleanup:
  675. if(view_port) {
  676. view_port_enabled_set(view_port, false);
  677. if(gui) {
  678. gui_remove_view_port(gui, view_port);
  679. furi_record_close("gui");
  680. }
  681. view_port_free(view_port);
  682. }
  683. furi_hal_infrared_set_tx_output(FuriHalInfraredTxPinInternal);
  684. if(furi_hal_power_is_otg_enabled() != otg_was_enabled) {
  685. if(otg_was_enabled) {
  686. furi_hal_power_enable_otg();
  687. } else {
  688. furi_hal_power_disable_otg();
  689. }
  690. }
  691. if(event_queue) {
  692. furi_message_queue_free(event_queue);
  693. }
  694. if(fv_priv) {
  695. furi_mutex_free(fv_priv->mutex);
  696. furi_timer_free(fv_priv->timer);
  697. }
  698. free(fv_priv);
  699. return ret;
  700. }