diff --git a/Documentation/bluetooth/card10.rst b/Documentation/bluetooth/card10.rst index a88115119cbb7fb255434c504f1e403914efdca5..16bf38e18c5beabcd78523b2b7626b6d232739ca 100644 --- a/Documentation/bluetooth/card10.rst +++ b/Documentation/bluetooth/card10.rst @@ -33,22 +33,22 @@ The current draft uses following service specification: - Background LED Bottom Left characteristic: UUID: ``42230211-2342-2342-2342-234223422342`` - write no response + read and write no response - Background LED Bottom Right characteristic: UUID: ``42230212-2342-2342-2342-234223422342`` - write no response + read and write no response - Background LED Top Right characteristic: UUID: ``42230213-2342-2342-2342-234223422342`` - write no response + read and write no response - Background LED Top Left characteristic: UUID: ``42230214-2342-2342-2342-234223422342`` - write no reponse + read and write no reponse - LEDS dim bottom characteristic: @@ -78,7 +78,7 @@ The current draft uses following service specification: - LEDs above characteristic: UUID: ``42230220-2342-2342-2342-234223422342`` - write no reponse + read and write no reponse - Light sensor characteristic: @@ -120,7 +120,7 @@ Background LED <Position> characteristic ---------------------------------------- The Background LEDs <Position> characteristic makes it possible to address the bottom LEDs by position. -Just write there three ``uint8`` for the rgb color. +Just write there three ``uint8`` for the rgb color or read the current value. Dataformat: @@ -170,7 +170,7 @@ It writes always as persistant and it gives feedback if the value is in range an LEDs above characteristic --------------------------------- -This characteristic set every 11 leds on the top module at once. +This characteristic set or read the current value of every 11 leds on the top module at once. By defining 11x rgb from left to right. You need also to set exchange a bigger MTU to use this feature. - set a rainbow beginnig with red on the right edge: ``0xff0000ff8b00e8ff005dff0000ff2e00ffb900b9ff002eff5d00ffe800ffff008b`` diff --git a/epicardium/ble/card10.c b/epicardium/ble/card10.c index 2105ac02ca03e168fe8711f8c2797799e6801070..27231114e447e9a908557972232fb0c21ccbb7ce 100644 --- a/epicardium/ble/card10.c +++ b/epicardium/ble/card10.c @@ -125,7 +125,7 @@ static const uint8_t UUID_attChar_rockets[] = { /* BLE UUID for card10 led background bottom left */ static const uint8_t UUID_char_led_bg_bottom_left[] = { - ATT_PROP_WRITE_NO_RSP, + ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, UINT16_TO_BYTES(CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL), CARD10_UUID_SUFFIX, 0x11, CARD10_UUID_PREFIX }; @@ -134,9 +134,13 @@ static const uint8_t UUID_attChar_led_bg_bottom_left[] = { CARD10_UUID_SUFFIX, 0x11, CARD10_UUID_PREFIX }; +static uint8_t ledBGBottomLeftValue[] = { 0,0,0 }; +// works vor everyone? +static uint16_t rgbLen = sizeof(ledBGBottomLeftValue); + /* BLE UUID for card10 led background bottom right */ static const uint8_t UUID_char_led_bg_bottom_right[] = { - ATT_PROP_WRITE_NO_RSP, + ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, UINT16_TO_BYTES(CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL), CARD10_UUID_SUFFIX, 0x12, CARD10_UUID_PREFIX }; @@ -145,9 +149,11 @@ static const uint8_t UUID_attChar_led_bg_bottom_right[] = { CARD10_UUID_SUFFIX, 0x12, CARD10_UUID_PREFIX }; +static uint8_t ledBGBottomRightValue[] = { 0,0,0 }; + /* BLE UUID for card10 led background top right */ static const uint8_t UUID_char_led_bg_top_right[] = { - ATT_PROP_WRITE_NO_RSP, + ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, UINT16_TO_BYTES(CARD10_LED_BG_TOP_RIGHT_VAL_HDL), CARD10_UUID_SUFFIX, 0x13, CARD10_UUID_PREFIX }; @@ -156,9 +162,11 @@ static const uint8_t UUID_attChar_led_bg_top_right[] = { CARD10_UUID_SUFFIX, 0x13, CARD10_UUID_PREFIX }; +static uint8_t ledBGTopRightValue[] = { 0,0,0 }; + /* BLE UUID for card10 led background top left */ static const uint8_t UUID_char_led_bg_top_left[] = { - ATT_PROP_WRITE_NO_RSP, + ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, UINT16_TO_BYTES(CARD10_LED_BG_TOP_LEFT_VAL_HDL), CARD10_UUID_SUFFIX, 0x14, CARD10_UUID_PREFIX }; @@ -167,6 +175,8 @@ static const uint8_t UUID_attChar_led_bg_top_left[] = { CARD10_UUID_SUFFIX, 0x14, CARD10_UUID_PREFIX }; +static uint8_t ledBGTopLeftValue[] = { 0,0,0 }; + /* BLE UUID for card10 dim leds on bottom */ static const uint8_t UUID_char_leds_bottom_dim[] = { ATT_PROP_WRITE, @@ -227,7 +237,7 @@ static uint16_t personalStateLen = sizeof(personalStateValue); /* BLE UUID for card10 above leds */ static const uint8_t UUID_char_leds_above[] = { - ATT_PROP_WRITE_NO_RSP, + ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP, UINT16_TO_BYTES(CARD10_LEDS_ABOVE_VAL_HDL), CARD10_UUID_SUFFIX, 0x20, CARD10_UUID_PREFIX }; @@ -235,6 +245,21 @@ static const uint8_t UUID_char_leds_above[] = { static const uint8_t UUID_attChar_leds_above[] = { CARD10_UUID_SUFFIX, 0x20, CARD10_UUID_PREFIX }; +static uint8_t aboveLEDsValue[] = { + 0,0,0, // 0 + 0,0,0, // 1 + 0,0,0, // 2 + 0,0,0, // 3 + 0,0,0, // 4 + 0,0,0, // 5 + 0,0,0, // 6 + 0,0,0, // 7 + 0,0,0, // 8 + 0,0,0, // 9 + 0,0,0, // 10 +}; +static uint16_t aboveLEDsLen = sizeof(aboveLEDsValue); + // starting at 0xf0 with read only characteristics /* BLE UUID for card10 char light sensor */ @@ -317,12 +342,14 @@ static const attsAttr_t card10SvcAttrList[] = { .maxLen = sizeof(UUID_char_led_bg_bottom_left), .permissions = ATTS_PERMIT_READ }, { .pUuid = UUID_attChar_led_bg_bottom_left, - .pValue = NULL, + .pValue = ledBGBottomLeftValue, + .pLen = &rgbLen, .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 right @@ -332,12 +359,14 @@ static const attsAttr_t card10SvcAttrList[] = { .maxLen = sizeof(UUID_char_led_bg_bottom_right), .permissions = ATTS_PERMIT_READ }, { .pUuid = UUID_attChar_led_bg_bottom_right, - .pValue = NULL, + .pValue = ledBGBottomRightValue, + .pLen = &rgbLen, .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 top right @@ -348,7 +377,8 @@ static const attsAttr_t card10SvcAttrList[] = { .settings = 0, .permissions = ATTS_PERMIT_READ }, { .pUuid = UUID_attChar_led_bg_top_right, - .pValue = NULL, + .pValue = ledBGTopRightValue, + .pLen = &rgbLen, .maxLen = 3 * sizeof(uint8_t), .settings = ATTS_SET_WRITE_CBACK, .permissions = @@ -363,7 +393,8 @@ static const attsAttr_t card10SvcAttrList[] = { .maxLen = sizeof(UUID_char_led_bg_top_left), .permissions = ATTS_PERMIT_READ }, { .pUuid = UUID_attChar_led_bg_top_left, - .pValue = NULL, + .pValue = ledBGTopLeftValue, + .pLen = &rgbLen, .maxLen = 3 * sizeof(uint8_t), .settings = ATTS_SET_WRITE_CBACK, .permissions = @@ -456,12 +487,14 @@ static const attsAttr_t card10SvcAttrList[] = { .maxLen = sizeof(UUID_char_leds_above), .permissions = ATTS_PERMIT_READ }, { .pUuid = UUID_attChar_leds_above, - .pValue = NULL, + .pValue = aboveLEDsValue, + .pLen = &aboveLEDsLen, .maxLen = 11 * 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) }, // Light sensor @@ -725,8 +758,10 @@ static uint8_t readCard10CB( ) { uint16_t ui16 = 0; uint64_t ui64 = 0; + uint8_t rgb[] = { 0, 0, 0 }; switch (handle) { + // time case CARD10_TIME_VAL_HDL: ui64 = epic_rtc_get_milliseconds(); uint64_t time; @@ -736,11 +771,66 @@ static uint8_t readCard10CB( APP_TRACE_INFO0("ble-card10: read time\n"); return ATT_SUCCESS; + // background leds + case CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL: + epic_leds_get_rgb(11, pAttr->pValue); + APP_TRACE_INFO3( + "ble-card10: set bg bottom left: #%02x%02x%02x\n", + pAttr->pValue[0], + pAttr->pValue[1], + pAttr->pValue[2] + ); + return ATT_SUCCESS; + case CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL: + epic_leds_get_rgb(12, pAttr->pValue); + APP_TRACE_INFO3( + "ble-card10: get bg bottom right: #%02x%02x%02x\n", + pAttr->pValue[0], + pAttr->pValue[1], + pAttr->pValue[2] + ); + return ATT_SUCCESS; + case CARD10_LED_BG_TOP_RIGHT_VAL_HDL: + epic_leds_get_rgb(13, pAttr->pValue); + APP_TRACE_INFO3( + "ble-card10: get bg top right: #%02x%02x%02x\n", + pAttr->pValue[0], + pAttr->pValue[1], + pAttr->pValue[2] + ); + return ATT_SUCCESS; + case CARD10_LED_BG_TOP_LEFT_VAL_HDL: + epic_leds_get_rgb(14, pAttr->pValue); + APP_TRACE_INFO3( + "ble-card10: get bg top left: #%02x%02x%02x\n", + pAttr->pValue[0], + pAttr->pValue[1], + pAttr->pValue[2] + ); + return ATT_SUCCESS; + // personal state case CARD10_PERSONAL_STATE_VAL_HDL: ui16 = epic_personal_state_get(); *pAttr->pValue = ui16; APP_TRACE_INFO1("ble-card10: read personal state: %d\n", ui16); return ATT_SUCCESS; + // leds above + case CARD10_LEDS_ABOVE_VAL_HDL: + for (ui16 = 0; ui16 < 11; ui16++) { + epic_leds_get_rgb(ui16, rgb); + pAttr->pValue[ui16 * 3] = rgb[0]; + pAttr->pValue[ui16 * 3 + 1] = rgb[1]; + pAttr->pValue[ui16 * 3 + 2] = rgb[2]; + APP_TRACE_INFO4( + "ble-card10: get led %ld above to #%02x%02x%02x\n", + ui16, + pAttr->pValue[ui16 * 3], + pAttr->pValue[ui16 * 3 + 1], + pAttr->pValue[ui16 * 3 + 2] + ); + } + return ATT_SUCCESS; + // light sensor case CARD10_LIGHT_SENSOR_VAL_HDL: epic_light_sensor_get(&ui16); *pAttr->pValue = ui16; diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 74b0c814653b0ad0193d2ffd11dc50f0d7fae8d7..8ae39eba5efb7208035187d5f22b323a1c022a6c 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -95,6 +95,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 0x6f #define API_VIBRA_SET 0x70 #define API_VIBRA_VIBRATE 0x71 @@ -580,6 +581,17 @@ API(API_GPIO_READ_PIN, int epic_gpio_read_pin(uint8_t pin)); */ API(API_LEDS_SET, void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b)); + +/** + * Get one of card10's RGB LEDs in format of RGB. + * + * :param int led: Which LED to set. 0-10 are the LEDs on the top and 11-14 + * are the 4 "ambient" LEDs. + * :param uint8_t * rgb: need tree byte array to get the value of red, green and blue. + * :return bool: if true the reading of value was not blocked by personal state + */ +API(API_LEDS_GET, bool epic_leds_get_rgb(int led, uint8_t * rgb)); + /** * 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 93b80a993717adaa0d058da554e3d7f6433e2dc5..27702835ab7f72090c3067fa31d5bee61a4401c5 100644 --- a/epicardium/modules/leds.c +++ b/epicardium/modules/leds.c @@ -49,6 +49,15 @@ void epic_leds_prep(int led, uint8_t r, uint8_t g, uint8_t b) leds_prep(led, r, g, b); } +bool epic_leds_get_rgb(int led, uint8_t *rgb) +{ + if (led == PERSONAL_STATE_LED && personal_state_enabled()) + return false; + + leds_get_rgb(led, rgb); + return true; +} + void epic_leds_prep_hsv(int led, float h, float s, float v) { if (led == PERSONAL_STATE_LED && personal_state_enabled()) diff --git a/lib/card10/leds.c b/lib/card10/leds.c index 3f284db4ab1d1dae0f561510e1671b93b83bcafe..d21049cfd6cf1337ee9eccee9627da6487fb91e2 100644 --- a/lib/card10/leds.c +++ b/lib/card10/leds.c @@ -206,6 +206,13 @@ void leds_prep(uint8_t led, uint8_t r, uint8_t g, uint8_t b) leds[led][2] = b; } +void leds_get_rgb(uint8_t led, uint8_t *rgb) +{ + rgb[0] = leds[led][0]; + rgb[1] = leds[led][1]; + rgb[2] = leds[led][2]; +} + #if 0 //don't use, is buggy void leds_set_autodim(uint8_t led, uint8_t r, uint8_t g, uint8_t b) diff --git a/lib/card10/leds.h b/lib/card10/leds.h index 8977daa7cc75233ac6d4b5bf9ace7a1f03781811..cdb4dfdb0b10e401f8872615ab7c3cca4331e9c0 100644 --- a/lib/card10/leds.h +++ b/lib/card10/leds.h @@ -6,6 +6,7 @@ void leds_set_dim_top(uint8_t value); void leds_set_dim_bottom(uint8_t value); void leds_prep(uint8_t led, uint8_t r, uint8_t g, uint8_t b); +void leds_get_rgb(uint8_t led, uint8_t * rgb); void leds_prep_hsv(uint8_t led, float h, float s, float v); void leds_update_power(void); void leds_update(void); diff --git a/pycardium/modules/py/leds.py b/pycardium/modules/py/leds.py index ad5563cfb2a3c8fca7bf4a4909ac460ba0e86808..9b7d2511544be91ab632d04cb3b598503b1c166f 100644 --- a/pycardium/modules/py/leds.py +++ b/pycardium/modules/py/leds.py @@ -131,6 +131,17 @@ def set(led, color): sys_leds.set(led, color) +def get_rgb(led): + """ + Get the current RGB value for an RGB LED. + + :param int led: Which LED to set. 0-10 are the LEDs on the top and 11-14 + are the 4 "ambient" LEDs + :return [r,g,b] color: RGB triplet + """ + return sys_leds.get_rgb(led) + + def set_hsv(led, color): """ Prepare an RGB LED to be set to an HSV value. diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 095f989871cbc0c84ee7941a6e21946278928453..5f2f2c7594ff8a63037943179a8456e188eb7388 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -8,6 +8,7 @@ Q(sys_leds) Q(update) Q(set) +Q(get_rgb) Q(set_hsv) Q(prep) Q(prep_hsv) diff --git a/pycardium/modules/sys_leds.c b/pycardium/modules/sys_leds.c index 7c1c2d5a35156ae06c31c424546bf1623d69d311..86012f25bc3b68d180dd56625e53d54dd264a2db 100644 --- a/pycardium/modules/sys_leds.c +++ b/pycardium/modules/sys_leds.c @@ -6,7 +6,6 @@ #include <stdio.h> - static mp_obj_t mp_leds_set(mp_obj_t led_in, mp_obj_t color_in) { int led = mp_obj_get_int(led_in); @@ -79,6 +78,23 @@ static mp_obj_t mp_leds_prep(mp_obj_t led_in, mp_obj_t color_in) } static MP_DEFINE_CONST_FUN_OBJ_2(leds_prep_obj, mp_leds_prep); +static mp_obj_t mp_leds_get_rgb(mp_obj_t led_in, mp_obj_t color_out) +{ + int led = mp_obj_get_int(led_in); + uint8_t rgb[] = { 0, 0, 0 }; + epic_leds_get_rgb(led, rgb); + + mp_obj_t ret = mp_obj_new_tuple(3, NULL); + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(ret); + + tuple->items[0] = MP_OBJ_NEW_SMALL_INT(rgb[0]); + tuple->items[1] = MP_OBJ_NEW_SMALL_INT(rgb[1]); + tuple->items[2] = MP_OBJ_NEW_SMALL_INT(rgb[2]); + + return ret; +} +static MP_DEFINE_CONST_FUN_OBJ_1(leds_get_rgb_obj, mp_leds_get_rgb); + static mp_obj_t mp_leds_prep_hsv(mp_obj_t led_in, mp_obj_t color_in) { int led = mp_obj_get_int(led_in); @@ -238,6 +254,7 @@ static const mp_rom_map_elem_t leds_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&leds_set_obj) }, { MP_ROM_QSTR(MP_QSTR_set_hsv), MP_ROM_PTR(&leds_set_hsv_obj) }, { MP_ROM_QSTR(MP_QSTR_prep), MP_ROM_PTR(&leds_prep_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_rgb), MP_ROM_PTR(&leds_get_rgb_obj) }, { MP_ROM_QSTR(MP_QSTR_prep_hsv), MP_ROM_PTR(&leds_prep_hsv_obj) }, { 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) },