Petter Bellander / Mbed 2 deprecated Saab-BT

Dependencies:   mbed

Revision:
10:8be92db98bf4
Parent:
9:9a4c81493a3d
Child:
11:74844f6ca8cf
--- a/CDC.cpp	Fri Jan 15 21:27:42 2016 +0000
+++ b/CDC.cpp	Fri Jan 15 22:10:11 2016 +0000
@@ -41,18 +41,23 @@
 /** Variables: **/
 bool cdc_active = false; // True while our module, the simulated CDC, is active.
 bool mute = false;
+char active_row_SID = 2;
 int toggle_shuffle = 1;
 int ninefive_cmd[] = {0x32,0x00,0x00,0x16,0x01,0x02,0x00,0x00,-1};
 int beep_cmd[] = {0x80,0x04,0x00,0x00,0x00,0x00,0x00,0x00,-1};
 int cdc_status_cmd[] = {0xE0,0x00,0x01,0x41,0x01,0x00,0x00,0xD0,-1};
-int display_request_cmd[] = {CDC_APL_ADR,0x02,0x05,CDC_SID_FUNCTION_ID,0x00,0x00,0x00,0x00,-1};
+int display_request_cmd[3][9] = {
+    {CDC_APL_ADR,0x00,0x05,CDC_SID_FUNCTION_ID,0x00,0x00,0x00,0x00,-1}, //whole display
+    {CDC_APL_ADR,0x01,0x05,CDC_SID_FUNCTION_ID,0x00,0x00,0x00,0x00,-1}, //only row 1
+    {CDC_APL_ADR,0x02,0x05,CDC_SID_FUNCTION_ID,0x00,0x00,0x00,0x00,-1}, //only row 2
+};
 //int display_clear_cmd[] = {CDC_APL_ADR,0x02,0xFF,CDC_SID_FUNCTION_ID,0x00,0x00,0x00,0x00,-1};
 
 /** Com: **/
 CAN can(p9, p10);
 CANMessage CAN_TxMsg;
 CANMessage CAN_RxMsg;
-CANMessage CAN_DispMsg[3];
+CANMessage CAN_DispMsg[2][3];
 
 /** I/O: **/
 DigitalOut led1(LED1, 0);
@@ -78,21 +83,36 @@
     printf("CAN Filters set\r\n");
     
     CAN_TxMsg.len = 8;
