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@6:81494b318e55, 2014-08-23 (annotated)
- Committer:
- Bobty
- Date:
- Sat Aug 23 20:04:36 2014 +0000
- Revision:
- 6:81494b318e55
- Parent:
- 5:ed9a4f932fcf
- Child:
- 7:dc08df448ddc
Still failing (eventually) on 50ms intervals
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 | |
aleksanb | 0:8d7583961274 | 12 | Puck* puck = &Puck::getPuck(); |
aleksanb | 0:8d7583961274 | 13 | |
Bobty | 1:1a59b4810261 | 14 | // Gatt characteristic and service UUIDs |
Bobty | 2:e400fd4f501b | 15 | const UUID SCORING_GATT_SERVICE = stringToUUID("nod.score1.serv "); |
Bobty | 2:e400fd4f501b | 16 | const UUID THRESHOLD_GATT_CHARACTERISTIC = stringToUUID("nod.score1.thres"); |
Bobty | 2:e400fd4f501b | 17 | const UUID DIVISOR_GATT_CHARACTERISTIC = stringToUUID("nod.score1.div "); |
Bobty | 2:e400fd4f501b | 18 | const UUID INTERVAL_US_GATT_CHARACTERISTIC = stringToUUID("nod.score1.intus"); |
Bobty | 1:1a59b4810261 | 19 | |
Bobty | 3:a155da1cbde3 | 20 | const int NUM_SAMPLE_CHANNELS = 1; |
Bobty | 2:e400fd4f501b | 21 | |
Bobty | 2:e400fd4f501b | 22 | // Sample interval (uS) |
Bobty | 6:81494b318e55 | 23 | uint32_t sampleIntervalUs = 50000; |
Bobty | 1:1a59b4810261 | 24 | |
Bobty | 4:cc164ecf6a36 | 25 | // Interrupt driven ticker to do the sampling |
Bobty | 4:cc164ecf6a36 | 26 | Ticker sampleTicker; |
Bobty | 4:cc164ecf6a36 | 27 | |
Bobty | 3:a155da1cbde3 | 28 | // Sample Channels |
Bobty | 3:a155da1cbde3 | 29 | SampleChannel sampleChannels[] = |
Bobty | 3:a155da1cbde3 | 30 | { |
Bobty | 3:a155da1cbde3 | 31 | SampleChannel(P0_1, stringToUUID("nod.score1.samp1"), &logger), |
Bobty | 3:a155da1cbde3 | 32 | SampleChannel(P0_2, stringToUUID("nod.score1.samp2"), &logger), |
Bobty | 3:a155da1cbde3 | 33 | SampleChannel(P0_3, stringToUUID("nod.score1.samp3"), &logger) |
Bobty | 3:a155da1cbde3 | 34 | }; |
aleksanb | 0:8d7583961274 | 35 | |
Bobty | 1:1a59b4810261 | 36 | // Timer to avoid repeat sampling |
Bobty | 3:a155da1cbde3 | 37 | Timer intervalTimer; |
Bobty | 3:a155da1cbde3 | 38 | int lastTriggerTime = 0; |
Bobty | 1:1a59b4810261 | 39 | int lastSampleTime = 0; |
Bobty | 1:1a59b4810261 | 40 | const int MIN_MS_BETWEEN_SAMPLES = 2000; |
Bobty | 1:1a59b4810261 | 41 | |
Bobty | 4:cc164ecf6a36 | 42 | // Function called in interrupt driven ticker to handle sampling |
Bobty | 4:cc164ecf6a36 | 43 | static volatile int serviceCount = 0; |
Bobty | 4:cc164ecf6a36 | 44 | void SampleService() |
Bobty | 4:cc164ecf6a36 | 45 | { |
Bobty | 4:cc164ecf6a36 | 46 | serviceCount++; |
Bobty | 4:cc164ecf6a36 | 47 | |
Bobty | 4:cc164ecf6a36 | 48 | return; |
Bobty | 4:cc164ecf6a36 | 49 | |
Bobty | 4:cc164ecf6a36 | 50 | // service all channel's state machines |
Bobty | 4:cc164ecf6a36 | 51 | bool isAnyChannelSampling = false; |
Bobty | 4:cc164ecf6a36 | 52 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 4:cc164ecf6a36 | 53 | { |
Bobty | 4:cc164ecf6a36 | 54 | sampleChannels[chanIdx].Service(); |
Bobty | 4:cc164ecf6a36 | 55 | if (sampleChannels[chanIdx].IsSampling()) |
Bobty | 4:cc164ecf6a36 | 56 | isAnyChannelSampling = true; |
Bobty | 4:cc164ecf6a36 | 57 | } |
Bobty | 4:cc164ecf6a36 | 58 | |
Bobty | 4:cc164ecf6a36 | 59 | if (!isAnyChannelSampling) |
Bobty | 4:cc164ecf6a36 | 60 | { |
Bobty | 4:cc164ecf6a36 | 61 | int curTimerVal = intervalTimer.read_ms(); |
Bobty | 4:cc164ecf6a36 | 62 | if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_SAMPLES)) |
Bobty | 4:cc164ecf6a36 | 63 | { |
Bobty | 4:cc164ecf6a36 | 64 | |
Bobty | 4:cc164ecf6a36 | 65 | // check each channel to see if it's been triggered |
Bobty | 4:cc164ecf6a36 | 66 | bool anythingTriggered = false; |
Bobty | 4:cc164ecf6a36 | 67 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 4:cc164ecf6a36 | 68 | { |
Bobty | 4:cc164ecf6a36 | 69 | if (sampleChannels[chanIdx].CheckTrigger()) |
Bobty | 4:cc164ecf6a36 | 70 | { |
Bobty | 4:cc164ecf6a36 | 71 | anythingTriggered = true; |
Bobty | 4:cc164ecf6a36 | 72 | LOG_INFO("Triggered\n"); |
Bobty | 4:cc164ecf6a36 | 73 | break; |
Bobty | 4:cc164ecf6a36 | 74 | } |
Bobty | 4:cc164ecf6a36 | 75 | } |
Bobty | 4:cc164ecf6a36 | 76 | if(anythingTriggered) |
Bobty | 4:cc164ecf6a36 | 77 | { |
Bobty | 4:cc164ecf6a36 | 78 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 4:cc164ecf6a36 | 79 | { |
Bobty | 4:cc164ecf6a36 | 80 | sampleChannels[chanIdx].StartSampling(); |
Bobty | 4:cc164ecf6a36 | 81 | } |
Bobty | 4:cc164ecf6a36 | 82 | // Set timer to disallow repeated readings |
Bobty | 4:cc164ecf6a36 | 83 | lastTriggerTime = curTimerVal; |
Bobty | 4:cc164ecf6a36 | 84 | } |
Bobty | 4:cc164ecf6a36 | 85 | } |
Bobty | 4:cc164ecf6a36 | 86 | } |
Bobty | 4:cc164ecf6a36 | 87 | } |
Bobty | 4:cc164ecf6a36 | 88 | |
Bobty | 2:e400fd4f501b | 89 | void onThresholdSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 90 | { |
Bobty | 2:e400fd4f501b | 91 | uint16_t threshold = value[0] * 256 + value[1]; |
Bobty | 2:e400fd4f501b | 92 | LOG_INFO("Threshold=%d\n", threshold); |
Bobty | 3:a155da1cbde3 | 93 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 94 | sampleChannels[chanIdx].SetThreshold(threshold); |
Bobty | 2:e400fd4f501b | 95 | } |
Bobty | 2:e400fd4f501b | 96 | |
Bobty | 2:e400fd4f501b | 97 | void onDivisorSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 98 | { |
Bobty | 2:e400fd4f501b | 99 | uint16_t divisor = value[0] * 256 + value[1]; |
Bobty | 2:e400fd4f501b | 100 | LOG_INFO("Divisor=%d\n", divisor); |
Bobty | 3:a155da1cbde3 | 101 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 102 | sampleChannels[chanIdx].SetDivisor(divisor); |
Bobty | 2:e400fd4f501b | 103 | } |
Bobty | 2:e400fd4f501b | 104 | |
Bobty | 2:e400fd4f501b | 105 | void onIntervalSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 106 | { |
Bobty | 2:e400fd4f501b | 107 | uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3]; |
Bobty | 2:e400fd4f501b | 108 | LOG_INFO("SampleInterval(uS)=%d\n", intervalUs); |
Bobty | 3:a155da1cbde3 | 109 | if (intervalUs <= 1000000) |
Bobty | 4:cc164ecf6a36 | 110 | { |
Bobty | 3:a155da1cbde3 | 111 | sampleIntervalUs = intervalUs; |
Bobty | 4:cc164ecf6a36 | 112 | // sampleTicker.detach(); |
Bobty | 4:cc164ecf6a36 | 113 | // sampleTicker.attach_us(&SampleService, sampleIntervalUs); |
Bobty | 4:cc164ecf6a36 | 114 | } |
Bobty | 2:e400fd4f501b | 115 | } |
Bobty | 2:e400fd4f501b | 116 | |
Bobty | 1:1a59b4810261 | 117 | int main(void) |
Bobty | 1:1a59b4810261 | 118 | { |
Bobty | 1:1a59b4810261 | 119 | |
Bobty | 1:1a59b4810261 | 120 | // Set baud rate |
Bobty | 1:1a59b4810261 | 121 | logger.baud(115200); |
Bobty | 1:1a59b4810261 | 122 | |
Bobty | 1:1a59b4810261 | 123 | // Add the Gatt characteristic for samples |
Bobty | 3:a155da1cbde3 | 124 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 125 | { |
Bobty | 3:a155da1cbde3 | 126 | puck->addCharacteristic( |
Bobty | 1:1a59b4810261 | 127 | SCORING_GATT_SERVICE, |
Bobty | 3:a155da1cbde3 | 128 | sampleChannels[chanIdx].GetUUID(), |
Bobty | 3:a155da1cbde3 | 129 | sampleChannels[chanIdx].GetSamplesLen(), |
aleksanb | 0:8d7583961274 | 130 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
Bobty | 3:a155da1cbde3 | 131 | } |
aleksanb | 0:8d7583961274 | 132 | |
Bobty | 2:e400fd4f501b | 133 | // Add the Gatt characteristic for threshold |
Bobty | 2:e400fd4f501b | 134 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 135 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 136 | THRESHOLD_GATT_CHARACTERISTIC, |
Bobty | 3:a155da1cbde3 | 137 | sizeof(uint16_t), |
Bobty | 2:e400fd4f501b | 138 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 139 | puck->onCharacteristicWrite(&THRESHOLD_GATT_CHARACTERISTIC, onThresholdSet); |
Bobty | 2:e400fd4f501b | 140 | |
Bobty | 2:e400fd4f501b | 141 | // Add the Gatt characteristic for sample divisor |
Bobty | 2:e400fd4f501b | 142 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 143 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 144 | DIVISOR_GATT_CHARACTERISTIC, |
Bobty | 3:a155da1cbde3 | 145 | sizeof(uint16_t), |
Bobty | 2:e400fd4f501b | 146 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 147 | puck->onCharacteristicWrite(&DIVISOR_GATT_CHARACTERISTIC, onDivisorSet); |
Bobty | 2:e400fd4f501b | 148 | |
Bobty | 2:e400fd4f501b | 149 | // Add the Gatt characteristic for sample interval (us) |
Bobty | 2:e400fd4f501b | 150 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 151 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 152 | INTERVAL_US_GATT_CHARACTERISTIC, |
Bobty | 2:e400fd4f501b | 153 | sizeof(sampleIntervalUs), |
Bobty | 2:e400fd4f501b | 154 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 155 | puck->onCharacteristicWrite(&INTERVAL_US_GATT_CHARACTERISTIC, onIntervalSet); |
Bobty | 2:e400fd4f501b | 156 | |
aleksanb | 0:8d7583961274 | 157 | // Initialize the puck |
Bobty | 1:1a59b4810261 | 158 | puck->init(0xCD01); |
Bobty | 1:1a59b4810261 | 159 | |
Bobty | 1:1a59b4810261 | 160 | // Start timer |
Bobty | 3:a155da1cbde3 | 161 | intervalTimer.start(); |
Bobty | 4:cc164ecf6a36 | 162 | |
Bobty | 4:cc164ecf6a36 | 163 | // Start ticker to service the sampling |
Bobty | 4:cc164ecf6a36 | 164 | sampleTicker.attach_us(&SampleService, sampleIntervalUs); |
aleksanb | 0:8d7583961274 | 165 | |
Bobty | 1:1a59b4810261 | 166 | // Wait for something to be found |
Bobty | 4:cc164ecf6a36 | 167 | unsigned int lastPuckDriveTime = 0; |
Bobty | 5:ed9a4f932fcf | 168 | unsigned int driveLoops = 0; |
Bobty | 3:a155da1cbde3 | 169 | while(true) |
Bobty | 1:1a59b4810261 | 170 | { |
Bobty | 4:cc164ecf6a36 | 171 | // Service the puck |
Bobty | 5:ed9a4f932fcf | 172 | puck->drive(); |
Bobty | 5:ed9a4f932fcf | 173 | driveLoops++; |
Bobty | 5:ed9a4f932fcf | 174 | |
Bobty | 5:ed9a4f932fcf | 175 | // Handle 1 second updates |
Bobty | 4:cc164ecf6a36 | 176 | if ((intervalTimer.read_ms() - lastPuckDriveTime >= 1000) || (intervalTimer.read_ms() < lastPuckDriveTime)) |
Bobty | 4:cc164ecf6a36 | 177 | { |
Bobty | 4:cc164ecf6a36 | 178 | lastPuckDriveTime = intervalTimer.read_ms(); |
Bobty | 5:ed9a4f932fcf | 179 | LOG_INFO("%u T%u L%u\n", intervalTimer.read_ms(), serviceCount, driveLoops); |
Bobty | 4:cc164ecf6a36 | 180 | } |
Bobty | 4:cc164ecf6a36 | 181 | |
Bobty | 4:cc164ecf6a36 | 182 | continue; |
Bobty | 4:cc164ecf6a36 | 183 | |
Bobty | 4:cc164ecf6a36 | 184 | // Check for data ready |
Bobty | 3:a155da1cbde3 | 185 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 1:1a59b4810261 | 186 | { |
Bobty | 3:a155da1cbde3 | 187 | if (sampleChannels[chanIdx].AreSamplesReady()) |
Bobty | 1:1a59b4810261 | 188 | { |
Bobty | 3:a155da1cbde3 | 189 | // Set the value of the characteristic |
Bobty | 4:cc164ecf6a36 | 190 | //puck->updateCharacteristicValue(sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamples(), sampleChannels[chanIdx].GetSamplesLen()); |
Bobty | 3:a155da1cbde3 | 191 | sampleChannels[chanIdx].StopSampling(); |
Bobty | 3:a155da1cbde3 | 192 | LOG_INFO("StopSampling\n"); |
Bobty | 3:a155da1cbde3 | 193 | } |
Bobty | 3:a155da1cbde3 | 194 | } |
Bobty | 1:1a59b4810261 | 195 | } |
Bobty | 1:1a59b4810261 | 196 | } |