Application running on nRF51822 PCA10001

Dependencies:   BLE_API MMA8652 nRF51822 mbed-src

Committer:
rosterloh84
Date:
Tue Oct 14 13:04:06 2014 +0000
Revision:
6:2fb6cf3c9047
Parent:
4:630f1560a0f3
Updates before refactoring BuddiService

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 "BuddiService.h"
rosterloh84 6:2fb6cf3c9047 30 #include "BatteryService.h"
rosterloh84 6:2fb6cf3c9047 31 #include "DeviceInformationService.h"
rosterloh84 0:90c13be263a0 32
rosterloh84 0:90c13be263a0 33 /******************************************************************************
rosterloh84 0:90c13be263a0 34 * Module Preprocessor Constants
rosterloh84 0:90c13be263a0 35 *******************************************************************************/
rosterloh84 0:90c13be263a0 36
rosterloh84 0:90c13be263a0 37 /******************************************************************************
rosterloh84 0:90c13be263a0 38 * Module Preprocessor Macros
rosterloh84 0:90c13be263a0 39 *******************************************************************************/
rosterloh84 0:90c13be263a0 40
rosterloh84 0:90c13be263a0 41 /******************************************************************************
rosterloh84 0:90c13be263a0 42 * Module Typedefs
rosterloh84 0:90c13be263a0 43 *******************************************************************************/
rosterloh84 0:90c13be263a0 44
rosterloh84 0:90c13be263a0 45 /******************************************************************************
rosterloh84 0:90c13be263a0 46 * Module Variable Definitions
rosterloh84 0:90c13be263a0 47 *******************************************************************************/
rosterloh84 3:596283411a00 48 //nRF51822n nrf;
rosterloh84 1:1c52fb502f6b 49 BLEDevice ble;
rosterloh84 1:1c52fb502f6b 50 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */
rosterloh84 1:1c52fb502f6b 51 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */
rosterloh84 1:1c52fb502f6b 52 //AnalogIn adc1(p0_0);
rosterloh84 0:90c13be263a0 53 //MMA8652 acc1(I2C_SDA0, I2C_SCL0);
rosterloh84 0:90c13be263a0 54
rosterloh84 3:596283411a00 55 static const uint8_t *uuidlist = Nudge::getServiceUUIDp();
rosterloh84 3:596283411a00 56 static uint8_t uuidlistrev[16];
rosterloh84 3:596283411a00 57 static const char *DEVICE_NAME = "Nudge";
rosterloh84 3:596283411a00 58
rosterloh84 3:596283411a00 59 void bluetoothInit();
rosterloh84 0:90c13be263a0 60
rosterloh84 2:2ddac99c3bde 61 static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that
rosterloh84 2:2ddac99c3bde 62 * polling is necessary. */
rosterloh84 2:2ddac99c3bde 63 static Gap::ConnectionParams_t connectionParams;
rosterloh84 6:2fb6cf3c9047 64
rosterloh84 6:2fb6cf3c9047 65 uint8_t batteryLevel = 100;
rosterloh84 6:2fb6cf3c9047 66 BatteryService *batteryServicePtr = NULL;
rosterloh84 6:2fb6cf3c9047 67
rosterloh84 0:90c13be263a0 68 /******************************************************************************
rosterloh84 0:90c13be263a0 69 * Function Prototypes
rosterloh84 0:90c13be263a0 70 *******************************************************************************/
rosterloh84 0:90c13be263a0 71
rosterloh84 0:90c13be263a0 72 /******************************************************************************
rosterloh84 0:90c13be263a0 73 * Function Definitions
rosterloh84 0:90c13be263a0 74 *******************************************************************************/
rosterloh84 0:90c13be263a0 75 void timeoutCallback(void)
rosterloh84 0:90c13be263a0 76 {
rosterloh84 0:90c13be263a0 77 DEBUG("Timeout!\r\n");
rosterloh84 0:90c13be263a0 78 }
rosterloh84 0:90c13be263a0 79
rosterloh84 4:630f1560a0f3 80 void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params)
rosterloh84 0:90c13be263a0 81 {
rosterloh84 3:596283411a00 82 DEBUG("Connected. Got handle %u\r\n", handle);
rosterloh84 4:630f1560a0f3 83 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rosterloh84 4:630f1560a0f3 84 params->minConnectionInterval, params->maxConnectionInterval, params->slaveLatency, params->connectionSupervisionTimeout);
rosterloh84 4:630f1560a0f3 85
rosterloh84 3:596283411a00 86 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rosterloh84 3:596283411a00 87 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rosterloh84 3:596283411a00 88 connectionParams.slaveLatency = Config::slaveLatency;
rosterloh84 3:596283411a00 89 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rosterloh84 2:2ddac99c3bde 90 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
rosterloh84 2:2ddac99c3bde 91 DEBUG("failed to update connection paramter\r\n");
rosterloh84 2:2ddac99c3bde 92 }
rosterloh84 3:596283411a00 93
rosterloh84 0:90c13be263a0 94 advertisingStateLed = 0;
rosterloh84 0:90c13be263a0 95 }
rosterloh84 0:90c13be263a0 96
rosterloh84 4:630f1560a0f3 97 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rosterloh84 0:90c13be263a0 98 {
rosterloh84 2:2ddac99c3bde 99 DEBUG("Disconnected handle %u!\n\r", handle);
rosterloh84 4:630f1560a0f3 100 switch(reason) {
rosterloh84 4:630f1560a0f3 101 case Gap::REMOTE_USER_TERMINATED_CONNECTION:
rosterloh84 4:630f1560a0f3 102 DEBUG("REASON: REMOTE_USER_TERMINATED_CONNECTION\r\n");
rosterloh84 4:630f1560a0f3 103 break;
rosterloh84 4:630f1560a0f3 104 case Gap::CONN_INTERVAL_UNACCEPTABLE:
rosterloh84 4:630f1560a0f3 105 DEBUG("REASON: CONN_INTERVAL_UNACCEPTABLE\r\n");
rosterloh84 4:630f1560a0f3 106 break;
rosterloh84 4:630f1560a0f3 107 case Gap::LOCAL_HOST_TERMINATED_CONNECTION:
rosterloh84 4:630f1560a0f3 108 DEBUG("REASON: LOCAL_HOST_TERMINATED_CONNECTION\r\n");
rosterloh84 4:630f1560a0f3 109 break;
rosterloh84 4:630f1560a0f3 110 default:
rosterloh84 4:630f1560a0f3 111 DEBUG("REASON: UNKNOWN\r\n");
rosterloh84 4:630f1560a0f3 112 break;
rosterloh84 4:630f1560a0f3 113 }
rosterloh84 2:2ddac99c3bde 114 DEBUG("Restarting the advertising process\n\r");
rosterloh84 0:90c13be263a0 115 ble.startAdvertising();
rosterloh84 0:90c13be263a0 116 advertisingStateLed = 1;
rosterloh84 0:90c13be263a0 117 }
rosterloh84 0:90c13be263a0 118
rosterloh84 3:596283411a00 119 void onUpdatesEnabled(Gap::Handle_t handle)
rosterloh84 3:596283411a00 120 {
rosterloh84 3:596283411a00 121 DEBUG("Notifications enabled for %d\r\n", handle);
rosterloh84 3:596283411a00 122 }
rosterloh84 3:596283411a00 123
rosterloh84 6:2fb6cf3c9047 124 void onDataWritten(const GattCharacteristicWriteCBParams *params)
rosterloh84 1:1c52fb502f6b 125 {
rosterloh84 3:596283411a00 126 // bubble up to services, they will emit callbacks if handle matches
rosterloh84 6:2fb6cf3c9047 127 //Nudge::handleDataWritten(params);
rosterloh84 1:1c52fb502f6b 128 }
rosterloh84 1:1c52fb502f6b 129
rosterloh84 0:90c13be263a0 130 /**
rosterloh84 0:90c13be263a0 131 * Triggered periodically by the 'ticker' interrupt
rosterloh84 0:90c13be263a0 132 */
rosterloh84 0:90c13be263a0 133 void periodicCallback(void)
rosterloh84 0:90c13be263a0 134 {
rosterloh84 0:90c13be263a0 135 oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */
rosterloh84 2:2ddac99c3bde 136 triggerSensorPolling = true; /* Note that the periodicCallback() executes in
rosterloh84 2:2ddac99c3bde 137 * interrupt context, so it is safer to do
rosterloh84 2:2ddac99c3bde 138 * heavy-weight sensor polling from the main
rosterloh84 2:2ddac99c3bde 139 * thread.*/
rosterloh84 0:90c13be263a0 140 }
rosterloh84 0:90c13be263a0 141
rosterloh84 3:596283411a00 142 void bluetoothInit()
rosterloh84 3:596283411a00 143 {
rosterloh84 3:596283411a00 144 DEBUG("Bluetooth initialising...\r\n");
rosterloh84 3:596283411a00 145 ble.init(); /* Initialise the nRF51822 */
rosterloh84 3:596283411a00 146 ble.setDeviceName(Config::deviceName);
rosterloh84 3:596283411a00 147 //ble.onTimeout(timeoutCallback);
rosterloh84 3:596283411a00 148 ble.onConnection(connectionCallback);
rosterloh84 3:596283411a00 149 ble.onDisconnection(disconnectionCallback);
rosterloh84 3:596283411a00 150 //ble.onDataSent(onDataSent);
rosterloh84 3:596283411a00 151 ble.onDataWritten(onDataWritten);
rosterloh84 3:596283411a00 152 ble.onUpdatesEnabled(onUpdatesEnabled);
rosterloh84 3:596283411a00 153 //ble.onUpdatesDisabled(onUpdatesDisabled);
rosterloh84 3:596283411a00 154 //ble.onConfirmationReceived(onConfirmationReceived);
rosterloh84 3:596283411a00 155
rosterloh84 3:596283411a00 156 // Make sure we use our preferred conn. parameters
rosterloh84 3:596283411a00 157 connectionParams.minConnectionInterval = Config::minConnectionInterval;
rosterloh84 3:596283411a00 158 connectionParams.maxConnectionInterval = Config::maxConnectionInterval;
rosterloh84 3:596283411a00 159 connectionParams.slaveLatency = Config::slaveLatency;
rosterloh84 3:596283411a00 160 connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout;
rosterloh84 3:596283411a00 161 ble.setPreferredConnectionParams(&connectionParams);
rosterloh84 3:596283411a00 162 ble.getPreferredConnectionParams(&connectionParams);
rosterloh84 3:596283411a00 163 DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
rosterloh84 3:596283411a00 164 connectionParams.minConnectionInterval,
rosterloh84 3:596283411a00 165 connectionParams.maxConnectionInterval,
rosterloh84 3:596283411a00 166 connectionParams.slaveLatency,
rosterloh84 3:596283411a00 167 connectionParams.connectionSupervisionTimeout);
rosterloh84 3:596283411a00 168
rosterloh84 3:596283411a00 169 // Initialise services
rosterloh84 6:2fb6cf3c9047 170 BatteryService batteryService(ble);
rosterloh84 6:2fb6cf3c9047 171 batteryServicePtr = &batteryService;
rosterloh84 6:2fb6cf3c9047 172 DeviceInformationService deviceInfo(ble, "Buddi Ltd.", "BlueBand", "1234567890", "Hardware: 0", "Firmware: 0001", "Build: 0001");
rosterloh84 3:596283411a00 173 Nudge::init(ble);
rosterloh84 3:596283411a00 174
rosterloh84 3:596283411a00 175 /* setup advertising */
rosterloh84 3:596283411a00 176 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rosterloh84 3:596283411a00 177 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16);
rosterloh84 3:596283411a00 178 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rosterloh84 3:596283411a00 179 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rosterloh84 3:596283411a00 180 ble.setAdvertisingInterval(Config::advertisingInterval);
rosterloh84 3:596283411a00 181 ble.startAdvertising();
rosterloh84 3:596283411a00 182 advertisingStateLed = 1;
rosterloh84 3:596283411a00 183 DEBUG("Ready. Advertising.\r\n");
rosterloh84 3:596283411a00 184 }
rosterloh84 3:596283411a00 185
rosterloh84 0:90c13be263a0 186 int main(void)
rosterloh84 0:90c13be263a0 187 {
rosterloh84 3:596283411a00 188 DEBUG("Initialising Buddi Nudge | Built %s %s\n\r", __DATE__, __TIME__);
rosterloh84 3:596283411a00 189
rosterloh84 3:596283411a00 190 for (int i = 0; i < 16; i++) {
rosterloh84 3:596283411a00 191 uuidlistrev[15 - i] = uuidlist[i];
rosterloh84 3:596283411a00 192 }
rosterloh84 3:596283411a00 193
rosterloh84 3:596283411a00 194 // TODO: Add battery and dev info to advertising list
rosterloh84 3:596283411a00 195 bluetoothInit();
rosterloh84 3:596283411a00 196
rosterloh84 0:90c13be263a0 197 oneSecondLed = 1;
rosterloh84 0:90c13be263a0 198 Ticker ticker;
rosterloh84 0:90c13be263a0 199 ticker.attach(periodicCallback, 1);
rosterloh84 3:596283411a00 200 //float acc_data[3];
rosterloh84 3:596283411a00 201 //int16_t acc_raw[3];
rosterloh84 0:90c13be263a0 202 /*
rosterloh84 0:90c13be263a0 203 DEBUG("MMA8652 Acc = %X\r\n", acc1.getWhoAmI());
rosterloh84 0:90c13be263a0 204 acc1.ReadXYZ(acc_data);
rosterloh84 0:90c13be263a0 205 acc1.ReadXYZraw(acc_raw);
rosterloh84 0:90c13be263a0 206 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 207 */
rosterloh84 0:90c13be263a0 208 while (true) {
rosterloh84 2:2ddac99c3bde 209 if (triggerSensorPolling) {
rosterloh84 2:2ddac99c3bde 210 triggerSensorPolling = false;
rosterloh84 2:2ddac99c3bde 211
rosterloh84 2:2ddac99c3bde 212 /* Do blocking calls or whatever is necessary for sensor polling. */
rosterloh84 2:2ddac99c3bde 213 if (ble.getGapState().connected) {
rosterloh84 2:2ddac99c3bde 214 /* Update the battery measurement */
rosterloh84 6:2fb6cf3c9047 215 //batteryLevel = adc1.read_u16()&0x0FFF) * 3.3/4096;
rosterloh84 2:2ddac99c3bde 216 batteryLevel--;
rosterloh84 2:2ddac99c3bde 217 if (batteryLevel == 1) {
rosterloh84 2:2ddac99c3bde 218 batteryLevel = 100;
rosterloh84 2:2ddac99c3bde 219 }
rosterloh84 6:2fb6cf3c9047 220 batteryServicePtr->updateBatteryLevel(batteryLevel);
rosterloh84 2:2ddac99c3bde 221 }
rosterloh84 2:2ddac99c3bde 222
rosterloh84 2:2ddac99c3bde 223 } else {
rosterloh84 2:2ddac99c3bde 224 ble.waitForEvent();
rosterloh84 2:2ddac99c3bde 225 }
rosterloh84 0:90c13be263a0 226 }
rosterloh84 0:90c13be263a0 227 }