MBED implementation of JAudioStream
JAudioStream.cpp
- Committer:
- ollie8
- Date:
- 2015-08-13
- Revision:
- 2:22ee01f24346
- Parent:
- 1:b49a6e72f353
- Child:
- 3:51a66c975910
File content as of revision 2:22ee01f24346:
#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; } bool JAudioStream::connect(char* ip, short port, char* name) { eth.init(); eth.connect(); udp.init(); udp.set_blocking(true); 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); return connected; } bool JAudioStream::read(char *buffer) { bool result = false; INFO("Reading..."); if (udp.receiveFrom(endPoint, buffer, sizeof(buffer)) > 0) { INFO("Received: %s", buffer); result = true; } return result; } bool JAudioStream::available() { if (inTransmission) { return true; } else { char resp[MAX_PACKT]; if (read(resp)) { if (memcmp(resp, begn, 4) == 0) { memmove(&nowPlaying[0], &resp[4], 20); inTransmission = true; return true; } } } return false; } bool JAudioStream::isConnected() { return connected; } void JAudioStream::request(char amount) { char message[MESSAGE_SIZE]; buildMeassge(rqst, amount, message); send(message, MESSAGE_SIZE); } void JAudioStream::disconnect() { char message[MESSAGE_SIZE]; buildMeassge(dcon, -1, message); send(message, MESSAGE_SIZE); } 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]; } intTocharArr(param, bParam); for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { message[i] = bParam[i-MESSAGE_COMP_SIZE]; } } 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]; } intTocharArr(param, bParam); 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++) { message[i] = data[i-MESSAGE_SIZE]; } } void JAudioStream::intTocharArr(int value, char* buff) { buff[0] = (value >> 24) & 0xFF; buff[1] = (value >> 16) & 0xFF; buff[2] = (value >> 8) & 0xFF; buff[3] = value; } 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); } } }