![](/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@3:a155da1cbde3, 2014-08-21 (annotated)
- Committer:
- Bobty
- Date:
- Thu Aug 21 11:26:26 2014 +0000
- Revision:
- 3:a155da1cbde3
- Parent:
- 2:e400fd4f501b
- Child:
- 4:cc164ecf6a36
Working but misses some quick pulses (thows)
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 | |
aleksanb | 0:8d7583961274 | 8 | #define LOG_LEVEL_INFO |
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 | 3:a155da1cbde3 | 23 | uint32_t sampleIntervalUs = 100000; |
Bobty | 1:1a59b4810261 | 24 | |
Bobty | 3:a155da1cbde3 | 25 | // Sample Channels |
Bobty | 3:a155da1cbde3 | 26 | SampleChannel sampleChannels[] = |
Bobty | 3:a155da1cbde3 | 27 | { |
Bobty | 3:a155da1cbde3 | 28 | SampleChannel(P0_1, stringToUUID("nod.score1.samp1"), &logger), |
Bobty | 3:a155da1cbde3 | 29 | SampleChannel(P0_2, stringToUUID("nod.score1.samp2"), &logger), |
Bobty | 3:a155da1cbde3 | 30 | SampleChannel(P0_3, stringToUUID("nod.score1.samp3"), &logger) |
Bobty | 3:a155da1cbde3 | 31 | }; |
aleksanb | 0:8d7583961274 | 32 | |
Bobty | 1:1a59b4810261 | 33 | // Timer to avoid repeat sampling |
Bobty | 3:a155da1cbde3 | 34 | Timer intervalTimer; |
Bobty | 3:a155da1cbde3 | 35 | int lastTriggerTime = 0; |
Bobty | 1:1a59b4810261 | 36 | int lastSampleTime = 0; |
Bobty | 1:1a59b4810261 | 37 | const int MIN_MS_BETWEEN_SAMPLES = 2000; |
Bobty | 1:1a59b4810261 | 38 | |
Bobty | 2:e400fd4f501b | 39 | void onThresholdSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 40 | { |
Bobty | 2:e400fd4f501b | 41 | uint16_t threshold = value[0] * 256 + value[1]; |
Bobty | 2:e400fd4f501b | 42 | LOG_INFO("Threshold=%d\n", threshold); |
Bobty | 3:a155da1cbde3 | 43 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 44 | sampleChannels[chanIdx].SetThreshold(threshold); |
Bobty | 2:e400fd4f501b | 45 | } |
Bobty | 2:e400fd4f501b | 46 | |
Bobty | 2:e400fd4f501b | 47 | void onDivisorSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 48 | { |
Bobty | 2:e400fd4f501b | 49 | uint16_t divisor = value[0] * 256 + value[1]; |
Bobty | 2:e400fd4f501b | 50 | LOG_INFO("Divisor=%d\n", divisor); |
Bobty | 3:a155da1cbde3 | 51 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 52 | sampleChannels[chanIdx].SetDivisor(divisor); |
Bobty | 2:e400fd4f501b | 53 | } |
Bobty | 2:e400fd4f501b | 54 | |
Bobty | 2:e400fd4f501b | 55 | void onIntervalSet(uint8_t* value) |
Bobty | 2:e400fd4f501b | 56 | { |
Bobty | 2:e400fd4f501b | 57 | uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3]; |
Bobty | 2:e400fd4f501b | 58 | LOG_INFO("SampleInterval(uS)=%d\n", intervalUs); |
Bobty | 3:a155da1cbde3 | 59 | if (intervalUs <= 1000000) |
Bobty | 3:a155da1cbde3 | 60 | sampleIntervalUs = intervalUs; |
Bobty | 2:e400fd4f501b | 61 | } |
Bobty | 2:e400fd4f501b | 62 | |
Bobty | 1:1a59b4810261 | 63 | int main(void) |
Bobty | 1:1a59b4810261 | 64 | { |
Bobty | 1:1a59b4810261 | 65 | |
Bobty | 1:1a59b4810261 | 66 | // Set baud rate |
Bobty | 1:1a59b4810261 | 67 | logger.baud(115200); |
Bobty | 1:1a59b4810261 | 68 | |
Bobty | 1:1a59b4810261 | 69 | // Add the Gatt characteristic for samples |
Bobty | 3:a155da1cbde3 | 70 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 71 | { |
Bobty | 3:a155da1cbde3 | 72 | puck->addCharacteristic( |
Bobty | 1:1a59b4810261 | 73 | SCORING_GATT_SERVICE, |
Bobty | 3:a155da1cbde3 | 74 | sampleChannels[chanIdx].GetUUID(), |
Bobty | 3:a155da1cbde3 | 75 | sampleChannels[chanIdx].GetSamplesLen(), |
aleksanb | 0:8d7583961274 | 76 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
Bobty | 3:a155da1cbde3 | 77 | } |
aleksanb | 0:8d7583961274 | 78 | |
Bobty | 2:e400fd4f501b | 79 | // Add the Gatt characteristic for threshold |
Bobty | 2:e400fd4f501b | 80 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 81 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 82 | THRESHOLD_GATT_CHARACTERISTIC, |
Bobty | 3:a155da1cbde3 | 83 | sizeof(uint16_t), |
Bobty | 2:e400fd4f501b | 84 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 85 | puck->onCharacteristicWrite(&THRESHOLD_GATT_CHARACTERISTIC, onThresholdSet); |
Bobty | 2:e400fd4f501b | 86 | |
Bobty | 2:e400fd4f501b | 87 | // Add the Gatt characteristic for sample divisor |
Bobty | 2:e400fd4f501b | 88 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 89 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 90 | DIVISOR_GATT_CHARACTERISTIC, |
Bobty | 3:a155da1cbde3 | 91 | sizeof(uint16_t), |
Bobty | 2:e400fd4f501b | 92 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 93 | puck->onCharacteristicWrite(&DIVISOR_GATT_CHARACTERISTIC, onDivisorSet); |
Bobty | 2:e400fd4f501b | 94 | |
Bobty | 2:e400fd4f501b | 95 | // Add the Gatt characteristic for sample interval (us) |
Bobty | 2:e400fd4f501b | 96 | puck->addCharacteristic( |
Bobty | 2:e400fd4f501b | 97 | SCORING_GATT_SERVICE, |
Bobty | 2:e400fd4f501b | 98 | INTERVAL_US_GATT_CHARACTERISTIC, |
Bobty | 2:e400fd4f501b | 99 | sizeof(sampleIntervalUs), |
Bobty | 2:e400fd4f501b | 100 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
Bobty | 2:e400fd4f501b | 101 | puck->onCharacteristicWrite(&INTERVAL_US_GATT_CHARACTERISTIC, onIntervalSet); |
Bobty | 2:e400fd4f501b | 102 | |
aleksanb | 0:8d7583961274 | 103 | // Initialize the puck |
Bobty | 1:1a59b4810261 | 104 | puck->init(0xCD01); |
Bobty | 1:1a59b4810261 | 105 | |
Bobty | 1:1a59b4810261 | 106 | // Start timer |
Bobty | 3:a155da1cbde3 | 107 | intervalTimer.start(); |
aleksanb | 0:8d7583961274 | 108 | |
Bobty | 1:1a59b4810261 | 109 | // Wait for something to be found |
Bobty | 3:a155da1cbde3 | 110 | while(true) |
Bobty | 1:1a59b4810261 | 111 | { |
Bobty | 3:a155da1cbde3 | 112 | // service all channel's state machines |
Bobty | 3:a155da1cbde3 | 113 | bool isAnyChannelSampling = false; |
Bobty | 3:a155da1cbde3 | 114 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 1:1a59b4810261 | 115 | { |
Bobty | 3:a155da1cbde3 | 116 | sampleChannels[chanIdx].Service(); |
Bobty | 3:a155da1cbde3 | 117 | if (sampleChannels[chanIdx].AreSamplesReady()) |
Bobty | 1:1a59b4810261 | 118 | { |
Bobty | 3:a155da1cbde3 | 119 | // Set the value of the characteristic |
Bobty | 3:a155da1cbde3 | 120 | puck->updateCharacteristicValue(sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamples(), sampleChannels[chanIdx].GetSamplesLen()); |
Bobty | 3:a155da1cbde3 | 121 | sampleChannels[chanIdx].StopSampling(); |
Bobty | 3:a155da1cbde3 | 122 | LOG_INFO("StopSampling\n"); |
Bobty | 3:a155da1cbde3 | 123 | } |
Bobty | 3:a155da1cbde3 | 124 | if (sampleChannels[chanIdx].IsSampling()) |
Bobty | 3:a155da1cbde3 | 125 | isAnyChannelSampling = true; |
Bobty | 3:a155da1cbde3 | 126 | } |
Bobty | 3:a155da1cbde3 | 127 | |
Bobty | 3:a155da1cbde3 | 128 | if (!isAnyChannelSampling) |
Bobty | 3:a155da1cbde3 | 129 | { |
Bobty | 3:a155da1cbde3 | 130 | // Service the puck |
Bobty | 3:a155da1cbde3 | 131 | puck->drive(); |
Bobty | 3:a155da1cbde3 | 132 | |
Bobty | 3:a155da1cbde3 | 133 | int curTimerVal = intervalTimer.read_ms(); |
Bobty | 3:a155da1cbde3 | 134 | if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_SAMPLES)) |
Bobty | 3:a155da1cbde3 | 135 | { |
Bobty | 3:a155da1cbde3 | 136 | |
Bobty | 3:a155da1cbde3 | 137 | // check each channel to see if it's been triggered |
Bobty | 3:a155da1cbde3 | 138 | bool anythingTriggered = false; |
Bobty | 3:a155da1cbde3 | 139 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 1:1a59b4810261 | 140 | { |
Bobty | 3:a155da1cbde3 | 141 | if (sampleChannels[chanIdx].CheckTrigger()) |
Bobty | 3:a155da1cbde3 | 142 | { |
Bobty | 3:a155da1cbde3 | 143 | anythingTriggered = true; |
Bobty | 3:a155da1cbde3 | 144 | LOG_INFO("Triggered\n"); |
Bobty | 3:a155da1cbde3 | 145 | break; |
Bobty | 3:a155da1cbde3 | 146 | } |
Bobty | 1:1a59b4810261 | 147 | } |
Bobty | 3:a155da1cbde3 | 148 | if(anythingTriggered) |
Bobty | 1:1a59b4810261 | 149 | { |
Bobty | 3:a155da1cbde3 | 150 | for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) |
Bobty | 3:a155da1cbde3 | 151 | { |
Bobty | 3:a155da1cbde3 | 152 | sampleChannels[chanIdx].StartSampling(); |
Bobty | 3:a155da1cbde3 | 153 | } |
Bobty | 3:a155da1cbde3 | 154 | // Set timer to disallow repeated readings |
Bobty | 3:a155da1cbde3 | 155 | lastTriggerTime = curTimerVal; |
Bobty | 1:1a59b4810261 | 156 | } |
Bobty | 3:a155da1cbde3 | 157 | } |
Bobty | 3:a155da1cbde3 | 158 | } |
Bobty | 3:a155da1cbde3 | 159 | else |
Bobty | 3:a155da1cbde3 | 160 | { |
Bobty | 3:a155da1cbde3 | 161 | wait_us(sampleIntervalUs); |
Bobty | 3:a155da1cbde3 | 162 | } |
Bobty | 1:1a59b4810261 | 163 | |
Bobty | 3:a155da1cbde3 | 164 | // Inter-sample interval |
Bobty | 3:a155da1cbde3 | 165 | //while ((intervalTimer.read_us() - lastSampleTime < sampleIntervalUs) && (intervalTimer.read_us() - lastSampleTime > 0)) |
Bobty | 3:a155da1cbde3 | 166 | // wait_us(100); |
Bobty | 3:a155da1cbde3 | 167 | // lastSampleTime = intervalTimer.read_us(); |
Bobty | 3:a155da1cbde3 | 168 | // LOG_INFO("Timer %d\n", intervalTimer.read_ms()); |
Bobty | 1:1a59b4810261 | 169 | } |
Bobty | 1:1a59b4810261 | 170 | } |