Laser Sensing Display for UI interfaces in the real world
Fork of skinGames_forktest by
mbedOSC.h@47:199042980678, 2014-04-17 (annotated)
- Committer:
- mbedalvaro
- Date:
- Thu Apr 17 08:04:14 2014 +0000
- Revision:
- 47:199042980678
- Parent:
- 42:5f21a710ebc5
publishing for sharing with Ken Iwasaki
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 30:d8af03f01cd4 | 1 | /* mbedOSC.h |
mbedalvaro | 30:d8af03f01cd4 | 2 | This is an OSC library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the |
mbedalvaro | 30:d8af03f01cd4 | 3 | Arduino with Ethernet shield. I have also used parts of the OSC Transceiver(Sender/Receiver) code by xshige |
mbedalvaro | 30:d8af03f01cd4 | 4 | written by: Alvaro Cassinelli, 7.10.2011 |
mbedalvaro | 30:d8af03f01cd4 | 5 | |
mbedalvaro | 30:d8af03f01cd4 | 6 | This library is free software; you can redistribute it and/or |
mbedalvaro | 30:d8af03f01cd4 | 7 | modify it under the terms of the GNU Lesser General Public |
mbedalvaro | 30:d8af03f01cd4 | 8 | License version 2.1 as published by the Free Software Foundation. |
mbedalvaro | 30:d8af03f01cd4 | 9 | Open Sound Control http://opensoundcontrol.org/ |
mbedalvaro | 30:d8af03f01cd4 | 10 | |
mbedalvaro | 30:d8af03f01cd4 | 11 | mbedOSC version 0.1 Specification (similar to Recotana's OSCClass library) |
mbedalvaro | 30:d8af03f01cd4 | 12 | |
mbedalvaro | 30:d8af03f01cd4 | 13 | ******** |
mbedalvaro | 30:d8af03f01cd4 | 14 | Address : max 2 |
mbedalvaro | 30:d8af03f01cd4 | 15 | "/ard" |
mbedalvaro | 30:d8af03f01cd4 | 16 | "/ard/output" --address[0]="/ard" :max 15character |
mbedalvaro | 30:d8af03f01cd4 | 17 | --address[1]="/output" :max 15character |
mbedalvaro | 30:d8af03f01cd4 | 18 | |
mbedalvaro | 30:d8af03f01cd4 | 19 | ******* |
mbedalvaro | 30:d8af03f01cd4 | 20 | TypeTag : max 2 |
mbedalvaro | 30:d8af03f01cd4 | 21 | |
mbedalvaro | 30:d8af03f01cd4 | 22 | "i" - long or unsigned long |
mbedalvaro | 30:d8af03f01cd4 | 23 | "f" - double |
mbedalvaro | 30:d8af03f01cd4 | 24 | |
mbedalvaro | 30:d8af03f01cd4 | 25 | ******* |
mbedalvaro | 30:d8af03f01cd4 | 26 | arg : max 2 |
mbedalvaro | 30:d8af03f01cd4 | 27 | |
mbedalvaro | 30:d8af03f01cd4 | 28 | ******* |
mbedalvaro | 30:d8af03f01cd4 | 29 | Example of an OSC message: "/mbed/test1, if 50 32.4" (Note: this is not the byte string |
mbedalvaro | 30:d8af03f01cd4 | 30 | sent as UDP packet - there are no spaces, and arguments are in binary, BIG ENDIAN) |
mbedalvaro | 30:d8af03f01cd4 | 31 | */ |
mbedalvaro | 30:d8af03f01cd4 | 32 | |
mbedalvaro | 30:d8af03f01cd4 | 33 | #ifndef mbedOSC_h |
mbedalvaro | 30:d8af03f01cd4 | 34 | #define mbedOSC_h |
mbedalvaro | 30:d8af03f01cd4 | 35 | |
mbedalvaro | 30:d8af03f01cd4 | 36 | #include "mbed.h" |
mbedalvaro | 30:d8af03f01cd4 | 37 | #include "EthernetNetIf.h" |
mbedalvaro | 30:d8af03f01cd4 | 38 | #include "UDPSocket.h" |
mbedalvaro | 30:d8af03f01cd4 | 39 | |
mbedalvaro | 30:d8af03f01cd4 | 40 | // setup IP of destination (computer): |
mbedalvaro | 30:d8af03f01cd4 | 41 | #define DEFAULT_SEND_PORT 12000 |
mbedalvaro | 30:d8af03f01cd4 | 42 | //Host sendHost(IpAddr(10, 0, 0, 1), DEFAULT_SEND_PORT, NULL); // Send Port |
mbedalvaro | 30:d8af03f01cd4 | 43 | // set IP of origin of UDP packets - the mbed acts as a SERVER here, and needs to bind the socket to the "client" (the computer) |
mbedalvaro | 30:d8af03f01cd4 | 44 | #define DEFAULT_RECEIVE_PORT 57130 |
mbedalvaro | 30:d8af03f01cd4 | 45 | //Host recHost(IpAddr(10, 0, 0, 1), DEFAULT_RECEIVE_PORT, NULL); // Receive Port |
mbedalvaro | 30:d8af03f01cd4 | 46 | //UDPSocket udpRec,udpSend; |
mbedalvaro | 30:d8af03f01cd4 | 47 | |
mbedalvaro | 30:d8af03f01cd4 | 48 | |
mbedalvaro | 30:d8af03f01cd4 | 49 | #define MAX_ADDRESS 2 |
mbedalvaro | 30:d8af03f01cd4 | 50 | #define MAX_ARG 2 |
mbedalvaro | 30:d8af03f01cd4 | 51 | |
mbedalvaro | 30:d8af03f01cd4 | 52 | #define TYPE_INT 1 |
mbedalvaro | 30:d8af03f01cd4 | 53 | #define TYPE_FLOAT 2 |
mbedalvaro | 30:d8af03f01cd4 | 54 | #define TYPE_BLOB 3 |
mbedalvaro | 30:d8af03f01cd4 | 55 | |
mbedalvaro | 30:d8af03f01cd4 | 56 | |
mbedalvaro | 30:d8af03f01cd4 | 57 | /* |
mbedalvaro | 30:d8af03f01cd4 | 58 | Container class for OSC messages (receiving or sending) |
mbedalvaro | 30:d8af03f01cd4 | 59 | */ |
mbedalvaro | 30:d8af03f01cd4 | 60 | class OSCMessage{ |
mbedalvaro | 30:d8af03f01cd4 | 61 | |
mbedalvaro | 30:d8af03f01cd4 | 62 | private: |
mbedalvaro | 30:d8af03f01cd4 | 63 | |
mbedalvaro | 30:d8af03f01cd4 | 64 | char *address[MAX_ADDRESS]; // these are strings (as char*) |
mbedalvaro | 30:d8af03f01cd4 | 65 | uint8_t addressNum; // current number of addresses in the message (ex: "/ard/test" --> the number of the addresses is 2) |
mbedalvaro | 30:d8af03f01cd4 | 66 | |
mbedalvaro | 30:d8af03f01cd4 | 67 | char typeTag[MAX_ARG]; |
mbedalvaro | 30:d8af03f01cd4 | 68 | |
mbedalvaro | 30:d8af03f01cd4 | 69 | void *arg[MAX_ARG]; |
mbedalvaro | 30:d8af03f01cd4 | 70 | uint8_t argNum; |
mbedalvaro | 30:d8af03f01cd4 | 71 | |
mbedalvaro | 30:d8af03f01cd4 | 72 | // Information about the connection: |
mbedalvaro | 30:d8af03f01cd4 | 73 | //uint8_t ip[4]; |
mbedalvaro | 30:d8af03f01cd4 | 74 | //uint16_t port; |
mbedalvaro | 30:d8af03f01cd4 | 75 | Host host; |
mbedalvaro | 30:d8af03f01cd4 | 76 | |
mbedalvaro | 30:d8af03f01cd4 | 77 | public: |
mbedalvaro | 30:d8af03f01cd4 | 78 | |
mbedalvaro | 30:d8af03f01cd4 | 79 | OSCMessage(); |
mbedalvaro | 30:d8af03f01cd4 | 80 | |
mbedalvaro | 30:d8af03f01cd4 | 81 | const IpAddr& getIp(); // return IpAddr object |
mbedalvaro | 30:d8af03f01cd4 | 82 | const int& getPort(); // return port |
mbedalvaro | 30:d8af03f01cd4 | 83 | |
mbedalvaro | 30:d8af03f01cd4 | 84 | //ex. address patern "/adr/test" |
mbedalvaro | 42:5f21a710ebc5 | 85 | // address[2]={"/adr" , "/test"} |
mbedalvaro | 30:d8af03f01cd4 | 86 | char *getAddress(uint8_t _index); //retturn address |
mbedalvaro | 30:d8af03f01cd4 | 87 | char *getTopAddress(); //return address[0] :"/ard" |
mbedalvaro | 30:d8af03f01cd4 | 88 | char *getSubAddress(); //return address[1] :"/test" |
mbedalvaro | 30:d8af03f01cd4 | 89 | uint8_t getAddressNum(); //return 2 |
mbedalvaro | 30:d8af03f01cd4 | 90 | |
mbedalvaro | 30:d8af03f01cd4 | 91 | // 'i': long(int32_t) |
mbedalvaro | 30:d8af03f01cd4 | 92 | // 'f': double |
mbedalvaro | 30:d8af03f01cd4 | 93 | //ex 'if' 123 54.21 |
mbedalvaro | 30:d8af03f01cd4 | 94 | char getTypeTag(uint8_t _index); //_index=0 ->'i' |
mbedalvaro | 30:d8af03f01cd4 | 95 | //_index=1 ->'f' |
mbedalvaro | 30:d8af03f01cd4 | 96 | |
mbedalvaro | 30:d8af03f01cd4 | 97 | uint8_t getArgNum(); //return 2 |
mbedalvaro | 30:d8af03f01cd4 | 98 | |
mbedalvaro | 42:5f21a710ebc5 | 99 | int32_t getArgInt(uint8_t _index); //_index=0 -> 123 |
mbedalvaro | 30:d8af03f01cd4 | 100 | double getArgFloat(uint8_t _index); //_index=1 -> 54.21 |
mbedalvaro | 30:d8af03f01cd4 | 101 | |
mbedalvaro | 30:d8af03f01cd4 | 102 | |
mbedalvaro | 30:d8af03f01cd4 | 103 | void setTopAddress(char *_address); //set address[0] |
mbedalvaro | 30:d8af03f01cd4 | 104 | void setSubAddress(char *_address); //set address[1] |
mbedalvaro | 30:d8af03f01cd4 | 105 | void setAddress(char *_topAddress, |
mbedalvaro | 30:d8af03f01cd4 | 106 | char *_subAddress); |
mbedalvaro | 30:d8af03f01cd4 | 107 | void setAddress(uint8_t _index, //set 0,address[0] |
mbedalvaro | 30:d8af03f01cd4 | 108 | char *_address); |
mbedalvaro | 30:d8af03f01cd4 | 109 | //set 1,address[1] |
mbedalvaro | 30:d8af03f01cd4 | 110 | |
mbedalvaro | 30:d8af03f01cd4 | 111 | void setIp( uint8_t *_ip ); //set ip |
mbedalvaro | 30:d8af03f01cd4 | 112 | |
mbedalvaro | 30:d8af03f01cd4 | 113 | void setIp(uint8_t _ip1, //set(192, |
mbedalvaro | 30:d8af03f01cd4 | 114 | uint8_t _ip2, // 168, |
mbedalvaro | 30:d8af03f01cd4 | 115 | uint8_t _ip3, // 0, |
mbedalvaro | 30:d8af03f01cd4 | 116 | uint8_t _ip4); // 100) |
mbedalvaro | 30:d8af03f01cd4 | 117 | |
mbedalvaro | 30:d8af03f01cd4 | 118 | void setPort( uint16_t _port ); |
mbedalvaro | 30:d8af03f01cd4 | 119 | |
mbedalvaro | 30:d8af03f01cd4 | 120 | //ex. long v1=100 |
mbedalvaro | 30:d8af03f01cd4 | 121 | // double v2=123.21 |
mbedalvaro | 30:d8af03f01cd4 | 122 | void setArgs( char *types , ... ); //set ("if",&v1,&v2) |
mbedalvaro | 30:d8af03f01cd4 | 123 | void clearArgs(); // clear arguments |
mbedalvaro | 30:d8af03f01cd4 | 124 | |
mbedalvaro | 30:d8af03f01cd4 | 125 | friend class OSCClass; |
mbedalvaro | 30:d8af03f01cd4 | 126 | |
mbedalvaro | 30:d8af03f01cd4 | 127 | }; |
mbedalvaro | 30:d8af03f01cd4 | 128 | |
mbedalvaro | 30:d8af03f01cd4 | 129 | |
mbedalvaro | 30:d8af03f01cd4 | 130 | |
mbedalvaro | 30:d8af03f01cd4 | 131 | /* ==================================== OSCClass for sending and receiving OSC messages using UDP protocol ===================================== */ |
mbedalvaro | 30:d8af03f01cd4 | 132 | |
mbedalvaro | 30:d8af03f01cd4 | 133 | #include "UDPSocket.h" |
mbedalvaro | 30:d8af03f01cd4 | 134 | |
mbedalvaro | 30:d8af03f01cd4 | 135 | class OSCClass { |
mbedalvaro | 30:d8af03f01cd4 | 136 | |
mbedalvaro | 30:d8af03f01cd4 | 137 | private: |
mbedalvaro | 30:d8af03f01cd4 | 138 | |
mbedalvaro | 30:d8af03f01cd4 | 139 | UDPSocket udpRec,udpSend; |
mbedalvaro | 30:d8af03f01cd4 | 140 | char rcvBuff[256]; // raw buffer for UDP packets (udpRec.recvfrom( buf, 256, &host ) )) |
mbedalvaro | 30:d8af03f01cd4 | 141 | int buflength; |
mbedalvaro | 30:d8af03f01cd4 | 142 | |
mbedalvaro | 30:d8af03f01cd4 | 143 | OSCMessage *receiverMessage; |
mbedalvaro | 30:d8af03f01cd4 | 144 | OSCMessage *sendContainer; |
mbedalvaro | 30:d8af03f01cd4 | 145 | |
mbedalvaro | 42:5f21a710ebc5 | 146 | char tempAddress[MAX_ADDRESS][32]; |
mbedalvaro | 42:5f21a710ebc5 | 147 | uint8_t tempArg[MAX_ARG][8]; |
mbedalvaro | 30:d8af03f01cd4 | 148 | |
mbedalvaro | 30:d8af03f01cd4 | 149 | void decodePacket( OSCMessage *_mes); // makes OSC message from packet |
mbedalvaro | 30:d8af03f01cd4 | 150 | |
mbedalvaro | 30:d8af03f01cd4 | 151 | public: |
mbedalvaro | 30:d8af03f01cd4 | 152 | |
mbedalvaro | 30:d8af03f01cd4 | 153 | OSCClass(); |
mbedalvaro | 30:d8af03f01cd4 | 154 | OSCClass(OSCMessage *_mes); // set the receiver message container |
mbedalvaro | 30:d8af03f01cd4 | 155 | void onUDPSocketEvent(UDPSocketEvent e); |
mbedalvaro | 30:d8af03f01cd4 | 156 | |
mbedalvaro | 30:d8af03f01cd4 | 157 | //init osc |
mbedalvaro | 30:d8af03f01cd4 | 158 | void begin(void (*fp)(UDPSocketEvent)); |
mbedalvaro | 30:d8af03f01cd4 | 159 | void begin(uint16_t _recievePort, void (*fp)(UDPSocketEvent)); |
mbedalvaro | 30:d8af03f01cd4 | 160 | void stop(); |
mbedalvaro | 30:d8af03f01cd4 | 161 | |
mbedalvaro | 30:d8af03f01cd4 | 162 | //new OSC data in the receiver message container: |
mbedalvaro | 30:d8af03f01cd4 | 163 | bool newMessage; |
mbedalvaro | 30:d8af03f01cd4 | 164 | |
mbedalvaro | 30:d8af03f01cd4 | 165 | void setReceiveMessage( OSCMessage *_mes ); //set receive OSCmessage container (note: the message has a "host" object from which we get the upd packets) |
mbedalvaro | 30:d8af03f01cd4 | 166 | OSCMessage *getMessage(); //return received OSCmessage |
mbedalvaro | 30:d8af03f01cd4 | 167 | |
mbedalvaro | 30:d8af03f01cd4 | 168 | //buffer clear |
mbedalvaro | 30:d8af03f01cd4 | 169 | //void flush(); |
mbedalvaro | 30:d8af03f01cd4 | 170 | |
mbedalvaro | 30:d8af03f01cd4 | 171 | //OSC send |
mbedalvaro | 30:d8af03f01cd4 | 172 | void sendOsc( OSCMessage *_mes ); //set&send OSCmessage (note: it will be sent to the host defined in the message container) |
mbedalvaro | 30:d8af03f01cd4 | 173 | |
mbedalvaro | 30:d8af03f01cd4 | 174 | // AND THIS IS BAD, need a better function: |
mbedalvaro | 30:d8af03f01cd4 | 175 | void sendOscBlob( uint8_t * myblob, int sizeblob, OSCMessage *_mes ); |
mbedalvaro | 30:d8af03f01cd4 | 176 | void sendOscString( uint8_t * myblob, int sizeblo, OSCMessage *_mesb ); |
mbedalvaro | 30:d8af03f01cd4 | 177 | }; |
mbedalvaro | 30:d8af03f01cd4 | 178 | |
mbedalvaro | 30:d8af03f01cd4 | 179 | #endif |