![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test changement de nom
Dependencies: mbed SimpleBLE Roller_C LIS3DH_spi USBDevice
main.cpp@8:2890498e11b6, 2019-12-27 (annotated)
- Committer:
- MaxenceGalopin
- Date:
- Fri Dec 27 15:30:51 2019 +0000
- Revision:
- 8:2890498e11b6
- Parent:
- 6:1670244c4eb4
- Child:
- 9:111c818e8740
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jimbaud | 6:1670244c4eb4 | 1 | //Includes |
jimbaud | 6:1670244c4eb4 | 2 | |
janjongboom | 0:ba1c49874d3c | 3 | #include "mbed.h" |
janjongboom | 0:ba1c49874d3c | 4 | #include "SimpleBLE.h" |
jimbaud | 6:1670244c4eb4 | 5 | #include "LIS3DH.h" |
jimbaud | 6:1670244c4eb4 | 6 | #include "USBSerial.h" |
janjongboom | 0:ba1c49874d3c | 7 | |
jimbaud | 6:1670244c4eb4 | 8 | //Accelerometer |
jimbaud | 6:1670244c4eb4 | 9 | |
jimbaud | 6:1670244c4eb4 | 10 | #define MOSI PC_12 |
jimbaud | 6:1670244c4eb4 | 11 | #define MISO PC_11 |
jimbaud | 6:1670244c4eb4 | 12 | #define CS PC_5 |
jimbaud | 6:1670244c4eb4 | 13 | #define SCLK PC_10 |
jimbaud | 6:1670244c4eb4 | 14 | |
jimbaud | 6:1670244c4eb4 | 15 | //Init simpleBLE |
jimbaud | 6:1670244c4eb4 | 16 | |
MaxenceGalopin | 8:2890498e11b6 | 17 | SimpleBLE ble("ObCP_ENSMM_CROC_test_max_beny"); |
jimbaud | 6:1670244c4eb4 | 18 | |
jimbaud | 6:1670244c4eb4 | 19 | |
jimbaud | 6:1670244c4eb4 | 20 | // GPIO set |
jimbaud | 6:1670244c4eb4 | 21 | |
jimbaud | 6:1670244c4eb4 | 22 | //Interrupt input |
jimbaud | 6:1670244c4eb4 | 23 | |
jimbaud | 6:1670244c4eb4 | 24 | InterruptIn user1(PC_13); //User1 |
jimbaud | 6:1670244c4eb4 | 25 | |
jimbaud | 6:1670244c4eb4 | 26 | //PWM output |
jimbaud | 6:1670244c4eb4 | 27 | |
jimbaud | 6:1670244c4eb4 | 28 | PwmOut PWMoutput(PB_1); //Main PWM output |
jimbaud | 6:1670244c4eb4 | 29 | PwmOut Green(PC_8); //PWM Red LED |
jimbaud | 6:1670244c4eb4 | 30 | PwmOut Red(PC_6); //PWM Green LED |
jimbaud | 6:1670244c4eb4 | 31 | PwmOut Blue(PC_9); //PWM Blue LED |
jimbaud | 6:1670244c4eb4 | 32 | |
jimbaud | 6:1670244c4eb4 | 33 | //USART |
jimbaud | 6:1670244c4eb4 | 34 | |
jimbaud | 6:1670244c4eb4 | 35 | USBSerial pc(0x1f00, 0x2012, 0x0001, false); |
jimbaud | 6:1670244c4eb4 | 36 | |
jimbaud | 6:1670244c4eb4 | 37 | //Init accelerometer |
janjongboom | 2:12a235e7691a | 38 | |
jimbaud | 6:1670244c4eb4 | 39 | LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G); |
jimbaud | 6:1670244c4eb4 | 40 | |
jimbaud | 6:1670244c4eb4 | 41 | // Characteristics Accelerometer input |
jimbaud | 6:1670244c4eb4 | 42 | |
jimbaud | 6:1670244c4eb4 | 43 | SimpleChar<float> accX = ble.readOnly_float(0xA000, 0xA002); |
jimbaud | 6:1670244c4eb4 | 44 | SimpleChar<float> accY = ble.readOnly_float(0xA000, 0xA003); |
jimbaud | 6:1670244c4eb4 | 45 | SimpleChar<float> accZ = ble.readOnly_float(0xA000, 0xA004); |
jimbaud | 6:1670244c4eb4 | 46 | |
jimbaud | 6:1670244c4eb4 | 47 | // Clear the screen |
jimbaud | 6:1670244c4eb4 | 48 | void clrscr () |
jimbaud | 6:1670244c4eb4 | 49 | { |
jimbaud | 6:1670244c4eb4 | 50 | char clrscr[] = {0x1B, '[', '2', 'J', 0}; |
jimbaud | 6:1670244c4eb4 | 51 | pc.printf(clrscr); |
jimbaud | 6:1670244c4eb4 | 52 | } |
jimbaud | 6:1670244c4eb4 | 53 | //Home the cursor |
jimbaud | 6:1670244c4eb4 | 54 | void homescr() |
jimbaud | 6:1670244c4eb4 | 55 | { |
jimbaud | 6:1670244c4eb4 | 56 | char homescr[] = {0x1B, '[', 'H', 0}; |
jimbaud | 6:1670244c4eb4 | 57 | pc.printf(homescr); |
jimbaud | 6:1670244c4eb4 | 58 | } |
jimbaud | 6:1670244c4eb4 | 59 | //goto specified line an column |
jimbaud | 6:1670244c4eb4 | 60 | void gotoscr(int line, int column) |
jimbaud | 6:1670244c4eb4 | 61 | { |
jimbaud | 6:1670244c4eb4 | 62 | char scr[] = {0x1B, '[', 0x00,';',0x00, 'H', 0}; |
jimbaud | 6:1670244c4eb4 | 63 | scr[2] = line; |
jimbaud | 6:1670244c4eb4 | 64 | scr[4] = column; |
jimbaud | 6:1670244c4eb4 | 65 | pc.printf(scr); |
jimbaud | 6:1670244c4eb4 | 66 | } |
jimbaud | 6:1670244c4eb4 | 67 | |
jimbaud | 6:1670244c4eb4 | 68 | // When characteristic LED RGB changing |
jimbaud | 6:1670244c4eb4 | 69 | |
jimbaud | 6:1670244c4eb4 | 70 | void LEDupdate(uint32_t newColor) |
jimbaud | 6:1670244c4eb4 | 71 | { |
jimbaud | 6:1670244c4eb4 | 72 | // read individual bytes |
jimbaud | 6:1670244c4eb4 | 73 | uint8_t* channels = (uint8_t*)&newColor; |
janjongboom | 0:ba1c49874d3c | 74 | |
jimbaud | 6:1670244c4eb4 | 75 | // cast to float, as PwmOut expects a value between 0.0f and 1.0f |
jimbaud | 6:1670244c4eb4 | 76 | Red = static_cast<float>(channels[0]) / 255.0f; |
jimbaud | 6:1670244c4eb4 | 77 | Green = static_cast<float>(channels[1]) / 255.0f; |
jimbaud | 6:1670244c4eb4 | 78 | Blue = static_cast<float>(channels[2]) / 255.0f; |
jimbaud | 6:1670244c4eb4 | 79 | gotoscr('5','0'); |
jimbaud | 6:1670244c4eb4 | 80 | pc.printf("PWM Red = "); |
jimbaud | 6:1670244c4eb4 | 81 | pc.printf("%5.2f",Red); |
jimbaud | 6:1670244c4eb4 | 82 | gotoscr('6','0'); |
jimbaud | 6:1670244c4eb4 | 83 | pc.printf("PWM Green = "); |
jimbaud | 6:1670244c4eb4 | 84 | pc.printf("%5.2f",Green); |
jimbaud | 6:1670244c4eb4 | 85 | gotoscr('7','0'); |
jimbaud | 6:1670244c4eb4 | 86 | pc.printf("PWM Blue = "); |
jimbaud | 6:1670244c4eb4 | 87 | pc.printf("%5.2f",Blue); |
jimbaud | 6:1670244c4eb4 | 88 | } |
janjongboom | 0:ba1c49874d3c | 89 | |
jimbaud | 6:1670244c4eb4 | 90 | // When characteristic PWM output changing |
jimbaud | 6:1670244c4eb4 | 91 | |
jimbaud | 6:1670244c4eb4 | 92 | void PWMupdate(uint8_t pwmvalue) |
jimbaud | 6:1670244c4eb4 | 93 | { |
jimbaud | 6:1670244c4eb4 | 94 | |
jimbaud | 6:1670244c4eb4 | 95 | // cast to float, as PwmOut expects a value between 0.0f and 1.0f |
jimbaud | 6:1670244c4eb4 | 96 | PWMoutput = static_cast<float>(pwmvalue) / 255.0f; |
jimbaud | 6:1670244c4eb4 | 97 | |
jimbaud | 6:1670244c4eb4 | 98 | gotoscr('4','0'); |
jimbaud | 6:1670244c4eb4 | 99 | pc.printf("PWM = "); |
jimbaud | 6:1670244c4eb4 | 100 | pc.printf("%5.2f",PWMoutput); |
jimbaud | 6:1670244c4eb4 | 101 | } |
jimbaud | 6:1670244c4eb4 | 102 | |
jimbaud | 6:1670244c4eb4 | 103 | // When characteristic input changing |
jimbaud | 6:1670244c4eb4 | 104 | void Accupdate() |
jimbaud | 6:1670244c4eb4 | 105 | { |
janjongboom | 0:ba1c49874d3c | 106 | |
jimbaud | 6:1670244c4eb4 | 107 | accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15; |
jimbaud | 6:1670244c4eb4 | 108 | accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15; |
jimbaud | 6:1670244c4eb4 | 109 | accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15; |
jimbaud | 6:1670244c4eb4 | 110 | |
jimbaud | 6:1670244c4eb4 | 111 | |
jimbaud | 6:1670244c4eb4 | 112 | gotoscr('0','0'); |
jimbaud | 6:1670244c4eb4 | 113 | pc.printf("X acceleration = "); |
jimbaud | 6:1670244c4eb4 | 114 | pc.printf("%5.2f",accX); |
jimbaud | 6:1670244c4eb4 | 115 | gotoscr('2','0'); |
jimbaud | 6:1670244c4eb4 | 116 | pc.printf("Y acceleration = "); |
jimbaud | 6:1670244c4eb4 | 117 | pc.printf("%5.2f",accY); |
jimbaud | 6:1670244c4eb4 | 118 | gotoscr('3','0'); |
jimbaud | 6:1670244c4eb4 | 119 | pc.printf("Z acceleration = "); |
jimbaud | 6:1670244c4eb4 | 120 | pc.printf("%5.2f",accZ); |
jimbaud | 6:1670244c4eb4 | 121 | } |
jimbaud | 6:1670244c4eb4 | 122 | |
jimbaud | 6:1670244c4eb4 | 123 | // Characteritic PWM LED RGB |
jimbaud | 6:1670244c4eb4 | 124 | SimpleChar<uint32_t> color = ble.writeOnly_u32(0x6200, 0x6201, &LEDupdate); |
jimbaud | 6:1670244c4eb4 | 125 | |
jimbaud | 6:1670244c4eb4 | 126 | // Characteristic PWM output |
jimbaud | 6:1670244c4eb4 | 127 | SimpleChar<uint8_t> pwmout = ble.writeOnly_u8(0xA000, 0xA001, &PWMupdate); |
jimbaud | 6:1670244c4eb4 | 128 | |
jimbaud | 6:1670244c4eb4 | 129 | |
jimbaud | 6:1670244c4eb4 | 130 | //Main program |
jimbaud | 6:1670244c4eb4 | 131 | |
jimbaud | 6:1670244c4eb4 | 132 | int main(int, char**) |
jimbaud | 6:1670244c4eb4 | 133 | { |
jimbaud | 6:1670244c4eb4 | 134 | |
jimbaud | 6:1670244c4eb4 | 135 | ble.start(); |
jimbaud | 6:1670244c4eb4 | 136 | clrscr(); |
jimbaud | 6:1670244c4eb4 | 137 | homescr(); |
jimbaud | 6:1670244c4eb4 | 138 | pc.printf("BLE started"); |
jimbaud | 6:1670244c4eb4 | 139 | Ticker t; |
jimbaud | 6:1670244c4eb4 | 140 | t.attach(&Accupdate, 5.0f); |
jimbaud | 6:1670244c4eb4 | 141 | |
jimbaud | 6:1670244c4eb4 | 142 | while (1) { |
jimbaud | 6:1670244c4eb4 | 143 | ble.waitForEvent(); |
jimbaud | 6:1670244c4eb4 | 144 | |
janjongboom | 0:ba1c49874d3c | 145 | } |
janjongboom | 0:ba1c49874d3c | 146 | } |