Compare commits

...

3 Commits

View File

@@ -12,6 +12,10 @@
// Port A7 -> Enable Output
// Port B2 -> Button Input (Pulled-Up)
// Button Press Times
#define BTN_LONG_PRESS_MS 1000
#define BTN_DEBOUNCE_MS 50
// Size of each strip
#define LED0_COUNT 12
#define LED1_COUNT 12
@@ -22,7 +26,7 @@
#define LED0_MS 500
#define LED1_MS 500
#define LED2_MS 500
#define LED3_MS 500
#define LED3_MS 400
// ms offset
#define LED0_MS_OFFSET 0
@@ -87,6 +91,9 @@ void show_leds();
// Hardware Init
void init();
// Function to switch states
void state_switch(uint8_t is_onoff);
#ifdef ENABLE_OLED
// I2C Display Funcs
void init_display();
@@ -99,14 +106,13 @@ void draw_digit(uint8_t pos, uint8_t digit);
// Call function to show a frame and prepare idx for next frame
void sun_and_moon(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led);
void clock_bg(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led);
void clock_no_bg(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led);
void loading(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led);
void off(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led);
void (*func_list[5])(uint8_t, uint8_t*, uint8_t, uint8_t) =
void (*func_list[4])(uint8_t, uint8_t*, uint8_t, uint8_t) =
{
sun_and_moon, clock_bg, clock_no_bg, loading, off
sun_and_moon, clock_no_bg, loading, off
};
void (*send_ptr[4])(uint8_t) =
@@ -117,13 +123,10 @@ void (*send_ptr[4])(uint8_t) =
enum states
{
STATE_SUN_MOON = 0,
STATE_CLOCK_BG1,
STATE_CLOCK_BG2,
STATE_CLOCK_NO1,
STATE_CLOCK_NO2,
STATE_LOADING,
STATE_PC,
STATE_OFF
STATE_PC
};
uint8_t led0_idx = 0;
@@ -132,9 +135,11 @@ uint8_t led2_idx = 0;
uint8_t led3_idx = 0;
uint8_t colour = 0;
uint8_t func_idx = 0;
uint8_t backup_func_idx = sizeof(func_list) / sizeof(func_list[0]) - 1;
uint8_t btn_old_state = 0;
uint8_t colour_state = STATE_SUN_MOON;
uint8_t current_state;
uint64_t btn_down = -1;
int main()
{
@@ -191,58 +196,26 @@ while(1)
current_state = PINB & 0x04;
if (btn_old_state && !current_state)
{
switch (colour_state)
btn_down = ms;
}
// Button Unpress
if (!btn_old_state && current_state)
{
if (ms - BTN_DEBOUNCE_MS >= btn_down)
{
case STATE_SUN_MOON:
func_idx = 1;
colour_state = STATE_CLOCK_BG1;
colour = 0;
break;
case STATE_CLOCK_BG1:
colour_state = STATE_CLOCK_BG2;
colour = 1;
break;
case STATE_CLOCK_BG2:
func_idx = 2;
colour_state = STATE_CLOCK_NO1;
colour = 0;
break;
case STATE_CLOCK_NO1:
colour_state = STATE_CLOCK_NO2;
colour = 1;
break;
case STATE_CLOCK_NO2:
func_idx = 3;
colour_state = STATE_LOADING;
colour = 0;
break;
case STATE_LOADING:
func_idx = 4;
PORTA ^= 0x20;
colour_state = STATE_PC;
break;
case STATE_PC:
PORTA ^= 0x20;
colour_state = STATE_OFF;
break;
case STATE_OFF:
func_idx = 0;
colour_state = STATE_SUN_MOON;
break;
btn_down = -1;
state_switch(0);
}
led0_idx = 0;
led1_idx = 0;
led2_idx = 0;
led3_idx = 0;
}
btn_old_state = current_state;
// Long Press Time Has Passed
if (ms - BTN_LONG_PRESS_MS >= btn_down)
{
btn_down = -1;
state_switch(1);
}
}
}
@@ -465,6 +438,61 @@ void init()
sei();
}
void state_switch(uint8_t is_onoff)
{
uint8_t tmp_func_idx;
if (is_onoff == 0)
{
// If we're off don't change anything
if (func_idx == sizeof(func_list) / sizeof(func_list[0]) - 1)
{
return;
}
switch (colour_state)
{
case STATE_SUN_MOON:
func_idx = 1;
colour_state = STATE_CLOCK_NO1;
colour = 0;
break;
case STATE_CLOCK_NO1:
colour_state = STATE_CLOCK_NO2;
colour = 1;
break;
case STATE_CLOCK_NO2:
func_idx = 2;
colour_state = STATE_LOADING;
colour = 0;
break;
case STATE_LOADING:
func_idx = 0;
PORTA ^= 0x20;
colour_state = STATE_PC;
break;
case STATE_PC:
PORTA ^= 0x20;
colour_state = STATE_SUN_MOON;
break;
}
}
else
{
tmp_func_idx = backup_func_idx;
backup_func_idx = func_idx;
func_idx = tmp_func_idx;
}
led0_idx = 0;
led1_idx = 0;
led2_idx = 0;
led3_idx = 0;
}
void sun_and_moon(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led)
{
uint8_t i;
@@ -513,45 +541,6 @@ void sun_and_moon(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led)
(*idx) %= count;
}
void clock_bg(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led)
{
uint8_t i;
uint8_t colour1[3] = {0xFF, 0x80, 0x00};
uint8_t colour2[3] = {0x00, 0x80, 0xFF};
if (colour == 0)
{
for (i = 0; i < count; ++i)
{
if (i == *idx)
{
send_pixel(colour1[0], colour1[1], colour1[2], led);
}
else
{
send_pixel(colour2[0], colour2[1], colour2[2], led);
}
}
}
else
{
for (i = 0; i < count; ++i)
{
if (i == *idx)
{
send_pixel(colour2[0], colour2[1], colour2[2], led);
}
else
{
send_pixel(colour1[0], colour1[1], colour1[2], led);
}
}
}
(*idx)++;
(*idx) %= count;
}
void clock_no_bg(uint8_t count, uint8_t* idx, uint8_t colour, uint8_t led)
{
uint8_t i;