Making a BMW E90 instrument cluster alive for demonstration purposes

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers T15Msg.cpp Source File

T15Msg.cpp

00001 #include "T15Msg.h"
00002 
00003 T15Msg::T15Msg () {
00004 
00005     counter1 = 0;
00006     counter2 = 0;
00007 
00008     enginerun = false;
00009     ignition = false;
00010     clutch = false;
00011 
00012     enginestate = 0;
00013     statecounter = 0;
00014 
00015     sstart = false;
00016     sstop = false;
00017 
00018     updateData();
00019 }
00020 
00021 void T15Msg::updateState() {
00022 
00023     switch (enginestate) {
00024         case 1:
00025             statecounter++;
00026             if (sstart && statecounter > 1) {
00027                 enginestate = 2;
00028                 statecounter = 0;
00029                 ignition = true;
00030                 updateData(4);
00031             }
00032             if (sstop && statecounter > 24) {
00033                 enginestate = 0;
00034                 statecounter = 0;
00035                 updateData(0xc);
00036                 sstop = false;
00037             }
00038             break;
00039 
00040         case 2:
00041             enginestate = 6;
00042             statecounter = 0;
00043             updateData(9);
00044             break;
00045 
00046         case 6:
00047             statecounter++;
00048             if (ignition) {
00049                 if (statecounter > 8) {
00050                     enginestate = 9;
00051                     statecounter = 0;
00052                     updateData(0xd);
00053                 }
00054             } else {
00055                 if (statecounter == 3) {
00056                     enginerun = false;
00057                     updateData();
00058                 } else if (statecounter > 3) {
00059                     enginestate = 5;
00060                     statecounter = 0;
00061                     updateData(7);
00062                 }
00063             }
00064             break;
00065 
00066         case 9:
00067             statecounter++;
00068             if (statecounter == 3) {
00069                 ignition = false;
00070                 enginerun = true;
00071                 updateData();
00072             }
00073             if (statecounter > 28) {
00074                 enginestate = 8;
00075                 updateData(8);
00076                 sstart = false;
00077             }
00078             break;
00079 
00080         case 4:
00081             enginestate = 6;
00082             statecounter = 0;
00083             updateData(0);
00084             break;
00085 
00086         case 5:
00087             statecounter++;
00088             if (statecounter > 1) {
00089                 enginestate = 1;
00090                 statecounter = 0;
00091                 updateData();
00092             }
00093             break;
00094     }
00095 }
00096 
00097 void T15Msg::updateData(char resetc1) {
00098 
00099     if (enginerun) {
00100         data[0] = 0x45;
00101     } else {
00102         if (ignition) data[0] = 0x55;
00103         else data[0] = 0x41;
00104     }
00105 
00106     data[1] = 0x42;
00107 
00108     if (clutch) data[2] = 0x61;
00109     else data[2] = 0x21;
00110     if (enginestate == 2 || enginestate == 6) data[2] |= 0x8;
00111 
00112     data[3] = enginestate << 4 | 0xf;
00113 
00114     if (resetc1 < 0x10) counter1 = resetc1;
00115 }
00116 
00117 bool T15Msg::sendMessage(CAN *can) {
00118 
00119     data[4] = counter1 << 4 | counter2;
00120     bool ok = can->write(CANMessage(0x130, data, 5));
00121     //bool ok = can->write(0x130, (char*) data, 5);
00122 
00123     counter1 = (counter1 + 1) & 0x0f;
00124     if (counter1 == 6) counter1++;
00125     counter2 = (counter2 + 1) & 0x0f;
00126     if (counter2 == 0xf) counter2 = 0;
00127 
00128     if (enginestate != 0 &&  enginestate != 8) {
00129         updateState();
00130     }
00131 
00132     return ok;
00133 }
00134 
00135 bool T15Msg::start() {
00136 
00137     if (enginestate == 8) return false;
00138     if (sstart || sstop) return false;
00139 
00140     enginestate = 1;
00141     statecounter = 0;
00142     sstart = true;
00143     updateData(4);
00144 
00145     return true;
00146 }
00147 
00148 bool T15Msg::stop() {
00149 
00150     if (enginestate == 0) return false;
00151     if (sstart || sstop) return false;
00152 
00153     enginestate = 4;
00154     statecounter = 0;
00155     sstop = true;
00156     updateData(5);
00157 
00158     return true;
00159 }