can't push chnages :(

Fork of FBRDash by Michael Allan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "PinDetect.h"
00004 #include "Menu.h"
00005 #include "Comms.h"
00006 #include "PCComms.h"
00007 #include "Gears.h"
00008 #include "LEDS.h"
00009 #include "bigchar.h"
00010 //Test comment 2
00011 //Refresh interval for LCD
00012 #define LCD_REFRESH_TIME 150000
00013 
00014 //Refresh interval for rev LEDs
00015 #define REV_REFRESH_TIME 50000
00016 
00017 //Warning lights
00018 DigitalOut warn[] = { (p20), (p19), (p27), (p18) };
00019 
00020 //mBED LEDs
00021 DigitalOut debug[] = { (LED1), (LED2), (LED3) };
00022 
00023 //Heartbeat LED
00024 DigitalOut heartbeat(LED4);
00025 
00026 //Rev LEDS
00027 PwmOut leds[] = { (p24), (p25), (p26), (p23), (p22), (p21) };
00028 
00029 //LCD
00030 TextLCD lcd(p5, p6, p7, p8, p9, p10, p11);
00031 
00032 //Tickers for refreshing
00033 Ticker lcdRefreshTicker;
00034 Ticker revRefreshTicker;
00035 
00036 //Used to animate LEDs for testing
00037 Ticker increment;
00038 
00039 //Main car state structure
00040 State car;
00041 
00042 //Initialise CAN
00043 CAN can(p30, p29);
00044 
00045 //Classes for various parts of the firmware
00046 Menu dashMenu(&lcd, p16, p17, p12); //*LCD, OK, Left, Right
00047 PCComms pc(&car);
00048 Gears gearButtons(p14, p13, p15, &car.gear, &pc); //Up, Neutral, Down, *Current Gear
00049 LEDS revs(leds);
00050 
00051 //Refresh the rev LEDs and warning LEDs
00052 void revRefresh()
00053 {
00054     CANMessage msg;
00055     revs.refresh(car.rpm);
00056     
00057     if(car.voltage<12){
00058         warn[0]=1;
00059     }else{
00060         warn[0]=0;
00061     }
00062     if(car.coolant_temp>110){
00063         warn[1]=1;
00064     }else{
00065         warn[1]=0;
00066     }
00067     if(car.rpm==0 and car.gear!=0){
00068         warn[2]=1;
00069     }else{
00070         warn[2]=0;
00071     }
00072     if(can.read(msg)) {
00073         if(msg.id==100 and msg.len==8){
00074             car.rpm = msg.data[0] + (msg.data[1] << 8);
00075             car.throttle_pos = msg.data[2];
00076             car.manifold_pres = msg.data[3];
00077             car.air_temp = msg.data[4];
00078             car.coolant_temp = msg.data[5];
00079             car.lambda = msg.data[6];
00080         }
00081         else if(msg.id==200 and msg.len==8){
00082             car.speed = msg.data[0];
00083             car.accel_x = msg.data[1];
00084             car.accel_y = msg.data[2];
00085             car.gear = msg.data[3];
00086             car.oil_temp = msg.data[4];
00087             car.voltage = msg.data[5]/16.0;
00088         } 
00089     }
00090 
00091 }
00092 
00093 //Refresh the LCD
00094 void lcdRefresh()
00095 {
00096     //If menu off screen, display HUD
00097     if(dashMenu.display == false)
00098     {
00099         lcd.locate(0, 0);
00100         lcd.printf("%3.0fC%5.1dRPM", car.coolant_temp, car.rpm);
00101         lcd.locate(0, 1);
00102         lcd.printf("%2dMPH %3.1fV", car.speed, car.voltage);
00103         
00104         write_bigchar(&lcd, 13, car.gear);
00105     }
00106     //Otherwise show menu
00107     else
00108     {
00109         dashMenu.refresh();
00110     }    
00111     
00112     //Blink heartbeat
00113     heartbeat = !heartbeat;
00114 }
00115 
00116 
00117 //Used to animate LEDs for testing
00118 /*void doIncrement()
00119 {
00120     if(car.rpm < LIMIT && car.gear > 0)
00121         car.rpm++;
00122 }*/
00123 
00124 //Startup animation
00125 void selfTest()
00126 {
00127     lcd.printf("    FBR 2012");
00128     lcd.locate(0,1);
00129     lcd.printf(" Ready to drive");
00130     int offset = 0;
00131     int type=1;
00132     int blk=7;
00133     int from=1;
00134     int to=0;
00135     int id = offset*262144+type*32768+from*2048+to*128+blk*8;
00136     CANMessage msg;
00137     msg = CANMessage(id,0,8,CANData,CANExtended);
00138     lcd.cls();
00139     lcd.printf("%x",id);
00140     can.reset();
00141     if(can.write(msg)){
00142     wait(0.01);
00143     lcd.printf(" %d errors",can.tderror());
00144     can.reset();
00145     while(can.read(msg)==false){
00146     wait(0.1);
00147     }
00148     wait(0.5);
00149     lcd.printf(" %d errors",can.tderror());
00150     }
00151     //Light up LEDs
00152     for(int i = 0; i < LEDS::NUM_LEDS; i++)
00153     {
00154         leds[i] = true;
00155         if(i < 4)
00156             warn[i] = true;
00157         wait(0.2);
00158     }
00159     
00160     //Turn off LEDs
00161     for(int i = LEDS::NUM_LEDS - 1; i >= 0; i--)
00162     {
00163         leds[i] = false;
00164         if(i < 4)
00165             warn[i] = false;
00166         wait(0.2);
00167     }
00168     
00169     lcd.cls();
00170 }
00171 
00172 int main()
00173 {   
00174     //Initialise CAN
00175     can.frequency(500000);
00176     //Initialise state
00177     car.rpm = 0;
00178     car.gear = 2;
00179     car.speed = 0;
00180     car.coolant_temp = 0;    
00181     car.throttle_pos = 0;
00182     car.manifold_pres = 0;
00183     car.air_temp = 0;
00184     car.lambda = 0;
00185     car.accel_x = 0;
00186     car.accel_y = 0;
00187     car.oil_temp = 0;
00188     car.voltage = 0;
00189 
00190     //Set up menu
00191     dashMenu.addItem<float>("Coolant Temp  ", "%12.1f\xDF\x43", &car.coolant_temp); // \xDF\x43 -> &#65533;C . Need code for C as otherwise it gets taken as hex digit.
00192     dashMenu.addItem<unsigned char>("Air Temp      ", "%12d\xDF\x43", &car.air_temp);
00193     dashMenu.addItem<unsigned char>("Throttle Pos  ", "%13d\xDF", &car.throttle_pos);
00194     dashMenu.addItem<unsigned char>("Manifold Pres ", "%10d psi", &car.manifold_pres);
00195     dashMenu.addItem<unsigned char>("Lambda        ", "%14d", &car.lambda);    
00196     dashMenu.addItem<unsigned char>("Oil Temp      ", "%12d\xDF\x43", &car.oil_temp);
00197     
00198     //Set up characters on LCS
00199     setup_bigchar(&lcd);
00200     
00201     //Do bootup animation
00202     selfTest();
00203     
00204     //Start refresh tickers
00205     lcdRefreshTicker.attach_us(&lcdRefresh, LCD_REFRESH_TIME);
00206     revRefreshTicker.attach_us(&revRefresh, REV_REFRESH_TIME);
00207     //increment.attach(&doIncrement, 0.0005);
00208     
00209     //Infinite loop - program is interrupt driven
00210     while(true)
00211     {
00212         __WFI();
00213     }
00214 }