-    CAN_DispMsg[0].len = 8;
-    CAN_DispMsg[1].len = 8;
-    CAN_DispMsg[2].len = 8;
-    CAN_DispMsg[0].id = WRITE_TEXT_ON_DISPLAY;
-    CAN_DispMsg[1].id = WRITE_TEXT_ON_DISPLAY;
-    CAN_DispMsg[2].id = WRITE_TEXT_ON_DISPLAY;
-    CAN_DispMsg[0].data[0] = 0x42; // order, new
-    CAN_DispMsg[1].data[0] = 0x01; // order
-    CAN_DispMsg[2].data[0] = 0x00; // order
-    CAN_DispMsg[0].data[1] = 0x96; // Address of the SID
-    CAN_DispMsg[1].data[1] = 0x96; // Address of the SID
-    CAN_DispMsg[2].data[1] = 0x96; // Address of the SID
-    CAN_DispMsg[0].data[2] = 0x02; // Writing to row 2
-    CAN_DispMsg[1].data[2] = 0x02; // Writing to row 2
-    CAN_DispMsg[2].data[2] = 0x02; // Writing to row 2
+    CAN_DispMsg[0][0].len = 8;
+    CAN_DispMsg[0][1].len = 8;
+    CAN_DispMsg[0][2].len = 8;
+    CAN_DispMsg[1][0].len = 8;
+    CAN_DispMsg[1][1].len = 8;
+    CAN_DispMsg[1][2].len = 8;
+    CAN_DispMsg[0][0].id = WRITE_TEXT_ON_DISPLAY;
+    CAN_DispMsg[0][1].id = WRITE_TEXT_ON_DISPLAY;
+    CAN_DispMsg[0][2].id = WRITE_TEXT_ON_DISPLAY;
+    CAN_DispMsg[1][0].id = WRITE_TEXT_ON_DISPLAY;
+    CAN_DispMsg[1][1].id = WRITE_TEXT_ON_DISPLAY;
+    CAN_DispMsg[1][2].id = WRITE_TEXT_ON_DISPLAY;
+    CAN_DispMsg[0][0].data[0] = 0x42; // order, new
+    CAN_DispMsg[1][0].data[0] = 0x42; // order, new
+    CAN_DispMsg[0][1].data[0] = 0x01; // order
+    CAN_DispMsg[1][1].data[0] = 0x01; // order
+    CAN_DispMsg[0][2].data[0] = 0x00; // order
+    CAN_DispMsg[1][2].data[0] = 0x00; // order
+    CAN_DispMsg[0][0].data[1] = 0x96; // Address of the SID
+    CAN_DispMsg[1][0].data[1] = 0x96; // Address of the SID
+    CAN_DispMsg[0][1].data[1] = 0x96; // Address of the SID
+    CAN_DispMsg[1][1].data[1] = 0x96; // Address of the SID
+    CAN_DispMsg[0][2].data[1] = 0x96; // Address of the SID
+    CAN_DispMsg[1][2].data[1] = 0x96; // Address of the SID
+    CAN_DispMsg[0][0].data[2] = 0x01; // Writing to row 1
+    CAN_DispMsg[1][0].data[2] = 0x02; // Writing to row 2
+    CAN_DispMsg[0][1].data[2] = 0x01; // Writing to row 1
+    CAN_DispMsg[1][1].data[2] = 0x02; // Writing to row 2
+    CAN_DispMsg[0][2].data[2] = 0x01; // Writing to row 1
+    CAN_DispMsg[1][2].data[2] = 0x02; // Writing to row 2
     display("PETTERS BT", 2);
 }
 
@@ -113,7 +133,7 @@
                 }
             }
             case DISPLAY_RESOURCE_REQ:
-                send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd);
+                send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd[active_row_SID]);
                 update_elapsed_time();
                 send_can_frame(GENERAL_STATUS_CDC, cdc_status_cmd);
                 led1 = !led1;
@@ -126,18 +146,16 @@
             case STEERING_WHEEL_BUTTONS:
                 return get_steering_wheel_cmd();
             case DISPLAY_RESOURCE_GRANT:
-                if ((CAN_RxMsg.data[0] == 0x02) && (CAN_RxMsg.data[1] == CDC_SID_FUNCTION_ID)) {
+                if (CAN_RxMsg.data[1] == CDC_SID_FUNCTION_ID) {
                     //We have been granted the right to write text to the second row in the SID
-                    display_request_cmd[2] = 0x04;
-                    display_update();
+                    display_request_cmd[CAN_RxMsg.data[0]][2] = 0x04;
+                    display_update(CAN_RxMsg.data[0]);
                 }
-                else if (CAN_RxMsg.data[0] == 0x02) {
-                    //Someone else has been granted the second row, we need to back down
-                    display_request_cmd[2] = 0x05;
-                }
-                else if (CAN_RxMsg.data[0] == 0x00) {
-                    //Someone else has been granted the entire display, we need to back down
-                    display_request_cmd[2] = 0x05;
+                else {
+                    //Someone else has been granted the display, we need to back down
+                    display_request_cmd[0][2] = 0x05; //message is considered new
+                    display_request_cmd[1][2] = 0x05; //message is considered new
+                    display_request_cmd[2][2] = 0x05; //message is considered new
                 }
                 break;
         }
@@ -213,7 +231,15 @@
     }
     
     //Notify the SID that we want to display a message
-    send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd);
+    switch (row) {
+        case 1:
+            active_row_SID = 0; //both row 1 & 2 
+            break;
+        case 2:
+            active_row_SID = 2;
+            break;    
+    }
+    send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd[active_row_SID]);
     
     // Copy the provided string and make sure we have a new array of the correct length:
     char display_text[15];
@@ -229,32 +255,53 @@
         display_text[i] = 0;
     }
     
