1 Commits

Author SHA1 Message Date
hueso
fc4b72b07f LED animation @ TMR0 ISR
Some checks failed
Badgemagic Firmware build / build (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
2026-02-11 00:12:36 -03:00
6 changed files with 87 additions and 95 deletions

View File

@@ -485,7 +485,7 @@ void ani_marque(bm_t *bm, uint16_t *fb, int step)
}
void ani_flash(volatile bm_t *bm, uint16_t *fb, int step)
void ani_flash(bm_t *bm, uint16_t *fb, int step)
{
if (!(step % 2))
fb_fill(fb, 0);

View File

@@ -13,22 +13,22 @@ int ani_xbm_scrollup_inf(xbm_t *xbm, uint16_t *fb,
int vh, int col, int row);
void fb_fill(uint16_t *fb, uint16_t v);
void ani_shift_y(volatile bm_t *bm, uint16_t *fb, int dir, int frame);
void ani_shift_y(bm_t *bm, uint16_t *fb, int dir, int frame);
void ani_scroll_x(volatile bm_t *bm, uint16_t *fb, int dir);
void ani_scroll_y(volatile bm_t *bm, uint16_t *fb);
void ani_scroll_x(bm_t *bm, uint16_t *fb, int dir);
void ani_scroll_y(bm_t *bm, uint16_t *fb);
int ani_scroll_left(volatile bm_t *bm, uint16_t *fb);
int ani_scroll_right(volatile bm_t *bm, uint16_t *fb);
int ani_scroll_up(volatile bm_t *bm, uint16_t *fb);
int ani_scroll_down(volatile bm_t *bm, uint16_t *fb);
int ani_fixed(volatile bm_t *bm, uint16_t *fb);
int ani_laser(volatile bm_t *bm, uint16_t *fb);
int ani_snowflake(volatile bm_t *bm, uint16_t *fb);
int ani_animation(volatile bm_t *bm, uint16_t *fb);
int ani_picture(volatile bm_t *bm, uint16_t *fb);
int ani_scroll_left(bm_t *bm, uint16_t *fb);
int ani_scroll_right(bm_t *bm, uint16_t *fb);
int ani_scroll_up(bm_t *bm, uint16_t *fb);
int ani_scroll_down(bm_t *bm, uint16_t *fb);
int ani_fixed(bm_t *bm, uint16_t *fb);
int ani_laser(bm_t *bm, uint16_t *fb);
int ani_snowflake(bm_t *bm, uint16_t *fb);
int ani_animation(bm_t *bm, uint16_t *fb);
int ani_picture(bm_t *bm, uint16_t *fb);
void ani_marque(volatile bm_t *bm, uint16_t *fb, int step);
void ani_flash(volatile bm_t *bm, uint16_t *fb, int step);
void ani_marque(bm_t *bm, uint16_t *fb, int step);
void ani_flash(bm_t *bm, uint16_t *fb, int step);
#endif /* __ANIMATION_H__ */

View File

@@ -1,9 +1,9 @@
#include "bmlist.h"
#include <memory.h>
volatile static bm_t *current, *head, *tail;
static bm_t *current, *head, *tail;
static void bm_add(bm_t *new, volatile bm_t *prev, volatile bm_t *next)
static void bm_add(bm_t *new, bm_t *prev, bm_t *next)
{
next->prev = new;
new->next = next;
@@ -11,7 +11,7 @@ static void bm_add(bm_t *new, volatile bm_t *prev, volatile bm_t *next)
prev->next = new;
}
bm_t *bmlist_insert(volatile bm_t *at, bm_t *new)
bm_t *bmlist_insert(bm_t *at, bm_t *new)
{
bm_add(new, at, at->next);
return new;
@@ -24,52 +24,52 @@ bm_t *bmlist_append(bm_t *new)
return new;
}
volatile bm_t *bmlist_gonext()
bm_t *bmlist_gonext()
{
current = current->next;
current->anim_step = 0;
return current;
}
volatile bm_t *bmlist_goprev()
bm_t *bmlist_goprev()
{
current = current->prev;
current->anim_step = 0;
return current;
}
volatile bm_t *bmlist_gohead()
bm_t *bmlist_gohead()
{
current = head;
current->anim_step = 0;
return current;
}
volatile bm_t *bmlist_head()
bm_t *bmlist_head()
{
return head;
}
volatile bm_t *bmlist_current()
bm_t *bmlist_current()
{
return current;
}
static void list_del(volatile bm_t *prev, volatile bm_t *next)
static void list_del(bm_t *prev, bm_t *next)
{
prev->next = next;
next->prev = prev;
}
volatile bm_t *bmlist_drop(volatile bm_t *bm)
bm_t *bmlist_drop(bm_t *bm)
{
volatile bm_t *next = bm->next;
bm_t *next = bm->next;
list_del(bm->prev, bm->next);
if (bm == head)
head = bm->next;
if (bm == tail)
tail = bm->prev;
free((bm_t *)bm);
free(bm);
return next;
}

View File

@@ -16,8 +16,8 @@ typedef struct bm_st {
uint32_t timeout; // zero mean no timeout
uint32_t anim_step; // Animation step, zero means restart animation
volatile struct bm_st *next;
volatile struct bm_st *prev;
struct bm_st *next;
struct bm_st *prev;
} bm_t;
bm_t *bm_new(uint16_t width);
@@ -27,16 +27,16 @@ static inline void bm_free(bm_t *bm)
free(bm);
}
bm_t *bmlist_insert(volatile bm_t *at, bm_t *new);
bm_t *bmlist_insert(bm_t *at, bm_t *new);
bm_t *bmlist_append(bm_t *new);
volatile bm_t *bmlist_drop(volatile bm_t *bm);
bm_t *bmlist_drop(bm_t *bm);
volatile bm_t *bmlist_gonext();
volatile bm_t *bmlist_goprev() ;
volatile bm_t *bmlist_gohead();
volatile bm_t *bmlist_current();
bm_t *bmlist_gonext();
bm_t *bmlist_goprev() ;
bm_t *bmlist_gohead();
bm_t *bmlist_current();
volatile bm_t *bmlist_head();
bm_t *bmlist_head();
void bmlist_init(uint16_t first_bm_width);

