Petter Bellander / Mbed 2 deprecated Saab-BT

Dependencies:   mbed

Revision:
16:7bb8b161e00b
Parent:
15:82c3cc87bd02
--- a/RN52.cpp	Sun Feb 07 18:24:13 2016 +0000
+++ b/RN52.cpp	Fri Mar 18 20:58:36 2016 +0000
@@ -16,11 +16,14 @@
 #define TRACK_METADATA  "AD\r"
 
 #define RESPONSE_TIMEOUT    20 //ms
+#define METADATA_RESPONSE_TIMEOUT    100 //ms
 
 Serial serial(p13, p14);  // tx, rx
 DigitalIn event_pin(p20);
 DigitalOut enable_pin(p5);
 Timer responsetime;
+Timer metadata_responsetime;
+
 char temp_response[255];
 
 
@@ -52,6 +55,11 @@
             case RN52_TRACK_CHANGE_EVENT:
                 get_track_metadata(result);
                 break;
+            case RN52_OTHER_EVENT:
+                if(result->connection == RN52_AUDIO_STREAMING) {
+                    get_track_metadata(result);
+                }
+                break;
         }
         return 1;   
     }
@@ -117,47 +125,52 @@
 }
 bool RN52::get_track_metadata(RN52_RESULT * result){
     clear_serial();
+    metadata_responsetime.start();
+    metadata_responsetime.reset();
     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
-                    copy_response(result->response, result->title, 6);
-                    break;
-                case 'i': //Artist
-                    copy_response(result->response, result->artist, 7);
-                    break; 
-                case 'u': //Album
-                    copy_response(result->response, result->album, 6);
-                    break; 
-                case 'r': //Genre
-                    copy_response(result->response, result->genre, 6);
-                    break;
-                case 'c': //TrackNumber or TrackCount
-                    {
-                        char number_string[5];
-                        if(result->response[5] == 'N') { //TrackNumber
-                            copy_response(result->response, number_string, 12);
-                            result->track_number = strtoul(number_string, NULL, 10);
-                        }
-                        else { //TrackCount
-                            copy_response(result->response, number_string, 11);
-                            result->track_count = strtoul(number_string, NULL, 10);
-                        }
+    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
+                copy_response(result->response, result->title, 6);
+                break;
+            case 'i': //Artist
+                copy_response(result->response, result->artist, 7);
+                break; 
+            case 'u': //Album
+                copy_response(result->response, result->album, 6);
+                break; 
+            case 'r': //Genre
+                copy_response(result->response, result->genre, 6);
+                break;
+            case 'c': //TrackNumber or TrackCount
+                {
+                    char number_string[5];
+                    if(result->response[5] == 'N') { //TrackNumber
+                        copy_response(result->response, number_string, 12);
+                        result->track_number = strtoul(number_string, NULL, 10);
                     }
-                    break;
-                case 'e':
-                    {
-                        char duration_string[10];                                    
-                        copy_response(result->response, duration_string, 9);
-                        result->duration = strtoul(duration_string, NULL, 10);
+                    else { //TrackCount
+                        copy_response(result->response, number_string, 11);
+                        result->track_count = strtoul(number_string, NULL, 10);
                     }
-                    break;                                   
-            }
+                }
+                break;
+            case 'e':
+                {
+                    char duration_string[10];                                    
+                    copy_response(result->response, duration_string, 9);
+                    result->duration = strtoul(duration_string, NULL, 10);
+                }
+                break;                                   
         }
-        clear_serial();
+        if(metadata_responsetime.read_ms() > METADATA_RESPONSE_TIMEOUT) {
+            clear_serial();
+            return 0;
+        }
     }
+    clear_serial();
     return 1;
 }
 
@@ -217,7 +230,7 @@
     enable_pin = 1;
     wait(0.5); //make sure booted up, not sure if needed
     serial.printf("S%%,18E7\r"); //extended features
-    serial.printf("SC,040420\r"); //device type
+    serial.printf("SC,200420\r"); //device type
     serial.printf("SN,%s\r", name); //name
     serial.printf(REBOOT);
     clear_serial();