Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: RN52.cpp
- Revision:
- 6:c454f88524d6
- Parent:
- 5:8e468fef2754
- Child:
- 7:2df2c6e8c0df
--- a/RN52.cpp Mon Jan 11 20:49:59 2016 +0000 +++ b/RN52.cpp Wed Jan 13 22:11:50 2016 +0000 @@ -22,11 +22,18 @@ void RN52::init(){ serial.baud(115200); printf("Serial baudrate set\r\n"); + + //make sure event pin is pulled low + if(event_pin == 0) { + char response[10]; + serial.printf(GETSTATUS); + capture_response(response); + } } bool RN52::check_event(RN52_RESULT * result){ - static bool last = 0; - if((last == 1) && (event_pin == 0)) { + clear_serial(); + if(event_pin == 0) { get(RN52_GETSTATUS, result); switch (result->event) { case RN52_CALLER_ID_EVENT: @@ -36,50 +43,44 @@ get(RN52_TRACK_METADATA, result); break; } - last = event_pin; return 1; } - last = event_pin; return 0; } bool RN52::connect(){ - char response[40]; + char response[5]; serial.printf(CONNECT); return capture_response(response); } bool RN52::disconnect(){ - char response[40]; + char response[5]; serial.printf(DISCONNECT); return capture_response(response); } bool RN52::next_track(){ - char response[40]; + char response[5]; serial.printf(NEXTTRACK); return capture_response(response); } bool RN52::prev_track(){ - char response[40]; + char response[5]; serial.printf(PREVTRACK); return capture_response(response); } bool RN52::toggle_play(){ - char response[40]; + char response[5]; serial.printf(PLAYPAUSE); return capture_response(response); } bool RN52::maxvolume(){ - char response[40]; - //serial.printf(MAXVOLUME); - serial.printf(VOLUMEUP); - serial.printf(VOLUMEUP); - serial.printf(VOLUMEUP); - serial.printf(VOLUMEUP); + char response[5]; + serial.printf(MAXVOLUME); return capture_response(response); } @@ -111,10 +112,63 @@ //parse the response break; case RN52_TRACK_METADATA: - serial.printf(TRACK_METADATA); - capture_response(result->response); - //parse the response - break; + serial.printf(TRACK_METADATA); + if(capture_response(result->response)) {//AOK + while(result->response[4] != '(') { //time(ms) - always the last one + capture_response(result->response); + switch (result->response[3]) { + case 'l': //Title + for( int n = 0; n < STRING_BUFFER_LENGTH-6; n=n+1 ) { + result->title[n] = result->response[n+6]; + } + break; + case 'i': //Artist + for( int n = 0; n < STRING_BUFFER_LENGTH-7; n=n+1 ) { + result->artist[n] = result->response[n+7]; + } + break; + case 'u': //Album + for( int n = 0; n < STRING_BUFFER_LENGTH-6; n=n+1 ) { + result->album[n] = result->response[n+6]; + } + break; + case 'r': //Genre + for( int n = 0; n < STRING_BUFFER_LENGTH-6; n=n+1 ) { + result->genre[n] = result->response[n+6]; + } + break; + case 'c': //TrackNumber or TrackCount + { + char number_string[5]; + if(result->response[5] == 'N') { //TrackNumber + for( int n = 0; n < 5; n=n+1 ) { + number_string[n] = result->response[n+12]; + } + result->track_number = strtoul(number_string, NULL, 10); + } + else { //TrackCount + for( int n = 0; n < 5; n=n+1 ) { + number_string[n] = result->response[n+11]; + } + result->track_count = strtoul(number_string, NULL, 10); + } + } + break; + case 'e': + { + char duration_string[10]; + for( int n = 0; n < 10; n=n+1 ) { + duration_string[n] = result->response[n+9]; + } + result->duration = strtoul(duration_string, NULL, 10); + } + break; + } + + } + clear_serial(); + } + break; default: return 0; } @@ -122,6 +176,7 @@ } + bool RN52::capture_response(char * str){ char c = ' '; char n = 0; @@ -134,4 +189,10 @@ } str[n] = '\0'; return (str[0] == 'A'); +} + +void RN52::clear_serial(){ + while(serial.readable()) { + serial.getc(); + } } \ No newline at end of file