-    CAN_DispMsg[0].data[2] = 0x82; // Sent on basetime, writing to row 2, message changed
-    CAN_DispMsg[0].data[3] = display_text[0];
-    CAN_DispMsg[0].data[4] = display_text[1];
-    CAN_DispMsg[0].data[5] = display_text[2];
-    CAN_DispMsg[0].data[6] = display_text[3];
-    CAN_DispMsg[0].data[7] = display_text[4];
+    CAN_DispMsg[row-1][0].data[2] = 0x82; // Sent on basetime, writing to row 2, message changed
+    CAN_DispMsg[row-1][0].data[3] = display_text[0];
+    CAN_DispMsg[row-1][0].data[4] = display_text[1];
+    CAN_DispMsg[row-1][0].data[5] = display_text[2];
+    CAN_DispMsg[row-1][0].data[6] = display_text[3];
+    CAN_DispMsg[row-1][0].data[7] = display_text[4];
     
-    CAN_DispMsg[1].data[3] = display_text[5];
-    CAN_DispMsg[1].data[4] = display_text[6];
-    CAN_DispMsg[1].data[5] = display_text[7];
-    CAN_DispMsg[1].data[6] = display_text[8];
-    CAN_DispMsg[1].data[7] = display_text[9];
+    CAN_DispMsg[row-1][1].data[3] = display_text[5];
+    CAN_DispMsg[row-1][1].data[4] = display_text[6];
+    CAN_DispMsg[row-1][1].data[5] = display_text[7];
+    CAN_DispMsg[row-1][1].data[6] = display_text[8];
+    CAN_DispMsg[row-1][1].data[7] = display_text[9];
     
-    CAN_DispMsg[2].data[3] = display_text[10];
-    CAN_DispMsg[2].data[4] = display_text[11];
-    CAN_DispMsg[2].data[5] = display_text[12];
-    CAN_DispMsg[2].data[6] = display_text[13];
-    CAN_DispMsg[2].data[7] = display_text[14];
+    CAN_DispMsg[row-1][2].data[3] = display_text[10];
+    CAN_DispMsg[row-1][2].data[4] = display_text[11];
+    CAN_DispMsg[row-1][2].data[5] = display_text[12];
+    CAN_DispMsg[row-1][2].data[6] = display_text[13];
+    CAN_DispMsg[row-1][2].data[7] = display_text[14];
 }
 
 /** Writes the provided text on the SID. NOTE the character set used by the SID is slightly nonstandard. "Normal" characters should work fine. **/
-void CDC::display_update() {
-    can.write(CAN_DispMsg[0]);
-    can.write(CAN_DispMsg[1]);
-    can.write(CAN_DispMsg[2]);
-    CAN_DispMsg[0].data[2] = 0x02; // Message not changed next transmission unless display() is called 
+void CDC::display_update(char row) {
+    switch(row) {
+        case 0: //send both rows
+            can.write(CAN_DispMsg[0][0]);
+            can.write(CAN_DispMsg[0][1]);
+            can.write(CAN_DispMsg[0][2]);
+            can.write(CAN_DispMsg[1][0]);
+            can.write(CAN_DispMsg[1][1]);
+            can.write(CAN_DispMsg[1][2]);
+            CAN_DispMsg[0][0].data[2] = 0x02; // Message not changed next transmission unless display() is called
+            CAN_DispMsg[1][0].data[2] = 0x02; // Message not changed next transmission unless display() is called
+            break;
+        case 1: //send row 1
+            can.write(CAN_DispMsg[0][0]);
+            can.write(CAN_DispMsg[0][1]);
+            can.write(CAN_DispMsg[0][2]);
+            CAN_DispMsg[0][0].data[2] = 0x02; // Message not changed next transmission unless display() is called
+            break;
+        case 2: //send row 2
+            can.write(CAN_DispMsg[1][0]);
+            can.write(CAN_DispMsg[1][1]);
+            can.write(CAN_DispMsg[1][2]);
+            CAN_DispMsg[1][0].data[2] = 0x02; // Message not changed next transmission unless display() is called
+            break;
+    }
+     
 }
 
 /** Sets the elapsed time in the cdc_status message **/