Demo program for LSM303 based boards
Dependencies: mbed AES BLE_API nRF51822 smallAES
Diff: main.cpp
- Revision:
- 7:46123a4652f2
- Parent:
- 5:fccb0823ceff
diff -r fccb0823ceff -r 46123a4652f2 main.cpp --- a/main.cpp Fri Aug 14 13:34:49 2020 +0000 +++ b/main.cpp Sun Oct 04 16:06:05 2020 +0000 @@ -16,11 +16,119 @@ #include "mbed.h" #include "ble/BLE.h" +#include "BLEservice.h" +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); +void onBleInitError(BLE &ble, ble_error_t error); +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); +Ticker ticker; +Serial pc(P0_24, P0_25); -int main(void) + +const static char DEVICE_NAME[] = "Test2020"; +static const uint16_t uuid16_list[] = {0x1234,0x1235}; +BLEservice * LED; +BLEservice * Button; + +DigitalOut col1(P0_4, 0); +DigitalOut alivenessLED(P0_13, 0); +DigitalOut actuatedLED(P0_14, 0); +void initLED() +{ + actuatedLED = 0; +} +void pollLED() +{ + uint16_t Value; + Value = LED->readCharacteristic(0); + actuatedLED = Value; +} +DigitalIn ButtonA(P0_17); +void initButton() +{ + +} +void pollButton() { + uint16_t Value = ButtonA; + pc.printf("Button = %d\r\n",Value); + Button->writeCharacteristic(0,Value); +} +void onDataWrittenCallback(const GattWriteCallbackParams *params) +{ + pc.printf("Got writeback for %d\r\n",params->handle); +} +void pollIO() +{ + pc.printf("Poll\r\n"); + LED->poll(); + Button->poll(); +} +int main(void) +{ + BLE &ble = BLE::Instance(); + ble.init(bleInitComplete); + /* SpinWait for initialization to complete. This is necessary because the + * BLE object is used in the main loop below. */ + while (ble.hasInitialized() == false) { /* spin loop */ } - while (true) { - + pc.printf("BLE init complete\r\n"); + + uint16_t LED_char_array[]={0xabcd}; + pc.printf("Creating LED Service\r\n"); + LED = new BLEservice(ble,0x1234,1,LED_char_array,initLED,pollLED); + LED->init(); + pc.printf("LED service ready\r\n"); + + uint16_t Button_char_array[]={0xa012}; + pc.printf("Creating Button Service\r\n"); + Button = new BLEservice(ble,0x1235,1,Button_char_array,initButton,pollButton); + Button->init(); + pc.printf("Button service ready\r\n"); + + + ticker.attach(pollIO, 0.1); /* Poll devices every 100ms */ + while (1) + { + ble.waitForEvent(); } } +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ + BLE& ble = params->ble; + ble_error_t error = params->error; + + if (error != BLE_ERROR_NONE) { + /* In case of error, forward the error handling to onBleInitError */ + onBleInitError(ble, error); + return; + } + + /* Ensure that it is the default instance of BLE */ + if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { + return; + } + + ble.gap().onDisconnection(disconnectionCallback); + ble.gattServer().onDataWritten(onDataWrittenCallback); + // ble.gattServer().onDataRead(onDataReadCallback); // Nordic Soft device will not call this so have to poll instead + + + /* setup advertising */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.gap().setAdvertisingInterval(500); /* 500ms. */ + pc.printf("Starting advertising\r\n"); + ble.gap().startAdvertising(); +} +void onBleInitError(BLE &ble, ble_error_t error) +{ + /* Initialization error handling should go here */ + pc.printf("BLE init error\r\n"); +} +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + pc.printf("Disconnected, restarting advertising\r\n"); + BLE::Instance().gap().startAdvertising(); +} \ No newline at end of file