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
Diff: main.cpp
- Revision:
- 2:e400fd4f501b
- Parent:
- 1:1a59b4810261
- Child:
- 3:a155da1cbde3
--- a/main.cpp Wed Aug 20 09:49:41 2014 +0000 +++ b/main.cpp Thu Aug 21 07:48:18 2014 +0000 @@ -11,12 +11,27 @@ Puck* puck = &Puck::getPuck(); // Gatt characteristic and service UUIDs -const UUID SCORING_GATT_SERVICE = stringToUUID("nodule.scoring "); -const UUID SAMPLES_GATT_CHARACTERISTIC = stringToUUID("nodule.samples "); +const UUID SCORING_GATT_SERVICE = stringToUUID("nod.score1.serv "); +const UUID SAMPLES_GATT_CHARACTERISTIC = stringToUUID("nod.score1.samp1"); +const UUID THRESHOLD_GATT_CHARACTERISTIC = stringToUUID("nod.score1.thres"); +const UUID OFFSET_GATT_CHARACTERISTIC = stringToUUID("nod.score1.offs "); +const UUID DIVISOR_GATT_CHARACTERISTIC = stringToUUID("nod.score1.div "); +const UUID INTERVAL_US_GATT_CHARACTERISTIC = stringToUUID("nod.score1.intus"); const int SAMPLES_LEN = 20; uint8_t SAMPLES_BUF[SAMPLES_LEN]; -const int SAMPLE_THRESHOLD = 800; + +// Sample threshold +uint16_t sampleThreshold = 800; + +// Sample offset +uint16_t samplesOffset = 760; + +// Sample divisor +uint16_t samplesDivisor = 1; + +// Sample interval (uS) +uint32_t sampleIntervalUs = 10000; // Setup ADC AnalogIn ain(P0_1); @@ -26,6 +41,34 @@ int lastSampleTime = 0; const int MIN_MS_BETWEEN_SAMPLES = 2000; +void onThresholdSet(uint8_t* value) +{ + uint16_t threshold = value[0] * 256 + value[1]; + LOG_INFO("Threshold=%d\n", threshold); + sampleThreshold = threshold; +} + +void onOffsetSet(uint8_t* value) +{ + uint16_t offset = value[0] * 256 + value[1]; + LOG_INFO("Offset=%d\n", offset); + samplesOffset = offset; +} + +void onDivisorSet(uint8_t* value) +{ + uint16_t divisor = value[0] * 256 + value[1]; + LOG_INFO("Divisor=%d\n", divisor); + samplesDivisor = 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); + sampleIntervalUs = intervalUs; +} + int main(void) { @@ -39,6 +82,38 @@ SAMPLES_LEN, 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(sampleThreshold), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + puck->onCharacteristicWrite(&THRESHOLD_GATT_CHARACTERISTIC, onThresholdSet); + + // Add the Gatt characteristic for offset + puck->addCharacteristic( + SCORING_GATT_SERVICE, + OFFSET_GATT_CHARACTERISTIC, + sizeof(samplesOffset), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + puck->onCharacteristicWrite(&OFFSET_GATT_CHARACTERISTIC, onOffsetSet); + + // Add the Gatt characteristic for sample divisor + puck->addCharacteristic( + SCORING_GATT_SERVICE, + DIVISOR_GATT_CHARACTERISTIC, + sizeof(samplesDivisor), + 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); @@ -53,12 +128,12 @@ { // Check threshold unsigned short val = ain.read_u16(); - if(val > SAMPLE_THRESHOLD) + if(val > sampleThreshold) { for (int i = 0; i < SAMPLES_LEN; i++) { - SAMPLES_BUF[i] = ain.read_u16() - 760; - wait_ms(10); + SAMPLES_BUF[i] = (ain.read_u16() - samplesOffset) / samplesDivisor; + wait_us(sampleIntervalUs); } // Set the value of the characteristic @@ -67,7 +142,7 @@ // Display readings for (int j = 0; j < SAMPLES_LEN; j++) { - LOG_INFO("%d ", SAMPLES_BUF[j]); + LOG_INFO("%02x ", SAMPLES_BUF[j]); } LOG_INFO("\n");