![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Using the MBED BLE library and Nordic Puck library this is a simple scoring application using Bluetooth LE. It monitors three analog inputs and triggers on reception of a pulse on any one recording data for a short period on all three. This is then published via BLE characteristics. It's a demonstrator for a new UI dev toolkit that is under development.
Fork of Example_Puck_BLE by
main.cpp@10:a2ba0cef85aa, 2014-08-26 (annotated)
- Committer:
- Bobty
- Date:
- Tue Aug 26 14:13:31 2014 +0000
- Revision:
- 10:a2ba0cef85aa
- Parent:
- 9:f2b2ebc6d908
Working with 3 channels
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aleksanb | 0:8d7583961274 | 1 | /** |
Bobty | 1:1a59b4810261 | 2 | Scoring Device - for generic game scoring |
Bobty | 1:1a59b4810261 | 3 | Using Puck BLE MBED library from Nordic |
Bobty | 1:1a59b4810261 | 4 | Copyright (C) Nodule.io 2014 |
Bobty | 1:1a59b4810261 | 5 | |
aleksanb | 0:8d7583961274 | 6 | */ |
aleksanb | 0:8d7583961274 | 7 | |
Bobty | 4:cc164ecf6a36 | 8 | #define LOG_LEVEL_DEBUG |
aleksanb | 0:8d7583961274 | 9 | #include "Puck.h" |
Bobty | 3:a155da1cbde3 | 10 | #include "SampleChannel.h" |
aleksanb | 0:8d7583961274 | 11 | |
Bobty | 10:a2ba0cef85aa | 12 | // This is a singleton for the Nordic Puck library which helps with some BLE init, etc. |
aleksanb | 0:8d7583961274 | 13 | Puck* puck = &Puck::getPuck(); |
aleksanb | 0:8d7583961274 | 14 | |
Bobty | 1:1a59b4810261 | 15 | // Gatt characteristic and service UUIDs |
Bobty | 2:e400fd4f501b | 16 | const UUID SCORING_GATT_SERVICE = stringToUUID("nod.score1.serv "); |
Bobty | 2:e400fd4f501b | 17 | const UUID THRESHOLD_GATT_CHARACTERISTIC = stringToUUID("nod.score1.thres"); |
Bobty | 2:e400fd4f501b | 18 | const UUID DIVISOR_GATT_CHARACTERISTIC = stringToUUID("nod.score1.div "); |
Bobty | 2:e400fd4f501b | 19 | const UUID INTERVAL_US_GATT_CHARACTERISTIC = stringToUUID("nod.score1.intus"); |
Bobty | 1:1a59b4810261 | 20 | |
Bobty | 10:a2ba0cef85aa | 21 | // Three channels for scoring |
Bobty | 9:f2b2ebc6d908 | 22 | const int NUM_SAMPLE_CHANNELS = 3; |
Bobty | 2:e400fd4f501b | 23 | |
Bobty | 2:e400fd4f501b | 24 | // Sample interval (uS) |
Bobty | 10:a2ba0cef85aa | 25 | volatile uint32_t sampleIntervalUs = 10000; |
Bobty | 1:1a59b4810261 | 26 | |
Bobty | 4:cc164ecf6a36 | 27 | // Interrupt driven ticker to do the sampling |
Bobty | 7:dc08df448ddc | 28 | Timeout sampleTimeout; |
Bobty | 4:cc164ecf6a36 | 29 | |
Bobty | 3:a155da1cbde3 | 30 | // Sample Channels |
Bobty | 3:a155da1cbde3 | 31 | SampleChannel sampleChannels[] = |
Bobty | 3:a155da1cbde3 | 32 | { |
Bobty | 3:a155da1cbde3 | 33 | SampleChannel(P0_1, stringToUUID("nod.score1.samp1"), &logger), |
Bobty | 3:a155da1cbde3 | 34 | SampleChannel(P0_2, stringToUUID("nod.score1.samp2"), &logger), |
Bobty | 3:a155da1cbde3 | 35 | SampleChannel(P0_3, stringToUUID("nod.score1.samp3"), &logger) |
Bobty | 3:a155da1cbde3 | 36 | }; |
aleksanb | 0:8d7583961274 | 37 | |
Bobty | 1:1a59b4810261 | 38 | // Timer to avoid repeat sampling |
Bobty | 3:a155da1cbde3 | 39 | Timer intervalTimer; |
Bobty | 3:a155da1cbde3 | 40 | int lastTriggerTime = 0; |
Bobty | 1:1a59b4810261 | 41 | int lastSampleTime = 0; |
Bobty | 10:a2ba0cef85aa | 42 | const int MIN_MS_BETWEEN_TRIGGERS = 2000; |
Bobty | 1:1a59b4810261 | 43 | |
Bobty | 7:dc08df448ddc | 44 | // Function called in interrupt driven timeout to handle sampling |
Bobty | 4:cc164ecf6a36 | 45 | static volatile int serviceCount = 0; |
Bobty | 4:cc164ecf6a36 | 46 | void SampleService() |
Bobty | 4:cc164ecf6a36 | 47 | { |
Bobty | 10:a2ba0cef85aa | 48 | // For debug timing |
Bobty | 4:cc164ecf6a36 | 49 | serviceCount++; |
Bobty | 4:cc164ecf6a36 | 50 | |
Bobty | 10:a2ba0cef85aa | 51 | // Service all channel's state machines |
Bobty | 4:cc164ecf6a36 | 52 | bool isAnyChannelSampling = false; |
Bobty | 4:cc164ecf6a36 | 53 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 4:cc164ecf6a36 | 54 | { |
Bobty | 4:cc164ecf6a36 | 55 | sampleChannels[chanIdx].Service(); |
Bobty | 4:cc164ecf6a36 | 56 | if (sampleChannels[chanIdx].IsSampling()) |
Bobty | 4:cc164ecf6a36 | 57 | isAnyChannelSampling = true; |
Bobty | 4:cc164ecf6a36 | 58 | } |
Bobty | 10:a2ba0cef85aa | 59 | |
Bobty | 10:a2ba0cef85aa | 60 | // Check if any channel is being sampled already (if so don't check for triggering) |
Bobty | 4:cc164ecf6a36 | 61 | if (!isAnyChannelSampling) |
Bobty | 4:cc164ecf6a36 | 62 | { |
Bobty | 10:a2ba0cef85aa | 63 | // Check for triggering only if previous trigger was a reasonable time ago |
Bobty | 4:cc164ecf6a36 | 64 | int curTimerVal = intervalTimer.read_ms(); |
Bobty | 10:a2ba0cef85aa | 65 | // Check for lastTriggerTime < curTimerVal is to handle (not perfectly) overflow/reset of intervalTimer |
Bobty | 10:a2ba0cef85aa | 66 | if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_TRIGGERS)) |
Bobty | 10:a2ba0cef85aa | 67 | { |
Bobty | 10:a2ba0cef85aa | 68 | // Check each channel to see if it's been triggered |
Bobty | 4:cc164ecf6a36 | 69 | bool anythingTriggered = false; |
Bobty | 4:cc164ecf6a36 | 70 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 4:cc164ecf6a36 | 71 | { |
Bobty | 4:cc164ecf6a36 | 72 | if (sampleChannels[chanIdx].CheckTrigger()) |
Bobty | 4:cc164ecf6a36 | 73 | { |
Bobty | 4:cc164ecf6a36 | 74 | anythingTriggered = true; |
Bobty | 4:cc164ecf6a36 | 75 | LOG_INFO("Triggered\n"); |
Bobty | 4:cc164ecf6a36 | 76 | break; |
Bobty | 4:cc164ecf6a36 | 77 | } |
Bobty | 4:cc164ecf6a36 | 78 | } |
Bobty | 10:a2ba0cef85aa | 79 | // If any channel has triggered ... |
Bobty | 4:cc164ecf6a36 | 80 | if(anythingTriggered) |
Bobty | 4:cc164ecf6a36 | 81 | { |
Bobty | 10:a2ba0cef85aa | 82 | // Start sampling on all channels |
Bobty | 4:cc164ecf6a36 | 83 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 4:cc164ecf6a36 | 84 | sampleChannels[chanIdx].StartSampling(); |
Bobty | 4:cc164ecf6a36 | 85 | // Set timer to disallow repeated readings |
Bobty | 4:cc164ecf6a36 | 86 | lastTriggerTime = curTimerVal; |
Bobty | 4:cc164ecf6a36 | 87 | } |
Bobty | 4:cc164ecf6a36 | 88 | } |
Bobty | 4:cc164ecf6a36 | 89 | } |
Bobty | 10:a2ba0cef85aa | 90 | |
Bobty | 10:a2ba0cef85aa | 91 | // Request a callback to this function after the sample interval |
Bobty | 10:a2ba0cef85aa | 92 | sampleTimeout.attach_us(&SampleService, sampleIntervalUs); |
Bobty | 4:cc164ecf6a36 | 93 | } |
Bobty | 10:a2ba0cef85aa | 94 | |
Bobty | 10:a2ba0cef85aa | 95 | // ThresholdSet ... BLE characteristic callback |
Bobty | 2:e400fd4f501b | 96 | void onThresholdSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 97 | { |
Bobty | 2:e400fd4f501b | 98 | uint16_t threshold = value[0] * 256 + value[1]; |
Bobty | 2:e400fd4f501b | 99 | LOG_INFO("Threshold=%d\n", threshold); |
Bobty | 3:a155da1cbde3 | 100 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 101 | sampleChannels[chanIdx].SetThreshold(threshold); |
Bobty | 2:e400fd4f501b | 102 | } |
Bobty | 2:e400fd4f501b | 103 | |
Bobty | 10:a2ba0cef85aa | 104 | // DivisorSet ... BLE characteristic callback |
Bobty | 2:e400fd4f501b | 105 | void onDivisorSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 106 | { |
Bobty | 2:e400fd4f501b | 107 | uint16_t divisor = value[0] * 256 + value[1]; |
Bobty | 2:e400fd4f501b | 108 | LOG_INFO("Divisor=%d\n", divisor); |
Bobty | 3:a155da1cbde3 | 109 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 110 | sampleChannels[chanIdx].SetDivisor(divisor); |
Bobty | 2:e400fd4f501b | 111 | } |
Bobty | 2:e400fd4f501b | 112 | |
Bobty | 10:a2ba0cef85aa | 113 | // Inverval ... BLE characteristic callback |
Bobty | 2:e400fd4f501b | 114 | void onIntervalSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 115 | { |
Bobty | 2:e400fd4f501b | 116 | uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3]; |
Bobty | 2:e400fd4f501b | 117 | LOG_INFO("SampleInterval(uS)=%d\n", intervalUs); |
Bobty | 10:a2ba0cef85aa | 118 | // Interval timer is restarted in the Ticker callback so just need to store this value |
Bobty | 3:a155da1cbde3 | 119 | if (intervalUs <= 1000000) |
Bobty | 3:a155da1cbde3 | 120 | sampleIntervalUs = intervalUs; |
Bobty | 2:e400fd4f501b | 121 | } |
Bobty | 2:e400fd4f501b | 122 | |
Bobty | 10:a2ba0cef85aa | 123 | // Main - Setup BLE and service the trigger sampling and BLE |
Bobty | 1:1a59b4810261 | 124 | int main(void) |
Bobty | 1:1a59b4810261 | 125 | { |
Bobty | 1:1a59b4810261 | 126 | |
Bobty | 1:1a59b4810261 | 127 | // Set baud rate |
Bobty | 1:1a59b4810261 | 128 | logger.baud(115200); |
Bobty | 1:1a59b4810261 | 129 | |
Bobty | 1:1a59b4810261 | 130 | // Add the Gatt characteristic for samples |
Bobty | 3:a155da1cbde3 | 131 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 132 | { |
Bobty | 3:a155da1cbde3 | 133 | puck->addCharacteristic( |
Bobty | 1:1a59b4810261 | 134 | SCORING_GATT_SERVICE, |
Bobty | 3:a155da1cbde3 | 135 | sampleChannels[chanIdx].GetUUID(), |
Bobty | 3:a155da1cbde3 | 136 | sampleChannels[chanIdx].GetSamplesLen(), |
aleksanb | 0:8d7583961274 | 137 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
Bobty | 3:a155da1cbde3 | 138 | } |
aleksanb | 0:8d7583961274 | 139 | |
Bobty | 2:e400fd4f501b | 140 | // Add the Gatt characteristic for threshold |
Bobty | 2:e400fd4f501b | 141 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 142 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 143 | THRESHOLD_GATT_CHARACTERISTIC, |
Bobty | 3:a155da1cbde3 | 144 | sizeof(uint16_t), |
Bobty | 2:e400fd4f501b | 145 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 146 | puck->onCharacteristicWrite(&THRESHOLD_GATT_CHARACTERISTIC, onThresholdSet); |
Bobty | 2:e400fd4f501b | 147 | |
Bobty | 2:e400fd4f501b | 148 | // Add the Gatt characteristic for sample divisor |
Bobty | 2:e400fd4f501b | 149 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 150 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 151 | DIVISOR_GATT_CHARACTERISTIC, |
Bobty | 3:a155da1cbde3 | 152 | sizeof(uint16_t), |
Bobty | 2:e400fd4f501b | 153 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 154 | puck->onCharacteristicWrite(&DIVISOR_GATT_CHARACTERISTIC, onDivisorSet); |
Bobty | 2:e400fd4f501b | 155 | |
Bobty | 2:e400fd4f501b | 156 | // Add the Gatt characteristic for sample interval (us) |
Bobty | 2:e400fd4f501b | 157 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 158 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 159 | INTERVAL_US_GATT_CHARACTERISTIC, |
Bobty | 2:e400fd4f501b | 160 | sizeof(sampleIntervalUs), |
Bobty | 2:e400fd4f501b | 161 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 162 | puck->onCharacteristicWrite(&INTERVAL_US_GATT_CHARACTERISTIC, onIntervalSet); |
Bobty | 2:e400fd4f501b | 163 | |
aleksanb | 0:8d7583961274 | 164 | // Initialize the puck |
Bobty | 1:1a59b4810261 | 165 | puck->init(0xCD01); |
Bobty | 1:1a59b4810261 | 166 | |
Bobty | 1:1a59b4810261 | 167 | // Start timer |
Bobty | 3:a155da1cbde3 | 168 | intervalTimer.start(); |
Bobty | 4:cc164ecf6a36 | 169 | |
Bobty | 7:dc08df448ddc | 170 | // Start timeout to service the sampling |
Bobty | 7:dc08df448ddc | 171 | sampleTimeout.attach_us(&SampleService, sampleIntervalUs); |
aleksanb | 0:8d7583961274 | 172 | |
Bobty | 1:1a59b4810261 | 173 | // Wait for something to be found |
Bobty | 4:cc164ecf6a36 | 174 | unsigned int lastPuckDriveTime = 0; |
Bobty | 5:ed9a4f932fcf | 175 | unsigned int driveLoops = 0; |
Bobty | 7:dc08df448ddc | 176 | unsigned int lastDriveLoops = 0; |
Bobty | 7:dc08df448ddc | 177 | unsigned int lastServiceCount = 0; |
Bobty | 3:a155da1cbde3 | 178 | while(true) |
Bobty | 1:1a59b4810261 | 179 | { |
Bobty | 4:cc164ecf6a36 | 180 | // Service the puck |
Bobty | 5:ed9a4f932fcf | 181 | puck->drive(); |
Bobty | 5:ed9a4f932fcf | 182 | driveLoops++; |
Bobty | 5:ed9a4f932fcf | 183 | |
Bobty | 5:ed9a4f932fcf | 184 | // Handle 1 second updates |
Bobty | 7:dc08df448ddc | 185 | unsigned int nowTime = intervalTimer.read_ms(); |
Bobty | 7:dc08df448ddc | 186 | if ((nowTime - lastPuckDriveTime >= 1000) || (nowTime < lastPuckDriveTime)) |
Bobty | 4:cc164ecf6a36 | 187 | { |
Bobty | 7:dc08df448ddc | 188 | unsigned int elapsed = nowTime - lastPuckDriveTime; |
Bobty | 10:a2ba0cef85aa | 189 | LOG_INFO("E%u C%u DC%u DTC%u L%u DL%u\n", elapsed, serviceCount, serviceCount-lastServiceCount, elapsed/(serviceCount-lastServiceCount), driveLoops, driveLoops-lastDriveLoops); |
Bobty | 10:a2ba0cef85aa | 190 | |
Bobty | 10:a2ba0cef85aa | 191 | // Check for overflow of timer |
Bobty | 10:a2ba0cef85aa | 192 | if (nowTime > 100000) |
Bobty | 10:a2ba0cef85aa | 193 | { |
Bobty | 10:a2ba0cef85aa | 194 | intervalTimer.reset(); |
Bobty | 10:a2ba0cef85aa | 195 | nowTime = 0; |
Bobty | 10:a2ba0cef85aa | 196 | } |
Bobty | 10:a2ba0cef85aa | 197 | |
Bobty | 10:a2ba0cef85aa | 198 | // Record last timer value |
Bobty | 10:a2ba0cef85aa | 199 | lastPuckDriveTime = nowTime; |
Bobty | 7:dc08df448ddc | 200 | lastDriveLoops = driveLoops; |
Bobty | 7:dc08df448ddc | 201 | lastServiceCount = serviceCount; |
Bobty | 4:cc164ecf6a36 | 202 | } |
Bobty | 4:cc164ecf6a36 | 203 | |
Bobty | 4:cc164ecf6a36 | 204 | // Check for data ready |
Bobty | 3:a155da1cbde3 | 205 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 1:1a59b4810261 | 206 | { |
Bobty | 3:a155da1cbde3 | 207 | if (sampleChannels[chanIdx].AreSamplesReady()) |
Bobty | 1:1a59b4810261 | 208 | { |
Bobty | 3:a155da1cbde3 | 209 | // Set the value of the characteristic |
Bobty | 4:cc164ecf6a36 | 210 | //puck->updateCharacteristicValue(sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamples(), sampleChannels[chanIdx].GetSamplesLen()); |
Bobty | 3:a155da1cbde3 | 211 | sampleChannels[chanIdx].StopSampling(); |
Bobty | 3:a155da1cbde3 | 212 | LOG_INFO("StopSampling\n"); |
Bobty | 3:a155da1cbde3 | 213 | } |
Bobty | 3:a155da1cbde3 | 214 | } |
Bobty | 1:1a59b4810261 | 215 | } |
Bobty | 1:1a59b4810261 | 216 | } |