MBED implementation of JAudioStream

Files at this revision

API Documentation at this revision

Comitter:
ollie8
Date:
Thu Aug 20 07:17:51 2015 +0000
Parent:
7:9dfba830a0c8
Commit message:
Add stream call back function. Internal workings of stream now contained.

Changed in this revision

JAudioStream.cpp Show annotated file Show diff for this revision Revisions of this file
JAudioStream.h Show annotated file Show diff for this revision Revisions of this file
--- a/JAudioStream.cpp	Wed Aug 19 15:38:05 2015 +0000
+++ b/JAudioStream.cpp	Thu Aug 20 07:17:51 2015 +0000
@@ -2,7 +2,10 @@
 #define LOG_LEVEL_INFO
 #include "logger.h"
 
-JAudioStream::JAudioStream() {
+JAudioStream::JAudioStream(OnStream streamCallback, int bufferSize) {
+    this->streamCallback = streamCallback;
+    this->bufferSize = bufferSize;
+    requestCount = bufferSize / MAX_PACKT;
     cont = "cont";
     cond = "cond";
     dcon = "dcon";
@@ -15,7 +18,7 @@
     inTransmission = false;
 }
 
-bool JAudioStream::connect(char* ip, short port, char* name) {
+bool JAudioStream::connect(char* ip, int port, char* name) {
     eth.init();
     eth.connect();
     socket.init();
@@ -97,24 +100,39 @@
     return nowPlaying;
 }
 
-void JAudioStream::receive() {
+int JAudioStream::loop() {
+    int result = 0;
     char resp[MAX_PACKT];
-    //if (!inTransmission) {
-        if (read(resp, MAX_PACKT)) {
-            if (memcmp(resp, cond, 4) == 0) {
-                connected = true;
-            } else if (memcmp(resp, dctd, 4) == 0) {
-                connected = false;
-            } else if (memcmp(resp, hrbt, 4) == 0) {
-                char message[MESSAGE_SIZE];
-                buildMeassge(hrbt, -1, message);
-                send(message, MESSAGE_SIZE);     
-            } else if (memcmp(resp, begn, 4) == 0) {
-                memmove(&nowPlaying[0], &resp[4], 20);
-                inTransmission = true;
-            } else if (memcmp(resp, comt, 4) == 0) {
-                inTransmission = false;
+    if (read(resp, MAX_PACKT)) {
+        if (memcmp(resp, cond, 4) == 0) {
+            connected = true;
+        } else if (memcmp(resp, dctd, 4) == 0) {
+            connected = false;
+            result = 1;
+        } else if (memcmp(resp, hrbt, 4) == 0) {
+            char message[MESSAGE_SIZE];
+            buildMeassge(hrbt, -1, message);
+            send(message, MESSAGE_SIZE);     
+        } else if (memcmp(resp, begn, 4) == 0) {
+            memmove(&nowPlaying[0], &resp[4], 20);
+            inTransmission = true;
+        } else if (memcmp(resp, comt, 4) == 0) {
+            inTransmission = false;
+        } else {
+            char buffer[bufferSize];
+            char i = 0;
+            while (i < requestCount) {
+                stream.request(MAX_PACKT);
+                char buff[MAX_PACKT];
+                if (stream.read(buff, MAX_PACKT)) {
+                    memmove(&buffer[i*MAX_PACKT], &buff[0], MAX_PACKT);
+                    i++;    
+                } else {
+                    WARN("Timed out waiting for response");    
+                }
             }
-        }    
-    //}
+            streamCallback(buffer);
+        }
+        return result;
+    }
 }
--- a/JAudioStream.h	Wed Aug 19 15:38:05 2015 +0000
+++ b/JAudioStream.h	Thu Aug 20 07:17:51 2015 +0000
@@ -19,6 +19,10 @@
         const char* begn;
         const char* comt;
         const char* hrbt;
+        typedef void (*OnStream)(char*);
+        void (*streamCallback) (char*);
+        int bufferSize;
+        int requestCount;
         EthernetInterface eth;
         UDPSocket socket;
         Endpoint endPoint;
@@ -30,16 +34,16 @@
         void buildMeassge(const char*, int, char*, char*);
         void intTocharArr(int, char*);
         void send(char*, int);
+        void request(char);
+        bool read(char*, int);
         char* nowPlaying;
     public:
-        JAudioStream();
-        bool connect(char *, short, char *);
-        bool read(char*, int);
+        JAudioStream(OnStream, int);
+        bool connect(char *, int, char *);
         bool isConnected();
         char* getNowPlaying();
-        void request(char);
         void disconnect();
-        void receive();
+        int loop();
 };
 
 #endif
\ No newline at end of file