![](/media/cache/profiles/531a7a573b64cf81c7dfbdbfe8ab8c6f.jpg.50x50_q85.jpg)
LineLedControl
Dependencies: EthernetNetIf LPD8806 mbed
Fork of OSCReceiver by
main.cpp@1:e0714e1a1568, 2013-08-26 (annotated)
- Committer:
- sfjmt
- Date:
- Mon Aug 26 17:22:36 2013 +0000
- Revision:
- 1:e0714e1a1568
line led control
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sfjmt | 1:e0714e1a1568 | 1 | #include "mbed.h" |
sfjmt | 1:e0714e1a1568 | 2 | #include "EthernetNetIf.h" |
sfjmt | 1:e0714e1a1568 | 3 | #include "UDPSocket.h" |
sfjmt | 1:e0714e1a1568 | 4 | #include "Control.h" |
sfjmt | 1:e0714e1a1568 | 5 | |
sfjmt | 1:e0714e1a1568 | 6 | extern "C" void mbed_reset(); |
sfjmt | 1:e0714e1a1568 | 7 | |
sfjmt | 1:e0714e1a1568 | 8 | //#define |
sfjmt | 1:e0714e1a1568 | 9 | #define INPUT_PORT 5678 |
sfjmt | 1:e0714e1a1568 | 10 | |
sfjmt | 1:e0714e1a1568 | 11 | #ifdef DHCP |
sfjmt | 1:e0714e1a1568 | 12 | EthernetNetIf eth; |
sfjmt | 1:e0714e1a1568 | 13 | #else |
sfjmt | 1:e0714e1a1568 | 14 | EthernetNetIf eth( |
sfjmt | 1:e0714e1a1568 | 15 | IpAddr(192,168,12,210), //IP Address |
sfjmt | 1:e0714e1a1568 | 16 | IpAddr(255,255,255,0), //Network Mask |
sfjmt | 1:e0714e1a1568 | 17 | IpAddr(), //Gateway |
sfjmt | 1:e0714e1a1568 | 18 | IpAddr() //DNS |
sfjmt | 1:e0714e1a1568 | 19 | ); |
sfjmt | 1:e0714e1a1568 | 20 | #endif |
sfjmt | 1:e0714e1a1568 | 21 | |
sfjmt | 1:e0714e1a1568 | 22 | Serial pc(USBTX, USBRX); |
sfjmt | 1:e0714e1a1568 | 23 | |
sfjmt | 1:e0714e1a1568 | 24 | LedControl ledControl; |
sfjmt | 1:e0714e1a1568 | 25 | |
sfjmt | 1:e0714e1a1568 | 26 | DigitalOut led1(LED1); |
sfjmt | 1:e0714e1a1568 | 27 | |
sfjmt | 1:e0714e1a1568 | 28 | //--- OSC related stuff --- |
sfjmt | 1:e0714e1a1568 | 29 | union OSCarg { |
sfjmt | 1:e0714e1a1568 | 30 | // char*, int and float are assumed four bytes |
sfjmt | 1:e0714e1a1568 | 31 | char *address; |
sfjmt | 1:e0714e1a1568 | 32 | char *typeTag; |
sfjmt | 1:e0714e1a1568 | 33 | int i; |
sfjmt | 1:e0714e1a1568 | 34 | float f; |
sfjmt | 1:e0714e1a1568 | 35 | char *s; |
sfjmt | 1:e0714e1a1568 | 36 | struct { |
sfjmt | 1:e0714e1a1568 | 37 | int len; // is "int i" |
sfjmt | 1:e0714e1a1568 | 38 | char *p; |
sfjmt | 1:e0714e1a1568 | 39 | } blob; |
sfjmt | 1:e0714e1a1568 | 40 | char m[4]; // for MIDI |
sfjmt | 1:e0714e1a1568 | 41 | char _b[4]; // endian conversion temp variable |
sfjmt | 1:e0714e1a1568 | 42 | }; |
sfjmt | 1:e0714e1a1568 | 43 | |
sfjmt | 1:e0714e1a1568 | 44 | void getOSCmsg(char *packet , union OSCarg *msg){ |
sfjmt | 1:e0714e1a1568 | 45 | // Caution: the returned result points to packet as blobs or strings (not newly allocatd) |
sfjmt | 1:e0714e1a1568 | 46 | char *p, *typeTag; char c; |
sfjmt | 1:e0714e1a1568 | 47 | |
sfjmt | 1:e0714e1a1568 | 48 | msg[0].address = packet; // address |
sfjmt | 1:e0714e1a1568 | 49 | msg[1].typeTag = packet+4*(strlen(msg[0].s)/4+1);//typeTag |
sfjmt | 1:e0714e1a1568 | 50 | typeTag=msg[1].s+1; // skip ',' |
sfjmt | 1:e0714e1a1568 | 51 | p= msg[1].s+4*(strlen(msg[1].s)/4+1); |
sfjmt | 1:e0714e1a1568 | 52 | for(int n=0; n<strlen(typeTag); n++){ |
sfjmt | 1:e0714e1a1568 | 53 | c = typeTag[n]; |
sfjmt | 1:e0714e1a1568 | 54 | if (('s'==c)) { |
sfjmt | 1:e0714e1a1568 | 55 | msg[n+2].s=p; |
sfjmt | 1:e0714e1a1568 | 56 | p += 4*(strlen(msg[n+2].s)/4+1); |
sfjmt | 1:e0714e1a1568 | 57 | } else if (('i'==c)||('f'==c)) { |
sfjmt | 1:e0714e1a1568 | 58 | // chang endian (big to little) |
sfjmt | 1:e0714e1a1568 | 59 | msg[n+2]._b[3]=p[0]; |
sfjmt | 1:e0714e1a1568 | 60 | msg[n+2]._b[2]=p[1]; |
sfjmt | 1:e0714e1a1568 | 61 | msg[n+2]._b[1]=p[2]; |
sfjmt | 1:e0714e1a1568 | 62 | msg[n+2]._b[0]=p[3]; |
sfjmt | 1:e0714e1a1568 | 63 | p +=4; |
sfjmt | 1:e0714e1a1568 | 64 | } else if ('b'==c) { |
sfjmt | 1:e0714e1a1568 | 65 | // chang endian (big to little) |
sfjmt | 1:e0714e1a1568 | 66 | // get lenth of blog (copy to msg[n].blog.len) |
sfjmt | 1:e0714e1a1568 | 67 | msg[n+2]._b[3]=p[0]; |
sfjmt | 1:e0714e1a1568 | 68 | msg[n+2]._b[2]=p[1]; |
sfjmt | 1:e0714e1a1568 | 69 | msg[n+2]._b[1]=p[2]; |
sfjmt | 1:e0714e1a1568 | 70 | msg[n+2]._b[0]=p[3]; |
sfjmt | 1:e0714e1a1568 | 71 | p +=4; |
sfjmt | 1:e0714e1a1568 | 72 | // get ponter of blog (copy to msg[n].blog.p) |
sfjmt | 1:e0714e1a1568 | 73 | msg[n+2].blob.p=p; |
sfjmt | 1:e0714e1a1568 | 74 | p += 4*(msg[n+2].blob.len/4+1); |
sfjmt | 1:e0714e1a1568 | 75 | } else if ('m'==c) { |
sfjmt | 1:e0714e1a1568 | 76 | // get midi data (copy to msg[n].m[]) |
sfjmt | 1:e0714e1a1568 | 77 | msg[n+2].m[0]=p[0]; |
sfjmt | 1:e0714e1a1568 | 78 | msg[n+2].m[1]=p[1]; |
sfjmt | 1:e0714e1a1568 | 79 | msg[n+2].m[2]=p[2]; |
sfjmt | 1:e0714e1a1568 | 80 | msg[n+2].m[3]=p[3]; |
sfjmt | 1:e0714e1a1568 | 81 | p +=4; |
sfjmt | 1:e0714e1a1568 | 82 | } else { |
sfjmt | 1:e0714e1a1568 | 83 | printf("*** Not Supported TypeTag:%s ****\n",typeTag); |
sfjmt | 1:e0714e1a1568 | 84 | } |
sfjmt | 1:e0714e1a1568 | 85 | }; |
sfjmt | 1:e0714e1a1568 | 86 | } |
sfjmt | 1:e0714e1a1568 | 87 | //------------------------------------------- |
sfjmt | 1:e0714e1a1568 | 88 | |
sfjmt | 1:e0714e1a1568 | 89 | UDPSocket udp; |
sfjmt | 1:e0714e1a1568 | 90 | |
sfjmt | 1:e0714e1a1568 | 91 | void onUDPSocketEvent(UDPSocketEvent e) |
sfjmt | 1:e0714e1a1568 | 92 | { |
sfjmt | 1:e0714e1a1568 | 93 | union OSCarg msg[10]; |
sfjmt | 1:e0714e1a1568 | 94 | |
sfjmt | 1:e0714e1a1568 | 95 | printf("receive."); |
sfjmt | 1:e0714e1a1568 | 96 | |
sfjmt | 1:e0714e1a1568 | 97 | switch(e) |
sfjmt | 1:e0714e1a1568 | 98 | { |
sfjmt | 1:e0714e1a1568 | 99 | case UDPSOCKET_READABLE: //The only event for now |
sfjmt | 1:e0714e1a1568 | 100 | char buf[256] = {0}; |
sfjmt | 1:e0714e1a1568 | 101 | Host host; |
sfjmt | 1:e0714e1a1568 | 102 | while( int len = udp.recvfrom( buf, 256, &host ) ) |
sfjmt | 1:e0714e1a1568 | 103 | { |
sfjmt | 1:e0714e1a1568 | 104 | if( len <= 0 ) |
sfjmt | 1:e0714e1a1568 | 105 | break; |
sfjmt | 1:e0714e1a1568 | 106 | printf("\r\nFrom %d.%d.%d.%d:\r\n", |
sfjmt | 1:e0714e1a1568 | 107 | host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]); |
sfjmt | 1:e0714e1a1568 | 108 | |
sfjmt | 1:e0714e1a1568 | 109 | getOSCmsg(buf,msg); |
sfjmt | 1:e0714e1a1568 | 110 | |
sfjmt | 1:e0714e1a1568 | 111 | if(msg[2].i==101) |
sfjmt | 1:e0714e1a1568 | 112 | { |
sfjmt | 1:e0714e1a1568 | 113 | ledControl.colorChase(127,127,127,3);//(r,g,b,delay) |
sfjmt | 1:e0714e1a1568 | 114 | |
sfjmt | 1:e0714e1a1568 | 115 | // colorChase(strip.Color(127,127,127), 3);//white |
sfjmt | 1:e0714e1a1568 | 116 | // led = 1; |
sfjmt | 1:e0714e1a1568 | 117 | // wait(0.2); |
sfjmt | 1:e0714e1a1568 | 118 | // led = 0; |
sfjmt | 1:e0714e1a1568 | 119 | printf("led blink complete."); |
sfjmt | 1:e0714e1a1568 | 120 | } |
sfjmt | 1:e0714e1a1568 | 121 | |
sfjmt | 1:e0714e1a1568 | 122 | printf("OSCmsg >>> %s %s %d\n", |
sfjmt | 1:e0714e1a1568 | 123 | msg[0].address, msg[1].typeTag,msg[2].i); |
sfjmt | 1:e0714e1a1568 | 124 | } |
sfjmt | 1:e0714e1a1568 | 125 | break; |
sfjmt | 1:e0714e1a1568 | 126 | } |
sfjmt | 1:e0714e1a1568 | 127 | } |
sfjmt | 1:e0714e1a1568 | 128 | |
sfjmt | 1:e0714e1a1568 | 129 | int main() |
sfjmt | 1:e0714e1a1568 | 130 | { |
sfjmt | 1:e0714e1a1568 | 131 | printf("Ethernet Setup..............................\r\n"); |
sfjmt | 1:e0714e1a1568 | 132 | EthernetErr ethErr = eth.setup(); |
sfjmt | 1:e0714e1a1568 | 133 | if(ethErr) |
sfjmt | 1:e0714e1a1568 | 134 | { |
sfjmt | 1:e0714e1a1568 | 135 | printf("Error %d in setup.\r\n", ethErr); |
sfjmt | 1:e0714e1a1568 | 136 | return -1; |
sfjmt | 1:e0714e1a1568 | 137 | } |
sfjmt | 1:e0714e1a1568 | 138 | printf("Ethernet Setup Complete..............................\r\n"); |
sfjmt | 1:e0714e1a1568 | 139 | |
sfjmt | 1:e0714e1a1568 | 140 | printf("Address&Port Setup..............................\r\n"); |
sfjmt | 1:e0714e1a1568 | 141 | // port setup |
sfjmt | 1:e0714e1a1568 | 142 | Host recHost(IpAddr(192, 168, 12, 210), INPUT_PORT, NULL); |
sfjmt | 1:e0714e1a1568 | 143 | udp.setOnEvent(&onUDPSocketEvent); |
sfjmt | 1:e0714e1a1568 | 144 | udp.bind(recHost); |
sfjmt | 1:e0714e1a1568 | 145 | printf("Address&Port Setup Complete..............................\r\n"); |
sfjmt | 1:e0714e1a1568 | 146 | |
sfjmt | 1:e0714e1a1568 | 147 | while(true) |
sfjmt | 1:e0714e1a1568 | 148 | { |
sfjmt | 1:e0714e1a1568 | 149 | Net::poll(); |
sfjmt | 1:e0714e1a1568 | 150 | |
sfjmt | 1:e0714e1a1568 | 151 | printf("."); |
sfjmt | 1:e0714e1a1568 | 152 | } |
sfjmt | 1:e0714e1a1568 | 153 | } |