Application running on nRF51822 PCA10001

Dependencies:   BLE_API MMA8652 nRF51822 mbed-src

Committer:
rosterloh84
Date:
Wed Sep 03 07:04:54 2014 +0000
Revision:
4:630f1560a0f3
Parent:
3:596283411a00
Child:
6:2fb6cf3c9047
Library updates

Who changed what in which revision?

UserRevisionLine numberNew 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 4:630f1560a0f3 89 void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params)
rosterloh84 0:90c13be263a0 90 {
rosterloh84 3:596283411a00 91 DEBUG("Connected. Got handle %u\r\n", handle);
rosterloh84 4:630f1560a0f3 92 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rosterloh84 4:630f1560a0f3 93 params->minConnectionInterval, params->maxConnectionInterval, params->slaveLatency, params->connectionSupervisionTimeout);
rosterloh84 4:630f1560a0f3 94
rosterloh84 3:596283411a00 95 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rosterloh84 3:596283411a00 96 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rosterloh84 3:596283411a00 97 connectionParams.slaveLatency = Config::slaveLatency;
rosterloh84 3:596283411a00 98 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rosterloh84 2:2ddac99c3bde 99 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
rosterloh84 2:2ddac99c3bde 100 DEBUG("failed to update connection paramter\r\n");
rosterloh84 2:2ddac99c3bde 101 }
rosterloh84 3:596283411a00 102
rosterloh84 0:90c13be263a0 103 advertisingStateLed = 0;
rosterloh84 0:90c13be263a0 104 }
rosterloh84 0:90c13be263a0 105
rosterloh84 4:630f1560a0f3 106 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rosterloh84 0:90c13be263a0 107 {
rosterloh84 2:2ddac99c3bde 108 DEBUG("Disconnected handle %u!\n\r", handle);
rosterloh84 4:630f1560a0f3 109 switch(reason) {
rosterloh84 4:630f1560a0f3 110 case Gap::REMOTE_USER_TERMINATED_CONNECTION:
rosterloh84 4:630f1560a0f3 111 DEBUG("REASON: REMOTE_USER_TERMINATED_CONNECTION\r\n");
rosterloh84 4:630f1560a0f3 112 break;
rosterloh84 4:630f1560a0f3 113 case Gap::CONN_INTERVAL_UNACCEPTABLE:
rosterloh84 4:630f1560a0f3 114 DEBUG("REASON: CONN_INTERVAL_UNACCEPTABLE\r\n");
rosterloh84 4:630f1560a0f3 115 break;
rosterloh84 4:630f1560a0f3 116 case Gap::LOCAL_HOST_TERMINATED_CONNECTION:
rosterloh84 4:630f1560a0f3 117 DEBUG("REASON: LOCAL_HOST_TERMINATED_CONNECTION\r\n");
rosterloh84 4:630f1560a0f3 118 break;
rosterloh84 4:630f1560a0f3 119 default:
rosterloh84 4:630f1560a0f3 120 DEBUG("REASON: UNKNOWN\r\n");
rosterloh84 4:630f1560a0f3 121 break;
rosterloh84 4:630f1560a0f3 122 }
rosterloh84 2:2ddac99c3bde 123 DEBUG("Restarting the advertising process\n\r");
rosterloh84 0:90c13be263a0 124 ble.startAdvertising();
rosterloh84 0:90c13be263a0 125 advertisingStateLed = 1;
rosterloh84 0:90c13be263a0 126 }
rosterloh84 0:90c13be263a0 127
rosterloh84 3:596283411a00 128 void onUpdatesEnabled(Gap::Handle_t handle)
rosterloh84 3:596283411a00 129 {
rosterloh84 3:596283411a00 130 DEBUG("Notifications enabled for %d\r\n", handle);
rosterloh84 3:596283411a00 131 }
rosterloh84 3:596283411a00 132
rosterloh84 4:630f1560a0f3 133 void onDataWritten(uint16_t charHandle, const GattCharacteristicWriteCBParams *params)
rosterloh84 1:1c52fb502f6b 134 {
rosterloh84 3:596283411a00 135 // bubble up to services, they will emit callbacks if handle matches
rosterloh84 4:630f1560a0f3 136 Nudge::handleDataWritten(charHandle, params);
rosterloh84 1:1c52fb502f6b 137 }
rosterloh84 1:1c52fb502f6b 138
rosterloh84 0:90c13be263a0 139 /**
rosterloh84 0:90c13be263a0 140 * Triggered periodically by the 'ticker' interrupt
rosterloh84 0:90c13be263a0 141 */
rosterloh84 0:90c13be263a0 142 void periodicCallback(void)
rosterloh84 0:90c13be263a0 143 {
rosterloh84 0:90c13be263a0 144 oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */
rosterloh84 2:2ddac99c3bde 145 triggerSensorPolling = true; /* Note that the periodicCallback() executes in
rosterloh84 2:2ddac99c3bde 146 * interrupt context, so it is safer to do
rosterloh84 2:2ddac99c3bde 147 * heavy-weight sensor polling from the main
rosterloh84 2:2ddac99c3bde 148 * thread.*/
rosterloh84 0:90c13be263a0 149 }
rosterloh84 0:90c13be263a0 150
rosterloh84 3:596283411a00 151 void bluetoothInit()
rosterloh84 3:596283411a00 152 {
rosterloh84 3:596283411a00 153 DEBUG("Bluetooth initialising...\r\n");
rosterloh84 3:596283411a00 154 ble.init(); /* Initialise the nRF51822 */
rosterloh84 3:596283411a00 155 ble.setDeviceName(Config::deviceName);
rosterloh84 3:596283411a00 156 //ble.onTimeout(timeoutCallback);
rosterloh84 3:596283411a00 157 ble.onConnection(connectionCallback);
rosterloh84 3:596283411a00 158 ble.onDisconnection(disconnectionCallback);
rosterloh84 3:596283411a00 159 //ble.onDataSent(onDataSent);
rosterloh84 3:596283411a00 160 ble.onDataWritten(onDataWritten);
rosterloh84 3:596283411a00 161 ble.onUpdatesEnabled(onUpdatesEnabled);
rosterloh84 3:596283411a00 162 //ble.onUpdatesDisabled(onUpdatesDisabled);
rosterloh84 3:596283411a00 163 //ble.onConfirmationReceived(onConfirmationReceived);
rosterloh84 3:596283411a00 164
rosterloh84 3:596283411a00 165 // Make sure we use our preferred conn. parameters
rosterloh84 3:596283411a00 166 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rosterloh84 3:596283411a00 167 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rosterloh84 3:596283411a00 168 connectionParams.slaveLatency = Config::slaveLatency;
rosterloh84 3:596283411a00 169 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rosterloh84 3:596283411a00 170 ble.setPreferredConnectionParams(&connectionParams);
rosterloh84 3:596283411a00 171 ble.getPreferredConnectionParams(&connectionParams);
rosterloh84 3:596283411a00 172 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rosterloh84 3:596283411a00 173 connectionParams.minConnectionInterval,
rosterloh84 3:596283411a00 174 connectionParams.maxConnectionInterval,
rosterloh84 3:596283411a00 175 connectionParams.slaveLatency,
rosterloh84 3:596283411a00 176 connectionParams.connectionSupervisionTimeout);
rosterloh84 3:596283411a00 177
rosterloh84 3:596283411a00 178 // Initialise services
rosterloh84 3:596283411a00 179 //ble.addService(devInfoService);
rosterloh84 3:596283411a00 180 DevInfo::init(ble);
rosterloh84 3:596283411a00 181 ble.addService(batteryService);
rosterloh84 3:596283411a00 182 //ble.addService(buddiService);
rosterloh84 3:596283411a00 183 Nudge::init(ble);
rosterloh84 3:596283411a00 184
rosterloh84 3:596283411a00 185 /* setup advertising */
rosterloh84 3:596283411a00 186 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rosterloh84 3:596283411a00 187 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16);
rosterloh84 3:596283411a00 188 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rosterloh84 3:596283411a00 189 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rosterloh84 3:596283411a00 190 ble.setAdvertisingInterval(Config::advertisingInterval);
rosterloh84 3:596283411a00 191 ble.startAdvertising();
rosterloh84 3:596283411a00 192 advertisingStateLed = 1;
rosterloh84 3:596283411a00 193 DEBUG("Ready. Advertising.\r\n");
rosterloh84 3:596283411a00 194 }
rosterloh84 3:596283411a00 195
rosterloh84 0:90c13be263a0 196 int main(void)
rosterloh84 0:90c13be263a0 197 {
rosterloh84 3:596283411a00 198 DEBUG("Initialising Buddi Nudge | Built %s %s\n\r", __DATE__, __TIME__);
rosterloh84 3:596283411a00 199
rosterloh84 3:596283411a00 200 for (int i = 0; i < 16; i++) {
rosterloh84 3:596283411a00 201 uuidlistrev[15 - i] = uuidlist[i];
rosterloh84 3:596283411a00 202 }
rosterloh84 3:596283411a00 203
rosterloh84 3:596283411a00 204 // TODO: Add battery and dev info to advertising list
rosterloh84 3:596283411a00 205 bluetoothInit();
rosterloh84 3:596283411a00 206
rosterloh84 0:90c13be263a0 207 oneSecondLed = 1;
rosterloh84 0:90c13be263a0 208 Ticker ticker;
rosterloh84 0:90c13be263a0 209 ticker.attach(periodicCallback, 1);
rosterloh84 3:596283411a00 210 //float acc_data[3];
rosterloh84 3:596283411a00 211 //int16_t acc_raw[3];
rosterloh84 0:90c13be263a0 212 /*
rosterloh84 0:90c13be263a0 213 DEBUG("MMA8652 Acc = %X\r\n", acc1.getWhoAmI());
rosterloh84 0:90c13be263a0 214 acc1.ReadXYZ(acc_data);
rosterloh84 0:90c13be263a0 215 acc1.ReadXYZraw(acc_raw);
rosterloh84 0:90c13be263a0 216 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 217 */
rosterloh84 0:90c13be263a0 218 while (true) {
rosterloh84 2:2ddac99c3bde 219 if (triggerSensorPolling) {
rosterloh84 2:2ddac99c3bde 220 triggerSensorPolling = false;
rosterloh84 2:2ddac99c3bde 221
rosterloh84 2:2ddac99c3bde 222 /* Do blocking calls or whatever is necessary for sensor polling. */
rosterloh84 2:2ddac99c3bde 223 if (ble.getGapState().connected) {
rosterloh84 2:2ddac99c3bde 224 /* Update the battery measurement */
rosterloh84 2:2ddac99c3bde 225 //batteryLevel = adc1.read_u16()&0x0FFF) * 3.3/4096;
rosterloh84 2:2ddac99c3bde 226 batteryLevel--;
rosterloh84 2:2ddac99c3bde 227 if (batteryLevel == 1) {
rosterloh84 2:2ddac99c3bde 228 batteryLevel = 100;
rosterloh84 2:2ddac99c3bde 229 }
rosterloh84 2:2ddac99c3bde 230 //ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel));
rosterloh84 2:2ddac99c3bde 231 }
rosterloh84 2:2ddac99c3bde 232
rosterloh84 2:2ddac99c3bde 233 } else {
rosterloh84 2:2ddac99c3bde 234 ble.waitForEvent();
rosterloh84 2:2ddac99c3bde 235 }
rosterloh84 0:90c13be263a0 236 }
rosterloh84 0:90c13be263a0 237 }