diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 8814692a62a06edce3b9db0cf58c6090ba78c86a..65a29744e752e4bf4f50f507cdae11f60df1d4d2 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -100,6 +100,7 @@ typedef _Bool bool; #define API_LEDS_CLEAR_ALL 0x6d #define API_LEDS_GET_ROCKET 0x6e #define API_LEDS_GET 0x6f +#define API_LEDS_FLASH_ROCKET 0x72 #define API_VIBRA_SET 0x70 #define API_VIBRA_VIBRATE 0x71 @@ -622,6 +623,19 @@ API(API_LEDS_SET, void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b)); */ API(API_LEDS_GET, int epic_leds_get_rgb(int led, uint8_t * rgb)); +/** + * Set one of the rockets to flash for a certain time. + * + * :c:func:`epic_leds_flash_rocket` will set a timer for the flash of a rocket. + * + * :param int led: Number of the rocket that sould flash + * :param uint8_t value: brightness of the 'on'-state of this rocket ( 0 < value < 32) + * :param int millis: time in milliseconds defining the duration of the flash (i.e. how long is the rocket 'on') + * + * .. versionadded:: 1.16 + */ +API(API_LEDS_FLASH_ROCKET, void epic_leds_flash_rocket(int led, uint8_t valiue, int millis)); + /** * Set one of card10's RGB LEDs to a certain color in HSV format. * diff --git a/epicardium/modules/leds.c b/epicardium/modules/leds.c index ffe5969e103c1e736d502a2c79d8933fc5e5cf6b..64cc8fea5ddf55f20174b15b86dd3f1e9e98962a 100644 --- a/epicardium/modules/leds.c +++ b/epicardium/modules/leds.c @@ -1,6 +1,7 @@ #include "leds.h" #include "pmic.h" #include "FreeRTOS.h" +#include "timers.h" #include "task.h" #include "epicardium.h" #include "modules.h" @@ -128,6 +129,33 @@ int epic_leds_get_rocket(int led) return ret; } +static StaticTimer_t flash_timer_data[3]; +static TimerHandle_t flash_timer[] = { NULL, NULL, NULL }; + +static void rocket_timer_callback(TimerHandle_t flash_timer) +{ + uint32_t id = (uint32_t)pvTimerGetTimerID(flash_timer); + epic_leds_set_rocket(id, 0); +} + +void epic_leds_flash_rocket(int led, uint8_t value, int millis) +{ + int ticks = millis * (configTICK_RATE_HZ / 1000); + int32_t id = led; + if (flash_timer[id] == NULL) { + flash_timer[id] = xTimerCreateStatic( + "flashtimer", + ticks, + pdFALSE, + (void *)id, + rocket_timer_callback, + &flash_timer_data[id] + ); + epic_leds_set_rocket(led, value); + } + epic_leds_set_rocket(led, value); + xTimerChangePeriod(flash_timer[id], ticks, 0); +} void epic_set_flashlight(bool power) { hwlock_acquire(HWLOCK_I2C); diff --git a/pycardium/modules/py/leds.py b/pycardium/modules/py/leds.py index 75f3aac87dbb362f16a8dc1c1f993c13f22ac158..db09834cc63a42c5885bb2dd00dc0d0e1544a3fb 100644 --- a/pycardium/modules/py/leds.py +++ b/pycardium/modules/py/leds.py @@ -95,6 +95,20 @@ def get_rocket(led): return sys_leds.get_rocket(led) +def flash_rocket(led, value, millis): + """ + Let one of the rockets flash for a certain time without + blocking the python interpreter. + + :param int led: Choose your rocket! + :param int value: brightness value (0 < value < 32) + :param int millis: duration of the rocket being on in milliseconds. + + .. versionadded:: 1.?? + """ + return sys_leds.flash_rocket(led, value, millis) + + def dim_top(value): """ Set global brightness for top RGB LEDs. diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 3a6e9dfcf47dfd16160b9b0cf76809436176bc55..2fed1b2c12052473bf878c2fb1536304df3be6fd 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -17,6 +17,7 @@ Q(set_all_hsv) Q(set_flashlight) Q(set_rocket) Q(get_rocket) +Q(flash_rocket) Q(set_powersave) Q(set_gamma) Q(dim_top) diff --git a/pycardium/modules/sys_leds.c b/pycardium/modules/sys_leds.c index 16efdcc9e69b2dc2f4c24b2b7630832faeea8b19..2e77fd833479f5d12dad7242edfa22bc2c6ee780 100644 --- a/pycardium/modules/sys_leds.c +++ b/pycardium/modules/sys_leds.c @@ -213,6 +213,22 @@ static mp_obj_t mp_leds_get_rocket(mp_obj_t led_in) } static MP_DEFINE_CONST_FUN_OBJ_1(leds_get_rocket_obj, mp_leds_get_rocket); +static mp_obj_t +mp_leds_flash_rocket(mp_obj_t led_in, mp_obj_t value_in, mp_obj_t time_in) +{ + int led = mp_obj_get_int(led_in); + int value = mp_obj_get_int(value_in); + int time = mp_obj_get_int(time_in); + + if (value > 31) { + mp_raise_ValueError("brightness must be < 32"); + } + + epic_leds_flash_rocket(led, value, time); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_3(leds_flash_rocket_obj, mp_leds_flash_rocket); + static mp_obj_t mp_leds_dim_top(mp_obj_t dim_in) { int dim = mp_obj_get_int(dim_in); @@ -283,6 +299,8 @@ static const mp_rom_map_elem_t leds_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_set_all_hsv), MP_ROM_PTR(&leds_set_all_hsv_obj) }, { MP_ROM_QSTR(MP_QSTR_set_rocket), MP_ROM_PTR(&leds_set_rocket_obj) }, { MP_ROM_QSTR(MP_QSTR_get_rocket), MP_ROM_PTR(&leds_get_rocket_obj) }, + { MP_ROM_QSTR(MP_QSTR_flash_rocket), + MP_ROM_PTR(&leds_flash_rocket_obj) }, { MP_ROM_QSTR(MP_QSTR_set_flashlight), MP_ROM_PTR(&leds_set_flashlight_obj) }, { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&leds_update_obj) },