Justin Edwards / Mbed 2 deprecated BLIRQ

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <string>
00003 #include "parse.h"
00004 #include "sense.h"
00005 #include "TextLCD.h"
00006 #include "FXOS8700CQ.h"
00007 #include <math.h>
00008 #include "Dht11.h"
00009 FXOS8700CQ fxos(PTE25,PTE24,FXOS8700CQ_SLAVE_ADDR1);
00010 TextLCD lcd(PTC5,PTC7,D13,D12,D11,D10,TextLCD::LCD16x2);
00011 Serial pc(USBTX, USBRX);
00012 InterruptIn gearUp(PTA4);
00013 InterruptIn gearDown(PTC6);
00014 DigitalIn Nswitch(D7);
00015 Serial blue(PTC15,PTC14);
00016 Ticker display;
00017 Ticker Wifi;
00018 Ticker lean;
00019 Ticker BLE;
00020 Ticker th;
00021 Dht11 sense(D3);
00022 char rx[40];
00023 Serial esp8266(PTC4,PTC3);
00024 //DigitalOut myled(PTB22);
00025 //DigitalOut myled3(PTB21);
00026 info* currInfo = (info*) malloc(sizeof(struct info));
00027 volatile bool flag = false;
00028 long y = 0;
00029 void gUp();
00030 void gDown();
00031 void updateDisplay();
00032 void updateWifi();
00033 void updateangle();
00034 void onBluetoothReceived();
00035 void flushBuff();
00036 void updateTempH();
00037 
00038 void onBluetoothReceived(void) {
00039         if(blue.readable()){
00040             //myled = !myled
00041             //pc.printf("Interrupt!\r\n");
00042             if(blue.readable()) blue.gets(rx,39);
00043             flushBuff();
00044             //pc.puts("Passed\r\n");
00045             //pc.puts("\n");
00046             //pc.puts(rx);
00047             //if(rx[0] == '['){
00048                 //pc.puts(rx);
00049             //}
00050             //else
00051                //rx[0] = '\0';
00052         }
00053 }
00054 
00055 int main() 
00056 {   
00057     initializeStruct(currInfo);
00058     th.attach(&updateTempH,5);
00059     lean.attach(&updateangle,.5);
00060     Wifi.attach(&updateWifi,1);
00061     BLE.attach(&onBluetoothReceived,.5);
00062     display.attach(&updateDisplay,.1);
00063     SetGear(currInfo,Nswitch);
00064     gearUp.rise(&gUp);
00065     gearDown.rise(&gDown);
00066     rx[0] = '\0';
00067     int status;
00068     currInfo->x = 0;
00069     //info* currInfo = (info*) malloc(sizeof(struct info));
00070     blue.attach(&onBluetoothReceived, Serial::RxIrq);
00071     blue.baud(115200);
00072     pc.baud(115200);
00073     esp8266.baud(115200);
00074     pc.printf("start!");
00075     // echo back characters and toggle the LED
00076     while (1) 
00077     {
00078         //pc.printf("%f %f %f %d \r\n",currInfo->x,currInfo->y,currInfo->speed,currInfo->gear);
00079         status = parse(rx,currInfo);
00080      }
00081 }
00082 
00083 void gUp(){
00084     currInfo->crash = 1;
00085     pc.printf("CRASH %d",currInfo->crash);
00086     if(currInfo->gear == 6)
00087         return;
00088     else if(currInfo->gear == 0){
00089         currInfo->gear = 2;
00090         return;
00091     }
00092     else
00093         currInfo->gear++;    
00094 }
00095 
00096 void gDown(){
00097     currInfo->crash = 0;
00098     if(currInfo->gear == 0)
00099         currInfo->gear++;
00100     else if(currInfo->gear == 1)
00101         return;
00102     else
00103         currInfo->gear--;    
00104 }
00105 
00106 void updateDisplay(){
00107     lcd.cls();
00108     lcd.locate(0,0);
00109     lcd.printf("Angle: %d",currInfo->lean);
00110     lcd.locate(0,1);
00111     lcd.printf("m/s:%.2f G:%d",currInfo->speed,currInfo->gear);
00112 }
00113 void updateWifi(){     //[,WIFI,hr,temp,humidity,x,y,speed,lean,accel,gear,]
00114        esp8266.printf("[,WIFI,%d,%d,%d,%f,%f,%f,%d,%d,%d,%d,]",currInfo->hr,currInfo->temp,currInfo->humidity,currInfo->x,currInfo->y,currInfo->speed,currInfo->lean,currInfo->accel,currInfo->gear,currInfo->crash);
00115        //pc.printf("[,WIFI,%d,%d,%d,%f,%f,%f,%d,%d,%d,%d,]\r\n",currInfo->hr,currInfo->temp,currInfo->humidity,currInfo->x,currInfo->y,currInfo->speed,currInfo->lean,currInfo->accel,currInfo->gear,currInfo->crash);
00116 }
00117 
00118 float prevx = 0, prevy = 0, prevz = 0;
00119 void updateangle(){
00120     SRAWDATA accdata,magdata;
00121     float ax,ay,az,mx,my,mz,axf,ayf,xh,yh;
00122     double xangle,yangle,zangle,heading;
00123     fxos.enable();
00124     fxos.get_data(&accdata,&magdata);
00125     ax = accdata.x;
00126     ay = accdata.y;
00127     az = accdata.z;
00128     //pc.printf("%f %f %f\r\n",ax,ay,az);
00129     xangle = atan2(ax, (sqrt(ay*ay + az*az)));
00130     xangle *= 180; xangle /= 3.141592;
00131     if(prevx - ax > 6144){
00132         currInfo->crash = 1;
00133     }
00134     currInfo->lean = (int)xangle;
00135     currInfo->accel = (float)((ax/4096)*9.8);
00136     //pc.printf("%d",currInfo->lean);
00137     prevx = ax;
00138     prevy = ay;
00139     prevz = az;
00140 }
00141 void flushBuff(){
00142     char temp = 0;
00143     while(blue.readable()){
00144         temp = blue.getc();
00145     }
00146     return;
00147 }
00148 void updateTempH(){
00149     sense.read();
00150     currInfo->temp = sense.getFahrenheit();
00151     currInfo->temp = currInfo->temp;
00152     currInfo->humidity = sense.getHumidity();
00153     currInfo->humidity = currInfo->humidity;
00154 }