diff --git a/src/main.c b/src/main.c index 377e14e..386f66f 100644 --- a/src/main.c +++ b/src/main.c @@ -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[])(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 - }; - - 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) { - 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) { - 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); } @@ -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); } }