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