From e94d439286b867a658468d2326b022fad3478ec0 Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Sun, 27 Jul 2025 15:07:24 +0200 Subject: space_light: fix timer --- usr/space_light/include/space_light.h | 2 +- usr/space_light/src/device.c | 5 +-- usr/space_light/src/space_light.c | 79 ++++++++--------------------------- 3 files changed, 20 insertions(+), 66 deletions(-) (limited to 'usr/space_light') diff --git a/usr/space_light/include/space_light.h b/usr/space_light/include/space_light.h index b5d1f9f..9c30f46 100644 --- a/usr/space_light/include/space_light.h +++ b/usr/space_light/include/space_light.h @@ -13,7 +13,7 @@ void init_led_thread(void *arg); void set_rgbcw(struct RGBCW *rgbcw); -void set_timer(uint16_t seconds); +void set_timer(uint32_t seconds); // WILL BE REMOVED just for testing void setPWM(uint8_t *values, size_t len); diff --git a/usr/space_light/src/device.c b/usr/space_light/src/device.c index 8b87879..7aa5caf 100644 --- a/usr/space_light/src/device.c +++ b/usr/space_light/src/device.c @@ -197,10 +197,7 @@ static void ble_write_callback(write_req_t *write_req) { return; } uint16_t seconds = write_req->value[1] << 8 | write_req->value[2]; - set_timer(seconds); - break; - case 0xFF: - setPWM(&write_req->value[1], write_req->len - 1); + set_timer((uint32_t)seconds); break; default: break; diff --git a/usr/space_light/src/space_light.c b/usr/space_light/src/space_light.c index 82e4e1f..85c5243 100644 --- a/usr/space_light/src/space_light.c +++ b/usr/space_light/src/space_light.c @@ -195,73 +195,30 @@ void set_rgbcw(struct RGBCW *rgbcw) { bk_pwm_update_param(WARM, pwm_period, m_period); } -static void oneShotTimerCallback(TimerHandle_t xTimer) { power_save_mode(); } +static void oneShotTimerCallback(TimerHandle_t xTimer) { + bk_printf("TIMER CALLBACK called\r\n"); + power_save_mode(); +} -void set_timer(uint16_t seconds) { - // FIXME: restructure the code - if (xOneShotTimer == NULL && seconds == 0) { - return; - } - if (seconds == 0) { - xTimerStop(xOneShotTimer, 0); +void set_timer(uint32_t seconds) { + bk_printf("seconds %u\r\n", seconds); + if (xOneShotTimer != NULL) { + bk_printf("Delete timer because we want to reconfigure it\r\n"); + xTimerDelete(xOneShotTimer, 0); + xOneShotTimer = NULL; } - if (xOneShotTimer == NULL) { - xOneShotTimer = xTimerCreate("OneShot", seconds * 1000, pdFALSE, 0, - oneShotTimerCallback); - } - if ((xOneShotTimer != NULL)) { - xTimerStart(xOneShotTimer, 0); + if (seconds == 0) { + return; } -} -// Complete hacky command which will be removed shortly -// this is just to shorten the developemnt cycle -void setPWM(uint8_t *values, size_t len) { - // at least try something ^^ + uint32_t ticks = pdMS_TO_TICKS(seconds * 1000); + bk_printf("timer to ticks: %u\r\n", ticks); + xOneShotTimer = + xTimerCreate("OneShotTimer", ticks, pdFALSE, 0, oneShotTimerCallback); - switch (values[0]) { - case 0x01: // initialzie - { - if (len < 6) { - return; - } - uint8_t pwm = values[1]; - uint16_t frequency = values[2] << 8 | values[3]; - uint16_t duty = values[4] << 8 | values[5]; - bk_pwm_initialize(pwm, frequency, duty); - break; - } - case 0x02: // start - { - if (len < 2) { - return; - } - uint8_t pwm = values[1]; - bk_pwm_start(pwm); - break; - } - case 0x03: // stop - { - if (len < 2) { - return; - } - uint8_t pwm = values[1]; - bk_pwm_stop(pwm); - break; - } - case 0x04: // update param - { - if (len < 6) { - return; - } - uint8_t pwm = values[1]; - uint16_t frequency = values[2] << 8 | values[3]; - uint16_t duty = values[4] << 8 | values[5]; - bk_pwm_update_param(pwm, frequency, duty); - } - default: - return; + if (xOneShotTimer != NULL) { + xTimerStart(xOneShotTimer, 0); } } -- cgit v1.2.3