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