MBED implementation of JAudioStream
JAudioStream.cpp@6:7e366261cd93, 2015-08-19 (annotated)
- Committer:
- ollie8
- Date:
- Wed Aug 19 09:04:41 2015 +0000
- Revision:
- 6:7e366261cd93
- Parent:
- 5:77a5d031f270
- Child:
- 7:9dfba830a0c8
Changed message string to constants
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 | #include "logger.h" |
ollie8 | 0:6f03883f9b43 | 3 | |
ollie8 | 0:6f03883f9b43 | 4 | JAudioStream::JAudioStream() { |
ollie8 | 2:22ee01f24346 | 5 | cont = "cont"; |
ollie8 | 2:22ee01f24346 | 6 | cond = "cond"; |
ollie8 | 2:22ee01f24346 | 7 | dcon = "dcon"; |
ollie8 | 4:ebdd1cf0c9f7 | 8 | dctd = "dctd"; |
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 | 3:51a66c975910 | 20 | socket.init(); |
ollie8 | 3:51a66c975910 | 21 | socket.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 | 3:51a66c975910 | 25 | char message[MESSAGE_SIZE*2]; |
ollie8 | 0:6f03883f9b43 | 26 | buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); |
ollie8 | 3:51a66c975910 | 27 | send(message, MESSAGE_SIZE*2); |
ollie8 | 0:6f03883f9b43 | 28 | return connected; |
ollie8 | 0:6f03883f9b43 | 29 | } |
ollie8 | 0:6f03883f9b43 | 30 | |
ollie8 | 3:51a66c975910 | 31 | bool JAudioStream::read(char *buffer, int size) { |
ollie8 | 6:7e366261cd93 | 32 | TRACE("Received %s", buffer); |
ollie8 | 3:51a66c975910 | 33 | socket.receiveFrom(endPoint, buffer, size); |
ollie8 | 3:51a66c975910 | 34 | return true; |
ollie8 | 0:6f03883f9b43 | 35 | } |
ollie8 | 0:6f03883f9b43 | 36 | |
ollie8 | 0:6f03883f9b43 | 37 | bool JAudioStream::isConnected() { |
ollie8 | 0:6f03883f9b43 | 38 | return connected; |
ollie8 | 0:6f03883f9b43 | 39 | } |
ollie8 | 0:6f03883f9b43 | 40 | |
ollie8 | 0:6f03883f9b43 | 41 | void JAudioStream::request(char amount) { |
ollie8 | 0:6f03883f9b43 | 42 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 43 | buildMeassge(rqst, amount, message); |
ollie8 | 0:6f03883f9b43 | 44 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 45 | } |
ollie8 | 0:6f03883f9b43 | 46 | |
ollie8 | 0:6f03883f9b43 | 47 | void JAudioStream::disconnect() { |
ollie8 | 0:6f03883f9b43 | 48 | char message[MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 49 | buildMeassge(dcon, -1, message); |
ollie8 | 0:6f03883f9b43 | 50 | send(message, MESSAGE_SIZE); |
ollie8 | 0:6f03883f9b43 | 51 | } |
ollie8 | 0:6f03883f9b43 | 52 | |
ollie8 | 6:7e366261cd93 | 53 | void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { |
ollie8 | 2:22ee01f24346 | 54 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 3:51a66c975910 | 55 | intTocharArr(param, bParam); |
ollie8 | 0:6f03883f9b43 | 56 | char i; |
ollie8 | 2:22ee01f24346 | 57 | for (i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 58 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 59 | } |
ollie8 | 2:22ee01f24346 | 60 | for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 61 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 62 | } |
ollie8 | 0:6f03883f9b43 | 63 | } |
ollie8 | 0:6f03883f9b43 | 64 | |
ollie8 | 6:7e366261cd93 | 65 | void JAudioStream::buildMeassge(const char* cmd, int param, char* data, char* message) { |
ollie8 | 2:22ee01f24346 | 66 | char bParam[MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 67 | char i; |
ollie8 | 2:22ee01f24346 | 68 | for (i=0; i<MESSAGE_COMP_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 69 | message[i] = cmd[i]; |
ollie8 | 0:6f03883f9b43 | 70 | } |
ollie8 | 0:6f03883f9b43 | 71 | intTocharArr(param, bParam); |
ollie8 | 2:22ee01f24346 | 72 | for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { |
ollie8 | 2:22ee01f24346 | 73 | message[i] = bParam[i-MESSAGE_COMP_SIZE]; |
ollie8 | 0:6f03883f9b43 | 74 | } |
ollie8 | 0:6f03883f9b43 | 75 | char dataSize = MESSAGE_SIZE*2; |
ollie8 | 2:22ee01f24346 | 76 | for (i=MESSAGE_SIZE; i<dataSize; i++) { |
ollie8 | 0:6f03883f9b43 | 77 | message[i] = data[i-MESSAGE_SIZE]; |
ollie8 | 0:6f03883f9b43 | 78 | } |
ollie8 | 0:6f03883f9b43 | 79 | } |
ollie8 | 0:6f03883f9b43 | 80 | |
ollie8 | 1:b49a6e72f353 | 81 | void JAudioStream::intTocharArr(int value, char* buff) { |
ollie8 | 0:6f03883f9b43 | 82 | buff[0] = (value >> 24) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 83 | buff[1] = (value >> 16) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 84 | buff[2] = (value >> 8) & 0xFF; |
ollie8 | 0:6f03883f9b43 | 85 | buff[3] = value; |
ollie8 | 0:6f03883f9b43 | 86 | } |
ollie8 | 0:6f03883f9b43 | 87 | |
ollie8 | 3:51a66c975910 | 88 | void JAudioStream::send(char* data, int size ) { |
ollie8 | 6:7e366261cd93 | 89 | TRACE("Sending %s", data); |
ollie8 | 3:51a66c975910 | 90 | socket.sendTo(endPoint, data, size); |
ollie8 | 0:6f03883f9b43 | 91 | } |
ollie8 | 0:6f03883f9b43 | 92 | |
ollie8 | 0:6f03883f9b43 | 93 | char* JAudioStream::getNowPlaying() { |
ollie8 | 0:6f03883f9b43 | 94 | return nowPlaying; |
ollie8 | 0:6f03883f9b43 | 95 | } |
ollie8 | 2:22ee01f24346 | 96 | |
ollie8 | 2:22ee01f24346 | 97 | void JAudioStream::receive() { |
ollie8 | 2:22ee01f24346 | 98 | char resp[MAX_PACKT]; |
ollie8 | 3:51a66c975910 | 99 | if (read(resp, MAX_PACKT)) { |
ollie8 | 2:22ee01f24346 | 100 | if (memcmp(resp, cond, 4) == 0) { |
ollie8 | 2:22ee01f24346 | 101 | connected = true; |
ollie8 | 4:ebdd1cf0c9f7 | 102 | } else if (memcmp(resp, dctd, 4) == 0) { |
ollie8 | 4:ebdd1cf0c9f7 | 103 | connected = false; |
ollie8 | 2:22ee01f24346 | 104 | } else if (memcmp(resp, hrbt, 4) == 0) { |
ollie8 | 2:22ee01f24346 | 105 | char message[MESSAGE_SIZE]; |
ollie8 | 2:22ee01f24346 | 106 | buildMeassge(hrbt, -1, message); |
ollie8 | 2:22ee01f24346 | 107 | send(message, MESSAGE_SIZE); |
ollie8 | 3:51a66c975910 | 108 | } else if (memcmp(resp, begn, 4) == 0) { |
ollie8 | 3:51a66c975910 | 109 | memmove(&nowPlaying[0], &resp[4], 20); |
ollie8 | 3:51a66c975910 | 110 | inTransmission = true; |
ollie8 | 4:ebdd1cf0c9f7 | 111 | } else if (memcmp(resp, comt, 4) == 0) { |
ollie8 | 4:ebdd1cf0c9f7 | 112 | inTransmission = false; |
ollie8 | 2:22ee01f24346 | 113 | } |
ollie8 | 2:22ee01f24346 | 114 | } |
ollie8 | 2:22ee01f24346 | 115 | } |