diff --git a/Documentation/bluetooth/card10.rst b/Documentation/bluetooth/card10.rst index 16bf38e18c5beabcd78523b2b7626b6d232739ca..8f080d4b97b063dd988c6bcc7e4dd9d24dd65497 100644 --- a/Documentation/bluetooth/card10.rst +++ b/Documentation/bluetooth/card10.rst @@ -28,7 +28,7 @@ The current draft uses following service specification: - Rockets characteristic: UUID: ``42230210-2342-2342-2342-234223422342`` - write no response + read and write no response - Background LED Bottom Left characteristic: @@ -104,6 +104,9 @@ Rockets characteristic The Rockets characteristic makes it possible to address every three rockets. Just write there three byte array, one for evey rocket. +On read you get the current value of all three rockets. +Range is between 0 and 31 (``0x1f`) if send higher value it will set to max of 31. + Dataformat: @@ -113,8 +116,8 @@ Dataformat: Rocket0 Rocket1 Rocket2 ======= ======= ======= -- Enable only Rocket0: ``0xff0000`` -- Enable all rockets with 50% brightness: ``0x7f7f7f`` +- Enable only Rocket0: ``0x1f0000`` +- Enable all rockets with 50% brightness: ``0x0f0f0f`` Background LED <Position> characteristic ---------------------------------------- diff --git a/epicardium/ble/card10.c b/epicardium/ble/card10.c index ebea5b3a09a339548ab59f3f080d3605b6ce9223..25697ff40614bacea196c63688aafa7f5bf169a4 100644 --- a/epicardium/ble/card10.c +++ b/epicardium/ble/card10.c @@ -114,7 +114,7 @@ static const uint8_t UUID_attChar_vibra[] = { /* BLE UUID for card10 char rockets */ static const uint8_t UUID_char_rockets[] = { - ATT_PROP_WRITE_NO_RSP, + ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, UINT16_TO_BYTES(CARD10_ROCKETS_VAL_HDL), CARD10_UUID_SUFFIX, 0x10, CARD10_UUID_PREFIX }; @@ -123,6 +123,9 @@ static const uint8_t UUID_attChar_rockets[] = { CARD10_UUID_SUFFIX, 0x10, CARD10_UUID_PREFIX }; +static uint8_t rocketsValue[] = { 0, 0, 0 }; +static uint16_t rocketsLen = sizeof(rocketsValue); + /* BLE UUID for card10 led background bottom left */ static const uint8_t UUID_char_led_bg_bottom_left[] = { ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, @@ -338,11 +341,13 @@ static const attsAttr_t card10SvcAttrList[] = { }, { .pUuid = UUID_attChar_rockets, - .pValue = NULL, + .pValue = rocketsValue, + .pLen = &rocketsLen, .maxLen = 3 * sizeof(uint8_t), - .settings = ATTS_SET_WRITE_CBACK, + .settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK, .permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC | - ATTS_PERMIT_WRITE_AUTH, + ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ | + ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH, }, // BG LED Bottom left @@ -820,6 +825,17 @@ static uint8_t readCard10CB( APP_TRACE_INFO0("ble-card10: read time\n"); return ATT_SUCCESS; + case CARD10_ROCKETS_VAL_HDL: + pAttr->pValue[0] = epic_leds_get_rocket(0); + pAttr->pValue[1] = epic_leds_get_rocket(1); + pAttr->pValue[2] = epic_leds_get_rocket(2); + APP_TRACE_INFO3( + "ble-card10: get rockets 0:%d, 1:%d, 2:%d\n", + pAttr->pValue[0], + pAttr->pValue[1], + pAttr->pValue[2] + ); + return ATT_SUCCESS; // background leds case CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL: return getRGBLed(11, pAttr); diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 0b440189db0de69265428055ae0fc5bee070ca1a..4fb34d8c6cc3839c88455209c085b3a63da33e0e 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -96,6 +96,7 @@ typedef _Bool bool; #define API_LEDS_SET_ALL_HSV 0x6b #define API_LEDS_SET_GAMMA_TABLE 0x6c #define API_LEDS_CLEAR_ALL 0x6d +#define API_LEDS_GET_ROCKET 0x6e #define API_LEDS_GET 0x6f #define API_VIBRA_SET 0x70 @@ -739,6 +740,24 @@ API(API_LEDS_UPDATE, void epic_leds_update(void)); */ API(API_LEDS_SET_ROCKET, void epic_leds_set_rocket(int led, uint8_t value)); +/** + * Get the brightness of one of the rocket LEDs. + * + * :param int led: Which LED to get. + * + * +-------+--------+----------+ + * | ID | Color | Location | + * +=======+========+==========+ + * | ``0`` | Blue | Left | + * +-------+--------+----------+ + * | ``1`` | Yellow | Top | + * +-------+--------+----------+ + * | ``2`` | Green | Right | + * +-------+--------+----------+ + * :returns value: Brightness of LED (value between 0 and 31) or ``-EINVAL`` if the LED/rocket does not exists. + */ +API(API_LEDS_GET_ROCKET, int epic_leds_get_rocket(int led)); + /** * Turn on the bright side LED which can serve as a flashlight if worn on the left wrist or as a rad tattoo illuminator if worn on the right wrist. * diff --git a/epicardium/modules/leds.c b/epicardium/modules/leds.c index aab84c86e2d80dddf7617cb1e667068421bc5753..d3d3e0ea71435beff65d8199fb9082ec737eb230 100644 --- a/epicardium/modules/leds.c +++ b/epicardium/modules/leds.c @@ -112,6 +112,18 @@ void epic_leds_set_rocket(int led, uint8_t value) pmic_set_led(led, value); } +int epic_leds_get_rocket(int led) +{ + int ret = 0; + while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(1)) < 0) { + vTaskDelay(pdMS_TO_TICKS(1)); + } + + ret = pmic_get_led(led); + hwlock_release(HWLOCK_I2C); + return ret; +} + void epic_set_flashlight(bool power) { leds_flashlight(power); diff --git a/lib/card10/pmic.c b/lib/card10/pmic.c index 5927ff55f8cbe7aa6efec7d5920affe44cabcb75..e60cf81ab034cb11f61d64a758a024080ca7ba18 100644 --- a/lib/card10/pmic.c +++ b/lib/card10/pmic.c @@ -3,6 +3,7 @@ #include "MAX77650-Arduino-Library.h" #include <stdint.h> #include <stdio.h> +#include <errno.h> static const gpio_cfg_t pmic_interrupt_pin = { PORT_0, PIN_12, GPIO_FUNC_IN, GPIO_PAD_PULL_UP @@ -123,6 +124,20 @@ void pmic_set_button_callback(pmic_button_callback_fn cb) pmic_button_callback = cb; } +int pmic_get_led(uint8_t led) +{ + if (led == 0) { + return MAX77650_getBRT_LED0(); + } + if (led == 1) { + return MAX77650_getBRT_LED1(); + } + if (led == 2) { + return MAX77650_getBRT_LED2(); + } + return -EINVAL; +} + void pmic_set_led(uint8_t led, uint8_t val) { if (led == 0) { diff --git a/lib/card10/pmic.h b/lib/card10/pmic.h index f913c99b7342dbe65b389c8172c88d25020cec9a..f58a192307ecb619bf251d3282d60b9dbeeab5bf 100644 --- a/lib/card10/pmic.h +++ b/lib/card10/pmic.h @@ -21,6 +21,7 @@ void pmic_init(void); void pmic_set_led(uint8_t led, uint8_t val); +int pmic_get_led(uint8_t led); void pmic_poll(void); /* weak, so it can be overwritten by applications */ diff --git a/pycardium/modules/py/leds.py b/pycardium/modules/py/leds.py index 9b7d2511544be91ab632d04cb3b598503b1c166f..25aa3c19747dd92f8d48397c6f0491553c2eecd8 100644 --- a/pycardium/modules/py/leds.py +++ b/pycardium/modules/py/leds.py @@ -72,6 +72,27 @@ def set_rocket(led, value): sys_leds.set_rocket(led, value) +def get_rocket(led): + """ + Get brightness of one of the rocket LEDs. + + :param int led: Choose your rocket! + + +-------+--------+----------+ + | ID | Color | Location | + +=======+========+==========+ + | ``0`` | Blue | Left | + +-------+--------+----------+ + | ``1`` | Yellow | Top | + +-------+--------+----------+ + | ``2`` | Green | Right | + +-------+--------+----------+ + :rtype: int + :returns: Brightness of LED (value between 0 and 31). + """ + return sys_leds.get_rocket(led) + + def dim_top(value): """ Set global brightness for top RGB LEDs. diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 50178cc8e578158a6362add774f04d67c5e50715..1c48037060bb14ded87839d4bdb2ff63a83c31cf 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -16,6 +16,7 @@ Q(set_all) Q(set_all_hsv) Q(set_flashlight) Q(set_rocket) +Q(get_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 990a4a36a3a97612ebe5d9dd745bd6317b61f438..a5f5747970e1060f545ab45702695a6e1985f247 100644 --- a/pycardium/modules/sys_leds.c +++ b/pycardium/modules/sys_leds.c @@ -200,6 +200,19 @@ static mp_obj_t mp_leds_set_rocket(mp_obj_t led_in, mp_obj_t value_in) } static MP_DEFINE_CONST_FUN_OBJ_2(leds_set_rocket_obj, mp_leds_set_rocket); +static mp_obj_t mp_leds_get_rocket(mp_obj_t led_in) +{ + int led = mp_obj_get_int(led_in); + uint8_t ret = epic_leds_get_rocket(led); + if (ret == -EINVAL) { + mp_raise_ValueError( + "invalid value: maybe the led does not exists" + ); + } + return MP_OBJ_NEW_SMALL_INT(ret); +} +static MP_DEFINE_CONST_FUN_OBJ_1(leds_get_rocket_obj, mp_leds_get_rocket); + static mp_obj_t mp_leds_dim_top(mp_obj_t dim_in) { int dim = mp_obj_get_int(dim_in); @@ -269,6 +282,7 @@ static const mp_rom_map_elem_t leds_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_set_all), MP_ROM_PTR(&leds_set_all_obj) }, { 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_set_flashlight), MP_ROM_PTR(&leds_set_flashlight_obj) }, { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&leds_update_obj) },