MBED implementation of JAudioStream
Diff: JAudioStream.cpp
- Revision:
- 0:6f03883f9b43
- Child:
- 1:b49a6e72f353
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/JAudioStream.cpp Wed Aug 12 13:19:20 2015 +0000 @@ -0,0 +1,139 @@ +#include "JAudioStream.h" + +const char cont[] = {'c','o','n','t'}; +const char cond[] = {'c','o','n','d'}; +const char dcon[] = {'d','c','o','n'}; +const char rqst[] = {'r','q','s','t'}; +const char begn[] = {'b','e','g','n'}; +const char comt[] = {'c','o','m','t'}; + +JAudioStream::JAudioStream() { + connected = false; + inTransmission = false; +} + +void JAudioStream::onUDPSocketEvent(UDPSocketEvent e) { + +} + +void JAudioStream::begin(char* mac, char* ip, short port) { + EthernetErr ethErr = eth.setup(); + if (ethErr) { + // do something + } + Host me(IpAddr(), PORT); + udp.setOnEvent(this, &JAudioStream::onUDPSocketEvent); + udp.bind(me); +} + +bool JAudioStream::connect(char* ip, short port, char* name) { + remotePort = port; + remoteIp = ip; + 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; + } + } + return connected; +} + +bool JAudioStream::read(char *buffer, short amount, short timeOut) { + Timer t; + t.start(); + short to = t.read()+timeOut; + bool result = false; + while (1) { + int packetSize = Udp.available(); + if (packetSize) { + Udp.readPacket(buffer, MAX_PACKT); + result = true; + break; + } + int c = t.read(); + if (to < c) { + break; + } + } + t.stop(); + return result; +} + +bool JAudioStream::available() { + if (inTransmission) { + return true; + } else { + char resp[MAX_PACKT]; + if (read(resp, MAX_PACKT, 500)) { + 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(const char* cmd, char param, char* message) { + char 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]; + } +} + +void JAudioStream::buildMeassge(const char* cmd, char param, char* data, char* message) { + char 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]; + } + char dataSize = MESSAGE_SIZE*2; + for(i=MESSAGE_SIZE; i<dataSize; i++) { + message[i] = data[i-MESSAGE_SIZE]; + } +} + +void JAudioStream::intTocharArr(char 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) { + udp.sendPacket(data, len, remoteIp, remotePort); +} + +char* JAudioStream::getNowPlaying() { + return nowPlaying; +}