icon.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "icon.h"
  2. #include "icon_i.h"
  3. #include <cmsis_os2.h>
  4. #include <flipper.h>
  5. #include <flipper_v2.h>
  6. Icon* icon_alloc(const IconData* data) {
  7. Icon* icon = furi_alloc(sizeof(Icon));
  8. icon->data = data;
  9. return icon;
  10. }
  11. void icon_free(Icon* icon) {
  12. furi_assert(icon);
  13. free(icon);
  14. }
  15. const uint8_t* icon_get_data(Icon* icon) {
  16. furi_assert(icon);
  17. if(icon->tick) {
  18. uint32_t now = osKernelGetTickCount();
  19. if(now < icon->tick) {
  20. icon->tick = now;
  21. icon_next_frame(icon);
  22. } else if(now - icon->tick > osKernelGetTickFreq() / icon->data->frame_rate) {
  23. icon->tick = now;
  24. icon_next_frame(icon);
  25. }
  26. }
  27. return icon->data->frames[icon->frame];
  28. }
  29. void icon_next_frame(Icon* icon) {
  30. furi_assert(icon);
  31. icon->frame = (icon->frame + 1) % icon->data->frame_count;
  32. }
  33. uint8_t icon_get_width(Icon* icon) {
  34. furi_assert(icon);
  35. return icon->data->width;
  36. }
  37. uint8_t icon_get_height(Icon* icon) {
  38. furi_assert(icon);
  39. return icon->data->height;
  40. }
  41. bool icon_is_animated(Icon* icon) {
  42. furi_assert(icon);
  43. return icon->data->frame_count > 1;
  44. }
  45. void icon_start_animation(Icon* icon) {
  46. furi_assert(icon);
  47. icon->tick = osKernelGetTickCount();
  48. }
  49. void icon_stop_animation(Icon* icon) {
  50. furi_assert(icon);
  51. icon->tick = 0;
  52. icon->frame = 0;
  53. }