MAX32630FTHR BLE Demo
Dependencies: max32630fthr
main.cpp
- Committer:
- jessexm
- Date:
- 2017-06-12
- Revision:
- 0:364860b25ae2
File content as of revision 0:364860b25ae2:
#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(); } }