Making a BMW E90 instrument cluster alive for demonstration purposes

Dependencies:   mbed

Revision:
1:a582aeb10c32
Parent:
0:830e074c92d5
Child:
2:ef9a8a114395
--- a/main.cpp	Tue Mar 07 23:39:16 2017 +0000
+++ b/main.cpp	Thu Mar 09 12:20:25 2017 +0000
@@ -3,6 +3,7 @@
 
 Ticker ticker;
 
+Serial pc(USBTX, USBRX);
 DigitalOut led1(PA_5);
 CAN can1(PB_8, PB_9);  // rd, td Transmitter
 T15Msg t15;
@@ -12,18 +13,22 @@
  0x1B4 (Kombi) OUT? Speed status, 100 ms
 */
 
+
 void sendT15() {
-    if (t15.sendMessage(&can1)) {
-        printf("T15 sent \n");
-    }
-    
-    led1 = !led1;
+    if (!t15.sendMessage(&can1)) {
+        pc.printf("Cannot send T15!\n");
+    }    
 /*
     char data_130 [] = { 0x45, 0x42, 0x8F, 0xE2, 0xFE };
     char data_130 [] = { 0x45, 0x40, 0x21, 0x8F, 0x00 };
 */
 }
 
+void tick100() {
+    sendT15();
+    led1 = !led1;
+}
+
 void sendLight(char mode) {
 
     /*
@@ -52,7 +57,7 @@
             break;
     }
     if (can1.write(CANMessage(0x21A, data, 3))) {
-        printf("Light sent \n");
+        pc.printf("Light sent %d\n", mode);
     }        
 }
 
@@ -118,12 +123,86 @@
     }    
     */
 
+bool sendMessage(int id, char *data, int len) {
+    
+    pc.printf("Sending message: %01X:%02X, ", id >> 8, id & 0xff);
+    for (int i = 0; i < len; i++) pc.printf("%02X ", data[i]);
+    pc.printf("\r\n");
+    
+    return can1.write(CANMessage(id, data, len));
+}
+
+
 //**********The main program*********************
+
+void doCommand(char *command) {
+
+    pc.printf("Command: %s\r\n", command);
+    for (int j = 0;;j++) {
+        pc.printf("%02x ", command[j]);
+        if (command[j] == 0) break;
+    }
+    pc.printf("\r\n");
+                
+    if (strcmp(command, "START") == 0) {
+        pc.printf("Start\r\n");
+        t15.start();
+    }
+    
+    else if (strcmp(command, "STOP") == 0) {
+        pc.printf("Stop\n");
+        t15.stop();
+    }
+    
+    else if (command[0] == 'L') {
+        int mode = command[1] - '0';
+        sendLight(mode);
+    }
+    
+    else if (command[0] == 'M') {
+        int i = 1, l = 0;
+        char hexmsg[20];
+        char canmsg[8];
+        int id;
+        
+        while (i < strlen(command)) {
+            char h = command[i++];
+            if (h < '0' || h > 'F') continue;
+            if (h >= 'A') hexmsg[l++] = 10 + (h - 'A');
+            else hexmsg[l++] = h - '0';
+        }
+        i = 0;
+        id = (int) (hexmsg[0] & 0x0f) << 8 + (int) (hexmsg[1] & 0x0f) << 4 + (hexmsg[2] & 0x0f);
+        while (i < (l-3) / 2) {
+            canmsg[i] = ((hexmsg[3 + i * 2] & 0x0f) << 4) | (hexmsg[3 + i * 2 + 1] & 0x0f);
+            i++;
+        }
+        sendMessage(id, canmsg, (l-3) / 2);
+    }
+}
+
 int main() {
 
+    pc.baud(115200);
     can1.frequency(100000);
-    ticker.attach(&sendT15, 0.1); //Send every 100 msec
+    ticker.attach(&tick100, 0.1); //Send messages every 100 msec
+    pc.printf("T15 started\r\n");
+    
+    char buffer[128];
+    int bufferlen = 0;
     
-    wait_us(1000);
-            
+    while (1) {
+        if (pc.readable()) {
+            char c = pc.getc();
+            pc.putc(c); // Local ECHO
+            if (c == '\r') {
+                buffer[bufferlen] = 0;
+                doCommand(buffer);
+                bufferlen = 0;
+            } else {
+                buffer[bufferlen++] = c;
+            }
+        }
+    }
+
 }