From 95f92f8a3c0d2c34e4af52adabb0cc4a0dee934f Mon Sep 17 00:00:00 2001 From: Vitalii Popov Date: Fri, 16 May 2025 23:16:48 +0300 Subject: [PATCH] fix bugs --- src/main.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 1ec3e4c..8ff24ae 100644 --- a/src/main.c +++ b/src/main.c @@ -37,14 +37,15 @@ const char *RANGE = "Range "; // Time defines -#define SHORT_PRESS_MS 25 -#define LONG_PRESS_MS 1000 +#define SHORT_PRESS_MS 100 +#define LONG_PRESS_MS 2000 #define LED_FADE_TIME 888 // 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 -volatile i16 timer = 0; +i16 timer = 0; // rx buffer params volatile u08 rx_buffer[BUFFER_LEN]; @@ -90,6 +91,7 @@ void start_cmd(u08 len) { */ inline void start_countdown(void) { countdown_tick = (TIM1.CounterH << 8) | TIM1.CounterL; + full_tick = 0; } /** @@ -97,6 +99,7 @@ inline void start_countdown(void) { */ inline void stop_countdown(void) { countdown_tick = -1; + full_tick = 0; } /** @@ -113,13 +116,14 @@ i16 elapsed(void) { if (!is_countdown_active()) { return -1; } - u16 next = (TIM1.CounterH << 8) | TIM1.CounterL; + i16 next = (TIM1.CounterH << 8) | TIM1.CounterL; if (next < countdown_tick) { // Handle case if timer overflowed - return (next + 2500) - countdown_tick; + next = (next + 2500) - countdown_tick; } else { - return next - countdown_tick; + next = next - countdown_tick; } + return next / 10 + full_tick * 250; } void send_some_command(void) { @@ -211,6 +215,9 @@ void tick_250ms(void) { // and skip other tasks if not finished return; } + if (is_countdown_active() && full_tick < 255) { + full_tick++; + } // Decrease light timer if (timer > 0) { timer--; @@ -221,6 +228,10 @@ void tick_250ms(void) { timer = SENSE_ACTIVATION_TIME; } } + if (elapsed() > LONG_PRESS_MS) { + timer = -1; + stop_countdown(); + } // If we still have time // note: timer can be below zero to work without timer if (timer != 0) { @@ -342,17 +353,18 @@ interrupt(IRQ_EXTI_C, touch) { } else if (is_countdown_active()) { // Get time on release i16 time = elapsed(); - if (time > LONG_PRESS_MS) { + stop_countdown(); + if (time > LONG_PRESS_MS && timer == 0) { + // Make light continuously if pressed for long + timer = -1; + } else if (time > SHORT_PRESS_MS) { if (timer == 0) { - // Make light continuously if pressed for long - timer = -1; + // If it was short touch set timer for enough time + timer = PRESS_ACTIVATION_TIME; // 30 seconds * 4 (250ms tick) } else { // Or turn it off 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) } } }