View File

@@ -105,7 +105,7 @@ void load_bmlist()
if (memcmp(header.header, "wang", 5))
return; // There is no bitmap stored in flash
volatile bm_t *curr_bm = bmlist_current();
bm_t *curr_bm = bmlist_current();
for (int i=0; i<8; i++) {
bm_t *bm = flash2newbm(i);
@@ -118,9 +118,10 @@ void load_bmlist()
bmlist_drop(curr_bm);
}
static int marque_step, flash_step;
static uint16_t common_tasks(tmosTaskID task_id, uint16_t events)
{
static int marque_step, flash_step;
if(events & SYS_EVENT_MSG) {
uint8 *pMsg = tmos_msg_receive(common_taskid);
@@ -131,47 +132,8 @@ static uint16_t common_tasks(tmosTaskID task_id, uint16_t events)
return (events ^ SYS_EVENT_MSG);
}
if(events & ANI_NEXT_STEP) {
static int (*animations[])(volatile bm_t *bm, uint16_t *fb) = {
ani_scroll_left,
ani_scroll_right,
ani_scroll_up,
ani_scroll_down,
ani_fixed,
ani_animation,
ani_snowflake,
ani_picture,
ani_laser
};
volatile bm_t *bm = bmlist_current();
if (animations[LEGACY_GET_ANIMATION(bm->modes)])
if (animations[LEGACY_GET_ANIMATION(bm->modes)](bm, fb) == 0
&& is_play_sequentially) {
bmlist_gonext();
}
if (bm->is_flash) {
ani_flash(bm, fb, flash_step);
}
if (bm->is_marquee) {
ani_marque(bm, fb, marque_step);
}
uint32_t t = ANI_SPEED_STRATEGY(LEGACY_GET_SPEED(bm->modes));
tmos_start_task(common_taskid, ANI_NEXT_STEP, t / 625);
return events ^ ANI_NEXT_STEP;
}
if (events & ANI_MARQUE) {
volatile bm_t *bm = bmlist_current();
marque_step++;
if (bm->is_marquee) {
ani_marque(bm, fb, marque_step);
}
return events ^ ANI_MARQUE;
}
@@ -186,19 +148,7 @@ static uint16_t common_tasks(tmosTaskID task_id, uint16_t events)
}
if (events & ANI_FLASH) {
volatile bm_t *bm = bmlist_current();
flash_step++;
if (bm->is_flash) {
ani_flash(bm, fb, flash_step);
}
/* After flash is applied, it will potentialy overwrite the marque
effect after it just wrote, results in flickering. So here apply the
marque effect again */
if (bm->is_marquee) {
ani_marque(bm, fb, marque_step);
}
return events ^ ANI_FLASH;
}
@@ -236,7 +186,6 @@ static void spawn_tasks()
tmos_start_reload_task(common_taskid, ANI_FLASH, ANI_FLASH_SPEED_T / 625);
tmos_start_reload_task(common_taskid, SCAN_BOOTLD_BTN,
SCAN_BOOTLD_BTN_SPEED_T / 625);
tmos_start_task(common_taskid, ANI_NEXT_STEP, 500000 / 625);
}
static void start_ble_animation()
@@ -253,7 +202,6 @@ static void start_normal_animation()
{
tmos_start_reload_task(common_taskid, ANI_MARQUE, ANI_MARQUE_SPEED_T / 625);
tmos_start_reload_task(common_taskid, ANI_FLASH, ANI_FLASH_SPEED_T / 625);
tmos_start_task(common_taskid, ANI_NEXT_STEP, 500000 / 625);
tmos_stop_task(common_taskid, BLE_NEXT_STEP);
}
@@ -381,7 +329,7 @@ static void mode_setup_download()
void clean_bmlist()
{
volatile bm_t *curr_bm = bmlist_current();
bm_t *curr_bm = bmlist_current();
while (curr_bm->next != curr_bm)
bmlist_drop(curr_bm->next);
}
@@ -408,6 +356,7 @@ void handle_after_rx()
}
}
int main()
{
SetSysClock(CLK_SOURCE_PLL_60MHz);
@@ -455,6 +404,10 @@ int main()
}
}
// Animation state for ISR-based updates
static uint32_t led_frame_counter = 0;
// TMR0 ISR - LED refresh + animation update
// Animation updates done in ISR to avoid BLE/TMOS interference
__INTERRUPT
__HIGH_CODE
void TMR0_IRQHandler(void)
@@ -471,9 +424,48 @@ void TMR0_IRQHandler(void)
i = 0;
led_write2dcol(i >> 2, fb[i >> 1], fb[(i >> 1) + 1]);
}
else if (state > (badge_cfg.led_brightness&3))
else if (state >= (badge_cfg.led_brightness&3))
leds_releaseall();
if (mode == NORMAL) {
// Animation update using configurable speed
bm_t *bm = bmlist_current();
uint32_t speed_us = ANI_SPEED_STRATEGY(LEGACY_GET_SPEED(bm->modes));
uint32_t anim_ticks = speed_us >> 8;
led_frame_counter++;
if (led_frame_counter >= anim_ticks) {
led_frame_counter = 0;
static int (*animations[])(bm_t *bm, uint16_t *fb) = {
ani_scroll_left,
ani_scroll_right,
ani_scroll_up,
ani_scroll_down,
ani_fixed,
ani_animation,
ani_snowflake,
ani_picture,
ani_laser
};
if (animations[LEGACY_GET_ANIMATION(bm->modes)])
if (animations[LEGACY_GET_ANIMATION(bm->modes)](bm, fb) == 0
&& is_play_sequentially) {
bmlist_gonext();
}
if (bm->is_flash) {
ani_flash(bm, fb, flash_step);
}
if (bm->is_marquee) {
ani_marque(bm, fb, marque_step);
}
}
}
TMR0_ClearITFlag(TMR0_3_IT_CYC_END);
}
}

View File

@@ -57,8 +57,8 @@ static uint16_t serial_number[] = {
#ifdef USBC_VERSION
4 +
#endif
((12 + sizeof(VERSION_ABBR) - 1) * 2 | /* bLength */
0x03 << 8), /* bDescriptorType */
(12 + sizeof(VERSION_ABBR) - 1) * 2 | /* bLength */
0x03 << 8, /* bDescriptorType */
/* bString */
'B', 'M', '1', '1', '4', '4',