Broadcasts raw accelerometer values
Dependencies: BLE_API MMA8452_tag_private mbed nRF51822
Fork of tag_final by
Diff: main.cpp
- Revision:
- 0:4c7b37b8faad
- Child:
- 1:1c14c1d3ce09
diff -r 000000000000 -r 4c7b37b8faad main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jan 08 20:41:56 2016 +0000 @@ -0,0 +1,86 @@ + +#include "mbed.h" +#include "BLEDevice.h" +#include "ADXL345.h" + +// The accelerometer sample rate defines how often advertising +// packets need to be sent. Each packet can hold 9 samples. +#define ACC_SAMPLE_RATE 9 + +// How many times to re-advertise the same packet. For example, +// if for the given sample rate an advertising packet needs to +// be sent every second, and ADV_RESEND_COUNT of 2 will send +// 2 packets per second. +#define ADV_RESEND_COUNT 10 + +BLEDevice ble; +ADXL345 acc(P0_9, P0_11, P0_8, P0_10); +DigitalOut led(P0_19); + +void accRead() { + int data[3]; + static int8_t buffer[28]; + static uint8_t index = 0; + static uint8_t count; + + acc.getOutput(data); + + // Fill buffer + buffer[index] = (int8_t)(data[0] >> 8); + buffer[index+1] = (int8_t)(data[1] >> 8); + buffer[index+2] = (int8_t)(data[2] >> 8); + index += 3; + + // If buffer is full (9 readings) update advertising packet + if (index >= 27) { + index = 0; // Reset buffer index + count += 1; // Increase packet count + buffer[27] = count; // Append packet count + ble.clearScanResponse(); // Clear advertising packet and add data + ble.accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t*)buffer, sizeof(buffer)); + ble.setAdvertisingPayload(); + led = 0; // Flash led + wait(0.01); + led = 1; + } +} + + +int main(void) { + // Turn off led + led = 0; + + // Calculate advertising interval + uint16_t adv_interval; + adv_interval = (uint16_t)((9.0 / (float)ACC_SAMPLE_RATE) * 1000.0 / (float)ADV_RESEND_COUNT); + + // Initialize BLE + uint8_t tagAddress[6]; + uint8_t tagName[8]; + ble.init(); // Initialize BLE stack + ble.setTxPower(4); // Set power level (in dB) + ble.setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, NULL); // Static random address + ble.getAddress(NULL, tagAddress); // Get random address from stack + sprintf((char*)tagName, "TAG_%2X%2X", tagAddress[1], tagAddress[0]); + ble.accumulateAdvertisingPayload( // Advertise as BLE + GapAdvertisingData::BREDR_NOT_SUPPORTED | + GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.accumulateAdvertisingPayload( // Set name + GapAdvertisingData::COMPLETE_LOCAL_NAME, + tagName, + sizeof(tagName)); + ble.setAdvertisingInterval(adv_interval); // Advertising interval + ble.startAdvertising(); // Start advertising + + // Setup ticker to read accelerometer + acc.setPowerControl(0x00); // Standby for configuration + acc.setDataFormatControl(0x05); // +-4g, left-justified, + acc.setDataRate(ADXL345_12HZ5); // 12.5Hz Internal Sample Rate + acc.setPowerControl(0x08); // Measurement mode + Ticker acc_read; + acc_read.attach(&accRead, (1.0 / (float)ACC_SAMPLE_RATE)); + + while(1) { + ble.waitForEvent(); // Sleep + } +}