Added long press for on/off

This commit is contained in:
nedko 2022-10-23 17:00:02 +03:00
parent 2c6865643e
commit 9312665dfa

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
@ -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();
@ -103,7 +110,7 @@ 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_no_bg, loading, off
};
@ -119,8 +126,7 @@ enum states
STATE_CLOCK_NO1,
STATE_CLOCK_NO2,
STATE_LOADING,
STATE_PC,
STATE_OFF
STATE_PC
};
uint8_t led0_idx = 0;
@ -129,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()
{
@ -188,47 +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_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 = 3;
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);
}
}
}
@ -451,6 +438,55 @@ void init()
sei();
}
void state_switch(uint8_t is_onoff)
{
uint8_t tmp_func_idx;
if (is_onoff == 0)
{
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;