desktop_animation.c 13 KB

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