Application running on nRF51822 PCA10001
Dependencies: BLE_API MMA8652 nRF51822 mbed-src
main.cpp
00001 /******************************************************************************* 00002 * Title : System Initialisation 00003 * Filename : main.cpp 00004 * Author : Richard Osterloh 00005 * Origin Date : 22/07/2014 00006 * Version : 1.0.0 00007 * Compiler : mbed compiler 00008 * Target : Nordic nRF51822 00009 * Notes : None 00010 *******************************************************************************/ 00011 /*************** MODULE REVISION LOG ****************************************** 00012 * 00013 * Date Software Version Initials Description 00014 * 22/07/2014 1.0.0 RO Module Created. 00015 * 00016 *******************************************************************************/ 00017 /** \file main.cpp 00018 * \brief This module contains the main function and setup 00019 */ 00020 /****************************************************************************** 00021 * Includes 00022 *******************************************************************************/ 00023 #include "mbed.h" 00024 #include "Logger.h" 00025 #include "Configuration.h" 00026 #include "BLEDevice.h" 00027 //#include "nRF51822n.h" 00028 //#include "MMA8652.h" 00029 #include "BuddiService.h" 00030 #include "BatteryService.h" 00031 #include "DeviceInformationService.h" 00032 00033 /****************************************************************************** 00034 * Module Preprocessor Constants 00035 *******************************************************************************/ 00036 00037 /****************************************************************************** 00038 * Module Preprocessor Macros 00039 *******************************************************************************/ 00040 00041 /****************************************************************************** 00042 * Module Typedefs 00043 *******************************************************************************/ 00044 00045 /****************************************************************************** 00046 * Module Variable Definitions 00047 *******************************************************************************/ 00048 //nRF51822n nrf; 00049 BLEDevice ble; 00050 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */ 00051 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */ 00052 //AnalogIn adc1(p0_0); 00053 //MMA8652 acc1(I2C_SDA0, I2C_SCL0); 00054 00055 static const uint8_t *uuidlist = Nudge::getServiceUUIDp(); 00056 static uint8_t uuidlistrev[16]; 00057 static const char *DEVICE_NAME = "Nudge"; 00058 00059 void bluetoothInit(); 00060 00061 static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that 00062 * polling is necessary. */ 00063 static Gap::ConnectionParams_t connectionParams; 00064 00065 uint8_t batteryLevel = 100; 00066 BatteryService *batteryServicePtr = NULL; 00067 00068 /****************************************************************************** 00069 * Function Prototypes 00070 *******************************************************************************/ 00071 00072 /****************************************************************************** 00073 * Function Definitions 00074 *******************************************************************************/ 00075 void timeoutCallback(void) 00076 { 00077 DEBUG("Timeout!\r\n"); 00078 } 00079 00080 void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params) 00081 { 00082 DEBUG("Connected. Got handle %u\r\n", handle); 00083 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n", 00084 params->minConnectionInterval, params->maxConnectionInterval, params->slaveLatency, params->connectionSupervisionTimeout); 00085 00086 connectionParams.minConnectionInterval = Config::minConnectionInterval; 00087 connectionParams.maxConnectionInterval = Config::maxConnectionInterval; 00088 connectionParams.slaveLatency = Config::slaveLatency; 00089 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout; 00090 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { 00091 DEBUG("failed to update connection paramter\r\n"); 00092 } 00093 00094 advertisingStateLed = 0; 00095 } 00096 00097 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00098 { 00099 DEBUG("Disconnected handle %u!\n\r", handle); 00100 switch(reason) { 00101 case Gap::REMOTE_USER_TERMINATED_CONNECTION: 00102 DEBUG("REASON: REMOTE_USER_TERMINATED_CONNECTION\r\n"); 00103 break; 00104 case Gap::CONN_INTERVAL_UNACCEPTABLE: 00105 DEBUG("REASON: CONN_INTERVAL_UNACCEPTABLE\r\n"); 00106 break; 00107 case Gap::LOCAL_HOST_TERMINATED_CONNECTION: 00108 DEBUG("REASON: LOCAL_HOST_TERMINATED_CONNECTION\r\n"); 00109 break; 00110 default: 00111 DEBUG("REASON: UNKNOWN\r\n"); 00112 break; 00113 } 00114 DEBUG("Restarting the advertising process\n\r"); 00115 ble.startAdvertising(); 00116 advertisingStateLed = 1; 00117 } 00118 00119 void onUpdatesEnabled(Gap::Handle_t handle) 00120 { 00121 DEBUG("Notifications enabled for %d\r\n", handle); 00122 } 00123 00124 void onDataWritten(const GattCharacteristicWriteCBParams *params) 00125 { 00126 // bubble up to services, they will emit callbacks if handle matches 00127 //Nudge::handleDataWritten(params); 00128 } 00129 00130 /** 00131 * Triggered periodically by the 'ticker' interrupt 00132 */ 00133 void periodicCallback(void) 00134 { 00135 oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ 00136 triggerSensorPolling = true; /* Note that the periodicCallback() executes in 00137 * interrupt context, so it is safer to do 00138 * heavy-weight sensor polling from the main 00139 * thread.*/ 00140 } 00141 00142 void bluetoothInit() 00143 { 00144 DEBUG("Bluetooth initialising...\r\n"); 00145 ble.init(); /* Initialise the nRF51822 */ 00146 ble.setDeviceName(Config::deviceName); 00147 //ble.onTimeout(timeoutCallback); 00148 ble.onConnection(connectionCallback); 00149 ble.onDisconnection(disconnectionCallback); 00150 //ble.onDataSent(onDataSent); 00151 ble.onDataWritten(onDataWritten); 00152 ble.onUpdatesEnabled(onUpdatesEnabled); 00153 //ble.onUpdatesDisabled(onUpdatesDisabled); 00154 //ble.onConfirmationReceived(onConfirmationReceived); 00155 00156 // Make sure we use our preferred conn. parameters 00157 connectionParams.minConnectionInterval = Config::minConnectionInterval; 00158 connectionParams.maxConnectionInterval = Config::maxConnectionInterval; 00159 connectionParams.slaveLatency = Config::slaveLatency; 00160 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout; 00161 ble.setPreferredConnectionParams(&connectionParams); 00162 ble.getPreferredConnectionParams(&connectionParams); 00163 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n", 00164 connectionParams.minConnectionInterval, 00165 connectionParams.maxConnectionInterval, 00166 connectionParams.slaveLatency, 00167 connectionParams.connectionSupervisionTimeout); 00168 00169 // Initialise services 00170 BatteryService batteryService(ble); 00171 batteryServicePtr = &batteryService; 00172 DeviceInformationService deviceInfo(ble, "Buddi Ltd.", "BlueBand", "1234567890", "Hardware: 0", "Firmware: 0001", "Build: 0001"); 00173 Nudge::init(ble); 00174 00175 /* setup advertising */ 00176 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00177 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16); 00178 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00179 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00180 ble.setAdvertisingInterval(Config::advertisingInterval); 00181 ble.startAdvertising(); 00182 advertisingStateLed = 1; 00183 DEBUG("Ready. Advertising.\r\n"); 00184 } 00185 00186 int main(void) 00187 { 00188 DEBUG("Initialising Buddi Nudge | Built %s %s\n\r", __DATE__, __TIME__); 00189 00190 for (int i = 0; i < 16; i++) { 00191 uuidlistrev[15 - i] = uuidlist[i]; 00192 } 00193 00194 // TODO: Add battery and dev info to advertising list 00195 bluetoothInit(); 00196 00197 oneSecondLed = 1; 00198 Ticker ticker; 00199 ticker.attach(periodicCallback, 1); 00200 //float acc_data[3]; 00201 //int16_t acc_raw[3]; 00202 /* 00203 DEBUG("MMA8652 Acc = %X\r\n", acc1.getWhoAmI()); 00204 acc1.ReadXYZ(acc_data); 00205 acc1.ReadXYZraw(acc_raw); 00206 DEBUG("MMA8652 Acc: X:%1.3f Y:%1.3f Z:%1.3f (Raw X:%3d Y:%3d Z:%3d)\r\n", acc_data[0], acc_data[1], acc_data[2], acc_raw[0], acc_raw[1], acc_raw[2]); 00207 */ 00208 while (true) { 00209 if (triggerSensorPolling) { 00210 triggerSensorPolling = false; 00211 00212 /* Do blocking calls or whatever is necessary for sensor polling. */ 00213 if (ble.getGapState().connected) { 00214 /* Update the battery measurement */ 00215 //batteryLevel = adc1.read_u16()&0x0FFF) * 3.3/4096; 00216 batteryLevel--; 00217 if (batteryLevel == 1) { 00218 batteryLevel = 100; 00219 } 00220 batteryServicePtr->updateBatteryLevel(batteryLevel); 00221 } 00222 00223 } else { 00224 ble.waitForEvent(); 00225 } 00226 } 00227 }
Generated on Tue Jul 12 2022 15:39:00 by 1.7.2