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:
- 7:2df2c6e8c0df
- Parent:
- 6:c454f88524d6
- Child:
- 8:beb6c399490a
--- a/RN52.cpp Wed Jan 13 22:11:50 2016 +0000 +++ b/RN52.cpp Thu Jan 14 22:20:38 2016 +0000 @@ -34,6 +34,7 @@ bool RN52::check_event(RN52_RESULT * result){ clear_serial(); if(event_pin == 0) { + clear_result(result); get(RN52_GETSTATUS, result); switch (result->event) { case RN52_CALLER_ID_EVENT: @@ -101,7 +102,7 @@ result->event = RN52_TRACK_CHANGE_EVENT; break; default: - result->event = RN52_NO_EVENT; + result->event = RN52_OTHER_EVENT; break; } break; @@ -118,53 +119,38 @@ 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]; - } + copy_response(result->response, result->title, 6); break; case 'i': //Artist - for( int n = 0; n < STRING_BUFFER_LENGTH-7; n=n+1 ) { - result->artist[n] = result->response[n+7]; - } + copy_response(result->response, result->artist, 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; + copy_response(result->response, result->album, 6); + break; case 'r': //Genre - for( int n = 0; n < STRING_BUFFER_LENGTH-6; n=n+1 ) { - result->genre[n] = result->response[n+6]; - } + copy_response(result->response, result->genre, 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]; - } + copy_response(result->response, number_string, 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]; - } + copy_response(result->response, number_string, 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]; - } + char duration_string[10]; + copy_response(result->response, duration_string, 9); result->duration = strtoul(duration_string, NULL, 10); } break; } - } clear_serial(); } @@ -176,7 +162,6 @@ } - bool RN52::capture_response(char * str){ char c = ' '; char n = 0; @@ -187,7 +172,7 @@ n++; } } - str[n] = '\0'; + str[n-2] = '\0'; //terminate string before \r\n return (str[0] == 'A'); } @@ -195,4 +180,28 @@ while(serial.readable()) { serial.getc(); } +} + +void RN52::copy_response(char * source, char * destination, char offset){ + int n = 0; + while(source[n+offset+1] != '\0') { //remove carraige return in the end + destination[n] = source[n+offset]; + n++; + } + destination[n] = '\0'; //end string where carriage was +} + +void RN52::clear_result(RN52_RESULT * result) { + result->event = RN52_NO_EVENT; + result->media_connected = 0; + result->phone_connected = 0; + result->connection = RN52_LIMBO; + result->title[0] = '\0'; + result->artist[0] = '\0'; + result->album[0] = '\0'; + result->genre[0] = '\0'; + result->duration = 0; + result->track_number = 0; + result->track_count = 0; + result->response[0] = '\0'; } \ No newline at end of file