Lab_interaccio/2016/FIWARE/FIWARE.ino

333 lines
6.9 KiB
Arduino
Raw Normal View History

2025-02-25 21:29:42 +01:00
//#include <avr/power.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
//#include <FastGPIO.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCBoards.h>
#define UDP_TX_PACKET_MAX_SIZE 32
#define debug 0
#define RED 6
#define GREEN 5
#define BLUE 11
#define WHITE 9
int rele[4] = { 7, 4, 3, 2};
#define midi Serial1
bool B1State, B2State, B3State, B4State;
bool lastB1State, lastB2State, lastB3State, lastB4State;
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0x58, 0x9B // scenario1
//0x90, 0xA2, 0xDA, 0x10, 0x1B, 0x07 // scenario2
}; // you can find this written on the board of some Arduino Ethernets or shields
//port numbers
unsigned int inPort = 10001;
unsigned int destPort = 10000; // TO SET SENDING PORT
//the Arduino's IP
IPAddress ip(10, 100, 50, 3 ); // Scenario 1 = 10.100.50.3 , scenario2 = 10.100.50.4
//the Computer's IP
IPAddress destIp(10, 100, 50, 1); // Scenario 1 = 10.100.50.1 , scenario2 = 10.100.50.2
EthernetUDP Udp;
void setup() {
pinMode(RED, OUTPUT);
digitalWrite(RED, LOW);
pinMode(GREEN, OUTPUT);
digitalWrite(GREEN, LOW);
pinMode(BLUE, OUTPUT);
digitalWrite(BLUE, LOW);
pinMode(WHITE, OUTPUT);
digitalWrite(WHITE, LOW);
for (int i = 0; i < 4; i++)
{
pinMode(rele[i], OUTPUT);
digitalWrite(rele[i], LOW);
}
midi.begin(31250);
if (debug)
{
Serial.begin(115200);
while (!Serial) ;
}
Ethernet.begin(mac, ip);
Udp.begin(inPort);
//FastGPIO::Pin<20>::setInputPulledUp();
//FastGPIO::Pin<21>::setInputPulledUp();
//FastGPIO::Pin<22>::setInputPulledUp();
//FastGPIO::Pin<23>::setInputPulledUp();
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
pinMode(A5, INPUT_PULLUP);
Serial.println("Ready");
}
void loop() {
/*
rele_test();
if (Serial1.available()) Serial.write(Serial1.read());
play notes from F#-0 (0x1E) to F#-5 (0x5A):
noteOn(0x90, 20, 0x45);
delay(500);
for (int note = 0x1E; note < 0x5A; note ++) {
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0x90, note, 0x45);
delay(100);
//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
noteOn(0x90, note, 0x00);
delay(100);
}
*/
////////////////////////////////////////////////////////////
/////// RUTINA TRATAMIENTO DE ENTRADAS ANALOGICAS ///////
////////////////////////////////////////////////////////////
//B1State = FastGPIO::Pin<20>::isInputHigh();
//B2State = FastGPIO::Pin<21>::isInputHigh();
//B3State = FastGPIO::Pin<22>::isInputHigh();
//B4State = FastGPIO::Pin<23>::isInputHigh();
B1State = digitalRead(A2);
B2State = digitalRead(A3);
B3State = digitalRead(A4);
B4State = digitalRead(A5);
if (B1State != lastB1State) {
OSCMessage msg("/b1");
if (B1State == HIGH) {
if(debug)
Serial.println("OSC B1=0");
msg.add(0);
}
else
{
if(debug)
Serial.println("OSC B1=1");
msg.add(1);
}
Udp.beginPacket(destIp, destPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
delay(20);
}
if (B2State != lastB2State) {
OSCMessage msg("/b2");
if (B2State == HIGH) {
if(debug)
Serial.println("OSC B2=0");
msg.add(0);
}
else
{
if(debug)
Serial.println("OSC B2=1");
msg.add(1);
}
Udp.beginPacket(destIp, destPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
delay(20);
}
if (B3State != lastB3State) {
OSCMessage msg("/b3");
if (B3State == HIGH) {
if(debug)
Serial.println("OSC B3=0");
msg.add(0);
}
else
{
if(debug)
Serial.println("OSC B3=1");
msg.add(1);
}
Udp.beginPacket(destIp, destPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
delay(20);
}
if (B4State != lastB4State) {
OSCMessage msg("/b4");
if (B4State == HIGH) {
if(debug)
Serial.println("OSC B4=0");
msg.add(0);
}
else
{
if(debug)
Serial.println("OSC B4=1");
msg.add(1);
}
Udp.beginPacket(destIp, destPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
delay(20);
}
lastB1State = B1State;
lastB2State = B2State;
lastB3State = B3State;
lastB4State = B4State;
////////////////////////////////////////////////////////////
/////// RUTINA TRATAMIENTO DE STRINGS DEL ETHERNET ///////
////////////////////////////////////////////////////////////
OSCMessage msg;
int size;
if ( (size = Udp.parsePacket()) > 0)
{
//Serial.print("mensaje recibido: ");
//Serial.println(size);
while (size--)
{
//msg.fill(Udp.read()); // 6ms
uint8_t packetBuffer[UDP_TX_PACKET_MAX_SIZE];
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // 500us
msg.fill(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
}
if (!msg.hasError())
{
msg.dispatch("/s1/c1", caracola1);
msg.dispatch("/s1/c2", caracola2);
msg.dispatch("/s1/midi", midiplay); // /s2 -> Scenario 2 , /s1 -> Scenario 1
// msg.dispatch("/s2/c1", caracola1);
// msg.dispatch("/s2/c2", caracola2);
// msg.dispatch("/s2/midi", midiplay); // /s2 -> Scenario 2 , /s1 -> Scenario 1
}
}
}
void rele_test()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(rele[i], HIGH);
delay(100);
digitalWrite(rele[i], LOW);
delay(100);
}
}
void rele_bang(int channel, int lenght)
{
digitalWrite(rele[channel], HIGH);
delay(lenght);
digitalWrite(rele[channel], LOW);
delay(lenght);
}
void rele_midi(int channel, int state)
{
if (state)
digitalWrite(rele[channel], HIGH);
else
digitalWrite(rele[channel], LOW);
}
// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity)
{
midi.write(cmd);
midi.write(pitch);
midi.write(velocity);
}
void caracola1(OSCMessage &msg)
{
int vel = msg.getInt(0);
bool state = msg.getInt(1);
if (debug)
{
Serial.print("C1 Vel: ");
Serial.print(vel);
Serial.print(" ");
Serial.print("C1 Note: ");
Serial.println(state);
}
rele_midi(0, state);
}
void caracola2(OSCMessage &msg)
{
int vel = msg.getInt(0);
bool state = msg.getInt(1);
if (debug)
{
Serial.print("C2 Vel: ");
Serial.print(vel);
Serial.print(" ");
Serial.print("C2 Note: ");
Serial.println(state);
}
rele_midi(1, state);
}
void midiplay(OSCMessage &msg)
{
}