MBED implementation of JAudioStream
JAudioStream.cpp@1:b49a6e72f353, 2015-08-13 (annotated)
- Committer:
- ollie8
- Date:
- Thu Aug 13 13:32:51 2015 +0000
- Revision:
- 1:b49a6e72f353
- Parent:
- 0:6f03883f9b43
- Child:
- 2:22ee01f24346
updated library for mbed.
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 | JAudioStream::JAudioStream() { |
ollie8 | 0:6f03883f9b43 | 4 | connected = false; |
ollie8 | 0:6f03883f9b43 | 5 | inTransmission = false; |
ollie8 | 0:6f03883f9b43 | 6 | } |
ollie8 | 0:6f03883f9b43 | 7 | |
ollie8 | 0:6f03883f9b43 | 8 | void JAudioStream::begin(char* mac, char* ip, short port) { |
ollie8 | 1:b49a6e72f353 | 9 | eth.init(); |
ollie8 | 1:b49a6e72f353 | 10 | eth.connect(); |
ollie8 | 1:b49a6e72f353 | 11 | udp.init(); |
ollie8 | 1:b49a6e72f353 | 12 | udp.set_blocking(true); |
ollie8 | 0:6f03883f9b43 | 13 | } |
ollie8 | 0:6f03883f9b43 | 14 | |
ollie8 | 0:6f03883f9b43 | 15 | bool JAudioStream::connect(char* ip, short port, char* name) { |
ollie8 | 0:6f03883f9b43 | 16 | remotePort = port; |
ollie8 | 0:6f03883f9b43 | 17 | remoteIp = ip; |
ollie8 | 1:b49a6e72f353 | 18 | endPoint.set_address(ip, port); |
ollie8 | 0:6f03883f9b43 | 19 | char size = MESSAGE_SIZE*2; |
ollie8 | 0:6f03883f9b43 | 20 | char message[size]; |
ollie8 | 0:6f03883f9b43 | 21 | buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); |
ollie8 | 0:6f03883f9b43 | 22 | send(message, size); |
ollie8 | 0:6f03883f9b43 | 23 | char resp[MAX_PACKT]; |
ollie8 | 0:6f03883f9b43 | 24 | if (read(resp, MAX_PACKT, 500)) { |
ollie8 | 0:6f03883f9b43 | 25 | if (memcmp(resp, cond, 4) == 0) { |
ollie8 | 0:6f03883f9b43 | 26 | connected = true; |
ollie8 | 0:6f03883f9b43 | 27 | } |
ollie8 | 0:6f03883f9b43 | 28 | } |
ollie8 | 0:6f03883f9b43 | 29 | return connected; |
ollie8 | 0:6f03883f9b43 | 30 | } |
ollie8 | 0:6f03883f9b43 | 31 | |
ollie8 | 0:6f03883f9b43 | 32 | bool JAudioStream::read(char *buffer, short amount, short timeOut) { |
ollie8 | 0:6f03883f9b43 | 33 | bool result = false; |
ollie8 | 1:b49a6e72f353 | 34 | if (udp.receiveFrom(endPoint,buffer, MAX_PACKT) > 0) { |
ollie8 | 1:b49a6e72f353 | 35 | result = true; |
ollie8 | 0:6f03883f9b43 | 36 | } |
ollie8 | 0:6f03883f9b43 | 37 | return result; |
ollie8 | 0:6f03883f9b43 | 38 | } |
ollie8 | 0:6f03883f9b43 | 39 | |
ollie8 | 0:6f03883f9b43 | 40 | bool JAudioStream::available() { |
ollie8 | 0:6f03883f9b43 | 41 | if (inTransmission) { |
ollie8 | 0:6f03883f9b43 | 42 | return true; |
ollie8 | 0:6f03883f9b43 | 43 | } else { |
ollie8 | 0:6f03883f9b43 | 44 | char resp[MAX_PACKT]; |
ollie8 | 0:6f03883f9b43 | 45 | if (read(resp, MAX_PACKT, 500)) { |
ollie8 | 0:6f03883f9b43 | 46 | if (memcmp(resp, begn, 4) == 0) { |
ollie8 | 0:6f03883f9b43 | 47 | memmove(&nowPlaying[0], &resp[4], 20); |
ollie8 | 0:6f03883f9b43 | 48 | inTransmission = true; |
ollie8 | 0:6f03883f9b43 | 49 | return true; |
ollie8 | 0:6f03883f9b43 | 50 | } |
ollie8 | 0:6f03883f9b43 | 51 | } |
ollie8 | 0:6f03883f9b43 | 52 | } |
ollie8 | 0:6f03883f9b43 | 53 | return false; |
ollie8 | 0:6f03883f9b43 | 54 | } |
ollie8 | 0:6f03883f9b43 | 55 | |
ollie8 | 0:6f03883f9b43 | 56 | bool JAudioStream::isConnected() { |
ollie8 | 0:6f03883f9b43 | 57 | return connected; |
ollie8 | 0:6f03883f9b43 | 58 | } |
ollie8 | 0:6f03883f9b43 | 59 | |
ollie8 | 0:6f03883f9b43 | 60 | void JAudioStream::request(char amount) { |
ollie8 | 0:6f03883f9b43 | 61 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 62 | buildMeassge(rqst, amount, message); |
ollie8 | 0:6f03883f9b43 | 63 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 64 | } |
ollie8 | 0:6f03883f9b43 | 65 | |
ollie8 | 0:6f03883f9b43 | 66 | void JAudioStream::disconnect() { |
ollie8 | 0:6f03883f9b43 | 67 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 68 | buildMeassge(dcon, -1, message); |
ollie8 | 0:6f03883f9b43 | 69 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 70 | } |
ollie8 | 0:6f03883f9b43 | 71 | |
ollie8 | 1:b49a6e72f353 | 72 | void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { |
ollie8 | 0:6f03883f9b43 | 73 | char i; |
ollie8 | 0:6f03883f9b43 | 74 | for(i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 75 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 76 | } |
ollie8 | 0:6f03883f9b43 | 77 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 78 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 79 | for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 80 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 81 | } |
ollie8 | 0:6f03883f9b43 | 82 | } |
ollie8 | 0:6f03883f9b43 | 83 | |
ollie8 | 1:b49a6e72f353 | 84 | void JAudioStream::buildMeassge(const char* cmd, int param, char* data, char* message) { |
ollie8 | 0:6f03883f9b43 | 85 | char i; |
ollie8 | 0:6f03883f9b43 | 86 | for(i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 87 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 88 | } |
ollie8 | 0:6f03883f9b43 | 89 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 90 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 91 | for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 0:6f03883f9b43 | 92 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 93 | } |
ollie8 | 0:6f03883f9b43 | 94 | char dataSize = MESSAGE_SIZE*2; |
ollie8 | 0:6f03883f9b43 | 95 | for(i=MESSAGE_SIZE; i<dataSize; i++) { |
ollie8 | 0:6f03883f9b43 | 96 | message[i] = data[i-MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 97 | } |
ollie8 | 0:6f03883f9b43 | 98 | } |
ollie8 | 0:6f03883f9b43 | 99 | |
ollie8 | 1:b49a6e72f353 | 100 | void JAudioStream::intTocharArr(int value, char* buff) { |
ollie8 | 0:6f03883f9b43 | 101 | buff[0] = (value >> 24) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 102 | buff[1] = (value >> 16) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 103 | buff[2] = (value >> 8) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 104 | buff[3] = value; |
ollie8 | 0:6f03883f9b43 | 105 | } |
ollie8 | 0:6f03883f9b43 | 106 | |
ollie8 | 0:6f03883f9b43 | 107 | void JAudioStream::send(char* data, short len) { |
ollie8 | 1:b49a6e72f353 | 108 | udp.sendTo(endPoint, data, len); |
ollie8 | 0:6f03883f9b43 | 109 | } |
ollie8 | 0:6f03883f9b43 | 110 | |
ollie8 | 0:6f03883f9b43 | 111 | char* JAudioStream::getNowPlaying() { |
ollie8 | 0:6f03883f9b43 | 112 | return nowPlaying; |
ollie8 | 0:6f03883f9b43 | 113 | } |