Pull request for i.a. sensor buffer template
Dependencies: BLE_API MPU6050 mbed nRF51822
main.cpp
- Committer:
- pietermaljaars
- Date:
- 2018-09-18
- Revision:
- 3:7875f062a4ea
- Parent:
- 2:bf1a19d489eb
- Child:
- 4:aea4ff8e52ef
File content as of revision 3:7875f062a4ea:
#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" #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 "ALTEN" #define MODELNUMBER "IoT BLE (PM)" #define SERIALNUMBER "serialnumber" #define HARDWAREREVISION "demo1" #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; void check_i2c_bus(void); unsigned short inv_orientation_matrix_to_scalar( const signed char *mtx); 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; } void tap_cb(unsigned char direction, unsigned char count) { LOG("Tap motion detected\n"); } void android_orient_cb(unsigned char orientation) { LOG("Oriention changed\n"); } 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.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, name, sizeof(name)); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); DFUService dfu(ble); UARTService uartService(ble); uartServicePtr = &uartService; //uartService.retargetStdout(); BatteryService battery(ble); DeviceInformationService deviceInfo(ble, MANUFACTURER, MODELNUMBER, SERIALNUMBER, HARDWAREREVISION, FIRMWAREREVISION, SOFTWAREREVISION); 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) { LOG("Start measuring the acceleration\n"); float a[3]; mpu.getAccelero(a); //writing current accelerometer and gyro position LOG("%.2f;%.2f;%.2f\n", a[0], a[1], a[2]); startMeasure = false; float temp = mpu.getTemp(); LOG("Temp = %f\n", temp); } } } /* These next two functions converts the orientation matrix (see * gyro_orientation) to a scalar representation for use by the DMP. * NOTE: These functions are borrowed from Invensense's MPL. */ static inline unsigned short inv_row_2_scale(const signed char *row) { unsigned short b; if (row[0] > 0) b = 0; else if (row[0] < 0) b = 4; else if (row[1] > 0) b = 1; else if (row[1] < 0) b = 5; else if (row[2] > 0) b = 2; else if (row[2] < 0) b = 6; else b = 7; // error return b; } unsigned short inv_orientation_matrix_to_scalar( const signed char *mtx) { unsigned short scalar; /* XYZ 010_001_000 Identity Matrix XZY 001_010_000 YXZ 010_000_001 YZX 000_010_001 ZXY 001_000_010 ZYX 000_001_010 */ scalar = inv_row_2_scale(mtx); scalar |= inv_row_2_scale(mtx + 3) << 3; scalar |= inv_row_2_scale(mtx + 6) << 6; return scalar; }