diff options
| -rw-r--r-- | usr/space_light/include/space_light.h | 8 | ||||
| -rw-r--r-- | usr/space_light/src/device.c | 39 | ||||
| -rw-r--r-- | usr/space_light/src/space_light.c | 82 |
3 files changed, 122 insertions, 7 deletions
diff --git a/usr/space_light/include/space_light.h b/usr/space_light/include/space_light.h index b0ca6b8..e5a604a 100644 --- a/usr/space_light/include/space_light.h +++ b/usr/space_light/include/space_light.h @@ -9,6 +9,10 @@ struct RGBCW { uint8_t w; // 0-100 }; -void init_led_thread (void *arg); +void init_led_thread(void *arg); -void setRGBCW(struct RGBCW* rgbcw); +void setRGBCW(struct RGBCW *rgbcw); +void power_save_mode(); + +// 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 32ac281..30bcf9e 100644 --- a/usr/space_light/src/device.c +++ b/usr/space_light/src/device.c @@ -20,6 +20,10 @@ #include "mcu_ps_pub.h" #include "start_type_pub.h" +#include "FreeRTOS.h" +#include "task.h" +#include "timers.h" + #include "space_light.h" #define ATT_DB_LENGTH 5 @@ -69,6 +73,10 @@ bk_attm_desc_t btl_att_db[ATT_DB_LENGTH] = { beken_semaphore_t app_sema = NULL; beken_semaphore_t ext_init_sema = NULL; +TimerHandle_t xOneShotTimer; + +static void oneShotTimerCallback(TimerHandle_t xTimer) { power_save_mode(); } + static void ble_event_callback(ble_event_t event, void *param) { switch (event) { case BLE_STACK_OK: @@ -155,11 +163,32 @@ static void ble_event_callback(ble_event_t event, void *param) { #define CMD_RGBCW 0x1 static void ble_write_callback(write_req_t *write_req) { - struct RGBCW rgbcw = { - write_req->value[1], write_req->value[2], write_req->value[3], - write_req->value[4], write_req->value[5], - }; - setRGBCW(&rgbcw); + switch (write_req->value[0]) { + case 0x1: { + struct RGBCW rgbcw = { + write_req->value[1], write_req->value[2], write_req->value[3], + write_req->value[4], write_req->value[5], + }; + setRGBCW(&rgbcw); + break; + } + case 0x2: + if (write_req->len != 3) { + return; + } + uint16_t seconds = write_req->value[1] << 8 | write_req->value[2]; + xOneShotTimer = xTimerCreate("OneShot", seconds * 1000, pdFALSE, 0, + oneShotTimerCallback); + if ((xOneShotTimer != NULL)) { + xTimerStart(xOneShotTimer, 0); + } + break; + case 0xFF: + setPWM(&write_req->value[1], write_req->len - 1); + break; + default: + break; + } } static unsigned char ble_read_callback(read_req_t *read_req) { diff --git a/usr/space_light/src/space_light.c b/usr/space_light/src/space_light.c index f1a0373..ebf0d26 100644 --- a/usr/space_light/src/space_light.c +++ b/usr/space_light/src/space_light.c @@ -20,6 +20,56 @@ static inline uint16_t get_led_frequency(const uint8_t value, return (uint16_t)pow((float)value / 255.0f, gamma) * max_freq + 0.5; } +// 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 ^^ + + switch (values[0]) { + case 0x01: // initialzie + { + if (len < 5) { + return; + } + uint8_t pwm = values[1]; + uint16_t frequency = values[2] << 8 | values[3]; + uint8_t duty = values[4]; + 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 < 5) { + return; + } + uint8_t pwm = values[1]; + uint16_t frequency = values[2] << 8 | values[3]; + uint8_t duty = values[4]; + bk_pwm_update_param(pwm, frequency, duty); + } + default: + return; + } +} + void setRGBCW(struct RGBCW *rgbcw) { if (rgbcw->r) { bk_pwm_initialize(RED, get_led_frequency(rgbcw->r, 2.8), 10); @@ -76,6 +126,38 @@ void setRGBCW(struct RGBCW *rgbcw) { } } +// FIXME: this is where we want to shut down everything exept ble +void power_save_mode() { + // FIXME: this is as it is stupid and bad but it works this way.. + // TODO: figure out how to just disable pwm without setting defaults first ^^ + // TODO: update pwm also is not working like it should + bk_pwm_stop(RED); + bk_pwm_stop(GREEN); + bk_pwm_stop(BLUE); + bk_pwm_stop(COLD); + bk_pwm_stop(WARM); + + bk_pwm_initialize(RED, 100, 50); + bk_pwm_start(RED); + bk_pwm_initialize(GREEN, 100, 50); + bk_pwm_start(GREEN); + bk_pwm_initialize(BLUE, 100, 50); + bk_pwm_start(BLUE); + // FIXME: laser is still a puzzle to me + bk_pwm_initialize(COLD, 100, 50); + bk_pwm_start(COLD); + bk_pwm_initialize(WARM, 1000, 50); + bk_pwm_start(WARM); + + // FIXME: doing this will start all leds to full color ^^ + // TODO: figgure out how to reverse the level in the configuration of each PWM + // bk_pwm_stop(RED); + // bk_pwm_stop(GREEN); + // bk_pwm_stop(BLUE); + // bk_pwm_stop(COLD); + // bk_pwm_stop(WARM); +} + void init_led_thread(void *arg) { // FIXME: this is as it is stupid and bad but it works this way.. // TODO: figure out how to just disable pwm without setting defaults first ^^ |
