desktop_animation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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(DesktopAnimation* animation) {
  184. const Icon* active_icon = animation->current->active->icon;
  185. const Icon* basic_icon = animation->current->basic->icon;
  186. return (animation->state == DesktopAnimationStateActive && active_icon) ? active_icon :
  187. basic_icon;
  188. }
  189. // Every time somebody starts 'desktop_animation_get_animation()'
  190. // 1) check if there is a new level
  191. // 2) check if there is SD card corruption
  192. // 3) check if the SD card is empty
  193. // 4) if all false - get idle animation
  194. const Icon* desktop_animation_get_animation(DesktopAnimation* animation) {
  195. Dolphin* dolphin = furi_record_open("dolphin");
  196. Storage* storage = furi_record_open("storage");
  197. const Icon* icon = NULL;
  198. furi_assert(animation);
  199. FS_Error sd_status = storage_sd_status(storage);
  200. if(IS_BLOCKING_ANIMATION(animation->state)) {
  201. // don't give new animation till blocked animation
  202. // is reseted
  203. icon = animation->current_blocking_icon;
  204. }
  205. if(!icon) {
  206. if(sd_status == FSE_INTERNAL) {
  207. osTimerStop(animation->timer);
  208. icon = &A_CardBad_128x51;
  209. animation->current_blocking_icon = icon;
  210. animation->state = DesktopAnimationStateSDCorrupted;
  211. animation->sd_shown_error_card_bad = true;
  212. animation->sd_shown_error_db = false;
  213. } else if(sd_status == FSE_NOT_READY) {
  214. animation->sd_shown_error_card_bad = false;
  215. animation->sd_shown_error_db = false;
  216. } else if(sd_status == FSE_OK) {
  217. bool db_exists = storage_common_stat(storage, "/ext/manifest.txt", NULL) == FSE_OK;
  218. if(db_exists && !animation->sd_shown_error_db) {
  219. osTimerStop(animation->timer);
  220. icon = &A_CardNoDB_128x51;
  221. animation->current_blocking_icon = icon;
  222. animation->state = DesktopAnimationStateSDEmpty;
  223. animation->sd_shown_error_db = true;
  224. }
  225. }
  226. }
  227. DolphinStats stats = dolphin_stats(dolphin);
  228. if(!icon && stats.level_up_is_pending) {
  229. osTimerStop(animation->timer);
  230. icon = &A_LevelUpPending_128x51;
  231. animation->current_blocking_icon = icon;
  232. animation->state = DesktopAnimationStateLevelUpIsPending;
  233. }
  234. if(!icon) {
  235. icon = desktop_animation_get_current_idle_animation(animation);
  236. }
  237. furi_record_close("storage");
  238. furi_record_close("dolphin");
  239. return icon;
  240. }
  241. DesktopAnimationState desktop_animation_handle_right(DesktopAnimation* animation) {
  242. furi_assert(animation);
  243. bool reset_animation = false;
  244. bool update_animation = false;
  245. switch(animation->state) {
  246. case DesktopAnimationStateActive:
  247. case DesktopAnimationStateBasic:
  248. /* nothing */
  249. break;
  250. case DesktopAnimationStateLevelUpIsPending:
  251. /* do nothing, main scene should change itself */
  252. break;
  253. case DesktopAnimationStateSDCorrupted:
  254. reset_animation = true;
  255. break;
  256. case DesktopAnimationStateSDEmpty:
  257. animation->state = DesktopAnimationStateSDEmptyURL;
  258. animation->current_blocking_icon = &A_CardNoDBUrl_128x51;
  259. update_animation = true;
  260. break;
  261. case DesktopAnimationStateSDEmptyURL:
  262. reset_animation = true;
  263. break;
  264. default:
  265. furi_crash("Unhandled desktop animation state");
  266. }
  267. if(reset_animation) {
  268. desktop_start_new_idle_animation(animation);
  269. update_animation = true;
  270. }
  271. if(update_animation) {
  272. if(animation->animation_changed_callback) {
  273. animation->animation_changed_callback(animation->animation_changed_callback_context);
  274. }
  275. }
  276. return animation->state;
  277. }
  278. #define LEVELUP_FRAME_RATE (0.2)
  279. void desktop_animation_start_oneshot_levelup(DesktopAnimation* animation) {
  280. animation->one_shot_animation_counter = 0;
  281. animation->state = DesktopAnimationStateLevelUpIsPending;
  282. Dolphin* dolphin = furi_record_open("dolphin");
  283. DolphinStats stats = dolphin_stats(dolphin);
  284. furi_record_close("dolphin");
  285. furi_assert(stats.level_up_is_pending);
  286. if(stats.level == 1) {
  287. animation->current_one_shot_icons = animation_level2up;
  288. animation->one_shot_animation_size = COUNT_OF(animation_level2up);
  289. } else if(stats.level == 2) {
  290. animation->current_one_shot_icons = animation_level3up;
  291. animation->one_shot_animation_size = COUNT_OF(animation_level3up);
  292. } else {
  293. furi_crash("Dolphin level is out of bounds");
  294. }
  295. osTimerStart(animation->timer, LEVELUP_FRAME_RATE * 1000);
  296. }
  297. const Icon* desktop_animation_get_oneshot_frame(DesktopAnimation* animation) {
  298. furi_assert(IS_ONESHOT_ANIMATION(animation->state));
  299. furi_assert(animation->one_shot_animation_size > 0);
  300. const Icon* icon = NULL;
  301. if(animation->one_shot_animation_counter < animation->one_shot_animation_size) {
  302. icon = animation->current_one_shot_icons[animation->one_shot_animation_counter];
  303. ++animation->one_shot_animation_counter;
  304. } else {
  305. animation->state = DesktopAnimationStateBasic;
  306. animation->one_shot_animation_size = 0;
  307. osTimerStop(animation->timer);
  308. icon = NULL;
  309. }
  310. return icon;
  311. }