Simulated the CD changer of a Saab to implement a bluetooth connection to the car stereo. Control of playback device (phone) with steering wheel buttons. Needs a RN52 bluetooth transciever and a CAN transiever. So far only audio playback and control via steering wheel buttons implemented. Hands free calling planned.

Dependencies:   mbed

Revision:
11:74844f6ca8cf
Parent:
9:9a4c81493a3d
Child:
12:4194c47ca60b
--- a/main.cpp	Fri Jan 15 22:10:11 2016 +0000
+++ b/main.cpp	Thu Jan 28 21:22:08 2016 +0000
@@ -3,34 +3,63 @@
 // Code: https://github.com/kveilands/SAAB-CDC/tree/master/SAAB-CDC
 // I-Bus information from http://pikkupossu.1g.fi/tomi/projects/i-bus/i-bus.html
 
+// Due to power saving functions special firmware required http://mbed.org/users/simon/notebook/interface-powerdown/
+
 #include "mbed.h"
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
+
 #include "CDC.h"
 #include "RN52.h"
 
+
 CDC cdc;
 RN52 rn52;
 RN52_RESULT res;
+Ticker wakeup;
 
 Serial pc(USBTX, USBRX); // tx, rx
+bool active = 1;
 
 void handle_pc_input();
 void handle_cdc_input();
 void handle_bt_input();
+void check_for_wakeup();
 
 int main() {
-    
     printf("Petters BT CDC-emulator\r\n");
     printf("Initializing\r\n");
+    printf("Disabling unused peripherals to decrease power consumption\r\n");
+    PHY_PowerDown(); // Power down Ethernet interface - saves around 175mW
+    semihost_powerdown(); // If you don't need the PC host USB interface power down magic USB interface chip - saves around 150mW | Needs new firmware (URL below) and USB cable not connected http://mbed.org/users/simon/notebook/interface-powerdown/
     cdc.init();
     rn52.init();
+    printf("Adding wakeup routine\r\n");
+    wakeup.attach(&check_for_wakeup, 1);
     printf("Starting loop\r\n");
     while(1) {
-        handle_pc_input();
-        handle_cdc_input();
-        handle_bt_input();
+        if(active) {
+            handle_pc_input();
+            handle_cdc_input();
+            handle_bt_input();
+        }
+        else {
+            sleep();
+        }
     }
 }
 
+void check_for_wakeup(){
+    if(!active) {
+        IBUS_COMMAND cmd = IBUS_OTHER_MESSAGE;
+        while(cmd != IBUS_NO_MESSAGE) {
+            cmd = cdc.get_cmd();
+            if(cmd == IBUS_HEAD_UNIT_ON) {
+                active = 1;
+            }
+        }
+    }
+}
 
 void handle_pc_input() {
     //Debug connection to PC
@@ -58,17 +87,19 @@
                 rn52.get_caller_id(&res);
                 printf("%s\r\n", res.response);
                 break;
+            case 'a':
+                printf("deactivating\r\n");
+                active = 0;
+                break;
         }
     }
 }
 
 void handle_cdc_input() {
     switch (cdc.get_cmd()) {
-        case IBUS_DOORS_LOCKED:
-            //Goto sleep
-            break;
-        case IBUS_DOORS_UNLOCKED:
-            //wake up
+        case IBUS_HEAD_UNIT_OFF:
+            cdc.display("Shutdown");
+//            active = 0;
             break;
         case IBUS_NEXT:
             rn52.toggle_play();
@@ -86,7 +117,6 @@
             rn52.prev_track();
             break;
         case IBUS_SET:
-            rn52.connect();
             break;
         case IBUS_CLEAR:
             break;
@@ -101,7 +131,6 @@
                 break;
             case RN52_TRACK_CHANGE_EVENT:
                 cdc.reset_elapsed_time();
-                /*
                 printf("Track change event\r\n");
                 printf("title:  %s\r\n", res.title);
                 printf("artist: %s\r\n", res.artist);
@@ -110,24 +139,23 @@
                 printf("duration:  %i\r\n", res.duration);
                 printf("track_count:  %i\r\n", res.track_count);
                 printf("track_number:  %i\r\n", res.track_number);
-                */
-                cdc.display(res.title, 2);
+                cdc.display(res.title);
                 cdc.set_track((char)res.track_number);
                 break;
             case RN52_OTHER_EVENT: //no specific event occured, check connection status
                 switch(res.connection) {
                     case RN52_CONNECTABLE_DISCOVERABLE:
                         //printf("Waiting for connection\r\n");
-                        cdc.display("BT REDO", 2);
+                        cdc.display("BT REDO");
                         break;
                     case RN52_CONNECTED:
                         //printf("Connected\r\n");
-                        cdc.display("BT ANSLUTEN", 2);
+                        cdc.display("BT ANSLUTEN");
                         rn52.maxvolume();
                         break;
                     case RN52_AUDIO_STREAMING:
                         //printf("Streaming\r\n");
-                        cdc.display(res.title, 2);
+                        cdc.display(res.title);
                         cdc.start_elapsed_time();
                         break;
                 }