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
- Committer:
- Bobty
- Date:
- 2014-08-23
- Revision:
- 7:dc08df448ddc
- Parent:
- 6:81494b318e55
- Child:
- 8:87a3708dca9c
File content as of revision 7:dc08df448ddc:
/** Scoring Device - for generic game scoring Using Puck BLE MBED library from Nordic Copyright (C) Nodule.io 2014 */ #define LOG_LEVEL_DEBUG #include "Puck.h" #include "SampleChannel.h" Puck* puck = &Puck::getPuck(); // Gatt characteristic and service UUIDs const UUID SCORING_GATT_SERVICE = stringToUUID("nod.score1.serv "); const UUID THRESHOLD_GATT_CHARACTERISTIC = stringToUUID("nod.score1.thres"); const UUID DIVISOR_GATT_CHARACTERISTIC = stringToUUID("nod.score1.div "); const UUID INTERVAL_US_GATT_CHARACTERISTIC = stringToUUID("nod.score1.intus"); const int NUM_SAMPLE_CHANNELS = 1; // Sample interval (uS) uint32_t sampleIntervalUs = 10000; // Interrupt driven ticker to do the sampling Timeout sampleTimeout; // Sample Channels SampleChannel sampleChannels[] = { SampleChannel(P0_1, stringToUUID("nod.score1.samp1"), &logger), SampleChannel(P0_2, stringToUUID("nod.score1.samp2"), &logger), SampleChannel(P0_3, stringToUUID("nod.score1.samp3"), &logger) }; // Timer to avoid repeat sampling Timer intervalTimer; int lastTriggerTime = 0; int lastSampleTime = 0; const int MIN_MS_BETWEEN_SAMPLES = 2000; // Function called in interrupt driven timeout to handle sampling static volatile int serviceCount = 0; void SampleService() { serviceCount++; sampleTimeout.attach_us(&SampleService, sampleIntervalUs); return; // service all channel's state machines bool isAnyChannelSampling = false; for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) { sampleChannels[chanIdx].Service(); if (sampleChannels[chanIdx].IsSampling()) isAnyChannelSampling = true; } if (!isAnyChannelSampling) { int curTimerVal = intervalTimer.read_ms(); if ((lastTriggerTime < curTimerVal) || (curTimerVal - lastTriggerTime > MIN_MS_BETWEEN_SAMPLES)) { // check each channel to see if it's been triggered bool anythingTriggered = false; for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) { if (sampleChannels[chanIdx].CheckTrigger()) { anythingTriggered = true; LOG_INFO("Triggered\n"); break; } } if(anythingTriggered) { for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) { sampleChannels[chanIdx].StartSampling(); } // Set timer to disallow repeated readings lastTriggerTime = curTimerVal; } } } } void onThresholdSet(uint8_t* value) { uint16_t threshold = value[0] * 256 + value[1]; LOG_INFO("Threshold=%d\n", threshold); for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) sampleChannels[chanIdx].SetThreshold(threshold); } void onDivisorSet(uint8_t* value) { uint16_t divisor = value[0] * 256 + value[1]; LOG_INFO("Divisor=%d\n", divisor); for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) sampleChannels[chanIdx].SetDivisor(divisor); } void onIntervalSet(uint8_t* value) { uint32_t intervalUs = (value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3]; LOG_INFO("SampleInterval(uS)=%d\n", intervalUs); if (intervalUs <= 1000000) { sampleIntervalUs = intervalUs; // sampleTicker.detach(); // sampleTicker.attach_us(&SampleService, sampleIntervalUs); } } int main(void) { // Set baud rate logger.baud(115200); // Add the Gatt characteristic for samples for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) { puck->addCharacteristic( SCORING_GATT_SERVICE, sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamplesLen(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); } // Add the Gatt characteristic for threshold puck->addCharacteristic( SCORING_GATT_SERVICE, THRESHOLD_GATT_CHARACTERISTIC, sizeof(uint16_t), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); puck->onCharacteristicWrite(&THRESHOLD_GATT_CHARACTERISTIC, onThresholdSet); // Add the Gatt characteristic for sample divisor puck->addCharacteristic( SCORING_GATT_SERVICE, DIVISOR_GATT_CHARACTERISTIC, sizeof(uint16_t), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); puck->onCharacteristicWrite(&DIVISOR_GATT_CHARACTERISTIC, onDivisorSet); // Add the Gatt characteristic for sample interval (us) puck->addCharacteristic( SCORING_GATT_SERVICE, INTERVAL_US_GATT_CHARACTERISTIC, sizeof(sampleIntervalUs), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); puck->onCharacteristicWrite(&INTERVAL_US_GATT_CHARACTERISTIC, onIntervalSet); // Initialize the puck puck->init(0xCD01); // Start timer intervalTimer.start(); // Start timeout to service the sampling sampleTimeout.attach_us(&SampleService, sampleIntervalUs); // Wait for something to be found unsigned int lastPuckDriveTime = 0; unsigned int driveLoops = 0; unsigned int lastDriveLoops = 0; unsigned int lastServiceCount = 0; while(true) { // Service the puck puck->drive(); driveLoops++; // Handle 1 second updates unsigned int nowTime = intervalTimer.read_ms(); if ((nowTime - lastPuckDriveTime >= 1000) || (nowTime < lastPuckDriveTime)) { unsigned int elapsed = nowTime - lastPuckDriveTime; LOG_INFO("%u E%u C%u DC%u TC%u DTC%u L%u DL%u\n", nowTime, elapsed, serviceCount, serviceCount-lastServiceCount, nowTime/serviceCount, elapsed/(serviceCount-lastServiceCount), driveLoops, driveLoops-lastDriveLoops); lastPuckDriveTime = intervalTimer.read_ms(); lastDriveLoops = driveLoops; lastServiceCount = serviceCount; } continue; // Check for data ready for (int chanIdx = 0; chanIdx < NUM_SAMPLE_CHANNELS; chanIdx++) { if (sampleChannels[chanIdx].AreSamplesReady()) { // Set the value of the characteristic //puck->updateCharacteristicValue(sampleChannels[chanIdx].GetUUID(), sampleChannels[chanIdx].GetSamples(), sampleChannels[chanIdx].GetSamplesLen()); sampleChannels[chanIdx].StopSampling(); LOG_INFO("StopSampling\n"); } } } }