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.

Dependencies:   Puck mbed

Fork of Example_Puck_BLE by Nordic Semiconductor

Committer:
Bobty
Date:
Thu Aug 21 07:48:18 2014 +0000
Revision:
2:e400fd4f501b
Parent:
1:1a59b4810261
Child:
3:a155da1cbde3
Added characteristics for threshold, sample offset, divisor and sample rate

Who changed what in which revision?

UserRevisionLine numberNew 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"
aleksanb 0:8d7583961274 10
aleksanb 0:8d7583961274 11 Puck* puck = &Puck::getPuck();
aleksanb 0:8d7583961274 12
Bobty 1:1a59b4810261 13 // Gatt characteristic and service UUIDs
Bobty 2:e400fd4f501b 14 const UUID SCORING_GATT_SERVICE = stringToUUID("nod.score1.serv ");
Bobty 2:e400fd4f501b 15 const UUID SAMPLES_GATT_CHARACTERISTIC = stringToUUID("nod.score1.samp1");
Bobty 2:e400fd4f501b 16 const UUID THRESHOLD_GATT_CHARACTERISTIC = stringToUUID("nod.score1.thres");
Bobty 2:e400fd4f501b 17 const UUID OFFSET_GATT_CHARACTERISTIC = stringToUUID("nod.score1.offs ");
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 1:1a59b4810261 21 const int SAMPLES_LEN = 20;
Bobty 1:1a59b4810261 22 uint8_t SAMPLES_BUF[SAMPLES_LEN];
Bobty 2:e400fd4f501b 23
Bobty 2:e400fd4f501b 24 // Sample threshold
Bobty 2:e400fd4f501b 25 uint16_t sampleThreshold = 800;
Bobty 2:e400fd4f501b 26
Bobty 2:e400fd4f501b 27 // Sample offset
Bobty 2:e400fd4f501b 28 uint16_t samplesOffset = 760;
Bobty 2:e400fd4f501b 29
Bobty 2:e400fd4f501b 30 // Sample divisor
Bobty 2:e400fd4f501b 31 uint16_t samplesDivisor = 1;
Bobty 2:e400fd4f501b 32
Bobty 2:e400fd4f501b 33 // Sample interval (uS)
Bobty 2:e400fd4f501b 34 uint32_t sampleIntervalUs = 10000;
Bobty 1:1a59b4810261 35
Bobty 1:1a59b4810261 36 // Setup ADC
Bobty 1:1a59b4810261 37 AnalogIn ain(P0_1);
aleksanb 0:8d7583961274 38
Bobty 1:1a59b4810261 39 // Timer to avoid repeat sampling
Bobty 1:1a59b4810261 40 Timer antiRepeatTimer;
Bobty 1:1a59b4810261 41 int lastSampleTime = 0;
Bobty 1:1a59b4810261 42 const int MIN_MS_BETWEEN_SAMPLES = 2000;
Bobty 1:1a59b4810261 43
Bobty 2:e400fd4f501b 44 void onThresholdSet(uint8_t* value)
Bobty 2:e400fd4f501b 45 {
Bobty 2:e400fd4f501b 46 uint16_t threshold = value[0] * 256 + value[1];
Bobty 2:e400fd4f501b 47 LOG_INFO("Threshold=%d\n", threshold);
Bobty 2:e400fd4f501b 48 sampleThreshold = threshold;
Bobty 2:e400fd4f501b 49 }
Bobty 2:e400fd4f501b 50
Bobty 2:e400fd4f501b 51 void onOffsetSet(uint8_t* value)
Bobty 2:e400fd4f501b 52 {
Bobty 2:e400fd4f501b 53 uint16_t offset = value[0] * 256 + value[1];
Bobty 2:e400fd4f501b 54 LOG_INFO("Offset=%d\n", offset);
Bobty 2:e400fd4f501b 55 samplesOffset = offset;
Bobty 2:e400fd4f501b 56 }
Bobty 2:e400fd4f501b 57
Bobty 2:e400fd4f501b 58 void onDivisorSet(uint8_t* value)
Bobty 2:e400fd4f501b 59 {
Bobty 2:e400fd4f501b 60 uint16_t divisor = value[0] * 256 + value[1];
Bobty 2:e400fd4f501b 61 LOG_INFO("Divisor=%d\n", divisor);
Bobty 2:e400fd4f501b 62 samplesDivisor = divisor;
Bobty 2:e400fd4f501b 63 }
Bobty 2:e400fd4f501b 64
Bobty 2:e400fd4f501b 65 void onIntervalSet(uint8_t* value)
Bobty 2:e400fd4f501b 66 {
Bobty 2:e400fd4f501b 67 uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3];
Bobty 2:e400fd4f501b 68 LOG_INFO("SampleInterval(uS)=%d\n", intervalUs);
Bobty 2:e400fd4f501b 69 sampleIntervalUs = intervalUs;
Bobty 2:e400fd4f501b 70 }
Bobty 2:e400fd4f501b 71
Bobty 1:1a59b4810261 72 int main(void)
Bobty 1:1a59b4810261 73 {
Bobty 1:1a59b4810261 74
Bobty 1:1a59b4810261 75 // Set baud rate
Bobty 1:1a59b4810261 76 logger.baud(115200);
Bobty 1:1a59b4810261 77
Bobty 1:1a59b4810261 78 // Add the Gatt characteristic for samples
aleksanb 0:8d7583961274 79 puck->addCharacteristic(
Bobty 1:1a59b4810261 80 SCORING_GATT_SERVICE,
Bobty 1:1a59b4810261 81 SAMPLES_GATT_CHARACTERISTIC,
Bobty 1:1a59b4810261 82 SAMPLES_LEN,
aleksanb 0:8d7583961274 83 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
aleksanb 0:8d7583961274 84
Bobty 2:e400fd4f501b 85 // Add the Gatt characteristic for threshold
Bobty 2:e400fd4f501b 86 puck->addCharacteristic(
Bobty 2:e400fd4f501b 87 SCORING_GATT_SERVICE,
Bobty 2:e400fd4f501b 88 THRESHOLD_GATT_CHARACTERISTIC,
Bobty 2:e400fd4f501b 89 sizeof(sampleThreshold),
Bobty 2:e400fd4f501b 90 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Bobty 2:e400fd4f501b 91 puck->onCharacteristicWrite(&THRESHOLD_GATT_CHARACTERISTIC, onThresholdSet);
Bobty 2:e400fd4f501b 92
Bobty 2:e400fd4f501b 93 // Add the Gatt characteristic for offset
Bobty 2:e400fd4f501b 94 puck->addCharacteristic(
Bobty 2:e400fd4f501b 95 SCORING_GATT_SERVICE,
Bobty 2:e400fd4f501b 96 OFFSET_GATT_CHARACTERISTIC,
Bobty 2:e400fd4f501b 97 sizeof(samplesOffset),
Bobty 2:e400fd4f501b 98 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Bobty 2:e400fd4f501b 99 puck->onCharacteristicWrite(&OFFSET_GATT_CHARACTERISTIC, onOffsetSet);
Bobty 2:e400fd4f501b 100
Bobty 2:e400fd4f501b 101 // Add the Gatt characteristic for sample divisor
Bobty 2:e400fd4f501b 102 puck->addCharacteristic(
Bobty 2:e400fd4f501b 103 SCORING_GATT_SERVICE,
Bobty 2:e400fd4f501b 104 DIVISOR_GATT_CHARACTERISTIC,
Bobty 2:e400fd4f501b 105 sizeof(samplesDivisor),
Bobty 2:e400fd4f501b 106 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Bobty 2:e400fd4f501b 107 puck->onCharacteristicWrite(&DIVISOR_GATT_CHARACTERISTIC, onDivisorSet);
Bobty 2:e400fd4f501b 108
Bobty 2:e400fd4f501b 109 // Add the Gatt characteristic for sample interval (us)
Bobty 2:e400fd4f501b 110 puck->addCharacteristic(
Bobty 2:e400fd4f501b 111 SCORING_GATT_SERVICE,
Bobty 2:e400fd4f501b 112 INTERVAL_US_GATT_CHARACTERISTIC,
Bobty 2:e400fd4f501b 113 sizeof(sampleIntervalUs),
Bobty 2:e400fd4f501b 114 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Bobty 2:e400fd4f501b 115 puck->onCharacteristicWrite(&INTERVAL_US_GATT_CHARACTERISTIC, onIntervalSet);
Bobty 2:e400fd4f501b 116
aleksanb 0:8d7583961274 117 // Initialize the puck
Bobty 1:1a59b4810261 118 puck->init(0xCD01);
Bobty 1:1a59b4810261 119
Bobty 1:1a59b4810261 120 // Start timer
Bobty 1:1a59b4810261 121 antiRepeatTimer.start();
aleksanb 0:8d7583961274 122
Bobty 1:1a59b4810261 123 // Wait for something to be found
Bobty 1:1a59b4810261 124 while(puck->drive())
Bobty 1:1a59b4810261 125 {
Bobty 1:1a59b4810261 126 int curTimerVal = antiRepeatTimer.read_ms();
Bobty 1:1a59b4810261 127 if ((lastSampleTime < curTimerVal) || (curTimerVal - lastSampleTime > MIN_MS_BETWEEN_SAMPLES))
Bobty 1:1a59b4810261 128 {
Bobty 1:1a59b4810261 129 // Check threshold
Bobty 1:1a59b4810261 130 unsigned short val = ain.read_u16();
Bobty 2:e400fd4f501b 131 if(val > sampleThreshold)
Bobty 1:1a59b4810261 132 {
Bobty 1:1a59b4810261 133 for (int i = 0; i < SAMPLES_LEN; i++)
Bobty 1:1a59b4810261 134 {
Bobty 2:e400fd4f501b 135 SAMPLES_BUF[i] = (ain.read_u16() - samplesOffset) / samplesDivisor;
Bobty 2:e400fd4f501b 136 wait_us(sampleIntervalUs);
Bobty 1:1a59b4810261 137 }
Bobty 1:1a59b4810261 138
Bobty 1:1a59b4810261 139 // Set the value of the characteristic
Bobty 1:1a59b4810261 140 puck->updateCharacteristicValue(SAMPLES_GATT_CHARACTERISTIC, SAMPLES_BUF, SAMPLES_LEN);
Bobty 1:1a59b4810261 141
Bobty 1:1a59b4810261 142 // Display readings
Bobty 1:1a59b4810261 143 for (int j = 0; j < SAMPLES_LEN; j++)
Bobty 1:1a59b4810261 144 {
Bobty 2:e400fd4f501b 145 LOG_INFO("%02x ", SAMPLES_BUF[j]);
Bobty 1:1a59b4810261 146 }
Bobty 1:1a59b4810261 147 LOG_INFO("\n");
Bobty 1:1a59b4810261 148
Bobty 1:1a59b4810261 149 // Set timer to disallow repeated readings
Bobty 1:1a59b4810261 150 lastSampleTime = curTimerVal;
Bobty 1:1a59b4810261 151 }
Bobty 1:1a59b4810261 152 }
Bobty 1:1a59b4810261 153 }
Bobty 1:1a59b4810261 154 }