desktop_animation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include "desktop/helpers/desktop_animation.h"
  2. #include "assets_icons.h"
  3. #include "desktop_animation_i.h"
  4. #include "cmsis_os2.h"
  5. #include "furi/common_defines.h"
  6. #include "furi/record.h"
  7. #include "storage/filesystem-api-defines.h"
  8. #include <power/power_service/power.h>
  9. #include <m-list.h>
  10. #include <storage/storage.h>
  11. #include <desktop/desktop.h>
  12. #include <dolphin/dolphin.h>
  13. LIST_DEF(AnimationList, const PairedAnimation*, M_PTR_OPLIST)
  14. #define M_OPL_AnimationList_t() LIST_OPLIST(AnimationList)
  15. #define PUSH_BACK_ANIMATIONS(listname, animations, butthurt) \
  16. for(int i = 0; i < COUNT_OF(animations); ++i) { \
  17. if(!(animations)[i].basic->butthurt_level_mask || \
  18. ((animations)[i].basic->butthurt_level_mask & BUTTHURT_LEVEL(butthurt))) { \
  19. AnimationList_push_back(animation_list, &(animations)[i]); \
  20. } \
  21. }
  22. #define IS_BLOCKING_ANIMATION(x) \
  23. (((x) != DesktopAnimationStateBasic) && ((x) != DesktopAnimationStateActive))
  24. #define IS_ONESHOT_ANIMATION(x) ((x) == DesktopAnimationStateLevelUpIsPending)
  25. static void desktop_animation_timer_callback(void* context);
  26. struct DesktopAnimation {
  27. bool sd_shown_error_db;
  28. bool sd_shown_error_card_bad;
  29. osTimerId_t timer;
  30. const PairedAnimation* current;
  31. const Icon* current_blocking_icon;
  32. const Icon** current_one_shot_icons;
  33. uint8_t one_shot_animation_counter;
  34. uint8_t one_shot_animation_size;
  35. DesktopAnimationState state;
  36. TickType_t basic_started_at;
  37. TickType_t active_finished_at;
  38. AnimationChangedCallback animation_changed_callback;
  39. void* animation_changed_callback_context;
  40. };
  41. DesktopAnimation* desktop_animation_alloc(void) {
  42. DesktopAnimation* animation = furi_alloc(sizeof(DesktopAnimation));
  43. animation->timer = osTimerNew(
  44. desktop_animation_timer_callback, osTimerPeriodic /* osTimerOnce */, animation, NULL);
  45. animation->active_finished_at = (TickType_t)(-30);
  46. animation->basic_started_at = 0;
  47. animation->animation_changed_callback = NULL;
  48. animation->animation_changed_callback_context = NULL;
  49. desktop_start_new_idle_animation(animation);
  50. return animation;
  51. }
  52. void desktop_animation_free(DesktopAnimation* animation) {
  53. furi_assert(animation);
  54. osTimerDelete(animation->timer);
  55. free(animation);
  56. }
  57. void desktop_animation_set_animation_changed_callback(
  58. DesktopAnimation* animation,
  59. AnimationChangedCallback callback,
  60. void* context) {
  61. furi_assert(animation);
  62. animation->animation_changed_callback = callback;
  63. animation->animation_changed_callback_context = context;
  64. }
  65. void desktop_start_new_idle_animation(DesktopAnimation* animation) {
  66. Dolphin* dolphin = furi_record_open("dolphin");
  67. DolphinStats stats = dolphin_stats(dolphin);
  68. furi_record_close("dolphin");
  69. furi_assert((stats.level >= 1) && (stats.level <= 3));
  70. uint8_t level = stats.level;
  71. AnimationList_t animation_list;
  72. AnimationList_init(animation_list);
  73. PUSH_BACK_ANIMATIONS(animation_list, mad_animation, stats.butthurt);
  74. PUSH_BACK_ANIMATIONS(animation_list, calm_animation, stats.butthurt);
  75. switch(level) {
  76. case 1:
  77. PUSH_BACK_ANIMATIONS(animation_list, level_1_animation, stats.butthurt);
  78. break;
  79. case 2:
  80. PUSH_BACK_ANIMATIONS(animation_list, level_2_animation, stats.butthurt);
  81. break;
  82. case 3:
  83. PUSH_BACK_ANIMATIONS(animation_list, level_3_animation, stats.butthurt);
  84. break;
  85. default:
  86. furi_crash("Dolphin level is out of bounds");
  87. }
  88. Power* power = furi_record_open("power");
  89. PowerInfo info;
  90. power_get_info(power, &info);
  91. if(!power_is_battery_well(&info)) {
  92. PUSH_BACK_ANIMATIONS(animation_list, check_battery_animation, stats.butthurt);
  93. }
  94. Storage* storage = furi_record_open("storage");
  95. FS_Error sd_status = storage_sd_status(storage);
  96. animation->current = NULL;
  97. if(sd_status == FSE_NOT_READY) {
  98. PUSH_BACK_ANIMATIONS(animation_list, no_sd_animation, stats.butthurt);
  99. animation->sd_shown_error_card_bad = false;
  100. animation->sd_shown_error_db = false;
  101. }
  102. uint32_t whole_weight = 0;
  103. for
  104. M_EACH(item, animation_list, AnimationList_t) {
  105. whole_weight += (*item)->basic->weight;
  106. }
  107. uint32_t lucky_number = random() % whole_weight;
  108. uint32_t weight = 0;
  109. const PairedAnimation* selected = NULL;
  110. for
  111. M_EACH(item, animation_list, AnimationList_t) {
  112. if(lucky_number < weight) {
  113. break;
  114. }
  115. weight += (*item)->basic->weight;
  116. selected = *item;
  117. }
  118. animation->basic_started_at = osKernelGetTickCount();
  119. animation->current = selected;
  120. osTimerStart(animation->timer, animation->current->basic->duration * 1000);
  121. animation->state = DesktopAnimationStateBasic;
  122. furi_assert(selected);
  123. AnimationList_clear(animation_list);
  124. }
  125. static void desktop_animation_timer_callback(void* context) {
  126. furi_assert(context);
  127. DesktopAnimation* animation = context;
  128. TickType_t now_ms = osKernelGetTickCount();
  129. AnimationList_t animation_list;
  130. AnimationList_init(animation_list);
  131. bool new_basic_animation = false;
  132. if(animation->state == DesktopAnimationStateActive) {
  133. animation->state = DesktopAnimationStateBasic;
  134. TickType_t basic_lasts_ms = now_ms - animation->basic_started_at;
  135. animation->active_finished_at = now_ms;
  136. TickType_t basic_duration_ms = animation->current->basic->duration * 1000;
  137. if(basic_lasts_ms > basic_duration_ms) {
  138. // if active animation finished, and basic duration came to an end
  139. // select new idle animation
  140. new_basic_animation = true;
  141. } else {
  142. // if active animation finished, but basic duration is not finished
  143. // play current animation for the rest of time
  144. furi_assert(basic_duration_ms != basic_lasts_ms);
  145. osTimerStart(animation->timer, basic_duration_ms - basic_lasts_ms);
  146. }
  147. } else if(animation->state == DesktopAnimationStateBasic) {
  148. // if basic animation finished
  149. // select new idle animation
  150. new_basic_animation = true;
  151. }
  152. if(new_basic_animation) {
  153. animation->basic_started_at = now_ms;
  154. desktop_start_new_idle_animation(animation);
  155. }
  156. // for oneshot generate events every time
  157. if(animation->animation_changed_callback) {
  158. animation->animation_changed_callback(animation->animation_changed_callback_context);
  159. }
  160. }
  161. void desktop_animation_activate(DesktopAnimation* animation) {
  162. furi_assert(animation);
  163. if(animation->state != DesktopAnimationStateBasic) {
  164. return;
  165. }
  166. if(animation->state == DesktopAnimationStateActive) {
  167. return;
  168. }
  169. if(!animation->current->active) {
  170. return;
  171. }
  172. TickType_t now = osKernelGetTickCount();
  173. TickType_t time_since_last_active = now - animation->active_finished_at;
  174. if(time_since_last_active > (animation->current->basic->active_cooldown * 1000)) {
  175. animation->state = DesktopAnimationStateActive;
  176. furi_assert(animation->current->active->duration > 0);
  177. osTimerStart(animation->timer, animation->current->active->duration * 1000);
  178. if(animation->animation_changed_callback) {
  179. animation->animation_changed_callback(animation->animation_changed_callback_context);
  180. }
  181. }
  182. }
  183. static const Icon* desktop_animation_get_current_idle_animation(
  184. DesktopAnimation* animation,
  185. bool* status_bar_background_black) {
  186. const ActiveAnimation* active = animation->current->active;
  187. const BasicAnimation* basic = animation->current->basic;
  188. if(animation->state == DesktopAnimationStateActive && active->icon) {
  189. *status_bar_background_black = active->black_status_bar;
  190. return active->icon;
  191. } else {
  192. *status_bar_background_black = basic->black_status_bar;
  193. return basic->icon;
  194. }
  195. }
  196. // Every time somebody starts 'desktop_animation_get_animation()'
  197. // 1) check if there is a new level
  198. // 2) check if there is SD card corruption
  199. // 3) check if the SD card is empty
  200. // 4) if all false - get idle animation
  201. const Icon* desktop_animation_get_animation(
  202. DesktopAnimation* animation,
  203. bool* status_bar_background_black) {
  204. Dolphin* dolphin = furi_record_open("dolphin");
  205. Storage* storage = furi_record_open("storage");
  206. const Icon* icon = NULL;
  207. furi_assert(animation);
  208. FS_Error sd_status = storage_sd_status(storage);
  209. if(IS_BLOCKING_ANIMATION(animation->state)) {
  210. // don't give new animation till blocked animation
  211. // is reseted
  212. icon = animation->current_blocking_icon;
  213. }
  214. if(!icon) {
  215. if(sd_status == FSE_INTERNAL) {
  216. osTimerStop(animation->timer);
  217. icon = &A_CardBad_128x51;
  218. animation->current_blocking_icon = icon;
  219. animation->state = DesktopAnimationStateSDCorrupted;
  220. animation->sd_shown_error_card_bad = true;
  221. animation->sd_shown_error_db = false;
  222. } else if(sd_status == FSE_NOT_READY) {
  223. animation->sd_shown_error_card_bad = false;
  224. animation->sd_shown_error_db = false;
  225. } else if(sd_status == FSE_OK) {
  226. bool db_exists = storage_common_stat(storage, "/ext/manifest.txt", NULL) == FSE_OK;
  227. if(db_exists && !animation->sd_shown_error_db) {
  228. osTimerStop(animation->timer);
  229. icon = &A_CardNoDB_128x51;
  230. animation->current_blocking_icon = icon;
  231. animation->state = DesktopAnimationStateSDEmpty;
  232. animation->sd_shown_error_db = true;
  233. }
  234. }
  235. }
  236. DolphinStats stats = dolphin_stats(dolphin);
  237. if(!icon && stats.level_up_is_pending) {
  238. osTimerStop(animation->timer);
  239. icon = &A_LevelUpPending_128x51;
  240. animation->current_blocking_icon = icon;
  241. animation->state = DesktopAnimationStateLevelUpIsPending;
  242. }
  243. if(!icon) {
  244. icon =
  245. desktop_animation_get_current_idle_animation(animation, status_bar_background_black);
  246. } else {
  247. status_bar_background_black = false;
  248. }
  249. furi_record_close("storage");
  250. furi_record_close("dolphin");
  251. return icon;
  252. }
  253. DesktopAnimationState desktop_animation_handle_right(DesktopAnimation* animation) {
  254. furi_assert(animation);
  255. bool reset_animation = false;
  256. bool update_animation = false;
  257. switch(animation->state) {
  258. case DesktopAnimationStateActive:
  259. case DesktopAnimationStateBasic:
  260. /* nothing */
  261. break;
  262. case DesktopAnimationStateLevelUpIsPending:
  263. /* do nothing, main scene should change itself */
  264. break;
  265. case DesktopAnimationStateSDCorrupted:
  266. reset_animation = true;
  267. break;
  268. case DesktopAnimationStateSDEmpty:
  269. animation->state = DesktopAnimationStateSDEmptyURL;
  270. animation->current_blocking_icon = &A_CardNoDBUrl_128x51;
  271. update_animation = true;
  272. break;
  273. case DesktopAnimationStateSDEmptyURL:
  274. reset_animation = true;
  275. break;
  276. default:
  277. furi_crash("Unhandled desktop animation state");
  278. }
  279. if(reset_animation) {
  280. desktop_start_new_idle_animation(animation);
  281. update_animation = true;
  282. }
  283. if(update_animation) {
  284. if(animation->animation_changed_callback) {
  285. animation->animation_changed_callback(animation->animation_changed_callback_context);
  286. }
  287. }
  288. return animation->state;
  289. }
  290. #define LEVELUP_FRAME_RATE (0.2)
  291. void desktop_animation_start_oneshot_levelup(DesktopAnimation* animation) {
  292. animation->one_shot_animation_counter = 0;
  293. animation->state = DesktopAnimationStateLevelUpIsPending;
  294. Dolphin* dolphin = furi_record_open("dolphin");
  295. DolphinStats stats = dolphin_stats(dolphin);
  296. furi_record_close("dolphin");
  297. furi_assert(stats.level_up_is_pending);
  298. if(stats.level == 1) {
  299. animation->current_one_shot_icons = animation_level2up;
  300. animation->one_shot_animation_size = COUNT_OF(animation_level2up);
  301. } else if(stats.level == 2) {
  302. animation->current_one_shot_icons = animation_level3up;
  303. animation->one_shot_animation_size = COUNT_OF(animation_level3up);
  304. } else {
  305. furi_crash("Dolphin level is out of bounds");
  306. }
  307. osTimerStart(animation->timer, LEVELUP_FRAME_RATE * 1000);
  308. }
  309. const Icon* desktop_animation_get_oneshot_frame(DesktopAnimation* animation) {
  310. furi_assert(IS_ONESHOT_ANIMATION(animation->state));
  311. furi_assert(animation->one_shot_animation_size > 0);
  312. const Icon* icon = NULL;
  313. if(animation->one_shot_animation_counter < animation->one_shot_animation_size) {
  314. icon = animation->current_one_shot_icons[animation->one_shot_animation_counter];
  315. ++animation->one_shot_animation_counter;
  316. } else {
  317. animation->state = DesktopAnimationStateBasic;
  318. animation->one_shot_animation_size = 0;
  319. osTimerStop(animation->timer);
  320. icon = NULL;
  321. }
  322. return icon;
  323. }