dolphin.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "dolphin_i.h"
  2. #include <stdlib.h>
  3. #include "applications.h"
  4. const Icon* idle_scenes[] = {&A_Wink_128x64, &A_WatchingTV_128x64};
  5. static void dolphin_switch_to_app(Dolphin* dolphin, const FlipperApplication* flipper_app) {
  6. furi_assert(dolphin);
  7. furi_assert(flipper_app);
  8. furi_assert(flipper_app->app);
  9. furi_assert(flipper_app->name);
  10. furi_thread_set_name(dolphin->scene_thread, flipper_app->name);
  11. furi_thread_set_stack_size(dolphin->scene_thread, flipper_app->stack_size);
  12. furi_thread_set_callback(dolphin->scene_thread, flipper_app->app);
  13. furi_thread_start(dolphin->scene_thread);
  14. }
  15. // temporary main screen animation managment
  16. void dolphin_scene_handler_set_scene(Dolphin* dolphin, const Icon* icon_data) {
  17. with_view_model(
  18. dolphin->idle_view_main, (DolphinViewMainModel * model) {
  19. if(model->animation) icon_animation_free(model->animation);
  20. model->animation = icon_animation_alloc(icon_data);
  21. icon_animation_start(model->animation);
  22. return true;
  23. });
  24. }
  25. void dolphin_scene_handler_switch_scene(Dolphin* dolphin) {
  26. with_view_model(
  27. dolphin->idle_view_main, (DolphinViewMainModel * model) {
  28. if(icon_animation_is_last_frame(model->animation)) {
  29. if(model->animation) icon_animation_free(model->animation);
  30. model->animation = icon_animation_alloc(idle_scenes[model->scene_num]);
  31. icon_animation_start(model->animation);
  32. model->scene_num = random() % COUNT_OF(idle_scenes);
  33. }
  34. return true;
  35. });
  36. }
  37. bool dolphin_view_first_start_input(InputEvent* event, void* context) {
  38. furi_assert(event);
  39. furi_assert(context);
  40. Dolphin* dolphin = context;
  41. if(event->type == InputTypeShort) {
  42. DolphinViewFirstStartModel* model = view_get_model(dolphin->idle_view_first_start);
  43. if(event->key == InputKeyLeft) {
  44. if(model->page > 0) model->page--;
  45. } else if(event->key == InputKeyRight) {
  46. uint32_t page = ++model->page;
  47. if(page > 8) {
  48. dolphin_save(dolphin);
  49. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  50. }
  51. }
  52. view_commit_model(dolphin->idle_view_first_start, true);
  53. }
  54. // All evennts cosumed
  55. return true;
  56. }
  57. void dolphin_lock_handler(InputEvent* event, Dolphin* dolphin) {
  58. furi_assert(event);
  59. furi_assert(dolphin);
  60. with_view_model(
  61. dolphin->idle_view_main, (DolphinViewMainModel * model) {
  62. model->hint_timeout = HINT_TIMEOUT_L;
  63. return true;
  64. });
  65. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  66. uint32_t press_time = HAL_GetTick();
  67. // check if pressed sequentially
  68. if(press_time - dolphin->lock_lastpress > UNLOCK_RST_TIMEOUT) {
  69. dolphin->lock_lastpress = press_time;
  70. dolphin->lock_count = 0;
  71. } else if(press_time - dolphin->lock_lastpress < UNLOCK_RST_TIMEOUT) {
  72. dolphin->lock_lastpress = press_time;
  73. dolphin->lock_count++;
  74. }
  75. if(dolphin->lock_count == 2) {
  76. dolphin->locked = false;
  77. dolphin->lock_count = 0;
  78. with_view_model(
  79. dolphin->view_lockmenu, (DolphinViewLockMenuModel * model) {
  80. model->locked = false;
  81. model->door_left_x = -57; // move doors to default pos
  82. model->door_right_x = 115;
  83. return true;
  84. });
  85. with_view_model(
  86. dolphin->idle_view_main, (DolphinViewMainModel * model) {
  87. model->hint_timeout = HINT_TIMEOUT_L; // "unlocked" hint timeout
  88. model->locked = false;
  89. return true;
  90. });
  91. view_port_enabled_set(dolphin->lock_viewport, false);
  92. }
  93. }
  94. }
  95. bool dolphin_view_idle_main_input(InputEvent* event, void* context) {
  96. furi_assert(event);
  97. furi_assert(context);
  98. Dolphin* dolphin = context;
  99. // unlocked
  100. if(!dolphin->locked) {
  101. if(event->key == InputKeyOk && event->type == InputTypeShort) {
  102. with_value_mutex(
  103. dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
  104. } else if(event->key == InputKeyUp && event->type == InputTypeShort) {
  105. osTimerStart(dolphin->timeout_timer, 40);
  106. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewLockMenu);
  107. } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  108. #if 0
  109. dolphin_switch_to_app(dolphin, &FAV_APP);
  110. #endif
  111. } else if(event->key == InputKeyRight && event->type == InputTypeShort) {
  112. dolphin_switch_to_app(dolphin, &FLIPPER_SCENE);
  113. } else if(event->key == InputKeyDown && event->type == InputTypeShort) {
  114. dolphin_switch_to_app(dolphin, &FLIPPER_ARCHIVE);
  115. } else if(event->key == InputKeyDown && event->type == InputTypeLong) {
  116. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewStats);
  117. } else if(event->key == InputKeyBack && event->type == InputTypeShort) {
  118. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  119. }
  120. with_view_model(
  121. dolphin->idle_view_main, (DolphinViewMainModel * model) {
  122. model->hint_timeout = 0; // clear hint timeout
  123. return true;
  124. });
  125. } else {
  126. // locked
  127. dolphin_lock_handler(event, dolphin);
  128. dolphin_scene_handler_switch_scene(dolphin);
  129. }
  130. // All events consumed
  131. return true;
  132. }
  133. void lock_menu_refresh_handler(void* p) {
  134. osMessageQueueId_t event_queue = p;
  135. DolphinEvent event;
  136. event.type = DolphinEventTypeTick;
  137. // Some tick events may lost and we don't care.
  138. osMessageQueuePut(event_queue, &event, 0, 0);
  139. }
  140. static void lock_menu_callback(void* context, uint8_t index) {
  141. furi_assert(context);
  142. Dolphin* dolphin = context;
  143. switch(index) {
  144. // lock
  145. case 0:
  146. dolphin->locked = true;
  147. with_view_model(
  148. dolphin->view_lockmenu, (DolphinViewLockMenuModel * model) {
  149. model->locked = true;
  150. model->exit_timeout = HINT_TIMEOUT_H;
  151. return true;
  152. });
  153. with_view_model(
  154. dolphin->idle_view_main, (DolphinViewMainModel * model) {
  155. model->locked = true;
  156. return true;
  157. });
  158. break;
  159. default:
  160. // wip message
  161. with_view_model(
  162. dolphin->view_lockmenu, (DolphinViewLockMenuModel * model) {
  163. model->hint_timeout = HINT_TIMEOUT_H;
  164. return true;
  165. });
  166. break;
  167. }
  168. }
  169. static void lock_icon_callback(Canvas* canvas, void* context) {
  170. furi_assert(context);
  171. Dolphin* dolphin = context;
  172. canvas_draw_icon_animation(canvas, 0, 0, dolphin->lock_icon);
  173. }
  174. bool dolphin_view_lockmenu_input(InputEvent* event, void* context) {
  175. furi_assert(event);
  176. furi_assert(context);
  177. Dolphin* dolphin = context;
  178. if(event->type != InputTypeShort) return false;
  179. DolphinViewLockMenuModel* model = view_get_model(dolphin->view_lockmenu);
  180. model->hint_timeout = 0; // clear hint timeout
  181. if(event->key == InputKeyUp) {
  182. model->idx = CLAMP(model->idx - 1, 2, 0);
  183. } else if(event->key == InputKeyDown) {
  184. model->idx = CLAMP(model->idx + 1, 2, 0);
  185. } else if(event->key == InputKeyOk) {
  186. lock_menu_callback(context, model->idx);
  187. } else if(event->key == InputKeyBack) {
  188. model->idx = 0;
  189. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  190. if(random() % 100 > 50)
  191. dolphin_scene_handler_set_scene(
  192. dolphin, idle_scenes[random() % COUNT_OF(idle_scenes)]);
  193. }
  194. view_commit_model(dolphin->view_lockmenu, true);
  195. return true;
  196. }
  197. bool dolphin_view_idle_down_input(InputEvent* event, void* context) {
  198. furi_assert(event);
  199. furi_assert(context);
  200. Dolphin* dolphin = context;
  201. DolphinViewStatsScreens current;
  202. if(event->type != InputTypeShort) return false;
  203. DolphinViewStatsModel* model = view_get_model(dolphin->idle_view_dolphin_stats);
  204. current = model->screen;
  205. if(event->key == InputKeyDown) {
  206. model->screen = (model->screen + 1) % DolphinViewStatsTotalCount;
  207. } else if(event->key == InputKeyUp) {
  208. model->screen =
  209. ((model->screen - 1) + DolphinViewStatsTotalCount) % DolphinViewStatsTotalCount;
  210. }
  211. view_commit_model(dolphin->idle_view_dolphin_stats, true);
  212. if(current == DolphinViewStatsMeta) {
  213. if(event->key == InputKeyLeft) {
  214. dolphin_deed(dolphin, DolphinDeedWrong);
  215. } else if(event->key == InputKeyRight) {
  216. dolphin_deed(dolphin, DolphinDeedIButtonRead);
  217. } else if(event->key == InputKeyOk) {
  218. dolphin_save(dolphin);
  219. } else {
  220. return false;
  221. }
  222. }
  223. if(event->key == InputKeyBack) {
  224. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  225. }
  226. return true;
  227. }
  228. Dolphin* dolphin_alloc() {
  229. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  230. // Message queue
  231. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  232. furi_check(dolphin->event_queue);
  233. // State
  234. dolphin->state = dolphin_state_alloc();
  235. // Menu
  236. dolphin->menu_vm = furi_record_open("menu");
  237. // Scene thread
  238. dolphin->scene_thread = furi_thread_alloc();
  239. // GUI
  240. dolphin->gui = furi_record_open("gui");
  241. // Dispatcher
  242. dolphin->idle_view_dispatcher = view_dispatcher_alloc();
  243. // First start View
  244. dolphin->idle_view_first_start = view_alloc();
  245. view_allocate_model(
  246. dolphin->idle_view_first_start, ViewModelTypeLockFree, sizeof(DolphinViewFirstStartModel));
  247. view_set_context(dolphin->idle_view_first_start, dolphin);
  248. view_set_draw_callback(dolphin->idle_view_first_start, dolphin_view_first_start_draw);
  249. view_set_input_callback(dolphin->idle_view_first_start, dolphin_view_first_start_input);
  250. view_dispatcher_add_view(
  251. dolphin->idle_view_dispatcher, DolphinViewFirstStart, dolphin->idle_view_first_start);
  252. // Main Idle View
  253. dolphin->idle_view_main = view_alloc();
  254. view_set_context(dolphin->idle_view_main, dolphin);
  255. view_allocate_model(
  256. dolphin->idle_view_main, ViewModelTypeLockFree, sizeof(DolphinViewMainModel));
  257. view_set_draw_callback(dolphin->idle_view_main, dolphin_view_idle_main_draw);
  258. view_set_input_callback(dolphin->idle_view_main, dolphin_view_idle_main_input);
  259. view_dispatcher_add_view(
  260. dolphin->idle_view_dispatcher, DolphinViewIdleMain, dolphin->idle_view_main);
  261. // Lock Menu View
  262. dolphin->view_lockmenu = view_alloc();
  263. view_set_context(dolphin->view_lockmenu, dolphin);
  264. view_allocate_model(
  265. dolphin->view_lockmenu, ViewModelTypeLockFree, sizeof(DolphinViewLockMenuModel));
  266. view_set_draw_callback(dolphin->view_lockmenu, dolphin_view_lockmenu_draw);
  267. view_set_input_callback(dolphin->view_lockmenu, dolphin_view_lockmenu_input);
  268. view_set_previous_callback(dolphin->view_lockmenu, dolphin_view_idle_back);
  269. view_dispatcher_add_view(
  270. dolphin->idle_view_dispatcher, DolphinViewLockMenu, dolphin->view_lockmenu);
  271. // default doors xpos
  272. with_view_model(
  273. dolphin->view_lockmenu, (DolphinViewLockMenuModel * model) {
  274. model->door_left_x = -57; // defaults
  275. model->door_right_x = 115; // defaults
  276. return true;
  277. });
  278. dolphin->timeout_timer =
  279. osTimerNew(lock_menu_refresh_handler, osTimerPeriodic, dolphin->event_queue, NULL);
  280. // Stats Idle View
  281. dolphin->idle_view_dolphin_stats = view_alloc();
  282. view_set_context(dolphin->idle_view_dolphin_stats, dolphin);
  283. view_allocate_model(
  284. dolphin->idle_view_dolphin_stats, ViewModelTypeLockFree, sizeof(DolphinViewStatsModel));
  285. view_set_draw_callback(dolphin->idle_view_dolphin_stats, dolphin_view_idle_down_draw);
  286. view_set_input_callback(dolphin->idle_view_dolphin_stats, dolphin_view_idle_down_input);
  287. view_set_previous_callback(dolphin->idle_view_dolphin_stats, dolphin_view_idle_back);
  288. view_dispatcher_add_view(
  289. dolphin->idle_view_dispatcher, DolphinViewStats, dolphin->idle_view_dolphin_stats);
  290. // HW Mismatch
  291. dolphin->view_hw_mismatch = view_alloc();
  292. view_set_draw_callback(dolphin->view_hw_mismatch, dolphin_view_hw_mismatch_draw);
  293. view_set_previous_callback(dolphin->view_hw_mismatch, dolphin_view_idle_back);
  294. view_dispatcher_add_view(
  295. dolphin->idle_view_dispatcher, DolphinViewHwMismatch, dolphin->view_hw_mismatch);
  296. // Lock icon
  297. dolphin->lock_icon = icon_animation_alloc(&I_Lock_8x8);
  298. dolphin->lock_viewport = view_port_alloc();
  299. view_port_set_width(dolphin->lock_viewport, icon_animation_get_width(dolphin->lock_icon));
  300. view_port_draw_callback_set(dolphin->lock_viewport, lock_icon_callback, dolphin);
  301. view_port_enabled_set(dolphin->lock_viewport, false);
  302. // Main screen animation
  303. dolphin_scene_handler_set_scene(dolphin, idle_scenes[random() % COUNT_OF(idle_scenes)]);
  304. view_dispatcher_attach_to_gui(
  305. dolphin->idle_view_dispatcher, dolphin->gui, ViewDispatcherTypeWindow);
  306. gui_add_view_port(dolphin->gui, dolphin->lock_viewport, GuiLayerStatusBarLeft);
  307. return dolphin;
  308. }
  309. void dolphin_free(Dolphin* dolphin) {
  310. furi_assert(dolphin);
  311. gui_remove_view_port(dolphin->gui, dolphin->lock_viewport);
  312. view_port_free(dolphin->lock_viewport);
  313. icon_animation_free(dolphin->lock_icon);
  314. osTimerDelete(dolphin->timeout_timer);
  315. view_dispatcher_free(dolphin->idle_view_dispatcher);
  316. furi_record_close("gui");
  317. dolphin->gui = NULL;
  318. furi_thread_free(dolphin->scene_thread);
  319. furi_record_close("menu");
  320. dolphin->menu_vm = NULL;
  321. dolphin_state_free(dolphin->state);
  322. osMessageQueueDelete(dolphin->event_queue);
  323. free(dolphin);
  324. }
  325. void dolphin_save(Dolphin* dolphin) {
  326. furi_assert(dolphin);
  327. DolphinEvent event;
  328. event.type = DolphinEventTypeSave;
  329. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  330. }
  331. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  332. furi_assert(dolphin);
  333. DolphinEvent event;
  334. event.type = DolphinEventTypeDeed;
  335. event.deed = deed;
  336. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  337. }
  338. int32_t dolphin_srv() {
  339. Dolphin* dolphin = dolphin_alloc();
  340. if(dolphin_state_load(dolphin->state)) {
  341. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  342. } else {
  343. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewFirstStart);
  344. }
  345. with_view_model(
  346. dolphin->idle_view_dolphin_stats, (DolphinViewStatsModel * model) {
  347. model->icounter = dolphin_state_get_icounter(dolphin->state);
  348. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  349. return true;
  350. });
  351. furi_record_create("dolphin", dolphin);
  352. if(!furi_hal_version_do_i_belong_here()) {
  353. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewHwMismatch);
  354. }
  355. DolphinEvent event;
  356. while(1) {
  357. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  358. DolphinViewLockMenuModel* lock_model = view_get_model(dolphin->view_lockmenu);
  359. if(lock_model->locked && lock_model->exit_timeout == 0 &&
  360. osTimerIsRunning(dolphin->timeout_timer)) {
  361. osTimerStop(dolphin->timeout_timer);
  362. osDelay(1); // smol enterprise delay
  363. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  364. }
  365. if(event.type == DolphinEventTypeTick) {
  366. view_commit_model(dolphin->view_lockmenu, true);
  367. } else if(event.type == DolphinEventTypeDeed) {
  368. dolphin_state_on_deed(dolphin->state, event.deed);
  369. with_view_model(
  370. dolphin->idle_view_dolphin_stats, (DolphinViewStatsModel * model) {
  371. model->icounter = dolphin_state_get_icounter(dolphin->state);
  372. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  373. return true;
  374. });
  375. } else if(event.type == DolphinEventTypeSave) {
  376. dolphin_state_save(dolphin->state);
  377. }
  378. }
  379. dolphin_free(dolphin);
  380. return 0;
  381. }