MAX32630FTHR BLE Demo
Dependencies: max32630fthr
main.cpp
00001 #include "mbed.h" 00002 #include "max32630fthr.h" 00003 #include "ble/BLE.h" 00004 #include "ble/services/BatteryService.h" 00005 #include "DeviceInformationService.h" 00006 00007 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); 00008 00009 DigitalOut rLED(LED1, LED_OFF); 00010 DigitalOut gLED(LED2, LED_OFF); 00011 00012 BatteryService *batteryServicePtr; 00013 DeviceInformationService *deviceInformationServicePtr; 00014 00015 const char DEVICE_NAME[] = "MAX32630FTHR"; 00016 const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00017 int batteryPercentage = 100; 00018 00019 void periodicCallback(void) 00020 { 00021 rLED = !rLED; /* Blink LED while we're waiting for BLE events */ 00022 00023 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00024 * heavy-weight sensor polling from the main thread. */ 00025 if (--batteryPercentage <= 0) { 00026 batteryPercentage = 100; 00027 } 00028 batteryServicePtr->updateBatteryLevel(batteryPercentage); 00029 } 00030 00031 /* Restart Advertising on disconnection*/ 00032 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00033 { 00034 BLE::Instance().gap().startAdvertising(); 00035 } 00036 00037 /* Connection */ 00038 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00039 { 00040 } 00041 00042 00043 /** 00044 * This function is called when the ble initialization process has failed 00045 */ 00046 void onBleInitError(BLE &ble, ble_error_t error) 00047 { 00048 /* Avoid compiler warnings */ 00049 (void) ble; 00050 (void) error; 00051 /* Initialization error handling should go here */ 00052 } 00053 00054 /** 00055 * Callback triggered when the ble initialization process has finished 00056 */ 00057 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00058 { 00059 BLE& ble = params->ble; 00060 ble_error_t error = params->error; 00061 00062 if (error != BLE_ERROR_NONE) { 00063 /* In case of error, forward the error handling to onBleInitError */ 00064 onBleInitError(ble, error); 00065 return; 00066 } 00067 00068 /* Ensure that it is the default instance of BLE */ 00069 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00070 return; 00071 } 00072 00073 ble.gap().onDisconnection(disconnectionCallback); 00074 ble.gap().onConnection(connectionCallback); 00075 00076 /* Setup primary service. */ 00077 deviceInformationServicePtr = new DeviceInformationService(ble, "Maxim", "FTHR", "00001", "0.1", "0.0", "0.0"); 00078 batteryServicePtr = new BatteryService(ble, batteryPercentage); 00079 00080 /* Setup advertising */ 00081 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00082 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00083 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00084 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00085 ble.gap().setAdvertisingInterval(250); /* 250ms */ 00086 ble.gap().startAdvertising(); 00087 } 00088 00089 int main() 00090 { 00091 uint32_t i = 0; 00092 LowPowerTicker ticker; 00093 00094 printf("******** MAX32630FTHR BLE Test ********\r\n"); 00095 00096 BLE &ble = BLE::Instance(); 00097 ble.init(bleInitComplete); 00098 00099 /* SpinWait for initialization to complete. This is necessary because the 00100 * BLE object is used in the main loop below. */ 00101 while (ble.hasInitialized() == false) { /* spin loop */ } 00102 00103 ticker.attach(periodicCallback, 1); 00104 00105 while (1) { 00106 if (++i == 1000) { 00107 i = 0; 00108 gLED = !gLED; 00109 } 00110 00111 Thread::wait(1); 00112 00113 ble.waitForEvent(); 00114 } 00115 }
Generated on Sun Jul 24 2022 03:37:30 by 1.7.2