Petter Bellander / Mbed 2 deprecated Saab-BT

Dependencies:   mbed

Revision:
11:74844f6ca8cf
Parent:
10:8be92db98bf4
Child:
12:4194c47ca60b
--- a/CDC.cpp	Fri Jan 15 22:10:11 2016 +0000
+++ b/CDC.cpp	Thu Jan 28 21:22:08 2016 +0000
@@ -41,23 +41,18 @@
 /** 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[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_request_cmd[] = {CDC_APL_ADR,0x02,0x05,CDC_SID_FUNCTION_ID,0x00,0x00,0x00,0x00,-1};
 //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[2][3];
+CANMessage CAN_DispMsg[3];
 
 /** I/O: **/
 DigitalOut led1(LED1, 0);
@@ -65,6 +60,7 @@
 
 /** Timers: **/
 Timer playback;
+Timer sleep_timer;
 
 /******************************************************************************
  * PUBLIC METHODS
@@ -74,7 +70,6 @@
 void CDC::init() {
     can.frequency(47619);
     printf("CAN Frequency set\r\n");
-    CAN_wrFilter(1, LOCK_STATUS);
     CAN_wrFilter(1, IHU_BUTTONS);
     CAN_wrFilter(1, DISPLAY_RESOURCE_GRANT);
     CAN_wrFilter(1, NODE_STATUS_RX);
@@ -83,61 +78,38 @@
     printf("CAN Filters set\r\n");
     
     CAN_TxMsg.len = 8;
-    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);
+    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] = 0x01; // Writing to row 1
+    CAN_DispMsg[1].data[2] = 0x01; // Writing to row 1
+    CAN_DispMsg[2].data[2] = 0x01; // Writing to row 1
+    display("");
+    
+    sleep_timer.start();
 }
 
 /** Handles an incoming (RX) frame **/
-IBUS_COMMAND CDC::get_cmd() {  
+IBUS_COMMAND CDC::get_cmd() {
      if(can.read(CAN_RxMsg)) {
         led2 = !led2;
         CAN_TxMsg.data[0]++;
         switch (CAN_RxMsg.id) {
-            case LOCK_STATUS:
-            {
-                int locked = (CAN_RxMsg.data[1] >> 7) & 1;
-                if(locked){
-                    return IBUS_DOORS_LOCKED;
-                }
-                else{
-                    return IBUS_DOORS_UNLOCKED;
-                }
-            }
             case DISPLAY_RESOURCE_REQ:
-                send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd[active_row_SID]);
+                sleep_timer.reset();
+                send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd);
                 update_elapsed_time();
                 send_can_frame(GENERAL_STATUS_CDC, cdc_status_cmd);
-                led1 = !led1;
-                break;
+                return IBUS_HEAD_UNIT_ON;
             case NODE_STATUS_RX:
                 send_can_frame(NODE_STATUS_TX, ninefive_cmd);
                 break;
@@ -148,18 +120,19 @@
             case DISPLAY_RESOURCE_GRANT:
                 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[CAN_RxMsg.data[0]][2] = 0x04;
-                    display_update(CAN_RxMsg.data[0]);
+                    display_request_cmd[2] = 0x04;
+                    display_update();
                 }
                 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
+                    display_request_cmd[2] = 0x05; //message is considered new
                 }
                 break;
         }
         return IBUS_OTHER_MESSAGE;
+    }    
+    if(sleep_timer.read() > 5) {
+        return IBUS_HEAD_UNIT_OFF;  
     }
     return IBUS_NO_MESSAGE;
 }
@@ -225,21 +198,13 @@
 
 
 /** 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(char text[], char row) {
+void CDC::display(char text[]) {
     if (!text) {
         return;
     }
     
     //Notify the SID that we want to display a message
-    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]);
+    send_can_frame(DISPLAY_RESOURCE_REQ, display_request_cmd);
     
     // Copy the provided string and make sure we have a new array of the correct length:
     char display_text[15];
@@ -255,53 +220,32 @@
         display_text[i] = 0;
     }
     
-    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[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][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[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][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];
+    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];
 }
 
 /** 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(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;
-    }
-     
+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
 }
 
 /** Sets the elapsed time in the cdc_status message **/