intervalometer.c 24 KB

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