modes
This commit is contained in:
50
main.c
50
main.c
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#define MAIN_LOOP_SLEEP 10U // Main loop delay in ms
|
#define MAIN_LOOP_SLEEP 10U // Main loop delay in ms
|
||||||
#define BUTTON_LONG_PRESS_DURATION_MS 1000U // Long press threshold
|
#define BUTTON_LONG_PRESS_DURATION_MS 1000U // Long press threshold
|
||||||
|
#define BUTTON_SHORT_PRESS_DURATION_MS 100U // Short press threshold
|
||||||
#define BUTTON_IGNORE_DURATION_MS 2000U // Time button ignored after long press
|
#define BUTTON_IGNORE_DURATION_MS 2000U // Time button ignored after long press
|
||||||
#define BUTTON_CONFIRMATION_BLINK_LOOPS 10U // Blink animation for confirmation
|
#define BUTTON_CONFIRMATION_BLINK_LOOPS 10U // Blink animation for confirmation
|
||||||
#define BUTTON_CONFIRMATION_BLINK_DURATION_MS 50U
|
#define BUTTON_CONFIRMATION_BLINK_DURATION_MS 50U
|
||||||
@ -27,11 +28,21 @@
|
|||||||
/** Convert milliseconds to system ticks */
|
/** Convert milliseconds to system ticks */
|
||||||
#define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP)
|
#define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP)
|
||||||
|
|
||||||
|
typedef enum _Mode
|
||||||
|
{
|
||||||
|
ANIMATION,
|
||||||
|
STATIC_GLOW,
|
||||||
|
STATIC_FULL,
|
||||||
|
MAX_COUNT,
|
||||||
|
} eMode;
|
||||||
|
|
||||||
/** Global flags */
|
/** Global flags */
|
||||||
volatile bool bLedEnabled = true;
|
volatile bool bLedEnabled = true;
|
||||||
volatile bool bBtnPressed = false;
|
volatile bool bBtnLongPressed = false;
|
||||||
|
volatile eMode eModeCurrent = ANIMATION;
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
|
static inline void toggleMode(void);
|
||||||
void blinkLed(bool resetCounters);
|
void blinkLed(bool resetCounters);
|
||||||
static inline void leds_off(void);
|
static inline void leds_off(void);
|
||||||
static inline void leds_on(void);
|
static inline void leds_on(void);
|
||||||
@ -57,13 +68,14 @@ int main(void)
|
|||||||
battery_level_indicator(); // TODO: Implement
|
battery_level_indicator(); // TODO: Implement
|
||||||
|
|
||||||
bool bLedEnabledOld = bLedEnabled;
|
bool bLedEnabledOld = bLedEnabled;
|
||||||
|
eModeCurrent = ANIMATION;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
handleSwitch(); // Check switch state
|
handleSwitch(); // Check switch state
|
||||||
|
|
||||||
// Light LEDs while button is pressed
|
// Light LEDs while button is pressed
|
||||||
if (bBtnPressed)
|
if (bBtnLongPressed)
|
||||||
{
|
{
|
||||||
leds_on();
|
leds_on();
|
||||||
}
|
}
|
||||||
@ -101,9 +113,22 @@ int main(void)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (bLedEnabled && !bBtnPressed)
|
if (bLedEnabled && !bBtnLongPressed)
|
||||||
{
|
{
|
||||||
blinkLed(false); // run normal blink
|
switch (eModeCurrent)
|
||||||
|
{
|
||||||
|
case ANIMATION:
|
||||||
|
blinkLed(false); // run normal blink
|
||||||
|
break;
|
||||||
|
case STATIC_GLOW:
|
||||||
|
blinkLed(false); // run normal blink
|
||||||
|
break;
|
||||||
|
case STATIC_FULL:
|
||||||
|
blinkLed(false); // run normal blink
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,6 +136,14 @@ int main(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Move to next mode
|
||||||
|
*/
|
||||||
|
static inline void toggleMode(void)
|
||||||
|
{
|
||||||
|
eModeCurrent = (eModeCurrent + 1) % MAX_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Turn off all controlled LEDs (PA1, PA2, PA3)
|
* @brief Turn off all controlled LEDs (PA1, PA2, PA3)
|
||||||
*/
|
*/
|
||||||
@ -150,16 +183,21 @@ static void handleSwitch(void)
|
|||||||
|
|
||||||
if (pressed)
|
if (pressed)
|
||||||
{
|
{
|
||||||
bBtnPressed = true;
|
bBtnLongPressed = true;
|
||||||
if (pressTicks < 0xFFFF)
|
if (pressTicks < 0xFFFF)
|
||||||
pressTicks++; // Prevent overflow
|
pressTicks++; // Prevent overflow
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bBtnPressed = false;
|
bBtnLongPressed = false;
|
||||||
pressTicks = 0;
|
pressTicks = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prevPressed && pressTicks >= MS_TO_TICKS(BUTTON_SHORT_PRESS_DURATION_MS))
|
||||||
|
{
|
||||||
|
toggleMode();
|
||||||
|
}
|
||||||
|
|
||||||
if (prevPressed && pressTicks >= MS_TO_TICKS(BUTTON_LONG_PRESS_DURATION_MS))
|
if (prevPressed && pressTicks >= MS_TO_TICKS(BUTTON_LONG_PRESS_DURATION_MS))
|
||||||
{
|
{
|
||||||
bLedEnabled = !bLedEnabled; // Toggle LED blinking
|
bLedEnabled = !bLedEnabled; // Toggle LED blinking
|
||||||
|
|||||||
Reference in New Issue
Block a user