dolphin.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. break;
  161. }
  162. }
  163. static void lock_icon_callback(Canvas* canvas, void* context) {
  164. furi_assert(context);
  165. Dolphin* dolphin = context;
  166. canvas_draw_icon_animation(canvas, 0, 0, dolphin->lock_icon);
  167. }
  168. bool dolphin_view_lockmenu_input(InputEvent* event, void* context) {
  169. furi_assert(event);
  170. furi_assert(context);
  171. Dolphin* dolphin = context;
  172. if(event->type != InputTypeShort) return false;
  173. DolphinViewLockMenuModel* model = view_get_model(dolphin->view_lockmenu);
  174. if(event->key == InputKeyUp) {
  175. model->idx = CLAMP(model->idx - 1, 2, 0);
  176. } else if(event->key == InputKeyDown) {
  177. model->idx = CLAMP(model->idx + 1, 2, 0);
  178. } else if(event->key == InputKeyOk) {
  179. lock_menu_callback(context, model->idx);
  180. } else if(event->key == InputKeyBack) {
  181. model->idx = 0;
  182. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  183. if(random() % 100 > 50)
  184. dolphin_scene_handler_set_scene(
  185. dolphin, idle_scenes[random() % COUNT_OF(idle_scenes)]);
  186. }
  187. view_commit_model(dolphin->view_lockmenu, true);
  188. return true;
  189. }
  190. bool dolphin_view_idle_down_input(InputEvent* event, void* context) {
  191. furi_assert(event);
  192. furi_assert(context);
  193. Dolphin* dolphin = context;
  194. DolphinViewStatsScreens current;
  195. if(event->type != InputTypeShort) return false;
  196. DolphinViewStatsModel* model = view_get_model(dolphin->idle_view_dolphin_stats);
  197. current = model->screen;
  198. if(event->key == InputKeyDown) {
  199. model->screen = (model->screen + 1) % DolphinViewStatsTotalCount;
  200. } else if(event->key == InputKeyUp) {
  201. model->screen =
  202. ((model->screen - 1) + DolphinViewStatsTotalCount) % DolphinViewStatsTotalCount;
  203. }
  204. view_commit_model(dolphin->idle_view_dolphin_stats, true);
  205. if(current == DolphinViewStatsMeta) {
  206. if(event->key == InputKeyLeft) {
  207. dolphin_deed(dolphin, DolphinDeedWrong);
  208. } else if(event->key == InputKeyRight) {
  209. dolphin_deed(dolphin, DolphinDeedIButtonRead);
  210. } else if(event->key == InputKeyOk) {
  211. dolphin_save(dolphin);
  212. } else {
  213. return false;
  214. }
  215. }
  216. if(event->key == InputKeyBack) {
  217. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  218. }
  219. return true;
  220. }
  221. Dolphin* dolphin_alloc() {
  222. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  223. // Message queue
  224. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  225. furi_check(dolphin->event_queue);
  226. // State
  227. dolphin->state = dolphin_state_alloc();
  228. // Menu
  229. dolphin->menu_vm = furi_record_open("menu");
  230. // Scene thread
  231. dolphin->scene_thread = furi_thread_alloc();
  232. // GUI
  233. dolphin->gui = furi_record_open("gui");
  234. // Dispatcher
  235. dolphin->idle_view_dispatcher = view_dispatcher_alloc();
  236. // First start View
  237. dolphin->idle_view_first_start = view_alloc();
  238. view_allocate_model(
  239. dolphin->idle_view_first_start, ViewModelTypeLockFree, sizeof(DolphinViewFirstStartModel));
  240. view_set_context(dolphin->idle_view_first_start, dolphin);
  241. view_set_draw_callback(dolphin->idle_view_first_start, dolphin_view_first_start_draw);
  242. view_set_input_callback(dolphin->idle_view_first_start, dolphin_view_first_start_input);
  243. view_dispatcher_add_view(
  244. dolphin->idle_view_dispatcher, DolphinViewFirstStart, dolphin->idle_view_first_start);
  245. // Main Idle View
  246. dolphin->idle_view_main = view_alloc();
  247. view_set_context(dolphin->idle_view_main, dolphin);
  248. view_allocate_model(
  249. dolphin->idle_view_main, ViewModelTypeLockFree, sizeof(DolphinViewMainModel));
  250. view_set_draw_callback(dolphin->idle_view_main, dolphin_view_idle_main_draw);
  251. view_set_input_callback(dolphin->idle_view_main, dolphin_view_idle_main_input);
  252. view_dispatcher_add_view(
  253. dolphin->idle_view_dispatcher, DolphinViewIdleMain, dolphin->idle_view_main);
  254. // Lock Menu View
  255. dolphin->view_lockmenu = view_alloc();
  256. view_set_context(dolphin->view_lockmenu, dolphin);
  257. view_allocate_model(
  258. dolphin->view_lockmenu, ViewModelTypeLockFree, sizeof(DolphinViewLockMenuModel));
  259. view_set_draw_callback(dolphin->view_lockmenu, dolphin_view_lockmenu_draw);
  260. view_set_input_callback(dolphin->view_lockmenu, dolphin_view_lockmenu_input);
  261. view_set_previous_callback(dolphin->view_lockmenu, dolphin_view_idle_back);
  262. view_dispatcher_add_view(
  263. dolphin->idle_view_dispatcher, DolphinViewLockMenu, dolphin->view_lockmenu);
  264. // default doors xpos
  265. with_view_model(
  266. dolphin->view_lockmenu, (DolphinViewLockMenuModel * model) {
  267. model->door_left_x = -57; // defaults
  268. model->door_right_x = 115; // defaults
  269. return true;
  270. });
  271. dolphin->timeout_timer =
  272. osTimerNew(lock_menu_refresh_handler, osTimerPeriodic, dolphin->event_queue, NULL);
  273. // Stats Idle View
  274. dolphin->idle_view_dolphin_stats = view_alloc();
  275. view_set_context(dolphin->idle_view_dolphin_stats, dolphin);
  276. view_allocate_model(
  277. dolphin->idle_view_dolphin_stats, ViewModelTypeLockFree, sizeof(DolphinViewStatsModel));
  278. view_set_draw_callback(dolphin->idle_view_dolphin_stats, dolphin_view_idle_down_draw);
  279. view_set_input_callback(dolphin->idle_view_dolphin_stats, dolphin_view_idle_down_input);
  280. view_set_previous_callback(dolphin->idle_view_dolphin_stats, dolphin_view_idle_back);
  281. view_dispatcher_add_view(
  282. dolphin->idle_view_dispatcher, DolphinViewStats, dolphin->idle_view_dolphin_stats);
  283. // HW Mismatch
  284. dolphin->view_hw_mismatch = view_alloc();
  285. view_set_draw_callback(dolphin->view_hw_mismatch, dolphin_view_hw_mismatch_draw);
  286. view_set_previous_callback(dolphin->view_hw_mismatch, dolphin_view_idle_back);
  287. view_dispatcher_add_view(
  288. dolphin->idle_view_dispatcher, DolphinViewHwMismatch, dolphin->view_hw_mismatch);
  289. // Lock icon
  290. dolphin->lock_icon = icon_animation_alloc(&I_Lock_8x8);
  291. dolphin->lock_viewport = view_port_alloc();
  292. view_port_set_width(dolphin->lock_viewport, icon_animation_get_width(dolphin->lock_icon));
  293. view_port_draw_callback_set(dolphin->lock_viewport, lock_icon_callback, dolphin);
  294. view_port_enabled_set(dolphin->lock_viewport, false);
  295. // Main screen animation
  296. dolphin_scene_handler_set_scene(dolphin, idle_scenes[random() % COUNT_OF(idle_scenes)]);
  297. view_dispatcher_attach_to_gui(
  298. dolphin->idle_view_dispatcher, dolphin->gui, ViewDispatcherTypeWindow);
  299. gui_add_view_port(dolphin->gui, dolphin->lock_viewport, GuiLayerStatusBarLeft);
  300. return dolphin;
  301. }
  302. void dolphin_free(Dolphin* dolphin) {
  303. furi_assert(dolphin);
  304. gui_remove_view_port(dolphin->gui, dolphin->lock_viewport);
  305. view_port_free(dolphin->lock_viewport);
  306. icon_animation_free(dolphin->lock_icon);
  307. osTimerDelete(dolphin->timeout_timer);
  308. view_dispatcher_free(dolphin->idle_view_dispatcher);
  309. furi_record_close("gui");
  310. dolphin->gui = NULL;
  311. furi_thread_free(dolphin->scene_thread);
  312. furi_record_close("menu");
  313. dolphin->menu_vm = NULL;
  314. dolphin_state_free(dolphin->state);
  315. osMessageQueueDelete(dolphin->event_queue);
  316. free(dolphin);
  317. }
  318. void dolphin_save(Dolphin* dolphin) {
  319. furi_assert(dolphin);
  320. DolphinEvent event;
  321. event.type = DolphinEventTypeSave;
  322. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  323. }
  324. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  325. furi_assert(dolphin);
  326. DolphinEvent event;
  327. event.type = DolphinEventTypeDeed;
  328. event.deed = deed;
  329. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  330. }
  331. int32_t dolphin_task() {
  332. Dolphin* dolphin = dolphin_alloc();
  333. if(dolphin_state_load(dolphin->state)) {
  334. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  335. } else {
  336. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewFirstStart);
  337. }
  338. with_view_model(
  339. dolphin->idle_view_dolphin_stats, (DolphinViewStatsModel * model) {
  340. model->icounter = dolphin_state_get_icounter(dolphin->state);
  341. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  342. return true;
  343. });
  344. furi_record_create("dolphin", dolphin);
  345. if(!api_hal_version_do_i_belong_here()) {
  346. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewHwMismatch);
  347. }
  348. DolphinEvent event;
  349. while(1) {
  350. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  351. DolphinViewLockMenuModel* lock_model = view_get_model(dolphin->view_lockmenu);
  352. if(lock_model->locked && lock_model->exit_timeout == 0 &&
  353. osTimerIsRunning(dolphin->timeout_timer)) {
  354. osTimerStop(dolphin->timeout_timer);
  355. osDelay(1); // smol enterprise delay
  356. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  357. }
  358. if(event.type == DolphinEventTypeTick) {
  359. view_commit_model(dolphin->view_lockmenu, true);
  360. } else if(event.type == DolphinEventTypeDeed) {
  361. dolphin_state_on_deed(dolphin->state, event.deed);
  362. with_view_model(
  363. dolphin->idle_view_dolphin_stats, (DolphinViewStatsModel * model) {
  364. model->icounter = dolphin_state_get_icounter(dolphin->state);
  365. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  366. return true;
  367. });
  368. } else if(event.type == DolphinEventTypeSave) {
  369. dolphin_state_save(dolphin->state);
  370. }
  371. }
  372. dolphin_free(dolphin);
  373. return 0;
  374. }