intervalometer.c 26 KB

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