467 lines
9.9 KiB
Plaintext
467 lines
9.9 KiB
Plaintext
/*
|
|
|
|
FIRMWARE PARA ARDUINO ETHERNET Y SHIELD CON 6 ENTRADAS ANALOGICAS Y 6 DIGITALES
|
|
ENVIO Y RECEPCION OSC + MIDI
|
|
VERSION 1.0
|
|
|
|
LIBRERIAS EXTERNAS NECESARIAS:
|
|
- EthernetDHCP.h
|
|
- Z_OSC.h
|
|
- MIDI.h
|
|
|
|
DESARROLLADO EN HANGAR.ORG
|
|
@ALEX POSADA
|
|
2011
|
|
|
|
*/
|
|
|
|
|
|
#include <SPI.h>
|
|
#include <Ethernet.h> // version IDE 0022
|
|
#include <EthernetDHCP.h>
|
|
#include <MIDI.h>
|
|
#include <Z_OSC.h>
|
|
|
|
byte mac[] = {
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
|
|
|
byte server[] = {
|
|
0,0,0,0 }; // TEMPORAL SERVER
|
|
|
|
int s1 = A0;
|
|
int s2 = A1;
|
|
int s3 = A2;
|
|
int s4 = A3;
|
|
int s5 = A4;
|
|
int s6 = A5;
|
|
|
|
int sensor_value[6] = {
|
|
0,0,0,0,0,0};
|
|
int sensor_value_old[6] = {
|
|
0,0,0,0,0,0};
|
|
|
|
byte destIp[] = {
|
|
172, 26, 0, 46 }; // CAMBIAR POR LA MAQUINA A LA QUE SE QUIERA ENVIAR
|
|
int destPort = 9889;
|
|
int serverPort = 8998;
|
|
|
|
int terminalRemote;
|
|
int sensorRemote;
|
|
int valueRemote;
|
|
|
|
|
|
char sensor1[] = "/terminal2/sensor1";
|
|
char sensor2[] = "/terminal2/sensor2";
|
|
char sensor3[] = "/terminal2/sensor3";
|
|
char sensor4[] = "/terminal2/sensor4";
|
|
char sensor5[] = "/terminal2/sensor5";
|
|
char sensor6[] = "/terminal2/sensor6";
|
|
|
|
Z_OSCClient client;
|
|
Z_OSCServer serverOSC;
|
|
Z_OSCMessage *rcvMes;
|
|
|
|
const char* ip_to_str(const uint8_t*);
|
|
boolean dhcpReady = 0;
|
|
boolean flagConnect = 0;
|
|
|
|
void setup(){
|
|
|
|
Serial.begin(9600);
|
|
//Serial.begin(31250); // Activar para midi fisico externo
|
|
|
|
for(int i=2 ; i<8 ; i++)
|
|
pinMode(i, OUTPUT);
|
|
for(int i=2 ; i<8 ; i++)
|
|
digitalWrite(i, LOW);
|
|
|
|
//MIDI.begin(MIDI_CHANNEL_OMNI);
|
|
|
|
EthernetDHCP.begin(mac);
|
|
|
|
serverOSC.sockOpen(serverPort);
|
|
|
|
DHCP_begin();
|
|
|
|
}
|
|
|
|
|
|
void loop(){
|
|
|
|
EthernetDHCP.maintain();
|
|
|
|
//-----------------------------------
|
|
// LECTURA OSC
|
|
//-----------------------------------
|
|
|
|
if(serverOSC.available()){
|
|
rcvMes=serverOSC.getMessage();
|
|
rcvOsc();
|
|
//logMessage();
|
|
}
|
|
|
|
/*
|
|
|
|
//-----------------------------------
|
|
// FUNCION MIDI A SALIDAS DIGITALES
|
|
//-----------------------------------
|
|
|
|
if (MIDI.read()) { // Is there a MIDI message incoming ?
|
|
switch(MIDI.getType()) { // Get the type of the message we caught
|
|
case ControlChange:
|
|
terminalRemote = MIDI.getChannel());
|
|
sensorRemote = MIDI.getData1());
|
|
ValueRemote = MIDI.getData2());
|
|
|
|
break;
|
|
// See the online reference for other message types
|
|
default:
|
|
break;
|
|
}
|
|
|
|
MIDI.sendNoteOn(sensorRemote, valueRemote, terminalRemote);
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
//-----------------------------------
|
|
// LECTURA DE SENSORES Y ENVIO
|
|
//-----------------------------------
|
|
|
|
for(int i=0; i<6; i++)
|
|
{
|
|
sensor_value[i] = analogRead(i)/8;
|
|
if(sensor_value[i] != sensor_value_old[i])
|
|
{
|
|
sensor_Process(i, sensor_value[i]);
|
|
sensor_value_old[i] = sensor_value[i];
|
|
}
|
|
}
|
|
|
|
|
|
delay(10); // Se puede eliminar al activar el midi
|
|
|
|
}
|
|
|
|
//------------------------------------------------
|
|
// FUNCION PARA EL ENVIO DE LOS SENSORES POR OSC
|
|
//------------------------------------------------
|
|
|
|
void sensor_Process(int sensor, int valor) {
|
|
|
|
long int tmp = (long int) valor;
|
|
|
|
Z_OSCMessage message;
|
|
|
|
message.setAddress(destIp,destPort);
|
|
|
|
switch(sensor)
|
|
{
|
|
case 0:
|
|
message.setZ_OSCMessage(sensor1 ,"i" ,&tmp );
|
|
//noteOn(0x90, 0, valor/8);
|
|
//MIDI.sendControlChange (1, valor/8, 1);
|
|
break;
|
|
case 1:
|
|
message.setZ_OSCMessage(sensor2 ,"i" ,&tmp );
|
|
//noteOn(0x90, 1, valor/8);
|
|
//MIDI.sendControlChange (2, valor/8, 1);
|
|
break;
|
|
case 2:
|
|
message.setZ_OSCMessage(sensor3 ,"i" ,&tmp );
|
|
//noteOn(0x90, 2, valor/8);
|
|
//MIDI.sendControlChange (3, valor/8, 1);
|
|
break;
|
|
case 3:
|
|
message.setZ_OSCMessage(sensor4 ,"i" ,&tmp );
|
|
//noteOn(0x90, 3, valor/8);
|
|
//MIDI.sendControlChange (4, valor/8, 1);
|
|
break;
|
|
case 4:
|
|
message.setZ_OSCMessage(sensor5 ,"i" ,&tmp );
|
|
//noteOn(0x90, 4, valor/8);
|
|
//MIDI.sendControlChange (5, valor/8, 1);
|
|
break;
|
|
case 5:
|
|
message.setZ_OSCMessage(sensor6 ,"i" ,&tmp );
|
|
//noteOn(0x90, 5, valor/8);
|
|
//MIDI.sendControlChange (6, valor/8, 1);
|
|
break;
|
|
}
|
|
|
|
client.send(&message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rcvOsc(){
|
|
|
|
long int intValue;
|
|
|
|
if( strcmp( rcvMes->getZ_OSCAddress() , "/terminal2/sensor1" ) ){ // check ON_M send by Max/Msp
|
|
intValue = rcvMes->getInteger32(0);
|
|
if( intValue < 63 ){
|
|
Serial.print("sensor1 LOW: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(2, LOW);
|
|
}
|
|
else {
|
|
Serial.print("sensor1 HIGH: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(2, HIGH);
|
|
}
|
|
}
|
|
else if( strcmp( rcvMes->getZ_OSCAddress() , "/terminal2/sensor2" ) ){ // check ON_M send by Max/Msp
|
|
intValue = rcvMes->getInteger32(0);
|
|
if( intValue < 63 ){
|
|
Serial.print("sensor2 LOW: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(3, LOW);
|
|
}
|
|
else {
|
|
Serial.print("sensor2 HIGH: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(3, HIGH);
|
|
}
|
|
}
|
|
else if( strcmp( rcvMes->getZ_OSCAddress() , "/terminal2/sensor3" ) ){ // check ON_M send by Max/Msp
|
|
intValue = rcvMes->getInteger32(0);
|
|
if( intValue < 63 ){
|
|
Serial.print("sensor3 LOW: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(4, LOW);
|
|
}
|
|
else {
|
|
Serial.print("sensor3 HIGH: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(4, HIGH);
|
|
}
|
|
}
|
|
else if( strcmp( rcvMes->getZ_OSCAddress() , "/terminal2/sensor4" ) ){ // check ON_M send by Max/Msp
|
|
intValue = rcvMes->getInteger32(0);
|
|
if( intValue < 63 ){
|
|
Serial.print("sensor4 LOW: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(5, LOW);
|
|
}
|
|
else {
|
|
Serial.print("sensor4 HIGH: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(5, HIGH);
|
|
}
|
|
}
|
|
else if( strcmp( rcvMes->getZ_OSCAddress() , "/terminal2/sensor5" ) ){ // check ON_M send by Max/Msp
|
|
intValue = rcvMes->getInteger32(0);
|
|
if( intValue < 63 ){
|
|
Serial.print("sensor5 LOW: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(6, LOW);
|
|
}
|
|
else {
|
|
Serial.print("sensor5 HIGH: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(6, HIGH);
|
|
}
|
|
}
|
|
else if( strcmp( rcvMes->getZ_OSCAddress() , "/terminal2/sensor6" ) ){ // check ON_M send by Max/Msp
|
|
intValue = rcvMes->getInteger32(0);
|
|
if( intValue < 63 ){
|
|
Serial.print("sensor6 LOW: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(7, LOW);
|
|
}
|
|
else {
|
|
Serial.print("sensor6 HIGH: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(7, HIGH);
|
|
}
|
|
}
|
|
else
|
|
Serial.println(rcvMes->getZ_OSCAddress() );
|
|
|
|
}
|
|
|
|
|
|
void logMessage(){
|
|
uint16_t i;
|
|
byte *ip=rcvMes->getIpAddress();
|
|
|
|
long int intValue;
|
|
float floatValue;
|
|
char *stringValue;
|
|
|
|
|
|
Serial.print(ip[0],DEC);
|
|
Serial.print(".");
|
|
Serial.print(ip[1],DEC);
|
|
Serial.print(".");
|
|
Serial.print(ip[2],DEC);
|
|
Serial.print(".");
|
|
Serial.print(ip[3],DEC);
|
|
Serial.print(":");
|
|
|
|
Serial.print(rcvMes->getPortNumber());
|
|
Serial.print(" ");
|
|
Serial.print(rcvMes->getZ_OSCAddress());
|
|
Serial.print(" ");
|
|
Serial.print(rcvMes->getTypeTags());
|
|
Serial.print("--");
|
|
|
|
|
|
for(i=0 ; i<rcvMes->getArgsNum(); i++){
|
|
|
|
switch( rcvMes->getTypeTag(i) ){
|
|
|
|
case 'i':
|
|
intValue = rcvMes->getInteger32(i);
|
|
|
|
Serial.print(intValue);
|
|
Serial.print(" ");
|
|
/*
|
|
if( intValue < 512 ){
|
|
Serial.print("sensor recibido 1: ");
|
|
Serial.println(intValue);
|
|
digitalWrite(2, LOW);
|
|
}
|
|
else {
|
|
digitalWrite(2, HIGH);
|
|
Serial.print("sensor recibido 1: ");
|
|
Serial.println(intValue);
|
|
}
|
|
*/
|
|
break;
|
|
|
|
case 'f':
|
|
floatValue = rcvMes->getFloat(i);
|
|
|
|
Serial.print(floatValue);
|
|
Serial.print(" ");
|
|
break;
|
|
|
|
|
|
case 's':
|
|
stringValue = rcvMes->getString(i);
|
|
|
|
Serial.print(stringValue);
|
|
Serial.print(" ");
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
}
|
|
|
|
|
|
void noteOn(int cmd, int pitch, int velocity) {
|
|
Serial.print(cmd, BYTE);
|
|
Serial.print(pitch, BYTE);
|
|
Serial.print(velocity, BYTE);
|
|
}
|
|
|
|
|
|
const char* ip_to_str(const uint8_t* ipAddr)
|
|
{
|
|
static char buf[16];
|
|
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
|
|
return buf;
|
|
}
|
|
|
|
|
|
void dhcp()
|
|
{
|
|
|
|
static DhcpState prevState = DhcpStateNone;
|
|
static unsigned long prevTime = 0;
|
|
|
|
DhcpState state = EthernetDHCP.poll();
|
|
|
|
if (prevState != state) {
|
|
Serial.println();
|
|
|
|
switch (state) {
|
|
case DhcpStateDiscovering:
|
|
Serial.print("Discovering servers.");
|
|
dhcpReady=0;
|
|
break;
|
|
case DhcpStateRequesting:
|
|
Serial.print("Requesting lease.");
|
|
dhcpReady=0;
|
|
break;
|
|
case DhcpStateRenewing:
|
|
Serial.print("Renewing lease.");
|
|
dhcpReady=0;
|
|
break;
|
|
case DhcpStateLeased:
|
|
{
|
|
Serial.println("Obtained lease!");
|
|
|
|
const byte* ipAddr = EthernetDHCP.ipAddress();
|
|
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
|
|
const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
|
|
|
|
Serial.print("My IP address is ");
|
|
Serial.println(ip_to_str(ipAddr));
|
|
|
|
Serial.print("Gateway IP address is ");
|
|
Serial.println(ip_to_str(gatewayAddr));
|
|
|
|
Serial.print("DNS IP address is ");
|
|
Serial.println(ip_to_str(dnsAddr));
|
|
|
|
Serial.println('\n');
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (state != DhcpStateLeased && millis() - prevTime > 300) {
|
|
prevTime = millis();
|
|
Serial.print('.');
|
|
}
|
|
else if (state == DhcpStateLeased) {
|
|
|
|
dhcpReady = 1;
|
|
|
|
}
|
|
|
|
prevState = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DHCP_begin()
|
|
{
|
|
|
|
|
|
// Since we're here, it means that we now have a DHCP lease, so we print
|
|
// out some information.
|
|
const byte* ipAddr = EthernetDHCP.ipAddress();
|
|
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
|
|
const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
|
|
|
|
Serial.println("A DHCP lease has been obtained.");
|
|
|
|
Serial.print("My IP address is ");
|
|
Serial.println(ip_to_str(ipAddr));
|
|
|
|
Serial.print("Gateway IP address is ");
|
|
Serial.println(ip_to_str(gatewayAddr));
|
|
|
|
Serial.print("DNS IP address is ");
|
|
Serial.println(ip_to_str(dnsAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|