intervalometer.c 25 KB

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