Code final

Dependencies:   mbed SimpleBLE X_NUCLEO_IDB0XA1 LIS3DH_spi

Committer:
Nthnthj
Date:
Fri Jan 17 12:42:51 2020 +0000
Revision:
9:3d83d0b410b8
Parent:
7:f5e10b18984d
Child:
10:a15e07c7ad61
yolo

Who changed what in which revision?

UserRevisionLine numberNew 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"
Nthnthj 9:3d83d0b410b8 6 #include "stdlib.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
Nthnthj 9:3d83d0b410b8 15 //Bluetooth hc05-6
Nthnthj 9:3d83d0b410b8 16
Nthnthj 9:3d83d0b410b8 17 #define TX PA_15
Nthnthj 9:3d83d0b410b8 18 #define RX PB_7
Nthnthj 9:3d83d0b410b8 19
jimbaud 6:1670244c4eb4 20 //Init simpleBLE
jimbaud 6:1670244c4eb4 21
Nthnthj 9:3d83d0b410b8 22 //SimpleBLE ble("ObCP_Roller_Catcher2");
jimbaud 6:1670244c4eb4 23
jimbaud 6:1670244c4eb4 24
jimbaud 6:1670244c4eb4 25 // GPIO set
jimbaud 6:1670244c4eb4 26
jimbaud 6:1670244c4eb4 27 //Interrupt input
jimbaud 6:1670244c4eb4 28
jimbaud 6:1670244c4eb4 29 InterruptIn user1(PC_13); //User1
Nthnthj 9:3d83d0b410b8 30 InterruptIn boutton1(D3);
Nthnthj 9:3d83d0b410b8 31 InterruptIn boutton2(D4);
Nthnthj 9:3d83d0b410b8 32 InterruptIn event(A0);
Nthnthj 9:3d83d0b410b8 33
Nthnthj 9:3d83d0b410b8 34 Timer timer;
Nthnthj 9:3d83d0b410b8 35
Nthnthj 9:3d83d0b410b8 36 DigitalOut led1(D14);
Nthnthj 9:3d83d0b410b8 37 DigitalOut transistor(D6);
jimbaud 6:1670244c4eb4 38
jimbaud 6:1670244c4eb4 39 //PWM output
jimbaud 6:1670244c4eb4 40
jimbaud 6:1670244c4eb4 41 PwmOut PWMoutput(PB_1); //Main PWM output
jimbaud 6:1670244c4eb4 42 PwmOut Green(PC_8); //PWM Red LED
jimbaud 6:1670244c4eb4 43 PwmOut Red(PC_6); //PWM Green LED
jimbaud 6:1670244c4eb4 44 PwmOut Blue(PC_9); //PWM Blue LED
jimbaud 6:1670244c4eb4 45
jimbaud 6:1670244c4eb4 46 //Init accelerometer
janjongboom 2:12a235e7691a 47
jimbaud 6:1670244c4eb4 48 LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G);
jimbaud 6:1670244c4eb4 49
jimbaud 6:1670244c4eb4 50 // Characteristics Accelerometer input
jimbaud 6:1670244c4eb4 51
Nthnthj 9:3d83d0b410b8 52 //SimpleChar<float> accX = ble.readOnly_float(0xA000, 0xA002);
Nthnthj 9:3d83d0b410b8 53 //SimpleChar<float> compteur = ble.readOnly_float(0xA000, 0xA003);
Nthnthj 9:3d83d0b410b8 54 //SimpleChar<float> Temps = ble.readOnly_float(0xA000, 0xA004);
Nthnthj 9:3d83d0b410b8 55
Nthnthj 9:3d83d0b410b8 56 Serial BT(TX,RX);
Nthnthj 9:3d83d0b410b8 57 Serial pc(USBTX, USBRX);
Nthnthj 9:3d83d0b410b8 58
Nthnthj 9:3d83d0b410b8 59 int compteur;
Nthnthj 9:3d83d0b410b8 60 bool flag = false;
Nthnthj 9:3d83d0b410b8 61 float end,begin;
Nthnthj 9:3d83d0b410b8 62
Nthnthj 9:3d83d0b410b8 63 void pressed(){
Nthnthj 9:3d83d0b410b8 64 compteur=1;
Nthnthj 9:3d83d0b410b8 65 led1= !led1;
Nthnthj 9:3d83d0b410b8 66 }
Nthnthj 9:3d83d0b410b8 67
Nthnthj 9:3d83d0b410b8 68
jimbaud 6:1670244c4eb4 69
jimbaud 6:1670244c4eb4 70
jimbaud 6:1670244c4eb4 71 // When characteristic LED RGB changing
jimbaud 6:1670244c4eb4 72
jimbaud 6:1670244c4eb4 73 void LEDupdate(uint32_t newColor)
Nthnthj 9:3d83d0b410b8 74 {/*
jimbaud 6:1670244c4eb4 75 // read individual bytes
jimbaud 6:1670244c4eb4 76 uint8_t* channels = (uint8_t*)&newColor;
janjongboom 0:ba1c49874d3c 77
jimbaud 6:1670244c4eb4 78 // cast to float, as PwmOut expects a value between 0.0f and 1.0f
jimbaud 6:1670244c4eb4 79 Red = static_cast<float>(channels[0]) / 255.0f;
jimbaud 6:1670244c4eb4 80 Green = static_cast<float>(channels[1]) / 255.0f;
Nthnthj 9:3d83d0b410b8 81 Blue = static_cast<float>(channels[2]) / 255.0f; */
jimbaud 6:1670244c4eb4 82 }
janjongboom 0:ba1c49874d3c 83
jimbaud 6:1670244c4eb4 84 // When characteristic PWM output changing
jimbaud 6:1670244c4eb4 85
jimbaud 6:1670244c4eb4 86 void PWMupdate(uint8_t pwmvalue)
jimbaud 6:1670244c4eb4 87 {
jimbaud 6:1670244c4eb4 88
jimbaud 6:1670244c4eb4 89 // cast to float, as PwmOut expects a value between 0.0f and 1.0f
Nthnthj 9:3d83d0b410b8 90 // PWMoutput = static_cast<float>(pwmvalue) / 255.0f;
jimbaud 6:1670244c4eb4 91 }
jimbaud 6:1670244c4eb4 92
jimbaud 6:1670244c4eb4 93 // When characteristic input changing
jimbaud 6:1670244c4eb4 94 void Accupdate()
jimbaud 6:1670244c4eb4 95 {
janjongboom 0:ba1c49874d3c 96
Nthnthj 9:3d83d0b410b8 97 //accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15;
Nthnthj 9:3d83d0b410b8 98 //accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15;
Nthnthj 9:3d83d0b410b8 99 //accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15;
Nthnthj 9:3d83d0b410b8 100 //Temps=15.68;
jimbaud 6:1670244c4eb4 101
jimbaud 6:1670244c4eb4 102 }
jimbaud 6:1670244c4eb4 103
jimbaud 6:1670244c4eb4 104 // Characteritic PWM LED RGB
Nthnthj 9:3d83d0b410b8 105 //SimpleChar<uint32_t> color = ble.writeOnly_u32(0x6200, 0x6201, &LEDupdate);
jimbaud 6:1670244c4eb4 106
jimbaud 6:1670244c4eb4 107 // Characteristic PWM output
Nthnthj 9:3d83d0b410b8 108 //SimpleChar<uint8_t> pwmout = ble.writeOnly_u8(0xA000, 0xA001, &PWMupdate);
jimbaud 6:1670244c4eb4 109
Nthnthj 9:3d83d0b410b8 110 void skater_d()
Nthnthj 9:3d83d0b410b8 111 {
Nthnthj 9:3d83d0b410b8 112 if(flag==false) {
Nthnthj 9:3d83d0b410b8 113 //printf("Ligne de depart coupee solo \n");
Nthnthj 9:3d83d0b410b8 114 if( flag == false) {
Nthnthj 9:3d83d0b410b8 115 //printf("Depart skate \n");
Nthnthj 9:3d83d0b410b8 116 begin = timer.read_ms();
Nthnthj 9:3d83d0b410b8 117 compteur=100;
Nthnthj 9:3d83d0b410b8 118 //pc.printf(" skater lance %.0f \n", begin);
Nthnthj 9:3d83d0b410b8 119 flag = true;
Nthnthj 9:3d83d0b410b8 120 } else if(flag == true) {
Nthnthj 9:3d83d0b410b8 121 //printf("erreur \n");
Nthnthj 9:3d83d0b410b8 122 //pc.printf(" Temps du skater : %.0f \n", end-begin);
Nthnthj 9:3d83d0b410b8 123 flag = false;
Nthnthj 9:3d83d0b410b8 124 }
Nthnthj 9:3d83d0b410b8 125 }
Nthnthj 9:3d83d0b410b8 126 else if(flag==true) {
Nthnthj 9:3d83d0b410b8 127 //printf("Ligne d'arrivee coupee \n");
Nthnthj 9:3d83d0b410b8 128 if( flag == false ) {
Nthnthj 9:3d83d0b410b8 129 //printf("arrivee coupe sans depart\n");
Nthnthj 9:3d83d0b410b8 130 flag = true;
Nthnthj 9:3d83d0b410b8 131 } else if(flag == true ) {
Nthnthj 9:3d83d0b410b8 132 //printf("Arrivee skate \n");
Nthnthj 9:3d83d0b410b8 133 end = timer.read_ms();
Nthnthj 9:3d83d0b410b8 134 //Temps = end-begin;
Nthnthj 9:3d83d0b410b8 135 compteur=200;
Nthnthj 9:3d83d0b410b8 136 //wait(1);
Nthnthj 9:3d83d0b410b8 137 //pc.printf(" Temps du skater : %.0f \n", end-begin);
Nthnthj 9:3d83d0b410b8 138 flag = false;
Nthnthj 9:3d83d0b410b8 139 }
Nthnthj 9:3d83d0b410b8 140 }
Nthnthj 9:3d83d0b410b8 141 }
jimbaud 6:1670244c4eb4 142
jimbaud 6:1670244c4eb4 143 //Main program
jimbaud 6:1670244c4eb4 144
jimbaud 6:1670244c4eb4 145 int main(int, char**)
jimbaud 6:1670244c4eb4 146 {
Nthnthj 9:3d83d0b410b8 147 transistor=1;
Nthnthj 9:3d83d0b410b8 148 //ble.start();
jimbaud 6:1670244c4eb4 149 Ticker t;
Nthnthj 9:3d83d0b410b8 150 //t.attach(&Accupdate, 15.0f);
Nthnthj 9:3d83d0b410b8 151 timer.start();
Nthnthj 9:3d83d0b410b8 152 user1.fall(&pressed);
Nthnthj 9:3d83d0b410b8 153 boutton1.fall(&pressed);
Nthnthj 9:3d83d0b410b8 154 boutton2.fall(&pressed);
Nthnthj 9:3d83d0b410b8 155 event.fall(&skater_d);
Nthnthj 9:3d83d0b410b8 156 char c;
Nthnthj 9:3d83d0b410b8 157 //char Buffer[10];
Nthnthj 9:3d83d0b410b8 158 int i = 0;
Nthnthj 9:3d83d0b410b8 159 int begin2 = 0;
Nthnthj 9:3d83d0b410b8 160
jimbaud 6:1670244c4eb4 161 while (1) {
Nthnthj 9:3d83d0b410b8 162 //ble.waitForEvent();
Nthnthj 9:3d83d0b410b8 163
Nthnthj 9:3d83d0b410b8 164 //if(timer.read_ms()>5000){
Nthnthj 9:3d83d0b410b8 165 timer.stop();
Nthnthj 9:3d83d0b410b8 166 timer.start();
Nthnthj 9:3d83d0b410b8 167 begin2 = timer.read_ms();
Nthnthj 9:3d83d0b410b8 168 //pc.printf("hello");
Nthnthj 9:3d83d0b410b8 169 Green = 0.001;
Nthnthj 9:3d83d0b410b8 170 c = compteur;
Nthnthj 9:3d83d0b410b8 171 BT.putc(i);
Nthnthj 9:3d83d0b410b8 172 BT.putc(begin2);
Nthnthj 9:3d83d0b410b8 173 wait(1);
Nthnthj 9:3d83d0b410b8 174 //BT.puts("Test");
Nthnthj 9:3d83d0b410b8 175 Green = 0;
Nthnthj 9:3d83d0b410b8 176 compteur = 0;
Nthnthj 9:3d83d0b410b8 177 i=i+1;
Nthnthj 9:3d83d0b410b8 178 wait(1);
Nthnthj 9:3d83d0b410b8 179 //}
jimbaud 6:1670244c4eb4 180
janjongboom 0:ba1c49874d3c 181 }
janjongboom 0:ba1c49874d3c 182 }
Nthnthj 9:3d83d0b410b8 183
Nthnthj 9:3d83d0b410b8 184 //blablabla