Compare commits
3 Commits
feature/ba
...
9213c814d6
| Author | SHA256 | Date | |
|---|---|---|---|
| 9213c814d6 | |||
| a9f342b45b | |||
| 74a0780219 |
42
main.c
42
main.c
@ -1,7 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @file main.c
|
* @file main.c
|
||||||
* @brief Bike rear light implementation for ATTINY202.
|
* @brief Bike rear light implementation for ATTINY202 with low-power standby.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -18,14 +17,13 @@
|
|||||||
#define PA6_SET_MASK 0x40 ///< Green LED pin
|
#define PA6_SET_MASK 0x40 ///< Green LED pin
|
||||||
#define PA7_SET_MASK 0x80 ///< Red LED
|
#define PA7_SET_MASK 0x80 ///< Red LED
|
||||||
|
|
||||||
#define MAIN_LOOP_SLEEP 10U // Main loop delay in ms
|
#define MAIN_LOOP_SLEEP 50U // Loop period 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 50U // Short press threshold
|
#define BUTTON_SHORT_PRESS_DURATION_MS 50U // 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
|
||||||
|
|
||||||
/** 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
|
typedef enum _Mode
|
||||||
@ -55,9 +53,25 @@ void ledAnimationBlink(bool resetCounters);
|
|||||||
void ledAnimationGlow(void);
|
void ledAnimationGlow(void);
|
||||||
void ledStaticFull(void);
|
void ledStaticFull(void);
|
||||||
|
|
||||||
/**
|
/* --- Timer init: Use RTC PIT for periodic wake-up --- */
|
||||||
* @brief Main entry point
|
void init_timer(void)
|
||||||
*/
|
{
|
||||||
|
RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; // 1 kHz ULP clock for RTC
|
||||||
|
while (RTC.STATUS > 0)
|
||||||
|
{
|
||||||
|
} // Wait for sync
|
||||||
|
|
||||||
|
RTC.PITINTCTRL = RTC_PI_bm; // Enable PIT interrupt
|
||||||
|
RTC.PITCTRLA = RTC_PERIOD_CYC64_gc // ≈64 ms wake-up (~50 ms)
|
||||||
|
| RTC_PITEN_bm; // Enable PIT
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(RTC_PIT_vect)
|
||||||
|
{
|
||||||
|
RTC.PITINTFLAGS = RTC_PI_bm; // Clear interrupt flag
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- MAIN --- */
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Configure LED pins as outputs
|
// Configure LED pins as outputs
|
||||||
@ -74,7 +88,13 @@ int main(void)
|
|||||||
bool bLedEnabledOld = bLedEnabled;
|
bool bLedEnabledOld = bLedEnabled;
|
||||||
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
|
eModeCurrent = ANIMATION_BLINK; // Set the mode to start with
|
||||||
|
|
||||||
while (true)
|
cli();
|
||||||
|
init_timer();
|
||||||
|
sei();
|
||||||
|
|
||||||
|
set_sleep_mode(SLEEP_MODE_STANDBY);
|
||||||
|
|
||||||
|
while (1)
|
||||||
{
|
{
|
||||||
bBtnPressed = handleSwitch(); // Check switch state
|
bBtnPressed = handleSwitch(); // Check switch state
|
||||||
|
|
||||||
@ -136,7 +156,9 @@ int main(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_delay_ms(MAIN_LOOP_SLEEP);
|
sleep_enable();
|
||||||
|
sleep_cpu(); // Sleep until PIT wakes
|
||||||
|
sleep_disable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -399,7 +421,7 @@ void ledAnimationBlink(bool resetCounters)
|
|||||||
if (counter >= T250)
|
if (counter >= T250)
|
||||||
{
|
{
|
||||||
counter = 0;
|
counter = 0;
|
||||||
state = 0; // restart normal sequence
|
state = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user