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-14
Revision:
7:2df2c6e8c0df
Parent:
6:c454f88524d6
Child:
8:beb6c399490a

File content as of revision 7:2df2c6e8c0df:

// 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

#include "mbed.h"
#include "CDC.h"
#include "RN52.h"

CDC cdc;
RN52 rn52;
RN52_RESULT res;

Serial pc(USBTX, USBRX); // tx, rx

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(RN52_GETSTATUS, &res);
                printf("%s\r\n", res.response);
                break;
            case 't':
                rn52.get(RN52_CALLER_ID, &res);
                printf("%s\r\n", res.response);
                break;
        }
    }
}

void handle_cdc_input() {
    switch (cdc.get_cmd()) {
        case IBUS_OTHER_MESSAGE:
            break;
        case IBUS_DOORS_LOCKED:
            //Goto sleep
            break;
        case IBUS_DOORS_UNLOCKED:
            //wake up
            break;
        case IBUS_NEXT:
            rn52.toggle_play();
            break;
        case IBUS_CDC_ON:
            rn52.connect();
            break;
        case IBUS_CDC_OFF:
            rn52.disconnect();
            break;
        case IBUS_VOLUME_UP:
            break;
        case IBUS_VOLUME_DOWN:
            break;
        case IBUS_SKIP_FW:
            rn52.next_track();
            break;
        case IBUS_SKIP_BW:
            rn52.prev_track();
            break;
        case IBUS_SET:
            rn52.connect();
            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.set_track((char)res.track_number);
                break;
            case RN52_NO_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("BT SPELAR");
                        cdc.start_elapsed_time();
                        break;
                }         
                if(res.connection != RN52_AUDIO_STREAMING) {
                    cdc.stop_elapsed_time();
                }            
        }
    }
}

int main() {
    printf("Petters BT CDC-emulator\r\n");
    printf("Initializing\r\n");
    cdc.init();
    rn52.init();
    printf("Starting loop\r\n");
    while(1) {
        handle_pc_input();
        handle_cdc_input();
        handle_bt_input();
    }
}