MBED implementation of JAudioStream
JAudioStream.cpp@0:6f03883f9b43, 2015-08-12 (annotated)
- Committer:
- ollie8
- Date:
- Wed Aug 12 13:19:20 2015 +0000
- Revision:
- 0:6f03883f9b43
- Child:
- 1:b49a6e72f353
Mbed implementation of JAudioStream
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ollie8 | 0:6f03883f9b43 | 1 | #include "JAudioStream.h" |
ollie8 | 0:6f03883f9b43 | 2 | |
ollie8 | 0:6f03883f9b43 | 3 | const char cont[] = {'c','o','n','t'}; |
ollie8 | 0:6f03883f9b43 | 4 | const char cond[] = {'c','o','n','d'}; |
ollie8 | 0:6f03883f9b43 | 5 | const char dcon[] = {'d','c','o','n'}; |
ollie8 | 0:6f03883f9b43 | 6 | const char rqst[] = {'r','q','s','t'}; |
ollie8 | 0:6f03883f9b43 | 7 | const char begn[] = {'b','e','g','n'}; |
ollie8 | 0:6f03883f9b43 | 8 | const char comt[] = {'c','o','m','t'}; |
ollie8 | 0:6f03883f9b43 | 9 | |
ollie8 | 0:6f03883f9b43 | 10 | JAudioStream::JAudioStream() { |
ollie8 | 0:6f03883f9b43 | 11 | connected = false; |
ollie8 | 0:6f03883f9b43 | 12 | inTransmission = false; |
ollie8 | 0:6f03883f9b43 | 13 | } |
ollie8 | 0:6f03883f9b43 | 14 | |
ollie8 | 0:6f03883f9b43 | 15 | void JAudioStream::onUDPSocketEvent(UDPSocketEvent e) { |
ollie8 | 0:6f03883f9b43 | 16 | |
ollie8 | 0:6f03883f9b43 | 17 | } |
ollie8 | 0:6f03883f9b43 | 18 | |
ollie8 | 0:6f03883f9b43 | 19 | void JAudioStream::begin(char* mac, char* ip, short port) { |
ollie8 | 0:6f03883f9b43 | 20 | EthernetErr ethErr = eth.setup(); |
ollie8 | 0:6f03883f9b43 | 21 | if (ethErr) { |
ollie8 | 0:6f03883f9b43 | 22 | // do something |
ollie8 | 0:6f03883f9b43 | 23 | } |
ollie8 | 0:6f03883f9b43 | 24 | Host me(IpAddr(), PORT); |
ollie8 | 0:6f03883f9b43 | 25 | udp.setOnEvent(this, &JAudioStream::onUDPSocketEvent); |
ollie8 | 0:6f03883f9b43 | 26 | udp.bind(me); |
ollie8 | 0:6f03883f9b43 | 27 | } |
ollie8 | 0:6f03883f9b43 | 28 | |
ollie8 | 0:6f03883f9b43 | 29 | bool JAudioStream::connect(char* ip, short port, char* name) { |
ollie8 | 0:6f03883f9b43 | 30 | remotePort = port; |
ollie8 | 0:6f03883f9b43 | 31 | remoteIp = ip; |
ollie8 | 0:6f03883f9b43 | 32 | char size = MESSAGE_SIZE*2; |
ollie8 | 0:6f03883f9b43 | 33 | char message[size]; |
ollie8 | 0:6f03883f9b43 | 34 | buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); |
ollie8 | 0:6f03883f9b43 | 35 | send(message, size); |
ollie8 | 0:6f03883f9b43 | 36 | char resp[MAX_PACKT]; |
ollie8 | 0:6f03883f9b43 | 37 | if (read(resp, MAX_PACKT, 500)) { |
ollie8 | 0:6f03883f9b43 | 38 | if (memcmp(resp, cond, 4) == 0) { |
ollie8 | 0:6f03883f9b43 | 39 | connected = true; |
ollie8 | 0:6f03883f9b43 | 40 | } |
ollie8 | 0:6f03883f9b43 | 41 | } |
ollie8 | 0:6f03883f9b43 | 42 | return connected; |
ollie8 | 0:6f03883f9b43 | 43 | } |
ollie8 | 0:6f03883f9b43 | 44 | |
ollie8 | 0:6f03883f9b43 | 45 | bool JAudioStream::read(char *buffer, short amount, short timeOut) { |
ollie8 | 0:6f03883f9b43 | 46 | Timer t; |
ollie8 | 0:6f03883f9b43 | 47 | t.start(); |
ollie8 | 0:6f03883f9b43 | 48 | short to = t.read()+timeOut; |
ollie8 | 0:6f03883f9b43 | 49 | bool result = false; |
ollie8 | 0:6f03883f9b43 | 50 | while (1) { |
ollie8 | 0:6f03883f9b43 | 51 | int packetSize = Udp.available(); |
ollie8 | 0:6f03883f9b43 | 52 | if (packetSize) { |
ollie8 | 0:6f03883f9b43 | 53 | Udp.readPacket(buffer, MAX_PACKT); |
ollie8 | 0:6f03883f9b43 | 54 | result = true; |
ollie8 | 0:6f03883f9b43 | 55 | break; |
ollie8 | 0:6f03883f9b43 | 56 | } |
ollie8 | 0:6f03883f9b43 | 57 | int c = t.read(); |
ollie8 | 0:6f03883f9b43 | 58 | if (to < c) { |
ollie8 | 0:6f03883f9b43 | 59 | break; |
ollie8 | 0:6f03883f9b43 | 60 | } |
ollie8 | 0:6f03883f9b43 | 61 | } |
ollie8 | 0:6f03883f9b43 | 62 | t.stop(); |
ollie8 | 0:6f03883f9b43 | 63 | return result; |
ollie8 | 0:6f03883f9b43 | 64 | } |
ollie8 | 0:6f03883f9b43 | 65 | |
ollie8 | 0:6f03883f9b43 | 66 | bool JAudioStream::available() { |
ollie8 | 0:6f03883f9b43 | 67 | if (inTransmission) { |
ollie8 | 0:6f03883f9b43 | 68 | return true; |
ollie8 | 0:6f03883f9b43 | 69 | } else { |
ollie8 | 0:6f03883f9b43 | 70 | char resp[MAX_PACKT]; |
ollie8 | 0:6f03883f9b43 | 71 | if (read(resp, MAX_PACKT, 500)) { |
ollie8 | 0:6f03883f9b43 | 72 | if (memcmp(resp, begn, 4) == 0) { |
ollie8 | 0:6f03883f9b43 | 73 | memmove(&nowPlaying[0], &resp[4], 20); |
ollie8 | 0:6f03883f9b43 | 74 | inTransmission = true; |
ollie8 | 0:6f03883f9b43 | 75 | return true; |
ollie8 | 0:6f03883f9b43 | 76 | } |
ollie8 | 0:6f03883f9b43 | 77 | } |
ollie8 | 0:6f03883f9b43 | 78 | } |
ollie8 | 0:6f03883f9b43 | 79 | return false; |
ollie8 | 0:6f03883f9b43 | 80 | } |
ollie8 | 0:6f03883f9b43 | 81 | |
ollie8 | 0:6f03883f9b43 | 82 | bool JAudioStream::isConnected() { |
ollie8 | 0:6f03883f9b43 | 83 | return connected; |
ollie8 | 0:6f03883f9b43 | 84 | } |
ollie8 | 0:6f03883f9b43 | 85 | |
ollie8 | 0:6f03883f9b43 | 86 | void JAudioStream::request(char amount) { |
ollie8 | 0:6f03883f9b43 | 87 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 88 | buildMeassge(rqst, amount, message); |
ollie8 | 0:6f03883f9b43 | 89 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 90 | } |
ollie8 | 0:6f03883f9b43 | 91 | |
ollie8 | 0:6f03883f9b43 | 92 | void JAudioStream::disconnect() { |
ollie8 | 0:6f03883f9b43 | 93 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 94 | buildMeassge(dcon, -1, message); |
ollie8 | 0:6f03883f9b43 | 95 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 96 | } |
ollie8 | 0:6f03883f9b43 | 97 | |
ollie8 | 0:6f03883f9b43 | 98 | void JAudioStream::buildMeassge(const char* cmd, char param, char* message) { |
ollie8 | 0:6f03883f9b43 | 99 | char i; |
ollie8 | 0:6f03883f9b43 | 100 | for(i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 101 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 102 | } |
ollie8 | 0:6f03883f9b43 | 103 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 104 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 105 | for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 106 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 107 | } |
ollie8 | 0:6f03883f9b43 | 108 | } |
ollie8 | 0:6f03883f9b43 | 109 | |
ollie8 | 0:6f03883f9b43 | 110 | void JAudioStream::buildMeassge(const char* cmd, char param, char* data, char* message) { |
ollie8 | 0:6f03883f9b43 | 111 | char i; |
ollie8 | 0:6f03883f9b43 | 112 | for(i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 113 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 114 | } |
ollie8 | 0:6f03883f9b43 | 115 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 116 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 117 | for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 118 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 119 | } |
ollie8 | 0:6f03883f9b43 | 120 | char dataSize = MESSAGE_SIZE*2; |
ollie8 | 0:6f03883f9b43 | 121 | for(i=MESSAGE_SIZE; i<dataSize; i++) { |
ollie8 | 0:6f03883f9b43 | 122 | message[i] = data[i-MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 123 | } |
ollie8 | 0:6f03883f9b43 | 124 | } |
ollie8 | 0:6f03883f9b43 | 125 | |
ollie8 | 0:6f03883f9b43 | 126 | void JAudioStream::intTocharArr(char value, char* buff) { |
ollie8 | 0:6f03883f9b43 | 127 | buff[0] = (value >> 24) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 128 | buff[1] = (value >> 16) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 129 | buff[2] = (value >> 8) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 130 | buff[3] = value; |
ollie8 | 0:6f03883f9b43 | 131 | } |
ollie8 | 0:6f03883f9b43 | 132 | |
ollie8 | 0:6f03883f9b43 | 133 | void JAudioStream::send(char* data, short len) { |
ollie8 | 0:6f03883f9b43 | 134 | udp.sendPacket(data, len, remoteIp, remotePort); |
ollie8 | 0:6f03883f9b43 | 135 | } |
ollie8 | 0:6f03883f9b43 | 136 | |
ollie8 | 0:6f03883f9b43 | 137 | char* JAudioStream::getNowPlaying() { |
ollie8 | 0:6f03883f9b43 | 138 | return nowPlaying; |
ollie8 | 0:6f03883f9b43 | 139 | } |