Application running on nRF51822 PCA10001
Dependencies: BLE_API MMA8652 nRF51822 mbed-src
main.cpp@3:596283411a00, 2014-08-29 (annotated)
- Committer:
- rosterloh84
- Date:
- Fri Aug 29 12:36:01 2014 +0000
- Revision:
- 3:596283411a00
- Parent:
- 2:2ddac99c3bde
- Child:
- 4:630f1560a0f3
Started seperating out services and refactored due to library updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rosterloh84 | 0:90c13be263a0 | 1 | /******************************************************************************* |
rosterloh84 | 1:1c52fb502f6b | 2 | * Title : System Initialisation |
rosterloh84 | 0:90c13be263a0 | 3 | * Filename : main.cpp |
rosterloh84 | 0:90c13be263a0 | 4 | * Author : Richard Osterloh |
rosterloh84 | 0:90c13be263a0 | 5 | * Origin Date : 22/07/2014 |
rosterloh84 | 0:90c13be263a0 | 6 | * Version : 1.0.0 |
rosterloh84 | 0:90c13be263a0 | 7 | * Compiler : mbed compiler |
rosterloh84 | 0:90c13be263a0 | 8 | * Target : Nordic nRF51822 |
rosterloh84 | 0:90c13be263a0 | 9 | * Notes : None |
rosterloh84 | 0:90c13be263a0 | 10 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 11 | /*************** MODULE REVISION LOG ****************************************** |
rosterloh84 | 0:90c13be263a0 | 12 | * |
rosterloh84 | 0:90c13be263a0 | 13 | * Date Software Version Initials Description |
rosterloh84 | 0:90c13be263a0 | 14 | * 22/07/2014 1.0.0 RO Module Created. |
rosterloh84 | 0:90c13be263a0 | 15 | * |
rosterloh84 | 0:90c13be263a0 | 16 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 17 | /** \file main.cpp |
rosterloh84 | 2:2ddac99c3bde | 18 | * \brief This module contains the main function and setup |
rosterloh84 | 0:90c13be263a0 | 19 | */ |
rosterloh84 | 0:90c13be263a0 | 20 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 21 | * Includes |
rosterloh84 | 0:90c13be263a0 | 22 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 23 | #include "mbed.h" |
rosterloh84 | 3:596283411a00 | 24 | #include "Logger.h" |
rosterloh84 | 3:596283411a00 | 25 | #include "Configuration.h" |
rosterloh84 | 0:90c13be263a0 | 26 | #include "BLEDevice.h" |
rosterloh84 | 3:596283411a00 | 27 | //#include "nRF51822n.h" |
rosterloh84 | 0:90c13be263a0 | 28 | //#include "MMA8652.h" |
rosterloh84 | 3:596283411a00 | 29 | #include "DevInfoService.h" |
rosterloh84 | 3:596283411a00 | 30 | #include "BuddiService.h" |
rosterloh84 | 0:90c13be263a0 | 31 | |
rosterloh84 | 0:90c13be263a0 | 32 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 33 | * Module Preprocessor Constants |
rosterloh84 | 0:90c13be263a0 | 34 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 35 | |
rosterloh84 | 0:90c13be263a0 | 36 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 37 | * Module Preprocessor Macros |
rosterloh84 | 0:90c13be263a0 | 38 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 39 | |
rosterloh84 | 0:90c13be263a0 | 40 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 41 | * Module Typedefs |
rosterloh84 | 0:90c13be263a0 | 42 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 43 | |
rosterloh84 | 0:90c13be263a0 | 44 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 45 | * Module Variable Definitions |
rosterloh84 | 0:90c13be263a0 | 46 | *******************************************************************************/ |
rosterloh84 | 3:596283411a00 | 47 | //nRF51822n nrf; |
rosterloh84 | 1:1c52fb502f6b | 48 | BLEDevice ble; |
rosterloh84 | 1:1c52fb502f6b | 49 | DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */ |
rosterloh84 | 1:1c52fb502f6b | 50 | DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */ |
rosterloh84 | 1:1c52fb502f6b | 51 | //AnalogIn adc1(p0_0); |
rosterloh84 | 0:90c13be263a0 | 52 | //MMA8652 acc1(I2C_SDA0, I2C_SCL0); |
rosterloh84 | 0:90c13be263a0 | 53 | |
rosterloh84 | 3:596283411a00 | 54 | static const uint8_t *uuidlist = Nudge::getServiceUUIDp(); |
rosterloh84 | 3:596283411a00 | 55 | static uint8_t uuidlistrev[16]; |
rosterloh84 | 3:596283411a00 | 56 | static const char *DEVICE_NAME = "Nudge"; |
rosterloh84 | 3:596283411a00 | 57 | |
rosterloh84 | 3:596283411a00 | 58 | void bluetoothInit(); |
rosterloh84 | 0:90c13be263a0 | 59 | |
rosterloh84 | 0:90c13be263a0 | 60 | // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml |
rosterloh84 | 0:90c13be263a0 | 61 | static uint8_t batteryLevel = 100; |
rosterloh84 | 0:90c13be263a0 | 62 | GattCharacteristic batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), |
rosterloh84 | 0:90c13be263a0 | 63 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
rosterloh84 | 0:90c13be263a0 | 64 | GattCharacteristic *batteryChars[] = {&batteryPercentage }; |
rosterloh84 | 0:90c13be263a0 | 65 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *)); |
rosterloh84 | 0:90c13be263a0 | 66 | |
rosterloh84 | 3:596283411a00 | 67 | //static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE, |
rosterloh84 | 3:596283411a00 | 68 | // GattService::UUID_BATTERY_SERVICE}; |
rosterloh84 | 1:1c52fb502f6b | 69 | //static const unit8_t uuid128_list[] = {(const uint8_t *)buddi_service_uuid_rev, |
rosterloh84 | 1:1c52fb502f6b | 70 | // (const uint8_t *)buddi_service_uuid_rev, |
rosterloh84 | 1:1c52fb502f6b | 71 | // (const uint8_t *)buddi_service_uuid_rev}; |
rosterloh84 | 0:90c13be263a0 | 72 | |
rosterloh84 | 2:2ddac99c3bde | 73 | static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that |
rosterloh84 | 2:2ddac99c3bde | 74 | * polling is necessary. */ |
rosterloh84 | 2:2ddac99c3bde | 75 | static Gap::ConnectionParams_t connectionParams; |
rosterloh84 | 1:1c52fb502f6b | 76 | |
rosterloh84 | 0:90c13be263a0 | 77 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 78 | * Function Prototypes |
rosterloh84 | 0:90c13be263a0 | 79 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 80 | |
rosterloh84 | 0:90c13be263a0 | 81 | /****************************************************************************** |
rosterloh84 | 0:90c13be263a0 | 82 | * Function Definitions |
rosterloh84 | 0:90c13be263a0 | 83 | *******************************************************************************/ |
rosterloh84 | 0:90c13be263a0 | 84 | void timeoutCallback(void) |
rosterloh84 | 0:90c13be263a0 | 85 | { |
rosterloh84 | 0:90c13be263a0 | 86 | DEBUG("Timeout!\r\n"); |
rosterloh84 | 0:90c13be263a0 | 87 | } |
rosterloh84 | 0:90c13be263a0 | 88 | |
rosterloh84 | 2:2ddac99c3bde | 89 | void connectionCallback(Gap::Handle_t handle) |
rosterloh84 | 0:90c13be263a0 | 90 | { |
rosterloh84 | 3:596283411a00 | 91 | DEBUG("Connected. Got handle %u\r\n", handle); |
rosterloh84 | 2:2ddac99c3bde | 92 | |
rosterloh84 | 3:596283411a00 | 93 | connectionParams.minConnectionInterval = Config::minConnectionInterval; |
rosterloh84 | 3:596283411a00 | 94 | connectionParams.maxConnectionInterval = Config::maxConnectionInterval; |
rosterloh84 | 3:596283411a00 | 95 | connectionParams.slaveLatency = Config::slaveLatency; |
rosterloh84 | 3:596283411a00 | 96 | connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout; |
rosterloh84 | 2:2ddac99c3bde | 97 | if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { |
rosterloh84 | 2:2ddac99c3bde | 98 | DEBUG("failed to update connection paramter\r\n"); |
rosterloh84 | 2:2ddac99c3bde | 99 | } |
rosterloh84 | 3:596283411a00 | 100 | |
rosterloh84 | 0:90c13be263a0 | 101 | advertisingStateLed = 0; |
rosterloh84 | 0:90c13be263a0 | 102 | } |
rosterloh84 | 0:90c13be263a0 | 103 | |
rosterloh84 | 2:2ddac99c3bde | 104 | void disconnectionCallback(Gap::Handle_t handle) |
rosterloh84 | 0:90c13be263a0 | 105 | { |
rosterloh84 | 2:2ddac99c3bde | 106 | DEBUG("Disconnected handle %u!\n\r", handle); |
rosterloh84 | 2:2ddac99c3bde | 107 | DEBUG("Restarting the advertising process\n\r"); |
rosterloh84 | 0:90c13be263a0 | 108 | ble.startAdvertising(); |
rosterloh84 | 0:90c13be263a0 | 109 | advertisingStateLed = 1; |
rosterloh84 | 0:90c13be263a0 | 110 | } |
rosterloh84 | 0:90c13be263a0 | 111 | |
rosterloh84 | 3:596283411a00 | 112 | void onUpdatesEnabled(Gap::Handle_t handle) |
rosterloh84 | 3:596283411a00 | 113 | { |
rosterloh84 | 3:596283411a00 | 114 | DEBUG("Notifications enabled for %d\r\n", handle); |
rosterloh84 | 3:596283411a00 | 115 | } |
rosterloh84 | 3:596283411a00 | 116 | |
rosterloh84 | 1:1c52fb502f6b | 117 | void onDataWritten(uint16_t charHandle) |
rosterloh84 | 1:1c52fb502f6b | 118 | { |
rosterloh84 | 3:596283411a00 | 119 | // bubble up to services, they will emit callbacks if handle matches |
rosterloh84 | 3:596283411a00 | 120 | Nudge::handleDataWritten(charHandle); |
rosterloh84 | 1:1c52fb502f6b | 121 | } |
rosterloh84 | 1:1c52fb502f6b | 122 | |
rosterloh84 | 0:90c13be263a0 | 123 | /** |
rosterloh84 | 0:90c13be263a0 | 124 | * Triggered periodically by the 'ticker' interrupt |
rosterloh84 | 0:90c13be263a0 | 125 | */ |
rosterloh84 | 0:90c13be263a0 | 126 | void periodicCallback(void) |
rosterloh84 | 0:90c13be263a0 | 127 | { |
rosterloh84 | 0:90c13be263a0 | 128 | oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ |
rosterloh84 | 2:2ddac99c3bde | 129 | triggerSensorPolling = true; /* Note that the periodicCallback() executes in |
rosterloh84 | 2:2ddac99c3bde | 130 | * interrupt context, so it is safer to do |
rosterloh84 | 2:2ddac99c3bde | 131 | * heavy-weight sensor polling from the main |
rosterloh84 | 2:2ddac99c3bde | 132 | * thread.*/ |
rosterloh84 | 0:90c13be263a0 | 133 | } |
rosterloh84 | 0:90c13be263a0 | 134 | |
rosterloh84 | 3:596283411a00 | 135 | void bluetoothInit() |
rosterloh84 | 3:596283411a00 | 136 | { |
rosterloh84 | 3:596283411a00 | 137 | DEBUG("Bluetooth initialising...\r\n"); |
rosterloh84 | 3:596283411a00 | 138 | ble.init(); /* Initialise the nRF51822 */ |
rosterloh84 | 3:596283411a00 | 139 | ble.setDeviceName(Config::deviceName); |
rosterloh84 | 3:596283411a00 | 140 | //ble.onTimeout(timeoutCallback); |
rosterloh84 | 3:596283411a00 | 141 | ble.onConnection(connectionCallback); |
rosterloh84 | 3:596283411a00 | 142 | ble.onDisconnection(disconnectionCallback); |
rosterloh84 | 3:596283411a00 | 143 | //ble.onDataSent(onDataSent); |
rosterloh84 | 3:596283411a00 | 144 | ble.onDataWritten(onDataWritten); |
rosterloh84 | 3:596283411a00 | 145 | ble.onUpdatesEnabled(onUpdatesEnabled); |
rosterloh84 | 3:596283411a00 | 146 | //ble.onUpdatesDisabled(onUpdatesDisabled); |
rosterloh84 | 3:596283411a00 | 147 | //ble.onConfirmationReceived(onConfirmationReceived); |
rosterloh84 | 3:596283411a00 | 148 | |
rosterloh84 | 3:596283411a00 | 149 | // Make sure we use our preferred conn. parameters |
rosterloh84 | 3:596283411a00 | 150 | connectionParams.minConnectionInterval = Config::minConnectionInterval; |
rosterloh84 | 3:596283411a00 | 151 | connectionParams.maxConnectionInterval = Config::maxConnectionInterval; |
rosterloh84 | 3:596283411a00 | 152 | connectionParams.slaveLatency = Config::slaveLatency; |
rosterloh84 | 3:596283411a00 | 153 | connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout; |
rosterloh84 | 3:596283411a00 | 154 | ble.setPreferredConnectionParams(&connectionParams); |
rosterloh84 | 3:596283411a00 | 155 | ble.getPreferredConnectionParams(&connectionParams); |
rosterloh84 | 3:596283411a00 | 156 | DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n", |
rosterloh84 | 3:596283411a00 | 157 | connectionParams.minConnectionInterval, |
rosterloh84 | 3:596283411a00 | 158 | connectionParams.maxConnectionInterval, |
rosterloh84 | 3:596283411a00 | 159 | connectionParams.slaveLatency, |
rosterloh84 | 3:596283411a00 | 160 | connectionParams.connectionSupervisionTimeout); |
rosterloh84 | 3:596283411a00 | 161 | |
rosterloh84 | 3:596283411a00 | 162 | // Initialise services |
rosterloh84 | 3:596283411a00 | 163 | //ble.addService(devInfoService); |
rosterloh84 | 3:596283411a00 | 164 | DevInfo::init(ble); |
rosterloh84 | 3:596283411a00 | 165 | ble.addService(batteryService); |
rosterloh84 | 3:596283411a00 | 166 | //ble.addService(buddiService); |
rosterloh84 | 3:596283411a00 | 167 | Nudge::init(ble); |
rosterloh84 | 3:596283411a00 | 168 | |
rosterloh84 | 3:596283411a00 | 169 | /* setup advertising */ |
rosterloh84 | 3:596283411a00 | 170 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rosterloh84 | 3:596283411a00 | 171 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16); |
rosterloh84 | 3:596283411a00 | 172 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rosterloh84 | 3:596283411a00 | 173 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rosterloh84 | 3:596283411a00 | 174 | ble.setAdvertisingInterval(Config::advertisingInterval); |
rosterloh84 | 3:596283411a00 | 175 | ble.startAdvertising(); |
rosterloh84 | 3:596283411a00 | 176 | advertisingStateLed = 1; |
rosterloh84 | 3:596283411a00 | 177 | DEBUG("Ready. Advertising.\r\n"); |
rosterloh84 | 3:596283411a00 | 178 | } |
rosterloh84 | 3:596283411a00 | 179 | |
rosterloh84 | 0:90c13be263a0 | 180 | int main(void) |
rosterloh84 | 0:90c13be263a0 | 181 | { |
rosterloh84 | 3:596283411a00 | 182 | DEBUG("Initialising Buddi Nudge | Built %s %s\n\r", __DATE__, __TIME__); |
rosterloh84 | 3:596283411a00 | 183 | |
rosterloh84 | 3:596283411a00 | 184 | for (int i = 0; i < 16; i++) { |
rosterloh84 | 3:596283411a00 | 185 | uuidlistrev[15 - i] = uuidlist[i]; |
rosterloh84 | 3:596283411a00 | 186 | } |
rosterloh84 | 3:596283411a00 | 187 | |
rosterloh84 | 3:596283411a00 | 188 | // TODO: Add battery and dev info to advertising list |
rosterloh84 | 3:596283411a00 | 189 | bluetoothInit(); |
rosterloh84 | 3:596283411a00 | 190 | |
rosterloh84 | 0:90c13be263a0 | 191 | oneSecondLed = 1; |
rosterloh84 | 0:90c13be263a0 | 192 | Ticker ticker; |
rosterloh84 | 0:90c13be263a0 | 193 | ticker.attach(periodicCallback, 1); |
rosterloh84 | 3:596283411a00 | 194 | //float acc_data[3]; |
rosterloh84 | 3:596283411a00 | 195 | //int16_t acc_raw[3]; |
rosterloh84 | 0:90c13be263a0 | 196 | /* |
rosterloh84 | 0:90c13be263a0 | 197 | DEBUG("MMA8652 Acc = %X\r\n", acc1.getWhoAmI()); |
rosterloh84 | 0:90c13be263a0 | 198 | acc1.ReadXYZ(acc_data); |
rosterloh84 | 0:90c13be263a0 | 199 | acc1.ReadXYZraw(acc_raw); |
rosterloh84 | 0:90c13be263a0 | 200 | 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]); |
rosterloh84 | 0:90c13be263a0 | 201 | */ |
rosterloh84 | 0:90c13be263a0 | 202 | while (true) { |
rosterloh84 | 2:2ddac99c3bde | 203 | if (triggerSensorPolling) { |
rosterloh84 | 2:2ddac99c3bde | 204 | triggerSensorPolling = false; |
rosterloh84 | 2:2ddac99c3bde | 205 | |
rosterloh84 | 2:2ddac99c3bde | 206 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
rosterloh84 | 2:2ddac99c3bde | 207 | if (ble.getGapState().connected) { |
rosterloh84 | 2:2ddac99c3bde | 208 | /* Update the battery measurement */ |
rosterloh84 | 2:2ddac99c3bde | 209 | //batteryLevel = adc1.read_u16()&0x0FFF) * 3.3/4096; |
rosterloh84 | 2:2ddac99c3bde | 210 | batteryLevel--; |
rosterloh84 | 2:2ddac99c3bde | 211 | if (batteryLevel == 1) { |
rosterloh84 | 2:2ddac99c3bde | 212 | batteryLevel = 100; |
rosterloh84 | 2:2ddac99c3bde | 213 | } |
rosterloh84 | 2:2ddac99c3bde | 214 | //ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel)); |
rosterloh84 | 2:2ddac99c3bde | 215 | } |
rosterloh84 | 2:2ddac99c3bde | 216 | |
rosterloh84 | 2:2ddac99c3bde | 217 | } else { |
rosterloh84 | 2:2ddac99c3bde | 218 | ble.waitForEvent(); |
rosterloh84 | 2:2ddac99c3bde | 219 | } |
rosterloh84 | 0:90c13be263a0 | 220 | } |
rosterloh84 | 0:90c13be263a0 | 221 | } |