MBED implementation of JAudioStream

Committer:
ollie8
Date:
Mon Aug 17 20:50:43 2015 +0000
Revision:
4:ebdd1cf0c9f7
Parent:
3:51a66c975910
Child:
5:77a5d031f270
added handlers for disconnect and commit messages.

Who changed what in which revision?

UserRevisionLine numberNew 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 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 3:51a66c975910 22 socket.set_blocking(true);
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 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 2:22ee01f24346 53 void JAudioStream::buildMeassge(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 2:22ee01f24346 65 void JAudioStream::buildMeassge(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 3:51a66c975910 89 socket.sendTo(endPoint, data, size);
ollie8 0:6f03883f9b43 90 }
ollie8 0:6f03883f9b43 91
ollie8 0:6f03883f9b43 92 char* JAudioStream::getNowPlaying() {
ollie8 0:6f03883f9b43 93 return nowPlaying;
ollie8 0:6f03883f9b43 94 }
ollie8 2:22ee01f24346 95
ollie8 2:22ee01f24346 96 void JAudioStream::receive() {
ollie8 2:22ee01f24346 97 char resp[MAX_PACKT];
ollie8 3:51a66c975910 98 if (read(resp, MAX_PACKT)) {
ollie8 2:22ee01f24346 99 if (memcmp(resp, cond, 4) == 0) {
ollie8 2:22ee01f24346 100 connected = true;
ollie8 4:ebdd1cf0c9f7 101 } else if (memcmp(resp, dctd, 4) == 0) {
ollie8 4:ebdd1cf0c9f7 102 connected = false;
ollie8 2:22ee01f24346 103 } else if (memcmp(resp, hrbt, 4) == 0) {
ollie8 2:22ee01f24346 104 char message[MESSAGE_SIZE];
ollie8 2:22ee01f24346 105 buildMeassge(hrbt, -1, message);
ollie8 2:22ee01f24346 106 send(message, MESSAGE_SIZE);
ollie8 3:51a66c975910 107 } else if (memcmp(resp, begn, 4) == 0) {
ollie8 3:51a66c975910 108 memmove(&nowPlaying[0], &resp[4], 20);
ollie8 3:51a66c975910 109 inTransmission = true;
ollie8 4:ebdd1cf0c9f7 110 } else if (memcmp(resp, comt, 4) == 0) {
ollie8 4:ebdd1cf0c9f7 111 inTransmission = false;
ollie8 2:22ee01f24346 112 }
ollie8 2:22ee01f24346 113 }
ollie8 2:22ee01f24346 114 }