From 3752709d6dabd3901224ac782eb550ad75d29411 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 21 Jun 2013 20:47:31 -0600 Subject: [PATCH] Updated code changes and improvments --- code/src/AVC.c | 131 +++++++++++++---- code/src/AVC.h | 4 - code/src/USART.h | 4 +- code/src/bluetooth.c | 339 ++++++++++++++++++++++++++++++++----------- code/src/bluetooth.h | 25 +++- code/src/main.c | 11 +- 6 files changed, 388 insertions(+), 126 deletions(-) diff --git a/code/src/AVC.c b/code/src/AVC.c index 19fc82e..f11debd 100644 --- a/code/src/AVC.c +++ b/code/src/AVC.c @@ -63,16 +63,29 @@ static MESSAGE* message_rx_tail;//insert here static uint8_t usart_quiet = 0; -static uint8_t IS_PLAYING = 0; static uint8_t SECONDS = 0; static uint8_t MINUTES = 0; +static uint8_t buttonSwitch = 0; + +void status_push(void) +{ + MESSAGE* msg = NULL; + while( (msg = AVC_add_tx_transaction_from_list(SELF_PLAYING_STATUS)) == NULL){ + sei(); + } + msg->data[8] = AVC_Toyota_digits(SECONDS); + msg->data[7] = AVC_Toyota_digits(MINUTES); + msg->data[5] = bluetooth_index() + 1; + sei(); + +} + void status_update(void){ - if(IS_PLAYING){ - MESSAGE* msg = AVC_add_tx_transaction_from_list(SELF_PLAYING_STATUS); - msg->data[8] = AVC_Toyota_digits(SECONDS++); - msg->data[7] = AVC_Toyota_digits(MINUTES); - sei(); + + status_push(); + if(bluetooth_is_playing()){ + SECONDS++; if(SECONDS == 60){ SECONDS = 0; MINUTES++; @@ -244,6 +257,15 @@ void handle_message(MSG_ID id){ sei();//release once we have altered what we need to. } break; + case LAN_REGISTER: + { + MESSAGE* message; + while( (message = AVC_add_tx_transaction_from_list(SELF_REGISTER)) == NULL){ + sei();//release while we don't have something. + } + sei();//release once we have altered what we need to. + } + break; case CONSOLE_DISK_BUTTON_1: { MESSAGE* message; @@ -268,20 +290,21 @@ void handle_message(MSG_ID id){ break; case CONSOLE_DISK_PLAY: { - if(!IS_PLAYING){ - MESSAGE* message; - while( (message = AVC_add_tx_transaction_from_list(RESP_CONSOLE_DISK_PLAY_0)) == NULL){ - sei(); - } - sei(); - while( (message = AVC_add_tx_transaction_from_list(RESP_CONSOLE_DISK_PLAY_1)) == NULL){ - sei(); - } + /* + MESSAGE* message; + while( (message = AVC_add_tx_transaction_from_list(RESP_CONSOLE_DISK_PLAY_0)) == NULL){ sei(); - - SECONDS = 0; - MINUTES = 0; - IS_PLAYING = 1; + } + sei(); + */ + if(!bluetooth_connected()) + { + bluetooth_enableSearch(); + status_push(); + } + if(!bluetooth_is_playing() && bluetooth_connected()) + { + status_push(); bluetooth_play_song(); } @@ -289,7 +312,6 @@ void handle_message(MSG_ID id){ break; case CONSOLE_DISK_STOP: { - if(IS_PLAYING){ MESSAGE* message; while( (message = AVC_add_tx_transaction_from_list(RESP_CONSOLE_DISK_STOP_0)) == NULL){ sei(); @@ -299,25 +321,84 @@ void handle_message(MSG_ID id){ sei(); } sei(); - IS_PLAYING = 0; + bluetooth_pause_song(); - } - } break; case CONSOLE_SEEK_UP: { - if(IS_PLAYING){ + if(bluetooth_is_playing()){ bluetooth_next_song(); } + else + { + bluetooth_play_song(); + } } break; case CONSOLE_SEEK_DOWN: { - if(IS_PLAYING){ + if(bluetooth_is_playing()){ bluetooth_previous_song(); } } + break; + case CONSOLE_BTN_1: + buttonSwitch = CONSOLE_BTN_1; + SECONDS = 0; + MINUTES = 0; + bluetooth_connectIndex(0); + break; + case CONSOLE_BTN_2: + if(buttonSwitch == CONSOLE_BTN_1) + { + buttonSwitch = CONSOLE_BTN_2; + } + SECONDS = 0; + MINUTES = 0; + bluetooth_connectIndex(1); + break; + case CONSOLE_BTN_3: + if(buttonSwitch == CONSOLE_BTN_2) + { + buttonSwitch = CONSOLE_BTN_3; + } + SECONDS = 0; + MINUTES = 0; + bluetooth_connectIndex(2); + break; + case CONSOLE_BTN_4: + if(buttonSwitch == CONSOLE_BTN_3) + { + buttonSwitch = CONSOLE_BTN_4; + } + SECONDS = 0; + MINUTES = 0; + bluetooth_connectIndex(3); + break; + case CONSOLE_BTN_5: + if(buttonSwitch == CONSOLE_BTN_4) + { + buttonSwitch = CONSOLE_BTN_5; + } + SECONDS = 0; + MINUTES = 0; + + bluetooth_disableSearch(); + bluetooth_disconnectAll(); + + break; + case CONSOLE_BTN_6: + if(buttonSwitch == CONSOLE_BTN_5) + { + buttonSwitch = 0; + reset_pairings(); + } + SECONDS = 0; + MINUTES = 0; + bluetooth_disconnectAll(); + bluetooth_enableSearch(); + break; default: break; } diff --git a/code/src/AVC.h b/code/src/AVC.h index e9506ef..3d28958 100644 --- a/code/src/AVC.h +++ b/code/src/AVC.h @@ -82,10 +82,6 @@ typedef struct message{ } MESSAGE; - - - - void AVC_init(uint8_t quiet); MESSAGE* AVC_add_tx_transaction(uint16_t address_arg, uint8_t broadcast_arg, uint8_t control_arg, uint8_t data_size_arg, uint8_t * data_arg, uint8_t print); MESSAGE* AVC_add_tx_transaction_from_list(MSG_ID id); diff --git a/code/src/USART.h b/code/src/USART.h index 76f4823..b14a13f 100644 --- a/code/src/USART.h +++ b/code/src/USART.h @@ -24,8 +24,8 @@ #define USART_CONTROL UCSR0B #define USART_DATA UDR0 - #define MAX_TX_BUFFER 355 - #define MAX_RX_BUFFER 50 + #define MAX_TX_BUFFER 170 + #define MAX_RX_BUFFER 220 enum UART_ERROR_CODES {UART_GOOD, UART_BUFFER_FULL, UART_BUFFER_EMPTY, USART_COMP_SHORT, USART_COMP_LONG}; diff --git a/code/src/bluetooth.c b/code/src/bluetooth.c index c096ae9..b569c72 100644 --- a/code/src/bluetooth.c +++ b/code/src/bluetooth.c @@ -26,121 +26,292 @@ #include "bluetooth.h" #include "USART.h" -static MATCH_STATES match_state = NO_MATCH; -static uint8_t match_place = 0; -static char connected_mac_address[MAC_ADDRESS_LENGTH]; +#define LINE_BUFFER 80 +static void bluetooth_connect(void); static void bluetooth_rec_callback(void); +static uint8_t connected = 0; +static uint8_t is_playing = 0; +static uint8_t pair_search = 0; +static uint8_t pair_count = 0; +static uint8_t pair_index = 0; +static uint8_t tmp_pair_index = 0; +static char line[LINE_BUFFER]; +static uint8_t line_index = 0; +static uint8_t callnumber = '0'; +uint8_t bluetooth_index() +{ + uint8_t index = pair_index; + if(index == 0) + { + index = pair_count; + } + index--; + return index; +} + +uint8_t bluetooth_is_playing() +{ + return is_playing; +} + +char* mac_to_string(char* mac_addr) +{ + static char mac_address[MAC_ADDRESS_STR_LENGTH]; + for(int i = 0; i < MAC_ADDRESS_LENGTH; i++ ) + { + char val = (mac_addr[i]&0xF0)>>4; + if(val <= 9) + { + mac_address[i*3] = '0' + val; + } + else + { + mac_address[i*3] = val+('a'-0xA); + } + val = mac_addr[i]&0x0F; + if(val <= 9) + { + mac_address[(i*3)+1] = '0' + val; + } + else + { + mac_address[(i*3)+1] = val+('a'-0xA); + } + mac_address[(i*3)+2] = ':'; + } + mac_address[MAC_ADDRESS_STR_LENGTH-1] = '\0'; + return mac_address; +} + +uint8_t bluetooth_connected() +{ + return connected; +} void bluetooth_init(){ + add_task_no_arg(bluetooth_config, 0, SYSTEM_5_ms, 0); add_task_no_arg(bluetooth_rec_callback, SYSTEM_20_ms, SYSTEM_5_ms, 0); + add_task_no_arg(bluetooth_connect, 10*SYSTEM_1_S, SYSTEM_5_ms, 2*SYSTEM_1_S); } +void reset_pairings() +{ + usart_put_c_str(PSTR("SET BT PAIR *\r\n")); + bluetooth_config(); + pair_count = 0; + pair_index = 0; +} + void bluetooth_config(){ - usart_put_c_str(PSTR("SET CONTROL ECHO 4\r\n")); - usart_put_c_str(PSTR("VOLUME 6\r\n")); - usart_put_c_str(PSTR("SET CONTROL MICBIAS 10 12\r\n")); - usart_put_c_str(PSTR("SET CONTROL CONFIG 100\r\n")); - usart_put_c_str(PSTR("SET PROFILE A2DP SINK\r\n")); - usart_put_c_str(PSTR("SET PROFILE AVRCP CONTROLLER\r\n")); - usart_put_c_str(PSTR("SET PROFILE HFP ON\r\n")); - usart_put_c_str(PSTR("SET BT AUTH * 2323\r\n")); - usart_put_c_str(PSTR("SET BT CLASS 240428\r\n")); - usart_put_c_str(PSTR("SET BT NAME Bluetooth Matrix\r\n")); - - usart_put_c_str(PSTR("SET CONTROL AUTOCALL 0017 5000 A2DP\r\n")); - - usart_put_c_str(PSTR("SET CONTROL RINGTONE 6,gfgfgf__gfgfgf______gfgfgf__gfgfgf\r\n")); - - usart_put_c_str(PSTR("RESET\r\n")); + usart_put_c_str(PSTR("SET CONTROL ECHO 4\n")); + usart_put_c_str(PSTR("VOLUME 6\n")); + usart_put_c_str(PSTR("SET CONTROL CONFIG 100\n")); + usart_put_c_str(PSTR("SET PROFILE A2DP SINK\n")); + usart_put_c_str(PSTR("SET PROFILE AVRCP CONTROLLER\n")); + usart_put_c_str(PSTR("SET BT AUTH * 2323\n")); + usart_put_c_str(PSTR("SET BT CLASS 200438\n")); + usart_put_c_str(PSTR("SET BT NAME Matrix\n")); +// usart_put_c_str(PSTR("SET CONTROL CODEC SBC JOINT_STEREO 48000 0\n")); +// usart_put_c_str(PSTR("SET CONTROL CODEC SBC JOINT_STEREO 44100 1\n")); + usart_put_c_str(PSTR("RESET\n")); } void bluetooth_next_song(){ - usart_put_c_str(PSTR("@0 AVRCP FORWARD\r\n")); + usart_put_c('@');usart_put_c(callnumber); + usart_put_c_str(PSTR(" AVRCP FORWARD\r\n")); } void bluetooth_previous_song(){ - usart_put_c_str(PSTR("@0 AVRCP BACKWARD\r\n")); + usart_put_c('@');usart_put_c(callnumber); + usart_put_c_str(PSTR(" AVRCP BACKWARD\r\n")); } void bluetooth_pause_song(){ - usart_put_c_str(PSTR("@0 AVRCP PAUSE\r\n")); + usart_put_c('@');usart_put_c(callnumber); + usart_put_c_str(PSTR(" AVRCP PAUSE\r\n")); } void bluetooth_play_song(){ - usart_put_c_str(PSTR("@0 AVRCP PLAY\r\n")); + usart_put_c('@');usart_put_c(callnumber); + usart_put_c_str(PSTR(" AVRCP PLAY\r\n")); } +void bluetooth_disableSearch() +{ + pair_search = 0; +} + +void bluetooth_enableSearch() +{ + pair_search = 1; +} + +void bluetooth_connect() +{ + + if(!connected && pair_search) + { + pair_count = 0; + tmp_pair_index = 0; + usart_put_c_str(PSTR("SET BT PAIR\r\n")); + //usart_put_c_str(PSTR("SET\r\n")); + } + +} + +void bluetooth_disconnectAll() +{ + pair_search = 0; + usart_put_c_str(PSTR("CLOSE 0\r\n")); + usart_put_c_str(PSTR("CLOSE 1\r\n")); + usart_put_c_str(PSTR("CLOSE 2\r\n")); + usart_put_c_str(PSTR("CLOSE 3\r\n")); + usart_put_c_str(PSTR("CLOSE 4\r\n")); + usart_put_c_str(PSTR("CLOSE 5\r\n")); + usart_put_c_str(PSTR("CLOSE 6\r\n")); + usart_put_c_str(PSTR("CLOSE 7\r\n")); + usart_put_c_str(PSTR("CLOSE 8\r\n")); + usart_put_c_str(PSTR("CLOSE 9\r\n")); + pair_count = 0; + tmp_pair_index = 0; + pair_index = 0; +} + +void bluetooth_connectIndex(uint8_t index) +{ + bluetooth_disconnectAll(); + pair_index = index; + usart_put_c_str(PSTR("SET BT PAIR\r\n")); +} + +typedef struct lineMatch +{ + uint8_t match; + uint8_t partial; + char line[80]; +} lineMatch; + +lineMatch matchList [] PROGMEM = { + {MATCHING_PAIR_LIST, 1, "SET BT PAIR \b\b:\b\b:\b\b:\b\b:\b\b:\b\b "}, + {MATCHING_CONNECT, 1, "CONNECT \b AVRCP 17"}, + {MATCHING_CONNECT_ASYNC, 1, "RING \b \b\b:\b\b:\b\b:\b\b:\b\b:\b\b 17 AVRCP"}, + {MATCHING_DISCONNECT, 1, "NO CARRIER \b ERROR 0 "}, + {END_PAIR, 0, "SET"}, + {START_PLAY, 1, "A2DP STREAMING START"}, + {STOP_PLAY, 1, "A2DP STREAMING STOP"}, + {SYNTAX_ERROR, 1, "SYNTAX ERROR"} +}; + +void parse_line() +{ + + +char matchEnum = 0xFF; +for(int l = 0; l < sizeof(matchList)/sizeof(lineMatch) && matchEnum == 0xFF; l++) +{ + for(int c = 0; c < 80; c++) + { + char character = pgm_read_byte(&(matchList[l].line[c])); + if( (character == '\0') && (line[c] == character || pgm_read_byte(&(matchList[l].partial)))) + { + matchEnum = pgm_read_byte(&(matchList[l].match)); + break; + } + else if(character == '\b' || line[c] == character) + { + + } + else + { + break; + } + } + +} + +if(matchEnum != 0xFF) +{ + switch(matchEnum){ + case MATCHING_PAIR_LIST: + if(pair_index == tmp_pair_index && !connected) + { + line[12+MAC_ADDRESS_STR_LENGTH] = '\0'; + usart_put_c_str(PSTR("CALL ")); + usart_put_str(&line[12]); + usart_put_c_str(PSTR("17 AVRCP\r\n")); + usart_put_c_str(PSTR("CALL ")); + usart_put_str(&line[12]); + usart_put_c_str(PSTR("19 A2DP\r\n")); + pair_index++; + //send connect string + } + else + { + tmp_pair_index++; + } + pair_count++; + break; + case MATCHING_CONNECT: + connected = 1; + callnumber = line[8]; + break; + case MATCHING_CONNECT_ASYNC: + connected = 1; + callnumber = line[5]; + break; + case MATCHING_DISCONNECT: + connected = 0; + is_playing = 0; + break; + case END_PAIR: + if(pair_index >= pair_count) + { + pair_index = 0; + } + break; + case START_PLAY: + is_playing = 1; + break; + case STOP_PLAY: + is_playing = 0; + break; + case SYNTAX_ERROR: + // usart_put_c_str(PSTR("SYNTAX_ERROR\r\n")); + break; + } +} +else +{ + //usart_put_c('~'); + //usart_put_str(line); + //usart_put_c_str(PSTR("\r\n")); +} + +} + + void bluetooth_rec_callback(){ uint8_t character; - const char* connect_str = PSTR("CONNECT 0 AVRCP 17\r\n"); - const char* list_str = PSTR("LIST 0 CONNECTED L2CAP 48 0 0 0 0 0 "); while((character = usart_get_c()) != UART_BUFFER_EMPTY){ - switch(match_state){ - - case NO_MATCH: - match_place = 0; - if( character == pgm_read_byte_near(connect_str)){ - match_state = MATCHING_CONNECT; - match_place++; - } - else if( character == pgm_read_byte_near(list_str)){ - match_state = MATCHING_LIST; - match_place++; - } - - break; - case MATCHING_CONNECT: - if( character == pgm_read_byte_near(connect_str + match_place)){ - if(character == '\n'){ - match_state = NO_MATCH; - usart_put_c_str(PSTR("LIST\r\n")); - } - else{ - match_place++; - } - } - else{ - match_state = NO_MATCH; - } - break; - - case MATCHING_LIST: - { - uint8_t list_char = pgm_read_byte_near(list_str + match_place); - if( character == list_char){ - match_place++; - } - else if(list_char == '\0'){ - match_state = FETCHING_MAC; - connected_mac_address[0] = character; - match_place = 1; - } - else{ - match_state = NO_MATCH; - } - break; - } - case FETCHING_MAC: - if(match_place < MAC_ADDRESS_LENGTH-1){ - connected_mac_address[match_place] = character; - match_place++; - } - else{ - connected_mac_address[match_place] = '\0'; - usart_put_c_str(PSTR("CALL ")); - usart_put_str(connected_mac_address); - usart_put_c_str(PSTR(" 111F HFP\r\n")); - match_state = NO_MATCH; - } - break; + line[line_index] = character; + if(line_index < LINE_BUFFER-1) + { + line_index++; + } + if( character == '\n') + { + line[line_index-2] = '\0'; + parse_line(); + line_index = 0; } } -} \ No newline at end of file +} + diff --git a/code/src/bluetooth.h b/code/src/bluetooth.h index d0e4329..a79744c 100644 --- a/code/src/bluetooth.h +++ b/code/src/bluetooth.h @@ -18,20 +18,35 @@ #ifndef __BLUETOOTH_H #define __BLUETOOTH_H -#define MAC_ADDRESS_LENGTH 18 +#define MAC_ADDRESS_LENGTH 6 +#define MAC_ADDRESS_STR_LENGTH 18 +#define PAIR_COUNT 3 +uint8_t bluetooth_connected(void); +void reset_pairings(void); void bluetooth_init(void); void bluetooth_config(void); +void bluetooth_connectIndex(uint8_t index); +void bluetooth_enableSearch(); +void bluetooth_disableSearch(); +void bluetooth_disconnectAll(); +uint8_t bluetooth_index(void); +uint8_t bluetooth_is_playing(void); void bluetooth_next_song(void); void bluetooth_previous_song(void); void bluetooth_pause_song(void); void bluetooth_play_song(void); typedef enum MATCH_STATES { - NO_MATCH, - MATCHING_CONNECT, - MATCHING_LIST, - FETCHING_MAC, + NO_MATCH = 0, + MATCHING_PAIR_LIST = 1, + END_PAIR = 2, + MATCHING_CONNECT = 3, + MATCHING_CONNECT_ASYNC = 4, + MATCHING_DISCONNECT = 5, + SYNTAX_ERROR = 6, + START_PLAY = 7, + STOP_PLAY = 8 } MATCH_STATES; #endif diff --git a/code/src/main.c b/code/src/main.c index 11873a4..6af8aba 100644 --- a/code/src/main.c +++ b/code/src/main.c @@ -29,20 +29,19 @@ int main(void) { -// wdt_disable(); - _delay_ms(1000); + + _delay_ms(50); init_task_man(); - bluetooth_init(); USART_INIT_POLLING(); + bluetooth_init(); // USART_INIT_INTERRUPT(); AVC_init(1); sei(); // usart_put_c('t'); - _delay_ms(500); - AVC_add_tx_transaction_from_list(SELF_REGISTER); + _delay_ms(200); +// AVC_add_tx_transaction_from_list(SELF_REGISTER); sei(); - add_task_no_arg(bluetooth_config, 0, SYSTEM_1_S, SYSTEM_1_S); // wdt_enable( WDTO_2S ); while(1){