![](/media/cache/group/logo_mPY59fz.jpg.50x50_q85.jpg)
BLE test of ObCP ENSMM
Dependencies: mbed SimpleBLE X_NUCLEO_IDB0XA1 LIS3DH_spi USBDevice
Diff: main.cpp
- Revision:
- 6:1670244c4eb4
- Parent:
- 3:e439dd384d7e
--- a/main.cpp Fri Sep 02 11:23:57 2016 +0000 +++ b/main.cpp Wed Nov 27 15:27:56 2019 +0000 @@ -1,47 +1,146 @@ +//Includes + #include "mbed.h" #include "SimpleBLE.h" +#include "LIS3DH.h" +#include "USBSerial.h" -DigitalOut led(LED1); +//Accelerometer + +#define MOSI PC_12 +#define MISO PC_11 +#define CS PC_5 +#define SCLK PC_10 + +//Init simpleBLE + +SimpleBLE ble("ObCP_ENSMM_CROC"); + + +// GPIO set + +//Interrupt input + +InterruptIn user1(PC_13); //User1 + +//PWM output + +PwmOut PWMoutput(PB_1); //Main PWM output +PwmOut Green(PC_8); //PWM Red LED +PwmOut Red(PC_6); //PWM Green LED +PwmOut Blue(PC_9); //PWM Blue LED + +//USART + +USBSerial pc(0x1f00, 0x2012, 0x0001, false); + +//Init accelerometer -// The first thing we need to do is create a SimpleBLE instance: -// * first argument is the device name -// * second is the advertisement interval in ms. (default 1000 ms.) -SimpleBLE ble("DEVICE_NAME"); +LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G); + +// Characteristics Accelerometer input + +SimpleChar<float> accX = ble.readOnly_float(0xA000, 0xA002); +SimpleChar<float> accY = ble.readOnly_float(0xA000, 0xA003); +SimpleChar<float> accZ = ble.readOnly_float(0xA000, 0xA004); + +// Clear the screen +void clrscr () +{ + char clrscr[] = {0x1B, '[', '2', 'J', 0}; + pc.printf(clrscr); +} +//Home the cursor +void homescr() +{ + char homescr[] = {0x1B, '[', 'H', 0}; + pc.printf(homescr); +} +//goto specified line an column +void gotoscr(int line, int column) +{ + char scr[] = {0x1B, '[', 0x00,';',0x00, 'H', 0}; + scr[2] = line; + scr[4] = column; + pc.printf(scr); +} + +// When characteristic LED RGB changing + +void LEDupdate(uint32_t newColor) +{ + // read individual bytes + uint8_t* channels = (uint8_t*)&newColor; -// Now we can declare some variables that we want to expose. -// After you created the variable you can use it like any other var, -// but it's value will be automatically updated over Bluetooth! + // cast to float, as PwmOut expects a value between 0.0f and 1.0f + Red = static_cast<float>(channels[0]) / 255.0f; + Green = static_cast<float>(channels[1]) / 255.0f; + Blue = static_cast<float>(channels[2]) / 255.0f; + gotoscr('5','0'); + pc.printf("PWM Red = "); + pc.printf("%5.2f",Red); + gotoscr('6','0'); + pc.printf("PWM Green = "); + pc.printf("%5.2f",Green); + gotoscr('7','0'); + pc.printf("PWM Blue = "); + pc.printf("%5.2f",Blue); +} -// F.e. here we declare service 0x180d (heartrate), char 0x2a37 (curr. value) as uint8_t -SimpleChar<uint8_t> heartrate = ble.readOnly_u8(0x180D, 0x2A37, true /* notify */, 100 /* default value */); +// When characteristic PWM output changing + +void PWMupdate(uint8_t pwmvalue) +{ + + // cast to float, as PwmOut expects a value between 0.0f and 1.0f + PWMoutput = static_cast<float>(pwmvalue) / 255.0f; + + gotoscr('4','0'); + pc.printf("PWM = "); + pc.printf("%5.2f",PWMoutput); +} + +// When characteristic input changing +void Accupdate() +{ -// now we can use this variable everywhere in our code like a normal uint8_t -void updateHeartrate() { - led = !led; // keep-alive LED - // we just loop between 100 and 180 - heartrate = heartrate + 1; - if (heartrate > 180) { - heartrate = 100; + accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15; + accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15; + accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15; + + + gotoscr('0','0'); + pc.printf("X acceleration = "); + pc.printf("%5.2f",accX); + gotoscr('2','0'); + pc.printf("Y acceleration = "); + pc.printf("%5.2f",accY); + gotoscr('3','0'); + pc.printf("Z acceleration = "); + pc.printf("%5.2f",accZ); +} + +// Characteritic PWM LED RGB +SimpleChar<uint32_t> color = ble.writeOnly_u32(0x6200, 0x6201, &LEDupdate); + +// Characteristic PWM output +SimpleChar<uint8_t> pwmout = ble.writeOnly_u8(0xA000, 0xA001, &PWMupdate); + + +//Main program + +int main(int, char**) +{ + + ble.start(); + clrscr(); + homescr(); + pc.printf("BLE started"); + Ticker t; + t.attach(&Accupdate, 5.0f); + + while (1) { + ble.waitForEvent(); + } } - -// And here we create a custom service (0x9310) and char (0x9311) with a callback -void callback(uint32_t newValue) { - // whenever someone updates this var over Bluetooth, this function will be called - printf("My value was updated to %d\n", newValue); -} -// FYI, you can also use long UUID strings here instead of short services :-) -SimpleChar<uint32_t> writeMe = ble.readWrite_u32(0x9310, 0x9311, &callback); - -int main(int, char**) { - // update the heart rate every second - Ticker t; - t.attach(updateHeartrate, 1.0f); - - // here's how we kick off our loop - ble.start(); - while (1) { - ble.waitForEvent(); - } - -} \ No newline at end of file