MBED implementation of JAudioStream
Diff: JAudioStream.cpp
- Revision:
- 2:22ee01f24346
- Parent:
- 1:b49a6e72f353
- Child:
- 3:51a66c975910
--- a/JAudioStream.cpp Thu Aug 13 13:32:51 2015 +0000 +++ b/JAudioStream.cpp Thu Aug 13 16:19:50 2015 +0000 @@ -1,37 +1,39 @@ #include "JAudioStream.h" +#define DEBUG +#include "logger.h" JAudioStream::JAudioStream() { + cont = "cont"; + cond = "cond"; + dcon = "dcon"; + rqst = "rqst"; + begn = "begn"; + comt = "comt"; + hrbt = "hrbt"; connected = false; inTransmission = false; } -void JAudioStream::begin(char* mac, char* ip, short port) { +bool JAudioStream::connect(char* ip, short port, char* name) { eth.init(); eth.connect(); udp.init(); udp.set_blocking(true); -} - -bool JAudioStream::connect(char* ip, short port, char* name) { remotePort = port; remoteIp = ip; endPoint.set_address(ip, port); char size = MESSAGE_SIZE*2; char message[size]; buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); - send(message, size); - char resp[MAX_PACKT]; - if (read(resp, MAX_PACKT, 500)) { - if (memcmp(resp, cond, 4) == 0) { - connected = true; - } - } + send(message, size); return connected; } -bool JAudioStream::read(char *buffer, short amount, short timeOut) { +bool JAudioStream::read(char *buffer) { bool result = false; - if (udp.receiveFrom(endPoint,buffer, MAX_PACKT) > 0) { + INFO("Reading..."); + if (udp.receiveFrom(endPoint, buffer, sizeof(buffer)) > 0) { + INFO("Received: %s", buffer); result = true; } return result; @@ -42,7 +44,7 @@ return true; } else { char resp[MAX_PACKT]; - if (read(resp, MAX_PACKT, 500)) { + if (read(resp)) { if (memcmp(resp, begn, 4) == 0) { memmove(&nowPlaying[0], &resp[4], 20); inTransmission = true; @@ -69,30 +71,30 @@ send(message, MESSAGE_SIZE); } -void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { +void JAudioStream::buildMeassge(char* cmd, int param, char* message) { + char bParam[MESSAGE_COMP_SIZE]; char i; - for(i=0; i<MESSAGE_COMP_SIZE; i++) { - message[i] = cmd[i]; + for (i=0; i<MESSAGE_COMP_SIZE; i++) { + message[i] = cmd[i]; } - char bParam[MESSAGE_COMP_SIZE]; intTocharArr(param, bParam); - for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { - message[i] = bParam[i-MESSAGE_COMP_SIZE]; + for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { + message[i] = bParam[i-MESSAGE_COMP_SIZE]; } } -void JAudioStream::buildMeassge(const char* cmd, int param, char* data, char* message) { +void JAudioStream::buildMeassge(char* cmd, int param, char* data, char* message) { + char bParam[MESSAGE_COMP_SIZE]; char i; - for(i=0; i<MESSAGE_COMP_SIZE; i++) { - message[i] = cmd[i]; + for (i=0; i<MESSAGE_COMP_SIZE; i++) { + message[i] = cmd[i]; } - char bParam[MESSAGE_COMP_SIZE]; intTocharArr(param, bParam); - for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { - message[i] = bParam[i-MESSAGE_COMP_SIZE]; + for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { + message[i] = bParam[i-MESSAGE_COMP_SIZE]; } char dataSize = MESSAGE_SIZE*2; - for(i=MESSAGE_SIZE; i<dataSize; i++) { + for (i=MESSAGE_SIZE; i<dataSize; i++) { message[i] = data[i-MESSAGE_SIZE]; } } @@ -105,9 +107,23 @@ } void JAudioStream::send(char* data, short len) { + INFO("Sending: %s", data); udp.sendTo(endPoint, data, len); } char* JAudioStream::getNowPlaying() { return nowPlaying; } + +void JAudioStream::receive() { + char resp[MAX_PACKT]; + if (read(resp)) { + if (memcmp(resp, cond, 4) == 0) { + connected = true; + } else if (memcmp(resp, hrbt, 4) == 0) { + char message[MESSAGE_SIZE]; + buildMeassge(hrbt, -1, message); + send(message, MESSAGE_SIZE); + } + } +}