Dodge/Chrysler Radio Emulator Connect to Interior CAN bus
Dependencies: mbed SDFileSystem
radioEmulator.cpp@0:9bc41d70bdd3, 2011-01-31 (annotated)
- Committer:
- rtgree01
- Date:
- Mon Jan 31 05:13:04 2011 +0000
- Revision:
- 0:9bc41d70bdd3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rtgree01 | 0:9bc41d70bdd3 | 1 | #include "mbed.h" |
rtgree01 | 0:9bc41d70bdd3 | 2 | #include "radioEmulator.h" |
rtgree01 | 0:9bc41d70bdd3 | 3 | |
rtgree01 | 0:9bc41d70bdd3 | 4 | DigitalOut led1(LED1); |
rtgree01 | 0:9bc41d70bdd3 | 5 | DigitalOut led2(LED2); |
rtgree01 | 0:9bc41d70bdd3 | 6 | DigitalOut led3(LED3); |
rtgree01 | 0:9bc41d70bdd3 | 7 | DigitalOut led4(LED4); |
rtgree01 | 0:9bc41d70bdd3 | 8 | |
rtgree01 | 0:9bc41d70bdd3 | 9 | //LocalFileSystem local("local"); |
rtgree01 | 0:9bc41d70bdd3 | 10 | #include "SDFileSystem.h" |
rtgree01 | 0:9bc41d70bdd3 | 11 | |
rtgree01 | 0:9bc41d70bdd3 | 12 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
rtgree01 | 0:9bc41d70bdd3 | 13 | |
rtgree01 | 0:9bc41d70bdd3 | 14 | char RadioEmulator::unlock[6] = {0x03,0x02,0x00,0x40,0x87,0xa5}; |
rtgree01 | 0:9bc41d70bdd3 | 15 | char RadioEmulator::lock[6] = {0x01, 0x02, 0x00, 0x40, 0x87, 0xa5}; |
rtgree01 | 0:9bc41d70bdd3 | 16 | char RadioEmulator::trunk[6] = {0x05, 0x02, 0x00, 0x40, 0x87, 0xa5}; |
rtgree01 | 0:9bc41d70bdd3 | 17 | |
rtgree01 | 0:9bc41d70bdd3 | 18 | RadioEmulator::RadioEmulator() |
rtgree01 | 0:9bc41d70bdd3 | 19 | { |
rtgree01 | 0:9bc41d70bdd3 | 20 | usb = new MODSERIAL(USBTX, USBRX, 1024, 128, NULL); // tx, rx |
rtgree01 | 0:9bc41d70bdd3 | 21 | usb->baud(115200); |
rtgree01 | 0:9bc41d70bdd3 | 22 | |
rtgree01 | 0:9bc41d70bdd3 | 23 | can2 = new CAN(p30,p29); |
rtgree01 | 0:9bc41d70bdd3 | 24 | can_RS = new DigitalOut(p28); |
rtgree01 | 0:9bc41d70bdd3 | 25 | canIRQ = new InterruptIn(p27); |
rtgree01 | 0:9bc41d70bdd3 | 26 | |
rtgree01 | 0:9bc41d70bdd3 | 27 | serialCounter = 0; |
rtgree01 | 0:9bc41d70bdd3 | 28 | prevSWC = 0; |
rtgree01 | 0:9bc41d70bdd3 | 29 | radioOn = false; |
rtgree01 | 0:9bc41d70bdd3 | 30 | for (int i = 0; i < 8; i++) |
rtgree01 | 0:9bc41d70bdd3 | 31 | { |
rtgree01 | 0:9bc41d70bdd3 | 32 | for (int j = 0; j < 8; j++) |
rtgree01 | 0:9bc41d70bdd3 | 33 | { |
rtgree01 | 0:9bc41d70bdd3 | 34 | memset(siriusText[i][j], 0, 8); |
rtgree01 | 0:9bc41d70bdd3 | 35 | } |
rtgree01 | 0:9bc41d70bdd3 | 36 | } |
rtgree01 | 0:9bc41d70bdd3 | 37 | |
rtgree01 | 0:9bc41d70bdd3 | 38 | // usb->printf("starting\r\n"); |
rtgree01 | 0:9bc41d70bdd3 | 39 | |
rtgree01 | 0:9bc41d70bdd3 | 40 | memset(&status, 0, sizeof(status)); |
rtgree01 | 0:9bc41d70bdd3 | 41 | status._radioMode = SAT; |
rtgree01 | 0:9bc41d70bdd3 | 42 | // readInitFile(); |
rtgree01 | 0:9bc41d70bdd3 | 43 | status._volume = 10; |
rtgree01 | 0:9bc41d70bdd3 | 44 | status._bass = 15; |
rtgree01 | 0:9bc41d70bdd3 | 45 | status._mid = 13; |
rtgree01 | 0:9bc41d70bdd3 | 46 | status._treble = 14; |
rtgree01 | 0:9bc41d70bdd3 | 47 | status._balance = 10; |
rtgree01 | 0:9bc41d70bdd3 | 48 | status._fade = 10; |
rtgree01 | 0:9bc41d70bdd3 | 49 | |
rtgree01 | 0:9bc41d70bdd3 | 50 | PowerUp(); |
rtgree01 | 0:9bc41d70bdd3 | 51 | |
rtgree01 | 0:9bc41d70bdd3 | 52 | ReceivedHostMsg = false; |
rtgree01 | 0:9bc41d70bdd3 | 53 | statusTicker.attach(this, &RadioEmulator::SendStatusToHost, 0.1); |
rtgree01 | 0:9bc41d70bdd3 | 54 | |
rtgree01 | 0:9bc41d70bdd3 | 55 | opMode = standalone; |
rtgree01 | 0:9bc41d70bdd3 | 56 | HostTimeout.attach(this, &RadioEmulator::CheckHostTimeout, 1); |
rtgree01 | 0:9bc41d70bdd3 | 57 | |
rtgree01 | 0:9bc41d70bdd3 | 58 | for (int i = 0; i < 6; i++) |
rtgree01 | 0:9bc41d70bdd3 | 59 | { |
rtgree01 | 0:9bc41d70bdd3 | 60 | buttonClicks[i] = 0; |
rtgree01 | 0:9bc41d70bdd3 | 61 | buttonHeld[i] = false; |
rtgree01 | 0:9bc41d70bdd3 | 62 | } |
rtgree01 | 0:9bc41d70bdd3 | 63 | reportButtonClick = false; |
rtgree01 | 0:9bc41d70bdd3 | 64 | buttonClickTimedOut = false; |
rtgree01 | 0:9bc41d70bdd3 | 65 | } |
rtgree01 | 0:9bc41d70bdd3 | 66 | |
rtgree01 | 0:9bc41d70bdd3 | 67 | void RadioEmulator::readInitFile() |
rtgree01 | 0:9bc41d70bdd3 | 68 | { |
rtgree01 | 0:9bc41d70bdd3 | 69 | FILE *fp = fopen("/sd/stereo.txt", "r"); // Open "out.txt" on the local file system for writing |
rtgree01 | 0:9bc41d70bdd3 | 70 | char temp[100]; |
rtgree01 | 0:9bc41d70bdd3 | 71 | |
rtgree01 | 0:9bc41d70bdd3 | 72 | while ( fscanf(fp, "%s", temp) > 0) |
rtgree01 | 0:9bc41d70bdd3 | 73 | { |
rtgree01 | 0:9bc41d70bdd3 | 74 | if (strcmp(temp, "volume") == 0) |
rtgree01 | 0:9bc41d70bdd3 | 75 | { |
rtgree01 | 0:9bc41d70bdd3 | 76 | fscanf(fp, "%d", &status._volume); |
rtgree01 | 0:9bc41d70bdd3 | 77 | } |
rtgree01 | 0:9bc41d70bdd3 | 78 | if (strcmp(temp, "bass") == 0) |
rtgree01 | 0:9bc41d70bdd3 | 79 | { |
rtgree01 | 0:9bc41d70bdd3 | 80 | fscanf(fp, "%d", &status._bass); |
rtgree01 | 0:9bc41d70bdd3 | 81 | } |
rtgree01 | 0:9bc41d70bdd3 | 82 | if (strcmp(temp, "mid") == 0) |
rtgree01 | 0:9bc41d70bdd3 | 83 | { |
rtgree01 | 0:9bc41d70bdd3 | 84 | fscanf(fp, "%d", &status._mid); |
rtgree01 | 0:9bc41d70bdd3 | 85 | } |
rtgree01 | 0:9bc41d70bdd3 | 86 | if (strcmp(temp, "treble") == 0) |
rtgree01 | 0:9bc41d70bdd3 | 87 | { |
rtgree01 | 0:9bc41d70bdd3 | 88 | fscanf(fp, "%d", &status._treble); |
rtgree01 | 0:9bc41d70bdd3 | 89 | } |
rtgree01 | 0:9bc41d70bdd3 | 90 | if (strcmp(temp, "balance") == 0) |
rtgree01 | 0:9bc41d70bdd3 | 91 | { |
rtgree01 | 0:9bc41d70bdd3 | 92 | fscanf(fp, "%d", &status._balance); |
rtgree01 | 0:9bc41d70bdd3 | 93 | } |
rtgree01 | 0:9bc41d70bdd3 | 94 | if (strcmp(temp, "fade") == 0) |
rtgree01 | 0:9bc41d70bdd3 | 95 | { |
rtgree01 | 0:9bc41d70bdd3 | 96 | fscanf(fp, "%d", &status._fade); |
rtgree01 | 0:9bc41d70bdd3 | 97 | } |
rtgree01 | 0:9bc41d70bdd3 | 98 | } |
rtgree01 | 0:9bc41d70bdd3 | 99 | |
rtgree01 | 0:9bc41d70bdd3 | 100 | fclose(fp); |
rtgree01 | 0:9bc41d70bdd3 | 101 | } |
rtgree01 | 0:9bc41d70bdd3 | 102 | |
rtgree01 | 0:9bc41d70bdd3 | 103 | void RadioEmulator::writeInitFile() |
rtgree01 | 0:9bc41d70bdd3 | 104 | { |
rtgree01 | 0:9bc41d70bdd3 | 105 | FILE *fp = fopen("/sd/stereo.txt", "w"); // Open "out.txt" on the local file system for writing |
rtgree01 | 0:9bc41d70bdd3 | 106 | |
rtgree01 | 0:9bc41d70bdd3 | 107 | fprintf(fp,"volume %d\r\n", status._volume); |
rtgree01 | 0:9bc41d70bdd3 | 108 | fprintf(fp,"bass %d\r\n", status._bass); |
rtgree01 | 0:9bc41d70bdd3 | 109 | fprintf(fp,"mid %d\r\n", status._mid); |
rtgree01 | 0:9bc41d70bdd3 | 110 | fprintf(fp,"treble %d\r\n", status._treble); |
rtgree01 | 0:9bc41d70bdd3 | 111 | fprintf(fp,"balance %d\r\n", status._balance); |
rtgree01 | 0:9bc41d70bdd3 | 112 | fprintf(fp,"fade %d\r\n", status._fade); |
rtgree01 | 0:9bc41d70bdd3 | 113 | fclose(fp); |
rtgree01 | 0:9bc41d70bdd3 | 114 | } |
rtgree01 | 0:9bc41d70bdd3 | 115 | |
rtgree01 | 0:9bc41d70bdd3 | 116 | void RadioEmulator::SendOnMsg() |
rtgree01 | 0:9bc41d70bdd3 | 117 | { |
rtgree01 | 0:9bc41d70bdd3 | 118 | CANMessage msg; |
rtgree01 | 0:9bc41d70bdd3 | 119 | msg.id = 0x416; |
rtgree01 | 0:9bc41d70bdd3 | 120 | msg.len = 8; |
rtgree01 | 0:9bc41d70bdd3 | 121 | char temp[8] = {0xfe, 0x1b, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff}; |
rtgree01 | 0:9bc41d70bdd3 | 122 | memcpy(msg.data, temp, 8); |
rtgree01 | 0:9bc41d70bdd3 | 123 | can2->write(msg); |
rtgree01 | 0:9bc41d70bdd3 | 124 | } |
rtgree01 | 0:9bc41d70bdd3 | 125 | |
rtgree01 | 0:9bc41d70bdd3 | 126 | void RadioEmulator::SendRadioModeMsg() |
rtgree01 | 0:9bc41d70bdd3 | 127 | { |
rtgree01 | 0:9bc41d70bdd3 | 128 | CANMessage msg; |
rtgree01 | 0:9bc41d70bdd3 | 129 | msg.id = 0x09F; |
rtgree01 | 0:9bc41d70bdd3 | 130 | msg.len = 8; |
rtgree01 | 0:9bc41d70bdd3 | 131 | |
rtgree01 | 0:9bc41d70bdd3 | 132 | msg.data[7] = 0x11; |
rtgree01 | 0:9bc41d70bdd3 | 133 | msg.data[6] = 0xff; |
rtgree01 | 0:9bc41d70bdd3 | 134 | msg.data[5] = 0xff; |
rtgree01 | 0:9bc41d70bdd3 | 135 | msg.data[4] = 0xff; |
rtgree01 | 0:9bc41d70bdd3 | 136 | msg.data[3] = 0x07; |
rtgree01 | 0:9bc41d70bdd3 | 137 | msg.data[2] = 0x00; |
rtgree01 | 0:9bc41d70bdd3 | 138 | msg.data[1] = 0x00; |
rtgree01 | 0:9bc41d70bdd3 | 139 | msg.data[0] = 0x00; |
rtgree01 | 0:9bc41d70bdd3 | 140 | |
rtgree01 | 0:9bc41d70bdd3 | 141 | if (status._radioMode == AM) |
rtgree01 | 0:9bc41d70bdd3 | 142 | { |
rtgree01 | 0:9bc41d70bdd3 | 143 | if (status._amPreset != 0) |
rtgree01 | 0:9bc41d70bdd3 | 144 | { |
rtgree01 | 0:9bc41d70bdd3 | 145 | msg.data[0] = (status._amPreset + 1) << 4; |
rtgree01 | 0:9bc41d70bdd3 | 146 | } |
rtgree01 | 0:9bc41d70bdd3 | 147 | msg.data[1] = (status._amFreq & 0xFF00) >> 8; |
rtgree01 | 0:9bc41d70bdd3 | 148 | msg.data[2] = (status._amFreq & 0x00FF); |
rtgree01 | 0:9bc41d70bdd3 | 149 | } |
rtgree01 | 0:9bc41d70bdd3 | 150 | else if (status._radioMode == FM) |
rtgree01 | 0:9bc41d70bdd3 | 151 | { |
rtgree01 | 0:9bc41d70bdd3 | 152 | if (status._fmPreset != 0) |
rtgree01 | 0:9bc41d70bdd3 | 153 | { |
rtgree01 | 0:9bc41d70bdd3 | 154 | msg.data[0] = (status._fmPreset + 1) << 4; |
rtgree01 | 0:9bc41d70bdd3 | 155 | } |
rtgree01 | 0:9bc41d70bdd3 | 156 | msg.data[0] = 0x01; |
rtgree01 | 0:9bc41d70bdd3 | 157 | msg.data[1] = (status._fmFreq & 0xFF00) >> 8; |
rtgree01 | 0:9bc41d70bdd3 | 158 | msg.data[2] = (status._fmFreq & 0x00FF); |
rtgree01 | 0:9bc41d70bdd3 | 159 | } |
rtgree01 | 0:9bc41d70bdd3 | 160 | else if (status._radioMode == CD) |
rtgree01 | 0:9bc41d70bdd3 | 161 | { |
rtgree01 | 0:9bc41d70bdd3 | 162 | msg.data[0] = ((status._cdNum + 1) << 4) + 0x3; |
rtgree01 | 0:9bc41d70bdd3 | 163 | msg.data[2] = status._cdTrackNum; |
rtgree01 | 0:9bc41d70bdd3 | 164 | msg.data[5] = status._cdHours; |
rtgree01 | 0:9bc41d70bdd3 | 165 | msg.data[6] = status._cdMinutes; |
rtgree01 | 0:9bc41d70bdd3 | 166 | msg.data[7] = status._cdSeconds; |
rtgree01 | 0:9bc41d70bdd3 | 167 | } |
rtgree01 | 0:9bc41d70bdd3 | 168 | else if (status._radioMode == SAT) |
rtgree01 | 0:9bc41d70bdd3 | 169 | { |
rtgree01 | 0:9bc41d70bdd3 | 170 | if (status._siriusPreset != 0) |
rtgree01 | 0:9bc41d70bdd3 | 171 | { |
rtgree01 | 0:9bc41d70bdd3 | 172 | msg.data[0] = (status._siriusPreset + 1) << 4; |
rtgree01 | 0:9bc41d70bdd3 | 173 | } |
rtgree01 | 0:9bc41d70bdd3 | 174 | msg.data[0] |= 0x04; |
rtgree01 | 0:9bc41d70bdd3 | 175 | msg.data[1] = (status._fmFreq & 0xFF00) >> 8; |
rtgree01 | 0:9bc41d70bdd3 | 176 | msg.data[2] = status._siriusChan; |
rtgree01 | 0:9bc41d70bdd3 | 177 | } |
rtgree01 | 0:9bc41d70bdd3 | 178 | else if (status._radioMode == VES) |
rtgree01 | 0:9bc41d70bdd3 | 179 | { |
rtgree01 | 0:9bc41d70bdd3 | 180 | msg.data[0] = 0x16; |
rtgree01 | 0:9bc41d70bdd3 | 181 | msg.data[1] = 0x10; |
rtgree01 | 0:9bc41d70bdd3 | 182 | msg.data[2] = 0x01; |
rtgree01 | 0:9bc41d70bdd3 | 183 | } |
rtgree01 | 0:9bc41d70bdd3 | 184 | |
rtgree01 | 0:9bc41d70bdd3 | 185 | msg.data[1] |= 0x10; |
rtgree01 | 0:9bc41d70bdd3 | 186 | |
rtgree01 | 0:9bc41d70bdd3 | 187 | can2->write(msg); |
rtgree01 | 0:9bc41d70bdd3 | 188 | } |
rtgree01 | 0:9bc41d70bdd3 | 189 | |
rtgree01 | 0:9bc41d70bdd3 | 190 | void RadioEmulator::SendEVICMsg() |
rtgree01 | 0:9bc41d70bdd3 | 191 | { |
rtgree01 | 0:9bc41d70bdd3 | 192 | CANMessage msg; |
rtgree01 | 0:9bc41d70bdd3 | 193 | msg.id = 0x394; |
rtgree01 | 0:9bc41d70bdd3 | 194 | msg.len = 6; |
rtgree01 | 0:9bc41d70bdd3 | 195 | |
rtgree01 | 0:9bc41d70bdd3 | 196 | memset(msg.data, 0, 8); |
rtgree01 | 0:9bc41d70bdd3 | 197 | |
rtgree01 | 0:9bc41d70bdd3 | 198 | if (status._radioMode == AM) |
rtgree01 | 0:9bc41d70bdd3 | 199 | { |
rtgree01 | 0:9bc41d70bdd3 | 200 | if (status._amPreset != 0) |
rtgree01 | 0:9bc41d70bdd3 | 201 | { |
rtgree01 | 0:9bc41d70bdd3 | 202 | msg.data[0] = (status._amPreset + 1) << 4; |
rtgree01 | 0:9bc41d70bdd3 | 203 | } |
rtgree01 | 0:9bc41d70bdd3 | 204 | msg.data[1] = (status._amFreq & 0xFF00) >> 8; |
rtgree01 | 0:9bc41d70bdd3 | 205 | msg.data[2] = (status._amFreq & 0x00FF); |
rtgree01 | 0:9bc41d70bdd3 | 206 | } |
rtgree01 | 0:9bc41d70bdd3 | 207 | else |
rtgree01 | 0:9bc41d70bdd3 | 208 | { |
rtgree01 | 0:9bc41d70bdd3 | 209 | if (status._fmPreset != 0) |
rtgree01 | 0:9bc41d70bdd3 | 210 | { |
rtgree01 | 0:9bc41d70bdd3 | 211 | msg.data[0] = (status._fmPreset + 1) << 4; |
rtgree01 | 0:9bc41d70bdd3 | 212 | } |
rtgree01 | 0:9bc41d70bdd3 | 213 | msg.data[0] = 0x01; |
rtgree01 | 0:9bc41d70bdd3 | 214 | msg.data[1] = (status._fmFreq & 0xFF00) >> 8; |
rtgree01 | 0:9bc41d70bdd3 | 215 | msg.data[2] = (status._fmFreq & 0x00FF); |
rtgree01 | 0:9bc41d70bdd3 | 216 | } |
rtgree01 | 0:9bc41d70bdd3 | 217 | |
rtgree01 | 0:9bc41d70bdd3 | 218 | can2->write(msg); |
rtgree01 | 0:9bc41d70bdd3 | 219 | } |
rtgree01 | 0:9bc41d70bdd3 | 220 | |
rtgree01 | 0:9bc41d70bdd3 | 221 | void RadioEmulator::SendStereoSettingsMsg() |
rtgree01 | 0:9bc41d70bdd3 | 222 | { |
rtgree01 | 0:9bc41d70bdd3 | 223 | CANMessage msg; |
rtgree01 | 0:9bc41d70bdd3 | 224 | msg.id = 0x3D0; |
rtgree01 | 0:9bc41d70bdd3 | 225 | msg.len = 7; |
rtgree01 | 0:9bc41d70bdd3 | 226 | |
rtgree01 | 0:9bc41d70bdd3 | 227 | msg.data[0] = status._volume; |
rtgree01 | 0:9bc41d70bdd3 | 228 | msg.data[1] = status._balance; |
rtgree01 | 0:9bc41d70bdd3 | 229 | msg.data[2] = status._fade; |
rtgree01 | 0:9bc41d70bdd3 | 230 | msg.data[3] = status._bass; |
rtgree01 | 0:9bc41d70bdd3 | 231 | msg.data[4] = status._mid; |
rtgree01 | 0:9bc41d70bdd3 | 232 | msg.data[5] = status._treble; |
rtgree01 | 0:9bc41d70bdd3 | 233 | |
rtgree01 | 0:9bc41d70bdd3 | 234 | can2->write(msg); |
rtgree01 | 0:9bc41d70bdd3 | 235 | } |
rtgree01 | 0:9bc41d70bdd3 | 236 | |
rtgree01 | 0:9bc41d70bdd3 | 237 | void RadioEmulator::ChangeSiriusStation(int station, bool turn_on) |
rtgree01 | 0:9bc41d70bdd3 | 238 | { |
rtgree01 | 0:9bc41d70bdd3 | 239 | CANMessage msg; |
rtgree01 | 0:9bc41d70bdd3 | 240 | msg.id = 0x3B0; |
rtgree01 | 0:9bc41d70bdd3 | 241 | msg.len = 6; |
rtgree01 | 0:9bc41d70bdd3 | 242 | |
rtgree01 | 0:9bc41d70bdd3 | 243 | if (turn_on) |
rtgree01 | 0:9bc41d70bdd3 | 244 | { |
rtgree01 | 0:9bc41d70bdd3 | 245 | msg.data[0] = 21; |
rtgree01 | 0:9bc41d70bdd3 | 246 | } |
rtgree01 | 0:9bc41d70bdd3 | 247 | else |
rtgree01 | 0:9bc41d70bdd3 | 248 | { |
rtgree01 | 0:9bc41d70bdd3 | 249 | msg.data[0] = 23; |
rtgree01 | 0:9bc41d70bdd3 | 250 | } |
rtgree01 | 0:9bc41d70bdd3 | 251 | msg.data[1] = station; |
rtgree01 | 0:9bc41d70bdd3 | 252 | |
rtgree01 | 0:9bc41d70bdd3 | 253 | can2->write(msg); |
rtgree01 | 0:9bc41d70bdd3 | 254 | |
rtgree01 | 0:9bc41d70bdd3 | 255 | memset(msg.data, 0, 8); |
rtgree01 | 0:9bc41d70bdd3 | 256 | msg.data[1] = station; |
rtgree01 | 0:9bc41d70bdd3 | 257 | |
rtgree01 | 0:9bc41d70bdd3 | 258 | can2->write(msg); |
rtgree01 | 0:9bc41d70bdd3 | 259 | |
rtgree01 | 0:9bc41d70bdd3 | 260 | status._siriusChan = station; |
rtgree01 | 0:9bc41d70bdd3 | 261 | } |
rtgree01 | 0:9bc41d70bdd3 | 262 | |
rtgree01 | 0:9bc41d70bdd3 | 263 | void RadioEmulator::ParseCANMessage(CANMessage can_MsgRx) |
rtgree01 | 0:9bc41d70bdd3 | 264 | { |
rtgree01 | 0:9bc41d70bdd3 | 265 | if (can_MsgRx.id == 0x000) |
rtgree01 | 0:9bc41d70bdd3 | 266 | { |
rtgree01 | 0:9bc41d70bdd3 | 267 | if (can_MsgRx.data[0] > 1) |
rtgree01 | 0:9bc41d70bdd3 | 268 | { |
rtgree01 | 0:9bc41d70bdd3 | 269 | radioOn = true; |
rtgree01 | 0:9bc41d70bdd3 | 270 | } |
rtgree01 | 0:9bc41d70bdd3 | 271 | else |
rtgree01 | 0:9bc41d70bdd3 | 272 | { |
rtgree01 | 0:9bc41d70bdd3 | 273 | radioOn = false; |
rtgree01 | 0:9bc41d70bdd3 | 274 | } |
rtgree01 | 0:9bc41d70bdd3 | 275 | } |
rtgree01 | 0:9bc41d70bdd3 | 276 | |
rtgree01 | 0:9bc41d70bdd3 | 277 | // this message seems to be a message requesting all other devices |
rtgree01 | 0:9bc41d70bdd3 | 278 | // to start announcing their presence |
rtgree01 | 0:9bc41d70bdd3 | 279 | if ((can_MsgRx.id >= 0x400) && (can_MsgRx.data[0] == 0xfd)) |
rtgree01 | 0:9bc41d70bdd3 | 280 | { |
rtgree01 | 0:9bc41d70bdd3 | 281 | // SendOnMsg(); |
rtgree01 | 0:9bc41d70bdd3 | 282 | } |
rtgree01 | 0:9bc41d70bdd3 | 283 | |
rtgree01 | 0:9bc41d70bdd3 | 284 | if (can_MsgRx.id == 0x012) |
rtgree01 | 0:9bc41d70bdd3 | 285 | { |
rtgree01 | 0:9bc41d70bdd3 | 286 | if (memcmp(can_MsgRx.data, unlock, 6) == 0) |
rtgree01 | 0:9bc41d70bdd3 | 287 | { |
rtgree01 | 0:9bc41d70bdd3 | 288 | } |
rtgree01 | 0:9bc41d70bdd3 | 289 | else if (memcmp(can_MsgRx.data, lock, 6) == 0) |
rtgree01 | 0:9bc41d70bdd3 | 290 | { |
rtgree01 | 0:9bc41d70bdd3 | 291 | } |
rtgree01 | 0:9bc41d70bdd3 | 292 | else if (memcmp(can_MsgRx.data, trunk, 6) == 0) |
rtgree01 | 0:9bc41d70bdd3 | 293 | { |
rtgree01 | 0:9bc41d70bdd3 | 294 | } |
rtgree01 | 0:9bc41d70bdd3 | 295 | } |
rtgree01 | 0:9bc41d70bdd3 | 296 | |
rtgree01 | 0:9bc41d70bdd3 | 297 | if (can_MsgRx.id == 0x1bd) |
rtgree01 | 0:9bc41d70bdd3 | 298 | { |
rtgree01 | 0:9bc41d70bdd3 | 299 | // SDAR status |
rtgree01 | 0:9bc41d70bdd3 | 300 | |
rtgree01 | 0:9bc41d70bdd3 | 301 | if (status._siriusChan == 0) |
rtgree01 | 0:9bc41d70bdd3 | 302 | { |
rtgree01 | 0:9bc41d70bdd3 | 303 | status._siriusChan = can_MsgRx.data[1]; |
rtgree01 | 0:9bc41d70bdd3 | 304 | } |
rtgree01 | 0:9bc41d70bdd3 | 305 | |
rtgree01 | 0:9bc41d70bdd3 | 306 | if (can_MsgRx.data[0] == 0x85) |
rtgree01 | 0:9bc41d70bdd3 | 307 | { |
rtgree01 | 0:9bc41d70bdd3 | 308 | ChangeSiriusStation(status._siriusChan, true); |
rtgree01 | 0:9bc41d70bdd3 | 309 | } |
rtgree01 | 0:9bc41d70bdd3 | 310 | |
rtgree01 | 0:9bc41d70bdd3 | 311 | if (status._siriusChan != can_MsgRx.data[1]) |
rtgree01 | 0:9bc41d70bdd3 | 312 | { |
rtgree01 | 0:9bc41d70bdd3 | 313 | ChangeSiriusStation(status._siriusChan, true); |
rtgree01 | 0:9bc41d70bdd3 | 314 | } |
rtgree01 | 0:9bc41d70bdd3 | 315 | } |
rtgree01 | 0:9bc41d70bdd3 | 316 | |
rtgree01 | 0:9bc41d70bdd3 | 317 | if (can_MsgRx.id == 0x3bd) |
rtgree01 | 0:9bc41d70bdd3 | 318 | { |
rtgree01 | 0:9bc41d70bdd3 | 319 | ReadSiriusText((char *)can_MsgRx.data); |
rtgree01 | 0:9bc41d70bdd3 | 320 | } |
rtgree01 | 0:9bc41d70bdd3 | 321 | |
rtgree01 | 0:9bc41d70bdd3 | 322 | if (can_MsgRx.id == 0x3a0) |
rtgree01 | 0:9bc41d70bdd3 | 323 | { |
rtgree01 | 0:9bc41d70bdd3 | 324 | // note = 0x01 |
rtgree01 | 0:9bc41d70bdd3 | 325 | // volume up = 0x02 |
rtgree01 | 0:9bc41d70bdd3 | 326 | // volume down = 0x04 |
rtgree01 | 0:9bc41d70bdd3 | 327 | // up arrow = 0x08 |
rtgree01 | 0:9bc41d70bdd3 | 328 | // down arrow = 0x10 |
rtgree01 | 0:9bc41d70bdd3 | 329 | // right arrow = 0x20 |
rtgree01 | 0:9bc41d70bdd3 | 330 | |
rtgree01 | 0:9bc41d70bdd3 | 331 | |
rtgree01 | 0:9bc41d70bdd3 | 332 | for ( int i = 0; i < 6; i++) |
rtgree01 | 0:9bc41d70bdd3 | 333 | { |
rtgree01 | 0:9bc41d70bdd3 | 334 | if (((can_MsgRx.data[0] & (0x01 << i)) == (0x01 << i)) && ((prevSWC & (0x01 << i)) == 0)) |
rtgree01 | 0:9bc41d70bdd3 | 335 | { |
rtgree01 | 0:9bc41d70bdd3 | 336 | buttonTimeout.detach(); |
rtgree01 | 0:9bc41d70bdd3 | 337 | buttonClicks[i] ++; |
rtgree01 | 0:9bc41d70bdd3 | 338 | buttonClickTimedOut = false; |
rtgree01 | 0:9bc41d70bdd3 | 339 | buttonTimeout.attach_us(this, &RadioEmulator::ClickTimeout, 200 * 1000); |
rtgree01 | 0:9bc41d70bdd3 | 340 | } |
rtgree01 | 0:9bc41d70bdd3 | 341 | else if (((can_MsgRx.data[0] & (0x01 << i)) == 0) && ((prevSWC & (0x01 << i)) != 0)) |
rtgree01 | 0:9bc41d70bdd3 | 342 | { |
rtgree01 | 0:9bc41d70bdd3 | 343 | buttonHeld[i] = false; |
rtgree01 | 0:9bc41d70bdd3 | 344 | |
rtgree01 | 0:9bc41d70bdd3 | 345 | if (buttonClickTimedOut == true) |
rtgree01 | 0:9bc41d70bdd3 | 346 | { |
rtgree01 | 0:9bc41d70bdd3 | 347 | buttonClickTimedOut = false; |
rtgree01 | 0:9bc41d70bdd3 | 348 | buttonClicks[i] = 0; |
rtgree01 | 0:9bc41d70bdd3 | 349 | } |
rtgree01 | 0:9bc41d70bdd3 | 350 | } |
rtgree01 | 0:9bc41d70bdd3 | 351 | } |
rtgree01 | 0:9bc41d70bdd3 | 352 | |
rtgree01 | 0:9bc41d70bdd3 | 353 | prevSWC = can_MsgRx.data[0]; |
rtgree01 | 0:9bc41d70bdd3 | 354 | } |
rtgree01 | 0:9bc41d70bdd3 | 355 | |
rtgree01 | 0:9bc41d70bdd3 | 356 | |
rtgree01 | 0:9bc41d70bdd3 | 357 | if (can_MsgRx.id == 0x000) |
rtgree01 | 0:9bc41d70bdd3 | 358 | { |
rtgree01 | 0:9bc41d70bdd3 | 359 | status._keyPosition = can_MsgRx.data[0]; |
rtgree01 | 0:9bc41d70bdd3 | 360 | } |
rtgree01 | 0:9bc41d70bdd3 | 361 | else if (can_MsgRx.id == 0x002) |
rtgree01 | 0:9bc41d70bdd3 | 362 | { |
rtgree01 | 0:9bc41d70bdd3 | 363 | status._rpm = (can_MsgRx.data[0] << 8) + can_MsgRx.data[1]; |
rtgree01 | 0:9bc41d70bdd3 | 364 | status._speed = ((can_MsgRx.data[2] << 8) + can_MsgRx.data[3]) / 200; |
rtgree01 | 0:9bc41d70bdd3 | 365 | } |
rtgree01 | 0:9bc41d70bdd3 | 366 | else if (can_MsgRx.id == 0x003) |
rtgree01 | 0:9bc41d70bdd3 | 367 | { |
rtgree01 | 0:9bc41d70bdd3 | 368 | status._brake = can_MsgRx.data[3] & 0x01; |
rtgree01 | 0:9bc41d70bdd3 | 369 | status._gear = can_MsgRx.data[4]; |
rtgree01 | 0:9bc41d70bdd3 | 370 | } |
rtgree01 | 0:9bc41d70bdd3 | 371 | else if (can_MsgRx.id == 0x15) |
rtgree01 | 0:9bc41d70bdd3 | 372 | { |
rtgree01 | 0:9bc41d70bdd3 | 373 | status._batteryVoltage = (float)(can_MsgRx.data[1]) / 10; |
rtgree01 | 0:9bc41d70bdd3 | 374 | } |
rtgree01 | 0:9bc41d70bdd3 | 375 | else if (can_MsgRx.id == 0x01b) |
rtgree01 | 0:9bc41d70bdd3 | 376 | { |
rtgree01 | 0:9bc41d70bdd3 | 377 | // vin number |
rtgree01 | 0:9bc41d70bdd3 | 378 | int part = can_MsgRx.data[0]; |
rtgree01 | 0:9bc41d70bdd3 | 379 | if (part < 3) |
rtgree01 | 0:9bc41d70bdd3 | 380 | { |
rtgree01 | 0:9bc41d70bdd3 | 381 | for (int i = 1; i < 8; i++) |
rtgree01 | 0:9bc41d70bdd3 | 382 | { |
rtgree01 | 0:9bc41d70bdd3 | 383 | status._vin[(part*8) + i-1] = can_MsgRx.data[i]; |
rtgree01 | 0:9bc41d70bdd3 | 384 | } |
rtgree01 | 0:9bc41d70bdd3 | 385 | } |
rtgree01 | 0:9bc41d70bdd3 | 386 | } |
rtgree01 | 0:9bc41d70bdd3 | 387 | else if (can_MsgRx.id == 0x0d0) |
rtgree01 | 0:9bc41d70bdd3 | 388 | { |
rtgree01 | 0:9bc41d70bdd3 | 389 | if (can_MsgRx.data[0] == 0x80) |
rtgree01 | 0:9bc41d70bdd3 | 390 | { |
rtgree01 | 0:9bc41d70bdd3 | 391 | status._parkingBrake = true; |
rtgree01 | 0:9bc41d70bdd3 | 392 | } |
rtgree01 | 0:9bc41d70bdd3 | 393 | else |
rtgree01 | 0:9bc41d70bdd3 | 394 | { |
rtgree01 | 0:9bc41d70bdd3 | 395 | status._parkingBrake = false; |
rtgree01 | 0:9bc41d70bdd3 | 396 | } |
rtgree01 | 0:9bc41d70bdd3 | 397 | } |
rtgree01 | 0:9bc41d70bdd3 | 398 | else if (can_MsgRx.id == 0x0EC) |
rtgree01 | 0:9bc41d70bdd3 | 399 | { |
rtgree01 | 0:9bc41d70bdd3 | 400 | if ((can_MsgRx.data[0] & 0x40) == 0x40) |
rtgree01 | 0:9bc41d70bdd3 | 401 | { |
rtgree01 | 0:9bc41d70bdd3 | 402 | status._fanRequested = true; |
rtgree01 | 0:9bc41d70bdd3 | 403 | } |
rtgree01 | 0:9bc41d70bdd3 | 404 | else |
rtgree01 | 0:9bc41d70bdd3 | 405 | { |
rtgree01 | 0:9bc41d70bdd3 | 406 | status._fanRequested = false; |
rtgree01 | 0:9bc41d70bdd3 | 407 | } |
rtgree01 | 0:9bc41d70bdd3 | 408 | |
rtgree01 | 0:9bc41d70bdd3 | 409 | if ((can_MsgRx.data[0] & 0x01) == 0x01) |
rtgree01 | 0:9bc41d70bdd3 | 410 | { |
rtgree01 | 0:9bc41d70bdd3 | 411 | status._fanOn = true; |
rtgree01 | 0:9bc41d70bdd3 | 412 | } |
rtgree01 | 0:9bc41d70bdd3 | 413 | else |
rtgree01 | 0:9bc41d70bdd3 | 414 | { |
rtgree01 | 0:9bc41d70bdd3 | 415 | status._fanOn = false; |
rtgree01 | 0:9bc41d70bdd3 | 416 | } |
rtgree01 | 0:9bc41d70bdd3 | 417 | } |
rtgree01 | 0:9bc41d70bdd3 | 418 | else if (can_MsgRx.id == 0x159) |
rtgree01 | 0:9bc41d70bdd3 | 419 | { |
rtgree01 | 0:9bc41d70bdd3 | 420 | status._fuel = can_MsgRx.data[5]; |
rtgree01 | 0:9bc41d70bdd3 | 421 | } |
rtgree01 | 0:9bc41d70bdd3 | 422 | else if (can_MsgRx.id == 0x1a2) |
rtgree01 | 0:9bc41d70bdd3 | 423 | { |
rtgree01 | 0:9bc41d70bdd3 | 424 | if ((can_MsgRx.data[0] & 0x80) == 0x80) |
rtgree01 | 0:9bc41d70bdd3 | 425 | { |
rtgree01 | 0:9bc41d70bdd3 | 426 | status._rearDefrost = true; |
rtgree01 | 0:9bc41d70bdd3 | 427 | } |
rtgree01 | 0:9bc41d70bdd3 | 428 | else |
rtgree01 | 0:9bc41d70bdd3 | 429 | { |
rtgree01 | 0:9bc41d70bdd3 | 430 | status._rearDefrost = false; |
rtgree01 | 0:9bc41d70bdd3 | 431 | } |
rtgree01 | 0:9bc41d70bdd3 | 432 | |
rtgree01 | 0:9bc41d70bdd3 | 433 | if ((can_MsgRx.data[0] & 0x40) == 0x40) |
rtgree01 | 0:9bc41d70bdd3 | 434 | { |
rtgree01 | 0:9bc41d70bdd3 | 435 | status._fanRequested = true; |
rtgree01 | 0:9bc41d70bdd3 | 436 | } |
rtgree01 | 0:9bc41d70bdd3 | 437 | else |
rtgree01 | 0:9bc41d70bdd3 | 438 | { |
rtgree01 | 0:9bc41d70bdd3 | 439 | status._fanRequested = false; |
rtgree01 | 0:9bc41d70bdd3 | 440 | } |
rtgree01 | 0:9bc41d70bdd3 | 441 | |
rtgree01 | 0:9bc41d70bdd3 | 442 | if ((can_MsgRx.data[0] & 0x01) == 0x01) |
rtgree01 | 0:9bc41d70bdd3 | 443 | { |
rtgree01 | 0:9bc41d70bdd3 | 444 | status._fanOn = true; |
rtgree01 | 0:9bc41d70bdd3 | 445 | } |
rtgree01 | 0:9bc41d70bdd3 | 446 | else |
rtgree01 | 0:9bc41d70bdd3 | 447 | { |
rtgree01 | 0:9bc41d70bdd3 | 448 | status._fanOn = false; |
rtgree01 | 0:9bc41d70bdd3 | 449 | } |
rtgree01 | 0:9bc41d70bdd3 | 450 | } |
rtgree01 | 0:9bc41d70bdd3 | 451 | else if (can_MsgRx.id == 0x1c8) |
rtgree01 | 0:9bc41d70bdd3 | 452 | { |
rtgree01 | 0:9bc41d70bdd3 | 453 | status._headlights = can_MsgRx.data[0]; |
rtgree01 | 0:9bc41d70bdd3 | 454 | } |
rtgree01 | 0:9bc41d70bdd3 | 455 | else if (can_MsgRx.id == 0x210) |
rtgree01 | 0:9bc41d70bdd3 | 456 | { |
rtgree01 | 0:9bc41d70bdd3 | 457 | status._dimmerMode = can_MsgRx.data[0]; |
rtgree01 | 0:9bc41d70bdd3 | 458 | if (can_MsgRx.data[0] == 0x03) |
rtgree01 | 0:9bc41d70bdd3 | 459 | { |
rtgree01 | 0:9bc41d70bdd3 | 460 | status._dimmer = -1; |
rtgree01 | 0:9bc41d70bdd3 | 461 | } |
rtgree01 | 0:9bc41d70bdd3 | 462 | else if (can_MsgRx.data[0] == 0x02) |
rtgree01 | 0:9bc41d70bdd3 | 463 | { |
rtgree01 | 0:9bc41d70bdd3 | 464 | status._dimmer = can_MsgRx.data[1]; |
rtgree01 | 0:9bc41d70bdd3 | 465 | } |
rtgree01 | 0:9bc41d70bdd3 | 466 | } |
rtgree01 | 0:9bc41d70bdd3 | 467 | |
rtgree01 | 0:9bc41d70bdd3 | 468 | } |
rtgree01 | 0:9bc41d70bdd3 | 469 | |
rtgree01 | 0:9bc41d70bdd3 | 470 | void RadioEmulator::ReadSiriusText(char *data) |
rtgree01 | 0:9bc41d70bdd3 | 471 | { |
rtgree01 | 0:9bc41d70bdd3 | 472 | int num = (data[0] & 0xF0) >> 4; |
rtgree01 | 0:9bc41d70bdd3 | 473 | if (num > 7) |
rtgree01 | 0:9bc41d70bdd3 | 474 | { |
rtgree01 | 0:9bc41d70bdd3 | 475 | return; |
rtgree01 | 0:9bc41d70bdd3 | 476 | } |
rtgree01 | 0:9bc41d70bdd3 | 477 | |
rtgree01 | 0:9bc41d70bdd3 | 478 | if ((data[0] & 0x01) == 1) |
rtgree01 | 0:9bc41d70bdd3 | 479 | { |
rtgree01 | 0:9bc41d70bdd3 | 480 | for (int i = 0; i < 8; i++) |
rtgree01 | 0:9bc41d70bdd3 | 481 | { |
rtgree01 | 0:9bc41d70bdd3 | 482 | memset(siriusText[num][i], 0, 8); |
rtgree01 | 0:9bc41d70bdd3 | 483 | } |
rtgree01 | 0:9bc41d70bdd3 | 484 | } |
rtgree01 | 0:9bc41d70bdd3 | 485 | int part = (data[0] & 0x0E) >> 1; |
rtgree01 | 0:9bc41d70bdd3 | 486 | if (part > 7) |
rtgree01 | 0:9bc41d70bdd3 | 487 | { |
rtgree01 | 0:9bc41d70bdd3 | 488 | return; |
rtgree01 | 0:9bc41d70bdd3 | 489 | } |
rtgree01 | 0:9bc41d70bdd3 | 490 | |
rtgree01 | 0:9bc41d70bdd3 | 491 | memset(siriusText[num][part], 0, 8); |
rtgree01 | 0:9bc41d70bdd3 | 492 | char tempString[8] = ""; |
rtgree01 | 0:9bc41d70bdd3 | 493 | memset(tempString,0,8); |
rtgree01 | 0:9bc41d70bdd3 | 494 | |
rtgree01 | 0:9bc41d70bdd3 | 495 | for (int i = 1; i < 8; i++) |
rtgree01 | 0:9bc41d70bdd3 | 496 | { |
rtgree01 | 0:9bc41d70bdd3 | 497 | tempString[i-1] = data[i]; |
rtgree01 | 0:9bc41d70bdd3 | 498 | } |
rtgree01 | 0:9bc41d70bdd3 | 499 | |
rtgree01 | 0:9bc41d70bdd3 | 500 | strncpy(siriusText[num][part],tempString,7); |
rtgree01 | 0:9bc41d70bdd3 | 501 | |
rtgree01 | 0:9bc41d70bdd3 | 502 | int cls = (data[0] & 0x0F) >> 1; |
rtgree01 | 0:9bc41d70bdd3 | 503 | if (cls - 1 == 0) |
rtgree01 | 0:9bc41d70bdd3 | 504 | { |
rtgree01 | 0:9bc41d70bdd3 | 505 | for (int i = 0; i < 8; i++) |
rtgree01 | 0:9bc41d70bdd3 | 506 | { |
rtgree01 | 0:9bc41d70bdd3 | 507 | memset(status._siriusTextLine[i], 0, 64); |
rtgree01 | 0:9bc41d70bdd3 | 508 | for (int j = 0; j < 8; j++) |
rtgree01 | 0:9bc41d70bdd3 | 509 | { |
rtgree01 | 0:9bc41d70bdd3 | 510 | strcat(status._siriusTextLine[i], siriusText[i][j]); |
rtgree01 | 0:9bc41d70bdd3 | 511 | } |
rtgree01 | 0:9bc41d70bdd3 | 512 | |
rtgree01 | 0:9bc41d70bdd3 | 513 | // usb->printf("%d: %s\r\n", i, status._siriusTextLine[i]); |
rtgree01 | 0:9bc41d70bdd3 | 514 | } |
rtgree01 | 0:9bc41d70bdd3 | 515 | } |
rtgree01 | 0:9bc41d70bdd3 | 516 | } |
rtgree01 | 0:9bc41d70bdd3 | 517 | |
rtgree01 | 0:9bc41d70bdd3 | 518 | void RadioEmulator::readCANbus(void) |
rtgree01 | 0:9bc41d70bdd3 | 519 | { |
rtgree01 | 0:9bc41d70bdd3 | 520 | if (can2->read(can_MsgRx)) |
rtgree01 | 0:9bc41d70bdd3 | 521 | { |
rtgree01 | 0:9bc41d70bdd3 | 522 | led3 = !led3; |
rtgree01 | 0:9bc41d70bdd3 | 523 | needToParseCANMessage = true; |
rtgree01 | 0:9bc41d70bdd3 | 524 | ReceivedCANMsg = true; |
rtgree01 | 0:9bc41d70bdd3 | 525 | } |
rtgree01 | 0:9bc41d70bdd3 | 526 | |
rtgree01 | 0:9bc41d70bdd3 | 527 | if (needToParseCANMessage) |
rtgree01 | 0:9bc41d70bdd3 | 528 | { |
rtgree01 | 0:9bc41d70bdd3 | 529 | needToParseCANMessage = false; |
rtgree01 | 0:9bc41d70bdd3 | 530 | |
rtgree01 | 0:9bc41d70bdd3 | 531 | ParseCANMessage(can_MsgRx); |
rtgree01 | 0:9bc41d70bdd3 | 532 | } |
rtgree01 | 0:9bc41d70bdd3 | 533 | } |
rtgree01 | 0:9bc41d70bdd3 | 534 | |
rtgree01 | 0:9bc41d70bdd3 | 535 | void RadioEmulator::HostComm(void) |
rtgree01 | 0:9bc41d70bdd3 | 536 | { |
rtgree01 | 0:9bc41d70bdd3 | 537 | if (usb->readable()) |
rtgree01 | 0:9bc41d70bdd3 | 538 | { |
rtgree01 | 0:9bc41d70bdd3 | 539 | msg[serialCounter] = usb->getc(); |
rtgree01 | 0:9bc41d70bdd3 | 540 | serialCounter++; |
rtgree01 | 0:9bc41d70bdd3 | 541 | |
rtgree01 | 0:9bc41d70bdd3 | 542 | if (serialCounter == 4) |
rtgree01 | 0:9bc41d70bdd3 | 543 | { |
rtgree01 | 0:9bc41d70bdd3 | 544 | if ((msg[0] != 0x42) || (msg[1] != 0x42) || (msg[2] != 0x42) || (msg[3] != 0x42)) |
rtgree01 | 0:9bc41d70bdd3 | 545 | { |
rtgree01 | 0:9bc41d70bdd3 | 546 | serialCounter = 0; |
rtgree01 | 0:9bc41d70bdd3 | 547 | msg[0] = msg[1]; |
rtgree01 | 0:9bc41d70bdd3 | 548 | msg[1] = msg[2]; |
rtgree01 | 0:9bc41d70bdd3 | 549 | msg[2] = msg[3]; |
rtgree01 | 0:9bc41d70bdd3 | 550 | } |
rtgree01 | 0:9bc41d70bdd3 | 551 | } |
rtgree01 | 0:9bc41d70bdd3 | 552 | } |
rtgree01 | 0:9bc41d70bdd3 | 553 | |
rtgree01 | 0:9bc41d70bdd3 | 554 | if (serialCounter > 13) |
rtgree01 | 0:9bc41d70bdd3 | 555 | { |
rtgree01 | 0:9bc41d70bdd3 | 556 | serialCounter = 0; |
rtgree01 | 0:9bc41d70bdd3 | 557 | |
rtgree01 | 0:9bc41d70bdd3 | 558 | if ((msg[0] == 0x42) && (msg[1] == 0x42) && (msg[2] == 0x42) && (msg[3] == 0x42)) |
rtgree01 | 0:9bc41d70bdd3 | 559 | { |
rtgree01 | 0:9bc41d70bdd3 | 560 | ReceivedHostMsg = true; |
rtgree01 | 0:9bc41d70bdd3 | 561 | |
rtgree01 | 0:9bc41d70bdd3 | 562 | switch (msg[4]) |
rtgree01 | 0:9bc41d70bdd3 | 563 | { |
rtgree01 | 0:9bc41d70bdd3 | 564 | case 0x00: |
rtgree01 | 0:9bc41d70bdd3 | 565 | opMode = slave; |
rtgree01 | 0:9bc41d70bdd3 | 566 | break; |
rtgree01 | 0:9bc41d70bdd3 | 567 | |
rtgree01 | 0:9bc41d70bdd3 | 568 | case 0x01: |
rtgree01 | 0:9bc41d70bdd3 | 569 | status._volume = msg[5]; |
rtgree01 | 0:9bc41d70bdd3 | 570 | status._balance = msg[6]; |
rtgree01 | 0:9bc41d70bdd3 | 571 | status._fade = msg[7]; |
rtgree01 | 0:9bc41d70bdd3 | 572 | status._bass = msg[8]; |
rtgree01 | 0:9bc41d70bdd3 | 573 | status._mid = msg[9]; |
rtgree01 | 0:9bc41d70bdd3 | 574 | status._treble = msg[10]; |
rtgree01 | 0:9bc41d70bdd3 | 575 | |
rtgree01 | 0:9bc41d70bdd3 | 576 | // writeInitFile(); |
rtgree01 | 0:9bc41d70bdd3 | 577 | break; |
rtgree01 | 0:9bc41d70bdd3 | 578 | |
rtgree01 | 0:9bc41d70bdd3 | 579 | case 0x02: |
rtgree01 | 0:9bc41d70bdd3 | 580 | status._siriusChan = msg[5]; |
rtgree01 | 0:9bc41d70bdd3 | 581 | ChangeSiriusStation(msg[5], false); |
rtgree01 | 0:9bc41d70bdd3 | 582 | break; |
rtgree01 | 0:9bc41d70bdd3 | 583 | |
rtgree01 | 0:9bc41d70bdd3 | 584 | case 0x03: |
rtgree01 | 0:9bc41d70bdd3 | 585 | status._radioMode = (radioMode)msg[5]; |
rtgree01 | 0:9bc41d70bdd3 | 586 | |
rtgree01 | 0:9bc41d70bdd3 | 587 | switch (status._radioMode) |
rtgree01 | 0:9bc41d70bdd3 | 588 | { |
rtgree01 | 0:9bc41d70bdd3 | 589 | case AM: |
rtgree01 | 0:9bc41d70bdd3 | 590 | status._amPreset = msg[6]; |
rtgree01 | 0:9bc41d70bdd3 | 591 | status._amFreq = msg[7] + (msg[8] << 8); |
rtgree01 | 0:9bc41d70bdd3 | 592 | break; |
rtgree01 | 0:9bc41d70bdd3 | 593 | |
rtgree01 | 0:9bc41d70bdd3 | 594 | case FM: |
rtgree01 | 0:9bc41d70bdd3 | 595 | status._fmPreset = msg[6]; |
rtgree01 | 0:9bc41d70bdd3 | 596 | status._fmFreq = msg[7] + (msg[8] << 8); |
rtgree01 | 0:9bc41d70bdd3 | 597 | break; |
rtgree01 | 0:9bc41d70bdd3 | 598 | |
rtgree01 | 0:9bc41d70bdd3 | 599 | case SAT: |
rtgree01 | 0:9bc41d70bdd3 | 600 | status._siriusPreset = msg[6]; |
rtgree01 | 0:9bc41d70bdd3 | 601 | status._siriusChan = msg[7]; |
rtgree01 | 0:9bc41d70bdd3 | 602 | break; |
rtgree01 | 0:9bc41d70bdd3 | 603 | |
rtgree01 | 0:9bc41d70bdd3 | 604 | case CD: |
rtgree01 | 0:9bc41d70bdd3 | 605 | status._cdNum = msg[6]; |
rtgree01 | 0:9bc41d70bdd3 | 606 | status._cdTrackNum = msg[7]; |
rtgree01 | 0:9bc41d70bdd3 | 607 | status._cdHours = msg[8]; |
rtgree01 | 0:9bc41d70bdd3 | 608 | status._cdMinutes = msg[9]; |
rtgree01 | 0:9bc41d70bdd3 | 609 | status._cdSeconds = msg[10]; |
rtgree01 | 0:9bc41d70bdd3 | 610 | break; |
rtgree01 | 0:9bc41d70bdd3 | 611 | |
rtgree01 | 0:9bc41d70bdd3 | 612 | case VES: |
rtgree01 | 0:9bc41d70bdd3 | 613 | break; |
rtgree01 | 0:9bc41d70bdd3 | 614 | } |
rtgree01 | 0:9bc41d70bdd3 | 615 | break; |
rtgree01 | 0:9bc41d70bdd3 | 616 | |
rtgree01 | 0:9bc41d70bdd3 | 617 | } |
rtgree01 | 0:9bc41d70bdd3 | 618 | |
rtgree01 | 0:9bc41d70bdd3 | 619 | } |
rtgree01 | 0:9bc41d70bdd3 | 620 | } |
rtgree01 | 0:9bc41d70bdd3 | 621 | } |
rtgree01 | 0:9bc41d70bdd3 | 622 | |
rtgree01 | 0:9bc41d70bdd3 | 623 | |
rtgree01 | 0:9bc41d70bdd3 | 624 | void RadioEmulator::ClickTimeout(void) |
rtgree01 | 0:9bc41d70bdd3 | 625 | { |
rtgree01 | 0:9bc41d70bdd3 | 626 | for ( int i = 0; i < 6; i++) |
rtgree01 | 0:9bc41d70bdd3 | 627 | { |
rtgree01 | 0:9bc41d70bdd3 | 628 | buttonClickTimedOut = true; |
rtgree01 | 0:9bc41d70bdd3 | 629 | buttonTimeout.detach(); |
rtgree01 | 0:9bc41d70bdd3 | 630 | if ((prevSWC & (0x01 << i)) == (0x01 << i)) |
rtgree01 | 0:9bc41d70bdd3 | 631 | { |
rtgree01 | 0:9bc41d70bdd3 | 632 | buttonHeld[i] = true; |
rtgree01 | 0:9bc41d70bdd3 | 633 | } |
rtgree01 | 0:9bc41d70bdd3 | 634 | else |
rtgree01 | 0:9bc41d70bdd3 | 635 | { |
rtgree01 | 0:9bc41d70bdd3 | 636 | buttonHeld[i] = false; |
rtgree01 | 0:9bc41d70bdd3 | 637 | } |
rtgree01 | 0:9bc41d70bdd3 | 638 | } |
rtgree01 | 0:9bc41d70bdd3 | 639 | } |
rtgree01 | 0:9bc41d70bdd3 | 640 | |
rtgree01 | 0:9bc41d70bdd3 | 641 | void RadioEmulator::PowerUp(void) |
rtgree01 | 0:9bc41d70bdd3 | 642 | { |
rtgree01 | 0:9bc41d70bdd3 | 643 | led1 = 1; |
rtgree01 | 0:9bc41d70bdd3 | 644 | poweredDown = 0; |
rtgree01 | 0:9bc41d70bdd3 | 645 | needToParseCANMessage = false; |
rtgree01 | 0:9bc41d70bdd3 | 646 | LPC_CAN2->BTR = 0x52001C; |
rtgree01 | 0:9bc41d70bdd3 | 647 | *can_RS = 0; |
rtgree01 | 0:9bc41d70bdd3 | 648 | ReceivedCANMsg = false; |
rtgree01 | 0:9bc41d70bdd3 | 649 | |
rtgree01 | 0:9bc41d70bdd3 | 650 | canIRQ->rise(this, &RadioEmulator::StartEmulation); |
rtgree01 | 0:9bc41d70bdd3 | 651 | // StartEmulation(); |
rtgree01 | 0:9bc41d70bdd3 | 652 | } |
rtgree01 | 0:9bc41d70bdd3 | 653 | |
rtgree01 | 0:9bc41d70bdd3 | 654 | void RadioEmulator::StartEmulation(void) |
rtgree01 | 0:9bc41d70bdd3 | 655 | { |
rtgree01 | 0:9bc41d70bdd3 | 656 | canIRQ->rise(0); |
rtgree01 | 0:9bc41d70bdd3 | 657 | |
rtgree01 | 0:9bc41d70bdd3 | 658 | CANBusTicker.attach(this, &RadioEmulator::WriteCANMessages, 0.5); |
rtgree01 | 0:9bc41d70bdd3 | 659 | |
rtgree01 | 0:9bc41d70bdd3 | 660 | ChangeSiriusStation(status._siriusChan, true); |
rtgree01 | 0:9bc41d70bdd3 | 661 | |
rtgree01 | 0:9bc41d70bdd3 | 662 | CANTimeout.attach(this, &RadioEmulator::CheckCANTimeout, 1); |
rtgree01 | 0:9bc41d70bdd3 | 663 | } |
rtgree01 | 0:9bc41d70bdd3 | 664 | |
rtgree01 | 0:9bc41d70bdd3 | 665 | //------------------------------------------------------------------------------------------ |
rtgree01 | 0:9bc41d70bdd3 | 666 | // These are all interrupts |
rtgree01 | 0:9bc41d70bdd3 | 667 | void RadioEmulator::RestartCAN(void) |
rtgree01 | 0:9bc41d70bdd3 | 668 | { |
rtgree01 | 0:9bc41d70bdd3 | 669 | if (poweredDown == 5) |
rtgree01 | 0:9bc41d70bdd3 | 670 | { |
rtgree01 | 0:9bc41d70bdd3 | 671 | canIRQ->rise(NULL); |
rtgree01 | 0:9bc41d70bdd3 | 672 | PowerUp(); |
rtgree01 | 0:9bc41d70bdd3 | 673 | } |
rtgree01 | 0:9bc41d70bdd3 | 674 | poweredDown++; |
rtgree01 | 0:9bc41d70bdd3 | 675 | } |
rtgree01 | 0:9bc41d70bdd3 | 676 | |
rtgree01 | 0:9bc41d70bdd3 | 677 | void RadioEmulator::WriteCANMessages() |
rtgree01 | 0:9bc41d70bdd3 | 678 | { |
rtgree01 | 0:9bc41d70bdd3 | 679 | if (radioOn) |
rtgree01 | 0:9bc41d70bdd3 | 680 | { |
rtgree01 | 0:9bc41d70bdd3 | 681 | led2 = !led2; |
rtgree01 | 0:9bc41d70bdd3 | 682 | SendRadioModeMsg(); |
rtgree01 | 0:9bc41d70bdd3 | 683 | SendEVICMsg(); |
rtgree01 | 0:9bc41d70bdd3 | 684 | SendStereoSettingsMsg(); |
rtgree01 | 0:9bc41d70bdd3 | 685 | } |
rtgree01 | 0:9bc41d70bdd3 | 686 | } |
rtgree01 | 0:9bc41d70bdd3 | 687 | |
rtgree01 | 0:9bc41d70bdd3 | 688 | void RadioEmulator::CheckCANTimeout(void) |
rtgree01 | 0:9bc41d70bdd3 | 689 | { |
rtgree01 | 0:9bc41d70bdd3 | 690 | if (!ReceivedCANMsg) |
rtgree01 | 0:9bc41d70bdd3 | 691 | { |
rtgree01 | 0:9bc41d70bdd3 | 692 | led1 = 0; |
rtgree01 | 0:9bc41d70bdd3 | 693 | // Need to Power Down |
rtgree01 | 0:9bc41d70bdd3 | 694 | CANBusTicker.detach(); |
rtgree01 | 0:9bc41d70bdd3 | 695 | CANTimeout.detach(); |
rtgree01 | 0:9bc41d70bdd3 | 696 | |
rtgree01 | 0:9bc41d70bdd3 | 697 | *can_RS = 1; |
rtgree01 | 0:9bc41d70bdd3 | 698 | |
rtgree01 | 0:9bc41d70bdd3 | 699 | canIRQ->rise(this, &RadioEmulator::RestartCAN); |
rtgree01 | 0:9bc41d70bdd3 | 700 | } |
rtgree01 | 0:9bc41d70bdd3 | 701 | |
rtgree01 | 0:9bc41d70bdd3 | 702 | ReceivedCANMsg = false; |
rtgree01 | 0:9bc41d70bdd3 | 703 | } |
rtgree01 | 0:9bc41d70bdd3 | 704 | |
rtgree01 | 0:9bc41d70bdd3 | 705 | void RadioEmulator::CheckHostTimeout(void) |
rtgree01 | 0:9bc41d70bdd3 | 706 | { |
rtgree01 | 0:9bc41d70bdd3 | 707 | if (!ReceivedHostMsg) |
rtgree01 | 0:9bc41d70bdd3 | 708 | { |
rtgree01 | 0:9bc41d70bdd3 | 709 | led4 = 1; |
rtgree01 | 0:9bc41d70bdd3 | 710 | opMode = standalone; |
rtgree01 | 0:9bc41d70bdd3 | 711 | } |
rtgree01 | 0:9bc41d70bdd3 | 712 | else |
rtgree01 | 0:9bc41d70bdd3 | 713 | { |
rtgree01 | 0:9bc41d70bdd3 | 714 | led4 = 0; |
rtgree01 | 0:9bc41d70bdd3 | 715 | } |
rtgree01 | 0:9bc41d70bdd3 | 716 | |
rtgree01 | 0:9bc41d70bdd3 | 717 | ReceivedHostMsg = false; |
rtgree01 | 0:9bc41d70bdd3 | 718 | } |
rtgree01 | 0:9bc41d70bdd3 | 719 | |
rtgree01 | 0:9bc41d70bdd3 | 720 | void RadioEmulator::SendStatusToHost(void) |
rtgree01 | 0:9bc41d70bdd3 | 721 | { |
rtgree01 | 0:9bc41d70bdd3 | 722 | int size = sizeof(status); |
rtgree01 | 0:9bc41d70bdd3 | 723 | |
rtgree01 | 0:9bc41d70bdd3 | 724 | bool holding = false; |
rtgree01 | 0:9bc41d70bdd3 | 725 | if (buttonClickTimedOut == true) |
rtgree01 | 0:9bc41d70bdd3 | 726 | { |
rtgree01 | 0:9bc41d70bdd3 | 727 | // an action has occurred.... but are we still holding a button? |
rtgree01 | 0:9bc41d70bdd3 | 728 | status.currentSWCAction = 0; |
rtgree01 | 0:9bc41d70bdd3 | 729 | for (int i = 0; i < 6; i++) |
rtgree01 | 0:9bc41d70bdd3 | 730 | { |
rtgree01 | 0:9bc41d70bdd3 | 731 | status.currentSWCAction |= buttonClicks[i] << (2 * i); |
rtgree01 | 0:9bc41d70bdd3 | 732 | if (buttonHeld[i] != true) |
rtgree01 | 0:9bc41d70bdd3 | 733 | { |
rtgree01 | 0:9bc41d70bdd3 | 734 | buttonClicks[i] = 0; |
rtgree01 | 0:9bc41d70bdd3 | 735 | } |
rtgree01 | 0:9bc41d70bdd3 | 736 | else |
rtgree01 | 0:9bc41d70bdd3 | 737 | { |
rtgree01 | 0:9bc41d70bdd3 | 738 | holding = true; |
rtgree01 | 0:9bc41d70bdd3 | 739 | } |
rtgree01 | 0:9bc41d70bdd3 | 740 | } |
rtgree01 | 0:9bc41d70bdd3 | 741 | } |
rtgree01 | 0:9bc41d70bdd3 | 742 | else |
rtgree01 | 0:9bc41d70bdd3 | 743 | { |
rtgree01 | 0:9bc41d70bdd3 | 744 | status.currentSWCAction = 0; |
rtgree01 | 0:9bc41d70bdd3 | 745 | } |
rtgree01 | 0:9bc41d70bdd3 | 746 | |
rtgree01 | 0:9bc41d70bdd3 | 747 | static int okToSend = 0; |
rtgree01 | 0:9bc41d70bdd3 | 748 | if (holding) |
rtgree01 | 0:9bc41d70bdd3 | 749 | { |
rtgree01 | 0:9bc41d70bdd3 | 750 | if (okToSend < 2) |
rtgree01 | 0:9bc41d70bdd3 | 751 | { |
rtgree01 | 0:9bc41d70bdd3 | 752 | status.currentSWCAction = 0; |
rtgree01 | 0:9bc41d70bdd3 | 753 | okToSend++; |
rtgree01 | 0:9bc41d70bdd3 | 754 | } |
rtgree01 | 0:9bc41d70bdd3 | 755 | else |
rtgree01 | 0:9bc41d70bdd3 | 756 | { |
rtgree01 | 0:9bc41d70bdd3 | 757 | okToSend = 0; |
rtgree01 | 0:9bc41d70bdd3 | 758 | } |
rtgree01 | 0:9bc41d70bdd3 | 759 | } |
rtgree01 | 0:9bc41d70bdd3 | 760 | else |
rtgree01 | 0:9bc41d70bdd3 | 761 | { |
rtgree01 | 0:9bc41d70bdd3 | 762 | okToSend = 0; |
rtgree01 | 0:9bc41d70bdd3 | 763 | } |
rtgree01 | 0:9bc41d70bdd3 | 764 | |
rtgree01 | 0:9bc41d70bdd3 | 765 | status.SWCButtons = prevSWC; |
rtgree01 | 0:9bc41d70bdd3 | 766 | |
rtgree01 | 0:9bc41d70bdd3 | 767 | if (opMode == standalone) |
rtgree01 | 0:9bc41d70bdd3 | 768 | { |
rtgree01 | 0:9bc41d70bdd3 | 769 | if (status.currentSWCAction != 0) |
rtgree01 | 0:9bc41d70bdd3 | 770 | { |
rtgree01 | 0:9bc41d70bdd3 | 771 | if (buttonClicks[2] != 0) |
rtgree01 | 0:9bc41d70bdd3 | 772 | { |
rtgree01 | 0:9bc41d70bdd3 | 773 | if (status._volume > 0) |
rtgree01 | 0:9bc41d70bdd3 | 774 | status._volume --; |
rtgree01 | 0:9bc41d70bdd3 | 775 | } |
rtgree01 | 0:9bc41d70bdd3 | 776 | else if (buttonClicks[1] != 0) |
rtgree01 | 0:9bc41d70bdd3 | 777 | { |
rtgree01 | 0:9bc41d70bdd3 | 778 | if (status._volume < 40) |
rtgree01 | 0:9bc41d70bdd3 | 779 | status._volume ++; |
rtgree01 | 0:9bc41d70bdd3 | 780 | } |
rtgree01 | 0:9bc41d70bdd3 | 781 | else if (buttonClicks[4] != 0) |
rtgree01 | 0:9bc41d70bdd3 | 782 | { |
rtgree01 | 0:9bc41d70bdd3 | 783 | if (status._siriusChan > 0) |
rtgree01 | 0:9bc41d70bdd3 | 784 | ChangeSiriusStation(status._siriusChan-1, false); |
rtgree01 | 0:9bc41d70bdd3 | 785 | } |
rtgree01 | 0:9bc41d70bdd3 | 786 | else if (buttonClicks[3] != 0) |
rtgree01 | 0:9bc41d70bdd3 | 787 | { |
rtgree01 | 0:9bc41d70bdd3 | 788 | if (status._siriusChan < 256) |
rtgree01 | 0:9bc41d70bdd3 | 789 | ChangeSiriusStation(status._siriusChan+1, false); |
rtgree01 | 0:9bc41d70bdd3 | 790 | } |
rtgree01 | 0:9bc41d70bdd3 | 791 | |
rtgree01 | 0:9bc41d70bdd3 | 792 | // writeInitFile(); |
rtgree01 | 0:9bc41d70bdd3 | 793 | } |
rtgree01 | 0:9bc41d70bdd3 | 794 | } |
rtgree01 | 0:9bc41d70bdd3 | 795 | |
rtgree01 | 0:9bc41d70bdd3 | 796 | |
rtgree01 | 0:9bc41d70bdd3 | 797 | RadioState tempStatus; |
rtgree01 | 0:9bc41d70bdd3 | 798 | memcpy(&tempStatus, &status, size); |
rtgree01 | 0:9bc41d70bdd3 | 799 | char *data = (char *)&tempStatus; |
rtgree01 | 0:9bc41d70bdd3 | 800 | |
rtgree01 | 0:9bc41d70bdd3 | 801 | usb->putc(0x42); |
rtgree01 | 0:9bc41d70bdd3 | 802 | usb->putc(0x42); |
rtgree01 | 0:9bc41d70bdd3 | 803 | usb->putc(0x42); |
rtgree01 | 0:9bc41d70bdd3 | 804 | usb->putc(0x42); |
rtgree01 | 0:9bc41d70bdd3 | 805 | |
rtgree01 | 0:9bc41d70bdd3 | 806 | for (int i = 0; i < size; i++) |
rtgree01 | 0:9bc41d70bdd3 | 807 | { |
rtgree01 | 0:9bc41d70bdd3 | 808 | while(!usb->writeable()) |
rtgree01 | 0:9bc41d70bdd3 | 809 | { |
rtgree01 | 0:9bc41d70bdd3 | 810 | wait_us(10); |
rtgree01 | 0:9bc41d70bdd3 | 811 | } |
rtgree01 | 0:9bc41d70bdd3 | 812 | |
rtgree01 | 0:9bc41d70bdd3 | 813 | usb->putc(data[i]); |
rtgree01 | 0:9bc41d70bdd3 | 814 | } |
rtgree01 | 0:9bc41d70bdd3 | 815 | |
rtgree01 | 0:9bc41d70bdd3 | 816 | } |