Test for acceleration sensor on the ObCP ENSMM
Dependencies: mbed LIS3DH_spi USBDevice
Diff: main.cpp
- Revision:
- 6:b0e1be376429
- Parent:
- 3:e439dd384d7e
--- a/main.cpp Fri Sep 02 11:23:57 2016 +0000 +++ b/main.cpp Wed Sep 30 11:27:51 2020 +0000 @@ -1,47 +1,146 @@ +//Includes #include "mbed.h" -#include "SimpleBLE.h" - -DigitalOut led(LED1); - -// 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"); +#include "LIS3DH.h" +#include "USBSerial.h" +//Accelerometer +#define MOSI PC_12 +#define MISO PC_11 +#define CS PC_5 +#define SCLK PC_10 +//Interrupt input +InterruptIn userb(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); +//USBSerial pc; //TX, RX +//Init accelerometer +LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G); +double accX, accY, accZ; +bool interrupt; +// 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); +} +//Interruption +void pressed(void) +{ + interrupt = 1; +} +//Main program +int main(int, char**) +{ + userb.fall(&pressed); // When button is pressed + interrupt = 0; + while (1) { + 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; + Red = 10 * abs(accX / 255.0f); + Green = 10 * abs(accY / 255.0f); + Blue = 10 * abs(accZ / 255.0f); + clrscr(); + homescr(); + // printf("\033[2J"); +//Print accelerations values + 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); + wait(0.5); -// 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! - -// 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 */); - -// 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; + if(interrupt == 1) { + interrupt = 0; + // printf("\033[2J"); + clrscr(); + homescr(); + gotoscr('0','0'); + pc.printf("Interrupt button pressed"); + Red = 0 ; + Green = 127 ; + Blue = 0 ; + wait(5); + } } } -// 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); + + + + + +/*//Includes + +#include "mbed.h" + +#include "LIS3DH.h" + +//Accelerometer + +#define MOSI PC_12 +#define MISO PC_11 +#define CS PC_5 +#define SCLK PC_10 + + +// GPIO set + +//Interrupt input + +//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 terminal; //TX, RX + +//Init accelerometer + +LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G); + +//Main program 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(); + + float x, y, z; + while (1) { - ble.waitForEvent(); + x = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15; + Red = sqrt(x*x)/4; + y = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15; + Green = sqrt(y*y)/4; + z = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15; + Blue = sqrt(z*z)/4; + wait(0.1); + } - -} \ No newline at end of file +} +*/ \ No newline at end of file