This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Help converting some Adafruit Arduino code to embedded C

I have some functional code working on an Adafruit nRF52 Feather board which I am trying to change over to run in embedded C on an nRF51 dongle. I am using Adafruit nRF52 feather library in the Arduino IDE. I have pulled out the below code from the Arduino IDE which I am having trouble understanding fully. I have been through a number of examples but can't work it out.

I have found some of the functions in the nRF5 SDK but am getting confused with the different formats used and was unsure as to the best approach. There are 5 other BLEcharacteristics for other functions such as "mute", so figured I should be creating a structure to hold all of the data for each characteristic?

The adafruit library for arduino is at : github.com/.../Adafruit_nRF52_Arduino

TIA

William

**********************************************************************************************************************

//Lock
uint8_t BLECharacteristic1[] =
{
0x00, 0x00, 0xaa, 0xa1, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
};

BLECharacteristic lockScreen = BLECharacteristic(BLECharacteristic1);

lockScreen.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
lockScreen.setPermission(SECMODE_OPEN, SECMODE_OPEN);
lockScreen.setMaxLen(5);
lockScreen.setStringDescriptor("");//lockscreen
lockScreen.begin();
lockScreen.write("none");

//Set Buffer, read characteristic value, output current value to serial monitor
char lockScreenBuffer[5] = { 0 };
//lock password initialization
char lockScreenPassword[5] = {"krs1"}; //lock
lockScreen.read(lockScreenBuffer, sizeof(lockScreenBuffer));

**************************************************************************************************************************

  • Looks like a custom "lockScreen" BLE service you have here? Maybe the BLE service and characteristics tutorial is a good starting point: devzone.nordicsemi.com/.../ble-characteristics-a-beginners-tutorial

  • Hi Matt, I had gone through the examples and was getting lost with creating multiple characteristics. I had derived the code below to add BLETestChar1 but was not sure what was required to add the additional characteristics and I can not find an example that helps me understand what I am doing. Edit of Arduino code and C function below:

    Arduino Code to create service and 4 characteristics:

    uint8_t BLETestServ1[] =
    {
    0x00, 0x00, 0xaa, 0xa0, 0x00, 0x00, 0x10, 0x00,
    0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    };

    uint8_t BLETestChar1[] =
    {
    0x00, 0x00, 0xaa, 0xa1, 0x00, 0x00, 0x10, 0x00,
    0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    };

    uint8_t BLETestChar2[] =
    {
    0x00, 0x00, 0xaa, 0xa2, 0x00, 0x00, 0x10, 0x00,
    0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    };

    uint8_t BLETestChar3[] =
    {
    0x00, 0x00, 0xaa, 0xa3, 0x00, 0x00, 0x10, 0x00,
    0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    };

    uint8_t BLETestChar4[] =
    {
    0x00, 0x00, 0xaa, 0xa4, 0x00, 0x00, 0x10, 0x00,
    0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    };

    BLEService myService = BLEService(BLETestServ1);
    BLECharacteristic test1 = BLECharacteristic(BLETestChar1);
    BLECharacteristic test2 = BLECharacteristic(BLETestChar2);
    BLECharacteristic test3 = BLECharacteristic(BLETestChar3);
    BLECharacteristic test4 = BLECharacteristic(BLETestChar4);

    void setupBLE(void)
    {

    myService.begin();


    test1.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
    test1.setPermission(SECMODE_OPEN, SECMODE_OPEN);
    test1.setMaxLen(5);
    test1.setStringDescriptor("");
    test1.begin();
    test1.write("none");

    test2.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
    test2.setPermission(SECMODE_OPEN, SECMODE_OPEN);
    test2.setMaxLen(20);
    test2.setStringDescriptor("");
    test2.begin();
    test2.write("none");

    test3.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
    test3.setPermission(SECMODE_OPEN, SECMODE_OPEN);
    test3.setMaxLen(20);
    test3.setStringDescriptor("");
    test3.begin();
    test3.write("none");

    ##########################################################################################################

    #define BLETestServ1 {{0x00, 0x00, 0xaa, 0xa0, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}} /// 128b UUID

    #define BLETestChar1 {{0x00, 0x00, 0xaa, 0xa1, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}} /// 128b UUID
    #define BLETestChar2 {{0x00, 0x00, 0xaa, 0xa2, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}} /// 128b UUID
    #define BLETestChar3 {{0x00, 0x00, 0xaa, 0xa3, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}} /// 128b UUID

    static uint32_t test_char_add(ble_ts_t * p_test_service)
    {
    uint32_t err_code=0;
    ble_uuid_t char_uuid;
    ble_uuid128_t service_uuid = BLETestServ1;
    char_uuid.uuid = BLETestChar1;
    sd_ble_uuid_vs_add(&base_uuid, &char_uuid.type);



    ble_gatts_char_md_t char_md;
    memset(&char_md, 0, sizeof(char_md));
    char_md.char_props.read=1;
    char_md.char_props.write=1;


    ble_gatts_attr_md_t cccd_md;
    memset(&cccd_md, 0, sizeof(cccd_md));
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
    cccd_md.vloc = BLE_GATTS_VLOC_STACK;
    char_md.char_props.notify =1;

    ble_gatts_attr_md_t attr_md;
    memset(&attr_md, 0, sizeof(attr_md));
    attr_md.vloc = BLE_GATTS_VLOC_STACK;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

    ble_gatts_attr_t attr_char_value;
    memset(&attr_char_value, 0, sizeof(attr_char_value));
    attr_char_value.p_uuid = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;

    attr_char_value.max_len = 4;
    attr_char_value.init_len = 4;
    uint8_t value[4] = {0x12,0x34,0x56,0x78};
    attr_char_value.p_value = value;

    err_code = sd_ble_gatts_characteristic_add(p_test_service->service_handle,
    &char_md,
    &attr_char_value,
    &p_test_service->char_handles);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_INFO("\r\nService handle: %#x\n\r", p_test_service->service_handle);
    NRF_LOG_INFO("Char value handle: %#x\r\n", p_test_service->char_handles.value_handle);
    NRF_LOG_INFO("Char cccd handle: %#x\r\n\r\n", p_test_service->char_handles.cccd_handle);

    return NRF_SUCCESS;
    }

  • Hi,

    Have a look at the ble_app_uart example in the SDK, expecially this function ble_nus_init(&m_nus, &nus_init). You can see how two characteristics are being added(RX and TX).

Related