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