138 lines
3.0 KiB
C
138 lines
3.0 KiB
C
/*
|
|
* ledprog.c: led programs
|
|
*/
|
|
|
|
#include "matrix.h"
|
|
#include "misc/tinymt.h"
|
|
|
|
|
|
static uint32_t s[8] = {0};
|
|
//static uint32_t t[8] = {0};
|
|
//static uint32_t l[8] = {0};
|
|
|
|
|
|
|
|
void lp_ribbon_init()
|
|
{
|
|
uint8_t i;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
s[i] = 0;
|
|
}
|
|
}
|
|
|
|
void lp_ribbon_upward(uint16_t wait, uint16_t rate, uint8_t fade)
|
|
{
|
|
uint32_t i;
|
|
uint32_t x;
|
|
|
|
/*
|
|
0: state
|
|
1: timeout
|
|
2: index
|
|
*/
|
|
|
|
if (fade > 0x80)
|
|
fade = 0x80;
|
|
|
|
// reduce 1-frame bright blip
|
|
for (i = 0; i < sizeof(led_set.ribbon); i++) {
|
|
if (led_set.ribbon[i] >= 0xd0) {
|
|
led_set.ribbon[i] = 0xd0;
|
|
}
|
|
}
|
|
|
|
switch (s[0]) {
|
|
case 0: { // timeout
|
|
if (!s[1]) {
|
|
s[0]++;
|
|
s[2] = 0;
|
|
return;
|
|
}
|
|
|
|
s[1]--;
|
|
|
|
break;
|
|
}
|
|
case 1: { // upward trails
|
|
// rate delay
|
|
if (!s[1]) {
|
|
s[1] = rate;
|
|
} else {
|
|
s[1]--;
|
|
break;
|
|
}
|
|
|
|
x = s[2]++;
|
|
|
|
// are we done?
|
|
if (x >= sizeof(led_set.ribbon)) {
|
|
s[0] = 0;
|
|
s[1] = wait;
|
|
break;
|
|
}
|
|
|
|
// fade in and up
|
|
led_set.ribbon[x] = 0x20;
|
|
if (x) led_set.ribbon[x - 1] = 0x66;
|
|
if (x > 1) led_set.ribbon[x - 2] = 0xef;
|
|
}
|
|
}
|
|
|
|
// fade out LEDs
|
|
for (i = 0; i < sizeof(led_set.ribbon); i++) {
|
|
if (led_set.ribbon[i] >= 0x70) {
|
|
led_set.ribbon[i] -= fade;
|
|
}
|
|
if (led_set.ribbon[i] >= fade) {
|
|
led_set.ribbon[i] -= fade;
|
|
} else {
|
|
// led_set.ribbon[i] >>= 1;
|
|
if (led_set.ribbon[i])
|
|
led_set.ribbon[i]--;
|
|
}
|
|
}
|
|
|
|
matrix_flag_update();
|
|
}
|
|
|
|
void lp_ribbon_twinkle(uint8_t idle, uint8_t max, uint8_t thresh_lo, uint8_t thresh_hi)
|
|
{
|
|
/*
|
|
idle: attempted idle target. ideally an odd number not a power of two.
|
|
max: maximum brightness. anything higher is clamped to this
|
|
thresh_lo: rand must be lower than this to make any adjustment this cycle
|
|
thresh_hi: rand must be this or higher to force a bright twinkle
|
|
*/
|
|
|
|
uint32_t i;
|
|
uint32_t w, x;
|
|
|
|
s[1]++;
|
|
|
|
for (i = 0; i < sizeof(led_set.ribbon); i++) {
|
|
x = prng_get8();
|
|
|
|
if (x < thresh_lo) {
|
|
w = (idle > 3) ? 3 : idle;
|
|
|
|
// swing around idle level
|
|
if (led_set.ribbon[i] > idle) {
|
|
led_set.ribbon[i] >>= 1;
|
|
} else {
|
|
led_set.ribbon[i] += (w - 1);
|
|
}
|
|
}
|
|
|
|
// only make bright stars every 4 frames
|
|
if ((x >= thresh_hi) && ((s[1] & 3) == 0)) {
|
|
w = prng_get8();
|
|
led_set.ribbon[i] += w;
|
|
if (led_set.ribbon[i] > max) {
|
|
led_set.ribbon[i] = max;
|
|
}
|
|
}
|
|
}
|
|
|
|
matrix_flag_update();
|
|
} |