This commit is contained in:
Vitalii 2025-05-16 23:16:48 +03:00
parent ae9789ca4a
commit 95f92f8a3c
Signed by: SymbX
GPG Key ID: FF51F4E4BCE459EE
1 changed files with 25 additions and 13 deletions

View File

@ -37,14 +37,15 @@
const char *RANGE = "Range "; const char *RANGE = "Range ";
// Time defines // Time defines
#define SHORT_PRESS_MS 25 #define SHORT_PRESS_MS 100
#define LONG_PRESS_MS 1000 #define LONG_PRESS_MS 2000
#define LED_FADE_TIME 888 #define LED_FADE_TIME 888
// Countdown timestamp (2^15 max, sign used to notify "unitialized") // Countdown timestamp (2^15 max, sign used to notify "unitialized")
volatile i16 countdown_tick = 0; i16 countdown_tick = -1;
u08 full_tick = 0;
// Light timer // Light timer
volatile i16 timer = 0; i16 timer = 0;
// rx buffer params // rx buffer params
volatile u08 rx_buffer[BUFFER_LEN]; volatile u08 rx_buffer[BUFFER_LEN];
@ -90,6 +91,7 @@ void start_cmd(u08 len) {
*/ */
inline void start_countdown(void) { inline void start_countdown(void) {
countdown_tick = (TIM1.CounterH << 8) | TIM1.CounterL; countdown_tick = (TIM1.CounterH << 8) | TIM1.CounterL;
full_tick = 0;
} }
/** /**
@ -97,6 +99,7 @@ inline void start_countdown(void) {
*/ */
inline void stop_countdown(void) { inline void stop_countdown(void) {
countdown_tick = -1; countdown_tick = -1;
full_tick = 0;
} }
/** /**
@ -113,13 +116,14 @@ i16 elapsed(void) {
if (!is_countdown_active()) { if (!is_countdown_active()) {
return -1; return -1;
} }
u16 next = (TIM1.CounterH << 8) | TIM1.CounterL; i16 next = (TIM1.CounterH << 8) | TIM1.CounterL;
if (next < countdown_tick) { if (next < countdown_tick) {
// Handle case if timer overflowed // Handle case if timer overflowed
return (next + 2500) - countdown_tick; next = (next + 2500) - countdown_tick;
} else { } else {
return next - countdown_tick; next = next - countdown_tick;
} }
return next / 10 + full_tick * 250;
} }
void send_some_command(void) { void send_some_command(void) {
@ -211,6 +215,9 @@ void tick_250ms(void) {
// and skip other tasks if not finished // and skip other tasks if not finished
return; return;
} }
if (is_countdown_active() && full_tick < 255) {
full_tick++;
}
// Decrease light timer // Decrease light timer
if (timer > 0) { if (timer > 0) {
timer--; timer--;
@ -221,6 +228,10 @@ void tick_250ms(void) {
timer = SENSE_ACTIVATION_TIME; timer = SENSE_ACTIVATION_TIME;
} }
} }
if (elapsed() > LONG_PRESS_MS) {
timer = -1;
stop_countdown();
}
// If we still have time // If we still have time
// note: timer can be below zero to work without timer // note: timer can be below zero to work without timer
if (timer != 0) { if (timer != 0) {
@ -342,17 +353,18 @@ interrupt(IRQ_EXTI_C, touch) {
} else if (is_countdown_active()) { } else if (is_countdown_active()) {
// Get time on release // Get time on release
i16 time = elapsed(); i16 time = elapsed();
if (time > LONG_PRESS_MS) { stop_countdown();
if (timer == 0) { if (time > LONG_PRESS_MS && timer == 0) {
// Make light continuously if pressed for long // Make light continuously if pressed for long
timer = -1; timer = -1;
} else if (time > SHORT_PRESS_MS) {
if (timer == 0) {
// If it was short touch set timer for enough time
timer = PRESS_ACTIVATION_TIME; // 30 seconds * 4 (250ms tick)
} else { } else {
// Or turn it off // Or turn it off
timer = 0; timer = 0;
} }
} else if (time > SHORT_PRESS_MS) {
// If it was short touch set timer for enough time
timer = PRESS_ACTIVATION_TIME; // 30 seconds * 4 (250ms tick)
} }
} }
} }