![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Making a BMW E90 instrument cluster alive for demonstration purposes
T15Msg.cpp@3:4a4463380739, 2017-03-11 (annotated)
- Committer:
- gume
- Date:
- Sat Mar 11 10:03:52 2017 +0000
- Revision:
- 3:4a4463380739
- Parent:
- 0:830e074c92d5
Initial Speed module (not yet working); Fuel data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gume | 0:830e074c92d5 | 1 | #include "T15Msg.h" |
gume | 0:830e074c92d5 | 2 | |
gume | 0:830e074c92d5 | 3 | T15Msg::T15Msg () { |
gume | 0:830e074c92d5 | 4 | |
gume | 0:830e074c92d5 | 5 | counter1 = 0; |
gume | 0:830e074c92d5 | 6 | counter2 = 0; |
gume | 0:830e074c92d5 | 7 | |
gume | 0:830e074c92d5 | 8 | enginerun = false; |
gume | 0:830e074c92d5 | 9 | ignition = false; |
gume | 0:830e074c92d5 | 10 | clutch = false; |
gume | 0:830e074c92d5 | 11 | |
gume | 0:830e074c92d5 | 12 | enginestate = 0; |
gume | 0:830e074c92d5 | 13 | statecounter = 0; |
gume | 0:830e074c92d5 | 14 | |
gume | 0:830e074c92d5 | 15 | sstart = false; |
gume | 0:830e074c92d5 | 16 | sstop = false; |
gume | 0:830e074c92d5 | 17 | |
gume | 0:830e074c92d5 | 18 | updateData(); |
gume | 0:830e074c92d5 | 19 | } |
gume | 0:830e074c92d5 | 20 | |
gume | 0:830e074c92d5 | 21 | void T15Msg::updateState() { |
gume | 0:830e074c92d5 | 22 | |
gume | 0:830e074c92d5 | 23 | switch (enginestate) { |
gume | 0:830e074c92d5 | 24 | case 1: |
gume | 0:830e074c92d5 | 25 | statecounter++; |
gume | 0:830e074c92d5 | 26 | if (sstart && statecounter > 1) { |
gume | 0:830e074c92d5 | 27 | enginestate = 2; |
gume | 0:830e074c92d5 | 28 | statecounter = 0; |
gume | 0:830e074c92d5 | 29 | ignition = true; |
gume | 0:830e074c92d5 | 30 | updateData(4); |
gume | 0:830e074c92d5 | 31 | } |
gume | 0:830e074c92d5 | 32 | if (sstop && statecounter > 24) { |
gume | 0:830e074c92d5 | 33 | enginestate = 0; |
gume | 0:830e074c92d5 | 34 | statecounter = 0; |
gume | 0:830e074c92d5 | 35 | updateData(0xc); |
gume | 0:830e074c92d5 | 36 | sstop = false; |
gume | 0:830e074c92d5 | 37 | } |
gume | 0:830e074c92d5 | 38 | break; |
gume | 0:830e074c92d5 | 39 | |
gume | 0:830e074c92d5 | 40 | case 2: |
gume | 0:830e074c92d5 | 41 | enginestate = 6; |
gume | 0:830e074c92d5 | 42 | statecounter = 0; |
gume | 0:830e074c92d5 | 43 | updateData(9); |
gume | 0:830e074c92d5 | 44 | break; |
gume | 0:830e074c92d5 | 45 | |
gume | 0:830e074c92d5 | 46 | case 6: |
gume | 0:830e074c92d5 | 47 | statecounter++; |
gume | 0:830e074c92d5 | 48 | if (ignition) { |
gume | 0:830e074c92d5 | 49 | if (statecounter > 8) { |
gume | 0:830e074c92d5 | 50 | enginestate = 9; |
gume | 0:830e074c92d5 | 51 | statecounter = 0; |
gume | 0:830e074c92d5 | 52 | updateData(0xd); |
gume | 0:830e074c92d5 | 53 | } |
gume | 0:830e074c92d5 | 54 | } else { |
gume | 0:830e074c92d5 | 55 | if (statecounter == 3) { |
gume | 0:830e074c92d5 | 56 | enginerun = false; |
gume | 0:830e074c92d5 | 57 | updateData(); |
gume | 0:830e074c92d5 | 58 | } else if (statecounter > 3) { |
gume | 0:830e074c92d5 | 59 | enginestate = 5; |
gume | 0:830e074c92d5 | 60 | statecounter = 0; |
gume | 0:830e074c92d5 | 61 | updateData(7); |
gume | 0:830e074c92d5 | 62 | } |
gume | 0:830e074c92d5 | 63 | } |
gume | 0:830e074c92d5 | 64 | break; |
gume | 0:830e074c92d5 | 65 | |
gume | 0:830e074c92d5 | 66 | case 9: |
gume | 0:830e074c92d5 | 67 | statecounter++; |
gume | 0:830e074c92d5 | 68 | if (statecounter == 3) { |
gume | 0:830e074c92d5 | 69 | ignition = false; |
gume | 0:830e074c92d5 | 70 | enginerun = true; |
gume | 0:830e074c92d5 | 71 | updateData(); |
gume | 0:830e074c92d5 | 72 | } |
gume | 0:830e074c92d5 | 73 | if (statecounter > 28) { |
gume | 0:830e074c92d5 | 74 | enginestate = 8; |
gume | 0:830e074c92d5 | 75 | updateData(8); |
gume | 0:830e074c92d5 | 76 | sstart = false; |
gume | 0:830e074c92d5 | 77 | } |
gume | 0:830e074c92d5 | 78 | break; |
gume | 0:830e074c92d5 | 79 | |
gume | 0:830e074c92d5 | 80 | case 4: |
gume | 0:830e074c92d5 | 81 | enginestate = 6; |
gume | 0:830e074c92d5 | 82 | statecounter = 0; |
gume | 0:830e074c92d5 | 83 | updateData(0); |
gume | 0:830e074c92d5 | 84 | break; |
gume | 0:830e074c92d5 | 85 | |
gume | 0:830e074c92d5 | 86 | case 5: |
gume | 0:830e074c92d5 | 87 | statecounter++; |
gume | 0:830e074c92d5 | 88 | if (statecounter > 1) { |
gume | 0:830e074c92d5 | 89 | enginestate = 1; |
gume | 0:830e074c92d5 | 90 | statecounter = 0; |
gume | 0:830e074c92d5 | 91 | updateData(); |
gume | 0:830e074c92d5 | 92 | } |
gume | 0:830e074c92d5 | 93 | break; |
gume | 0:830e074c92d5 | 94 | } |
gume | 0:830e074c92d5 | 95 | } |
gume | 0:830e074c92d5 | 96 | |
gume | 0:830e074c92d5 | 97 | void T15Msg::updateData(char resetc1) { |
gume | 0:830e074c92d5 | 98 | |
gume | 0:830e074c92d5 | 99 | if (enginerun) { |
gume | 0:830e074c92d5 | 100 | data[0] = 0x45; |
gume | 0:830e074c92d5 | 101 | } else { |
gume | 0:830e074c92d5 | 102 | if (ignition) data[0] = 0x55; |
gume | 0:830e074c92d5 | 103 | else data[0] = 0x41; |
gume | 0:830e074c92d5 | 104 | } |
gume | 0:830e074c92d5 | 105 | |
gume | 0:830e074c92d5 | 106 | data[1] = 0x42; |
gume | 0:830e074c92d5 | 107 | |
gume | 0:830e074c92d5 | 108 | if (clutch) data[2] = 0x61; |
gume | 0:830e074c92d5 | 109 | else data[2] = 0x21; |
gume | 0:830e074c92d5 | 110 | if (enginestate == 2 || enginestate == 6) data[2] |= 0x8; |
gume | 0:830e074c92d5 | 111 | |
gume | 0:830e074c92d5 | 112 | data[3] = enginestate << 4 | 0xf; |
gume | 0:830e074c92d5 | 113 | |
gume | 0:830e074c92d5 | 114 | if (resetc1 < 0x10) counter1 = resetc1; |
gume | 0:830e074c92d5 | 115 | } |
gume | 0:830e074c92d5 | 116 | |
gume | 0:830e074c92d5 | 117 | bool T15Msg::sendMessage(CAN *can) { |
gume | 0:830e074c92d5 | 118 | |
gume | 0:830e074c92d5 | 119 | data[4] = counter1 << 4 | counter2; |
gume | 0:830e074c92d5 | 120 | bool ok = can->write(CANMessage(0x130, data, 5)); |
gume | 0:830e074c92d5 | 121 | //bool ok = can->write(0x130, (char*) data, 5); |
gume | 0:830e074c92d5 | 122 | |
gume | 0:830e074c92d5 | 123 | counter1 = (counter1 + 1) & 0x0f; |
gume | 0:830e074c92d5 | 124 | if (counter1 == 6) counter1++; |
gume | 0:830e074c92d5 | 125 | counter2 = (counter2 + 1) & 0x0f; |
gume | 0:830e074c92d5 | 126 | if (counter2 == 0xf) counter2 = 0; |
gume | 0:830e074c92d5 | 127 | |
gume | 0:830e074c92d5 | 128 | if (enginestate != 0 && enginestate != 8) { |
gume | 0:830e074c92d5 | 129 | updateState(); |
gume | 0:830e074c92d5 | 130 | } |
gume | 0:830e074c92d5 | 131 | |
gume | 0:830e074c92d5 | 132 | return ok; |
gume | 0:830e074c92d5 | 133 | } |
gume | 0:830e074c92d5 | 134 | |
gume | 0:830e074c92d5 | 135 | bool T15Msg::start() { |
gume | 0:830e074c92d5 | 136 | |
gume | 0:830e074c92d5 | 137 | if (enginestate == 8) return false; |
gume | 0:830e074c92d5 | 138 | if (sstart || sstop) return false; |
gume | 0:830e074c92d5 | 139 | |
gume | 0:830e074c92d5 | 140 | enginestate = 1; |
gume | 0:830e074c92d5 | 141 | statecounter = 0; |
gume | 0:830e074c92d5 | 142 | sstart = true; |
gume | 0:830e074c92d5 | 143 | updateData(4); |
gume | 0:830e074c92d5 | 144 | |
gume | 0:830e074c92d5 | 145 | return true; |
gume | 0:830e074c92d5 | 146 | } |
gume | 0:830e074c92d5 | 147 | |
gume | 0:830e074c92d5 | 148 | bool T15Msg::stop() { |
gume | 0:830e074c92d5 | 149 | |
gume | 0:830e074c92d5 | 150 | if (enginestate == 0) return false; |
gume | 0:830e074c92d5 | 151 | if (sstart || sstop) return false; |
gume | 0:830e074c92d5 | 152 | |
gume | 0:830e074c92d5 | 153 | enginestate = 4; |
gume | 0:830e074c92d5 | 154 | statecounter = 0; |
gume | 0:830e074c92d5 | 155 | sstop = true; |
gume | 0:830e074c92d5 | 156 | updateData(5); |
gume | 0:830e074c92d5 | 157 | |
gume | 0:830e074c92d5 | 158 | return true; |
gume | 0:830e074c92d5 | 159 | } |