2021-07-07 08:57:49 +00:00
|
|
|
#include "icon_animation_i.h"
|
|
|
|
#include "icon_i.h"
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
IconAnimation* icon_animation_alloc(const Icon* icon) {
|
|
|
|
furi_assert(icon);
|
|
|
|
IconAnimation* instance = furi_alloc(sizeof(IconAnimation));
|
|
|
|
instance->icon = icon;
|
2021-10-02 17:00:56 +00:00
|
|
|
instance->timer = osTimerNew(icon_animation_timer_callback, osTimerPeriodic, instance, NULL);
|
2021-07-07 08:57:49 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_free(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
2021-10-02 17:00:56 +00:00
|
|
|
furi_check(osTimerDelete(instance->timer) == osOK);
|
2021-07-07 08:57:49 +00:00
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2021-10-02 17:00:56 +00:00
|
|
|
void icon_animation_set_update_callback(
|
|
|
|
IconAnimation* instance,
|
|
|
|
IconAnimationCallback callback,
|
|
|
|
void* context) {
|
2021-07-07 08:57:49 +00:00
|
|
|
furi_assert(instance);
|
2021-10-02 17:00:56 +00:00
|
|
|
instance->callback = callback;
|
|
|
|
instance->callback_context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* icon_animation_get_data(IconAnimation* instance) {
|
2021-07-07 08:57:49 +00:00
|
|
|
return instance->icon->frames[instance->frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_next_frame(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
instance->frame = (instance->frame + 1) % instance->icon->frame_count;
|
|
|
|
}
|
|
|
|
|
2021-10-02 17:00:56 +00:00
|
|
|
void icon_animation_timer_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
IconAnimation* instance = context;
|
|
|
|
|
|
|
|
if(!instance->animating) return;
|
|
|
|
|
|
|
|
icon_animation_next_frame(instance);
|
|
|
|
if(instance->callback) {
|
|
|
|
instance->callback(instance, instance->callback_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 08:57:49 +00:00
|
|
|
uint8_t icon_animation_get_width(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->icon->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t icon_animation_get_height(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->icon->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_start(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
2021-10-02 17:00:56 +00:00
|
|
|
if(!instance->animating) {
|
|
|
|
instance->animating = true;
|
2021-11-24 16:21:12 +00:00
|
|
|
furi_assert(instance->icon->frame_rate);
|
2021-10-02 17:00:56 +00:00
|
|
|
furi_check(
|
2021-10-04 07:01:41 +00:00
|
|
|
xTimerChangePeriod(
|
|
|
|
instance->timer, (osKernelGetTickFreq() / instance->icon->frame_rate), 0) ==
|
|
|
|
pdPASS);
|
2021-10-02 17:00:56 +00:00
|
|
|
}
|
2021-07-07 08:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_stop(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
2021-10-02 17:00:56 +00:00
|
|
|
if(instance->animating) {
|
|
|
|
instance->animating = false;
|
2021-10-04 07:01:41 +00:00
|
|
|
furi_check(xTimerStop(instance->timer, 0) == pdPASS);
|
2021-10-02 17:00:56 +00:00
|
|
|
instance->frame = 0;
|
|
|
|
}
|
2021-07-07 08:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool icon_animation_is_last_frame(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->icon->frame_count - instance->frame <= 1;
|
2021-10-02 17:00:56 +00:00
|
|
|
}
|