Commits

bjoern committed 43cbe1a

cleaned up udp library; added license; removed send-only example

Comments (0)

Files changed (4)

+# use glob syntax.
+syntax: glob
+
+applet
+.DS_Store
+*.svn
+*.swf
+*.o
+*~

arduino_osc_udp.pde

  * The single integer argument for each OSC message
  * represents either HIGH/LOW, or an 8bit analog value.
  *
- * 
+ * PC->ARDUINO MESSAGE FORMAT
+ * /pinmode/[2..12]  [0|1] - set a pin to input or output mode
+ *
+ * /report/adc [0|1]    - turn all analog pin reporting on/off (Default: off)
+ * /report/adc/[0..5] [0|1]  - set analog pin reporting for one pin on/off
+ * /report/in [0|1]     - turn digital pin reporting on/off (default: on)
+ *
+ * /out/5 [0|1]         - set a digital pin to [low|high]
+ * /pwm/10 [0..255]     - set duty on a pwm-enabled pin 
+ *
  * ARDUINO->PC PROTOCOL 
  * /in/[0..9] [0|1]    - a digital input pin changed to [high|low]
  * /adc/[0..5] [0..255] - analog input value changed to [0..255]
  * Therefore, 0 means HIGH, 1 means LOW (pulled to ground). 
  *
  *
+ * EXAMPLES: PC->ARDUINO 
+ * /pinmode/5 0         - set pin 5 to INPUT
+ * /pinmode/9 1         - set pin 9 to OUTPUT
+ * /out/9 0             - set pin 9 to LOW
+ * /out/7 1            - set pin 7 to HIGH
+ * /pwm/9 255          - set PWM duty on pin 9 to 255 (100%)
+ * /report/in 1         - turn digital input pin reporting on
+ * /report/adc/4 0      - turn reporting of analog input 4 off
+ *
  * EXAMPLES: ARDUINO->PC
  * /in/4 1              - digital input pin 4 pulled to ground
  * /adc/2 128           - analog input pin2 read 128 (=2.5V)
  *   - Analog reporting is disabled 
  *     (change variable reportAnalog to 0xFF to enable by default)
  *
+ * YOU WILL HAVE TO CHANGE ETHERNET CONFIGURATION TO MATCH YOUR
+ * ENVIRONMENT BEFORE COMPILING/UPLOADING!
+ *
  * NOTES:
  *   - Pins 10-13 cannot be used (needed for SPI communication with ethernet shield)
- *   - Resolution on analog in and out is 8 bit.
+ *   - Resolution on analog in and out is 8 bit. 
  * 
  * MIT License:
  * Copyright (c) 2008 Bjoern Hartmann
 
 
 /* ETHERNET CONFIGURATION *************************************/
-/* ARDUINO: set MAC, IP address of Ethernet shield and its gateway */
+/* ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
+   and local port to listen on for incoming packets */
 byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
 byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
 byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; //local port to listen on
 
 /* TARGET: set this to IP/Port of computer that will receive
  * UDP messages from Arduino */
   } 
   
   Ethernet.begin(mac,ip,gw);
-  Udp.begin(targetIp,targetPort);
+  Udp.begin(localPort);
   //DEBUG: Serial.begin(9600);
 }
 
 
   //send message as one packet
   for(i=0;i<offset;i++) {
-    Udp.sendPacket((const byte *)oscBuffer,offset);
+    Udp.sendPacket((byte *)oscBuffer,offset,targetIp,targetPort);
   }
 }
 

libraries/Ethernet/Udp.cpp

 /*
- *  Udp.cpp
- *  
+ *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
  *
- *  Created by Bjoern Hartmann on 12/28/08.
- *  Copyright 2008 Stanford Computer Science. All rights reserved.
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
  *
+ * bjoern@cs.stanford.edu 12/29/2008
  */
 
 extern "C" {
 #include "Ethernet.h"
 #include "Udp.h"
 
-void UdpClass::begin(uint8_t *ip, uint16_t port) {
-	_ip = ip;
+/* Start UDP socket, listening at local port PORT */
+void UdpClass::begin(uint16_t port) {
 	_port = port;
-	_sock = 0;
-	socket(_sock,Sn_MR_UDP,8888,0);
+	_sock = 0; //TODO: should not be hardcoded
+	socket(_sock,Sn_MR_UDP,_port,0);
 }
 
-uint16_t UdpClass::sendPacket(const uint8_t * buf, uint16_t len){
-	return sendto(_sock,buf,len,_ip,_port);
+
+/* Send packet contained in buf of length len to peer at specified ip, and port */
+/* This function returns sent data size for success else -1. */
+uint16_t UdpClass::sendPacket(uint8_t * buf, uint16_t len,  uint8_t * ip, uint16_t port){
+	return sendto(_sock,(const uint8_t *)buf,len,ip,port);
 }
 
-/* is data available in rx buffer? 0 if no, non-zero if yes */
+/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes. */
 int UdpClass::available() {
 	return getSn_RX_RSR(_sock);
 }
 
-/*
- * read a received packet into buffer buf; store calling ip 
- */
+
+/* Read a received packet into buffer buf (whis is of maximum length len); */
+/* store calling ip and port as well. Call available() to make sure data is ready first. */
+/* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
+/*       so it's easy to overflow buf. */
 uint16_t UdpClass::readPacket(uint8_t * buf, uint16_t len, uint8_t *ip, uint16_t *port) {
 	return recvfrom(_sock,buf,len,ip,port);
 }
 
-/* read a received packet, throw away peer's ip and port */
+/* Read a received packet, throw away peer's ip and port.  See note above. */
 uint16_t UdpClass::readPacket(uint8_t * buf, uint16_t len) {
 	uint8_t ip[4];
 	uint16_t port[1];
 	return recvfrom(_sock,buf,len,ip,port);
 }
 
+/* Create one global object */
 UdpClass Udp;

libraries/Ethernet/Udp.h

 /*
- *  Udp.h
- *  
+ *  Udp.h: Library to send/receive UDP packets with the Arduino ethernet shield.
+ *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
  *
- *  Created by Bjoern Hartmann on 12/28/08.
- *  Copyright 2008 Stanford Computer Science. All rights reserved.
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
  *
+ * bjoern@cs.stanford.edu 12/29/2008
  */
 
 #ifndef Udp_h
 class UdpClass {
 private:
 	uint8_t _sock;  // socket ID for Wiz5100
-	uint8_t *_ip;   // peer's IP address
-	uint16_t _port; // peer's port
+	uint16_t _port; // local port to listen on
 	
 public:
-	void begin(uint8_t *, uint16_t);				// initialize
-	uint16_t sendPacket(const uint8_t *, uint16_t); // send a packet
+	void begin(uint16_t);				// initialize, start listening on specified port
+	uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer
 	int available();								// has data been received?
 	uint16_t readPacket(uint8_t *, uint16_t);		// read a received packet 
-	uint16_t readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet 
+	uint16_t readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet, also return sender's ip and port 
 };
 
 extern UdpClass Udp;