MAX32630FTHR BLE Demo
Dependencies: max32630fthr
Revision 0:364860b25ae2, committed 2017-06-12
- Comitter:
- jessexm
- Date:
- Mon Jun 12 20:03:20 2017 +0000
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jun 12 20:03:20 2017 +0000 @@ -0,0 +1,115 @@ +#include "mbed.h" +#include "max32630fthr.h" +#include "ble/BLE.h" +#include "ble/services/BatteryService.h" +#include "DeviceInformationService.h" + +MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); + +DigitalOut rLED(LED1, LED_OFF); +DigitalOut gLED(LED2, LED_OFF); + +BatteryService *batteryServicePtr; +DeviceInformationService *deviceInformationServicePtr; + +const char DEVICE_NAME[] = "MAX32630FTHR"; +const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, GattService::UUID_DEVICE_INFORMATION_SERVICE}; +int batteryPercentage = 100; + +void periodicCallback(void) +{ + rLED = !rLED; /* Blink LED while we're waiting for BLE events */ + + /* Note that the periodicCallback() executes in interrupt context, so it is safer to do + * heavy-weight sensor polling from the main thread. */ + if (--batteryPercentage <= 0) { + batteryPercentage = 100; + } + batteryServicePtr->updateBatteryLevel(batteryPercentage); +} + +/* Restart Advertising on disconnection*/ +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + BLE::Instance().gap().startAdvertising(); +} + +/* Connection */ +void connectionCallback(const Gap::ConnectionCallbackParams_t *params) +{ +} + + +/** + * This function is called when the ble initialization process has failed + */ +void onBleInitError(BLE &ble, ble_error_t error) +{ + /* Avoid compiler warnings */ + (void) ble; + (void) error; + /* Initialization error handling should go here */ +} + +/** + * Callback triggered when the ble initialization process has finished + */ +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.gap().onConnection(connectionCallback); + + /* Setup primary service. */ + deviceInformationServicePtr = new DeviceInformationService(ble, "Maxim", "FTHR", "00001", "0.1", "0.0", "0.0"); + batteryServicePtr = new BatteryService(ble, batteryPercentage); + + /* Setup advertising */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.gap().setAdvertisingInterval(250); /* 250ms */ + ble.gap().startAdvertising(); +} + +int main() +{ + uint32_t i = 0; + LowPowerTicker ticker; + + printf("******** MAX32630FTHR BLE Test ********\r\n"); + + 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 */ } + + ticker.attach(periodicCallback, 1); + + while (1) { + if (++i == 1000) { + i = 0; + gLED = !gLED; + } + + Thread::wait(1); + + ble.waitForEvent(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/max32630fthr.lib Mon Jun 12 20:03:20 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/MaximIntegrated/code/max32630fthr/#60997adf01a2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.lib Mon Jun 12 20:03:20 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/maximmbed/mbed.git/#83348083677696a51d9b0250308e88815d77f10a