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

main.cpp

Committer:
petter
Date:
2016-01-29
Revision:
12:4194c47ca60b
Parent:
11:74844f6ca8cf
Child:
13:968af0520530

File content as of revision 12:4194c47ca60b:

// Inspired from BlueSaab
// Project/Hardware: http://bluesaab.blogspot.com/
// 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) {
        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
    if(pc.readable()) {
        switch (pc.getc()) {
            case 'p':
                if(rn52.toggle_play()) {
                    printf("play/pause\r\n");
                }
                break;
            case 'v':
                printf("volume up\r\n");
                rn52.maxvolume();
                break;             
            case 'n':
                printf("next track\r\n");
                rn52.next_track();
                break;  
            case 'q':
                printf("getting status\r\n");
                rn52.get_status(&res);
                printf("%s\r\n", res.response);
                break;
            case 't':
                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_HEAD_UNIT_OFF:
            cdc.display("Shutdown");
//            active = 0;
            break;
        case IBUS_NEXT:
            rn52.toggle_play();
            break;
        case IBUS_CDC_ON:
            rn52.connect();
            break;
        case IBUS_CDC_OFF:
            rn52.disconnect();
            break;
        case IBUS_SKIP_FW:
            rn52.next_track();
            break;
        case IBUS_SKIP_BW:
            rn52.prev_track();
            break;
        case IBUS_SET:
            break;
        case IBUS_CLEAR:
            break;
    }    
}

void handle_bt_input() { 
    if(rn52.check_event(&res)) {
        switch (res.event) {
            case RN52_CALLER_ID_EVENT:
                //printf("Caller ID event\r\n");
                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);
                printf("album:  %s\r\n", res.album);
                printf("genre:  %s\r\n", res.genre);
                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);
                cdc.display("BT SPELAR");
                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");
                        break;
                    case RN52_CONNECTED:
                        //printf("Connected\r\n");
                        cdc.display("BT ANSLUTEN");
                        rn52.maxvolume();
                        break;
                    case RN52_AUDIO_STREAMING:
                        //printf("Streaming\r\n");
                        cdc.display(res.title);
                        cdc.start_elapsed_time();
                        break;
                }         
                if(res.connection != RN52_AUDIO_STREAMING) {
                    cdc.stop_elapsed_time();
                }            
        }
    }
}