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:
Wed Aug 20 09:49:41 2014 +0000
Revision:
1:1a59b4810261
Parent:
0:8d7583961274
Child:
2:e400fd4f501b
Initial version - fixed threshold, 10ms sampling, samples characteristic is a 20 byte array

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 1:1a59b4810261 14 const UUID SCORING_GATT_SERVICE = stringToUUID("nodule.scoring ");
Bobty 1:1a59b4810261 15 const UUID SAMPLES_GATT_CHARACTERISTIC = stringToUUID("nodule.samples ");
Bobty 1:1a59b4810261 16
Bobty 1:1a59b4810261 17 const int SAMPLES_LEN = 20;
Bobty 1:1a59b4810261 18 uint8_t SAMPLES_BUF[SAMPLES_LEN];
Bobty 1:1a59b4810261 19 const int SAMPLE_THRESHOLD = 800;
Bobty 1:1a59b4810261 20
Bobty 1:1a59b4810261 21 // Setup ADC
Bobty 1:1a59b4810261 22 AnalogIn ain(P0_1);
aleksanb 0:8d7583961274 23
Bobty 1:1a59b4810261 24 // Timer to avoid repeat sampling
Bobty 1:1a59b4810261 25 Timer antiRepeatTimer;
Bobty 1:1a59b4810261 26 int lastSampleTime = 0;
Bobty 1:1a59b4810261 27 const int MIN_MS_BETWEEN_SAMPLES = 2000;
Bobty 1:1a59b4810261 28
Bobty 1:1a59b4810261 29 int main(void)
Bobty 1:1a59b4810261 30 {
Bobty 1:1a59b4810261 31
Bobty 1:1a59b4810261 32 // Set baud rate
Bobty 1:1a59b4810261 33 logger.baud(115200);
Bobty 1:1a59b4810261 34
Bobty 1:1a59b4810261 35 // Add the Gatt characteristic for samples
aleksanb 0:8d7583961274 36 puck->addCharacteristic(
Bobty 1:1a59b4810261 37 SCORING_GATT_SERVICE,
Bobty 1:1a59b4810261 38 SAMPLES_GATT_CHARACTERISTIC,
Bobty 1:1a59b4810261 39 SAMPLES_LEN,
aleksanb 0:8d7583961274 40 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
aleksanb 0:8d7583961274 41
aleksanb 0:8d7583961274 42 // Initialize the puck
Bobty 1:1a59b4810261 43 puck->init(0xCD01);
Bobty 1:1a59b4810261 44
Bobty 1:1a59b4810261 45 // Start timer
Bobty 1:1a59b4810261 46 antiRepeatTimer.start();
aleksanb 0:8d7583961274 47
Bobty 1:1a59b4810261 48 // Wait for something to be found
Bobty 1:1a59b4810261 49 while(puck->drive())
Bobty 1:1a59b4810261 50 {
Bobty 1:1a59b4810261 51 int curTimerVal = antiRepeatTimer.read_ms();
Bobty 1:1a59b4810261 52 if ((lastSampleTime < curTimerVal) || (curTimerVal - lastSampleTime > MIN_MS_BETWEEN_SAMPLES))
Bobty 1:1a59b4810261 53 {
Bobty 1:1a59b4810261 54 // Check threshold
Bobty 1:1a59b4810261 55 unsigned short val = ain.read_u16();
Bobty 1:1a59b4810261 56 if(val > SAMPLE_THRESHOLD)
Bobty 1:1a59b4810261 57 {
Bobty 1:1a59b4810261 58 for (int i = 0; i < SAMPLES_LEN; i++)
Bobty 1:1a59b4810261 59 {
Bobty 1:1a59b4810261 60 SAMPLES_BUF[i] = ain.read_u16() - 760;
Bobty 1:1a59b4810261 61 wait_ms(10);
Bobty 1:1a59b4810261 62 }
Bobty 1:1a59b4810261 63
Bobty 1:1a59b4810261 64 // Set the value of the characteristic
Bobty 1:1a59b4810261 65 puck->updateCharacteristicValue(SAMPLES_GATT_CHARACTERISTIC, SAMPLES_BUF, SAMPLES_LEN);
Bobty 1:1a59b4810261 66
Bobty 1:1a59b4810261 67 // Display readings
Bobty 1:1a59b4810261 68 for (int j = 0; j < SAMPLES_LEN; j++)
Bobty 1:1a59b4810261 69 {
Bobty 1:1a59b4810261 70 LOG_INFO("%d ", SAMPLES_BUF[j]);
Bobty 1:1a59b4810261 71 }
Bobty 1:1a59b4810261 72 LOG_INFO("\n");
Bobty 1:1a59b4810261 73
Bobty 1:1a59b4810261 74 // Set timer to disallow repeated readings
Bobty 1:1a59b4810261 75 lastSampleTime = curTimerVal;
Bobty 1:1a59b4810261 76 }
Bobty 1:1a59b4810261 77 }
Bobty 1:1a59b4810261 78 }
Bobty 1:1a59b4810261 79 }