MBED implementation of JAudioStream
JAudioStream.cpp@7:9dfba830a0c8, 2015-08-19 (annotated)
- Committer:
- ollie8
- Date:
- Wed Aug 19 15:38:05 2015 +0000
- Revision:
- 7:9dfba830a0c8
- Parent:
- 6:7e366261cd93
- Child:
- 8:65d8e8882907
Added trace logging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ollie8 | 0:6f03883f9b43 | 1 | #include "JAudioStream.h" |
ollie8 | 7:9dfba830a0c8 | 2 | #define LOG_LEVEL_INFO |
ollie8 | 2:22ee01f24346 | 3 | #include "logger.h" |
ollie8 | 0:6f03883f9b43 | 4 | |
ollie8 | 0:6f03883f9b43 | 5 | JAudioStream::JAudioStream() { |
ollie8 | 2:22ee01f24346 | 6 | cont = "cont"; |
ollie8 | 2:22ee01f24346 | 7 | cond = "cond"; |
ollie8 | 2:22ee01f24346 | 8 | dcon = "dcon"; |
ollie8 | 4:ebdd1cf0c9f7 | 9 | dctd = "dctd"; |
ollie8 | 2:22ee01f24346 | 10 | rqst = "rqst"; |
ollie8 | 2:22ee01f24346 | 11 | begn = "begn"; |
ollie8 | 2:22ee01f24346 | 12 | comt = "comt"; |
ollie8 | 2:22ee01f24346 | 13 | hrbt = "hrbt"; |
ollie8 | 0:6f03883f9b43 | 14 | connected = false; |
ollie8 | 0:6f03883f9b43 | 15 | inTransmission = false; |
ollie8 | 0:6f03883f9b43 | 16 | } |
ollie8 | 0:6f03883f9b43 | 17 | |
ollie8 | 2:22ee01f24346 | 18 | bool JAudioStream::connect(char* ip, short port, char* name) { |
ollie8 | 1:b49a6e72f353 | 19 | eth.init(); |
ollie8 | 1:b49a6e72f353 | 20 | eth.connect(); |
ollie8 | 3:51a66c975910 | 21 | socket.init(); |
ollie8 | 7:9dfba830a0c8 | 22 | socket.set_blocking(false, 500); |
ollie8 | 0:6f03883f9b43 | 23 | remotePort = port; |
ollie8 | 0:6f03883f9b43 | 24 | remoteIp = ip; |
ollie8 | 1:b49a6e72f353 | 25 | endPoint.set_address(ip, port); |
ollie8 | 3:51a66c975910 | 26 | char message[MESSAGE_SIZE*2]; |
ollie8 | 0:6f03883f9b43 | 27 | buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); |
ollie8 | 3:51a66c975910 | 28 | send(message, MESSAGE_SIZE*2); |
ollie8 | 0:6f03883f9b43 | 29 | return connected; |
ollie8 | 0:6f03883f9b43 | 30 | } |
ollie8 | 0:6f03883f9b43 | 31 | |
ollie8 | 3:51a66c975910 | 32 | bool JAudioStream::read(char *buffer, int size) { |
ollie8 | 6:7e366261cd93 | 33 | TRACE("Received %s", buffer); |
ollie8 | 7:9dfba830a0c8 | 34 | if (socket.receiveFrom(endPoint, buffer, size)) { |
ollie8 | 7:9dfba830a0c8 | 35 | return true; |
ollie8 | 7:9dfba830a0c8 | 36 | } |
ollie8 | 7:9dfba830a0c8 | 37 | return false; |
ollie8 | 0:6f03883f9b43 | 38 | } |
ollie8 | 0:6f03883f9b43 | 39 | |
ollie8 | 0:6f03883f9b43 | 40 | bool JAudioStream::isConnected() { |
ollie8 | 0:6f03883f9b43 | 41 | return connected; |
ollie8 | 0:6f03883f9b43 | 42 | } |
ollie8 | 0:6f03883f9b43 | 43 | |
ollie8 | 0:6f03883f9b43 | 44 | void JAudioStream::request(char amount) { |
ollie8 | 0:6f03883f9b43 | 45 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 46 | buildMeassge(rqst, amount, message); |
ollie8 | 0:6f03883f9b43 | 47 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 48 | } |
ollie8 | 0:6f03883f9b43 | 49 | |
ollie8 | 0:6f03883f9b43 | 50 | void JAudioStream::disconnect() { |
ollie8 | 0:6f03883f9b43 | 51 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 52 | buildMeassge(dcon, -1, message); |
ollie8 | 0:6f03883f9b43 | 53 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 54 | } |
ollie8 | 0:6f03883f9b43 | 55 | |
ollie8 | 6:7e366261cd93 | 56 | void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { |
ollie8 | 2:22ee01f24346 | 57 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 3:51a66c975910 | 58 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 59 | char i; |
ollie8 | 2:22ee01f24346 | 60 | for (i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 61 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 62 | } |
ollie8 | 2:22ee01f24346 | 63 | for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 64 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 65 | } |
ollie8 | 0:6f03883f9b43 | 66 | } |
ollie8 | 0:6f03883f9b43 | 67 | |
ollie8 | 6:7e366261cd93 | 68 | void JAudioStream::buildMeassge(const char* cmd, int param, char* data, char* message) { |
ollie8 | 2:22ee01f24346 | 69 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 70 | char i; |
ollie8 | 2:22ee01f24346 | 71 | for (i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 72 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 73 | } |
ollie8 | 0:6f03883f9b43 | 74 | intTocharArr(param, bParam); |
ollie8 | 2:22ee01f24346 | 75 | for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 76 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 77 | } |
ollie8 | 0:6f03883f9b43 | 78 | char dataSize = MESSAGE_SIZE*2; |
ollie8 | 2:22ee01f24346 | 79 | for (i=MESSAGE_SIZE; i<dataSize; i++) { |
ollie8 | 0:6f03883f9b43 | 80 | message[i] = data[i-MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 81 | } |
ollie8 | 0:6f03883f9b43 | 82 | } |
ollie8 | 0:6f03883f9b43 | 83 | |
ollie8 | 1:b49a6e72f353 | 84 | void JAudioStream::intTocharArr(int value, char* buff) { |
ollie8 | 0:6f03883f9b43 | 85 | buff[0] = (value >> 24) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 86 | buff[1] = (value >> 16) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 87 | buff[2] = (value >> 8) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 88 | buff[3] = value; |
ollie8 | 0:6f03883f9b43 | 89 | } |
ollie8 | 0:6f03883f9b43 | 90 | |
ollie8 | 3:51a66c975910 | 91 | void JAudioStream::send(char* data, int size ) { |
ollie8 | 6:7e366261cd93 | 92 | TRACE("Sending %s", data); |
ollie8 | 3:51a66c975910 | 93 | socket.sendTo(endPoint, data, size); |
ollie8 | 0:6f03883f9b43 | 94 | } |
ollie8 | 0:6f03883f9b43 | 95 | |
ollie8 | 0:6f03883f9b43 | 96 | char* JAudioStream::getNowPlaying() { |
ollie8 | 0:6f03883f9b43 | 97 | return nowPlaying; |
ollie8 | 0:6f03883f9b43 | 98 | } |
ollie8 | 2:22ee01f24346 | 99 | |
ollie8 | 2:22ee01f24346 | 100 | void JAudioStream::receive() { |
ollie8 | 2:22ee01f24346 | 101 | char resp[MAX_PACKT]; |
ollie8 | 7:9dfba830a0c8 | 102 | //if (!inTransmission) { |
ollie8 | 7:9dfba830a0c8 | 103 | if (read(resp, MAX_PACKT)) { |
ollie8 | 7:9dfba830a0c8 | 104 | if (memcmp(resp, cond, 4) == 0) { |
ollie8 | 7:9dfba830a0c8 | 105 | connected = true; |
ollie8 | 7:9dfba830a0c8 | 106 | } else if (memcmp(resp, dctd, 4) == 0) { |
ollie8 | 7:9dfba830a0c8 | 107 | connected = false; |
ollie8 | 7:9dfba830a0c8 | 108 | } else if (memcmp(resp, hrbt, 4) == 0) { |
ollie8 | 7:9dfba830a0c8 | 109 | char message[MESSAGE_SIZE]; |
ollie8 | 7:9dfba830a0c8 | 110 | buildMeassge(hrbt, -1, message); |
ollie8 | 7:9dfba830a0c8 | 111 | send(message, MESSAGE_SIZE); |
ollie8 | 7:9dfba830a0c8 | 112 | } else if (memcmp(resp, begn, 4) == 0) { |
ollie8 | 7:9dfba830a0c8 | 113 | memmove(&nowPlaying[0], &resp[4], 20); |
ollie8 | 7:9dfba830a0c8 | 114 | inTransmission = true; |
ollie8 | 7:9dfba830a0c8 | 115 | } else if (memcmp(resp, comt, 4) == 0) { |
ollie8 | 7:9dfba830a0c8 | 116 | inTransmission = false; |
ollie8 | 7:9dfba830a0c8 | 117 | } |
ollie8 | 7:9dfba830a0c8 | 118 | } |
ollie8 | 7:9dfba830a0c8 | 119 | //} |
ollie8 | 2:22ee01f24346 | 120 | } |