Pull request for i.a. sensor buffer template
Dependencies: BLE_API MPU6050 mbed nRF51822
main.cpp
- Committer:
- Pieter Maljaars
- Date:
- 2018-10-16
- Revision:
- 8:d1f5c8801d59
- Parent:
- 7:d92f1383bd81
- Child:
- 9:e9d6a9758cf7
File content as of revision 8:d1f5c8801d59:
#include "mbed.h" #include "nrf51.h" #include "nrf51_bitfields.h" #include "MPU6050.h" #include "BLE.h" #include "DFUService.h" #include "UARTService.h" #include "BatteryService.h" #include "DeviceInformationService.h" #include "TemperatureService.h" #include "AccelerationService.h" #define LOG(...) { pc.printf(__VA_ARGS__); } #define LED_GREEN p21 #define LED_RED p22 #define LED_BLUE p23 #define BUTTON_PIN p17 #define BATTERY_PIN p1 #define MPU6050_SDA p12 #define MPU6050_SCL p13 #define UART_TX p9 #define UART_RX p11 #define UART_CTS p8 #define UART_RTS p10 /* Starting sampling rate. */ #define DEFAULT_MPU_HZ (100) #define MANUFACTURER "PM @ ALTEN" #define MODELNUMBER "IoT BLE" #define SERIALNUMBER "123456" #define HARDWAREREVISION "Seeed demo IoT" #define FIRMWAREREVISION "1.0" #define SOFTWAREREVISION "1.0" DigitalOut blue(LED_BLUE); DigitalOut green(LED_GREEN); DigitalOut red(LED_RED); InterruptIn button(BUTTON_PIN); AnalogIn battery(BATTERY_PIN); Serial pc(UART_TX, UART_RX); MPU6050 mpu(MPU6050_SDA, MPU6050_SCL); InterruptIn motion_probe(p14); int read_none_count = 0; BLEDevice ble; UARTService *uartServicePtr; // variables to monitor the battery voltage volatile float batteryVoltage = 100.0f; volatile bool batteryVoltageChanged = false; volatile bool startMeasure = false; volatile bool bleIsConnected = false; volatile uint8_t tick_event = 0; int16_t ax, ay, az; int16_t gx, gy, gz; //char acceleration128bitUUIDlist[] = {0x5D,0x34,0xE2,0xF0 ,0x3D,0xD2 ,0x07,0xAC ,0x38,0xF1 ,0xBB,0xAD,0x12,0x0E,0xF8,0x53}; char acceleration128bitUUIDlist[] = { 0x53, 0xF8, 0x0E, 0x12, 0xAD, 0xBB, 0xF1, 0x38, 0xAC, 0x07, 0xD2, 0x3D, 0xF0, 0xE2, 0x34, 0x5D }; void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { LOG("Connected!\n"); bleIsConnected = true; } void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams) { LOG("Disconnected!\n"); LOG("Restarting the advertising process\n"); ble.startAdvertising(); bleIsConnected = false; } void tick(void) { green = !green; startMeasure = true; // notify the main-loop to start measuring the MPU6050 } // timer callback function to measure the ADC battery level void batteryMonitorCallback(void) { float sample; sample = battery.read(); /* cannot use (uart.)printf() in a ISR like this. */ batteryVoltage = sample; batteryVoltageChanged = true; } void detect(void) { LOG("Button pressed\n"); blue = !blue; } int main(void) { blue = 1; green = 1; red = 1; pc.baud(115200); wait(1); LOG("---- Seeed Tiny BLE ----\n"); LOG("MPU6050 testConnection \n"); bool mpu6050TestResult = mpu.testConnection(); if(mpu6050TestResult) { LOG("MPU6050 test passed \n"); } else { LOG("MPU6050 test failed \n"); } Ticker ticker; ticker.attach(tick, 5); Ticker batteryMonitorTicker; batteryMonitorTicker.attach(batteryMonitorCallback, 60.0f); button.fall(detect); LOG("Initialising the nRF51822\n"); ble.init(); ble.gap().onDisconnection(disconnectionCallback); ble.gap().onConnection(connectionCallback); uint8_t name[] = "iot aabbccddeeff"; Gap::AddressType_t addr_type; Gap::Address_t address; ble_error_t error = ble.gap().getAddress(&addr_type, address); if (error == BLE_ERROR_NONE) { for (int i = 5; i >= 0; i--){ char buffer[3]; sprintf(buffer, "%02x", address[i]); name[4 + ((5-i)*2)] = buffer[0]; name[4 + ((5-i)*2) + 1] = buffer[1]; } } LOG("name = %s\n", name); /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)acceleration128bitUUIDlist, sizeof(acceleration128bitUUIDlist)); ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, name, sizeof(name)); DFUService dfu(ble); UARTService uartService(ble); uartServicePtr = &uartService; //uartService.retargetStdout(); BatteryService battery(ble); DeviceInformationService deviceInfo(ble, MANUFACTURER, MODELNUMBER, SERIALNUMBER, HARDWAREREVISION, FIRMWAREREVISION, SOFTWAREREVISION); TemperatureService tempService(ble); AccelerationService accelerationService(ble); ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ ble.gap().startAdvertising(); while (true) { ble.waitForEvent(); // update battery level after the level is measured if (batteryVoltageChanged == true) { LOG("VBat: %4.3f, ADC: %4.3f, Vadc: %4.3f\n", batteryVoltage*2.0f, batteryVoltage, batteryVoltage*3.3f); battery.updateBatteryLevel((uint8_t)(batteryVoltage*100.0f)); // input is 0-1.0 of 3.3V -> *100 = percentage of 3.3V batteryVoltageChanged = false; } else if (startMeasure == true) { //float a[3]; //mpu.getAccelero(a); //LOG("Acceleration %.2f;%.2f;%.2f\n", a[0], a[1], a[2]); int a[3]; mpu.getAcceleroRaw(a); accelerationService.addAcceleration((int16_t*)a); LOG("Acceleration %d;%d;%d\n", a[0], a[1], a[2]); float temp = mpu.getTemp(); LOG("Temp = %.01f\n", temp); int16_t tempRaw = mpu.getTempRaw(); // convert into 0.1 degrees Celsius tempRaw *= 10; tempRaw += 5210; tempRaw /= 340; tempRaw += 350; // update BLE tmperature tempService.updateTemperature(tempRaw); startMeasure = false; } } }