Lab_interaccio/2020/Audio-Ethernet (iguzzini)/AudioEthernet_MIGUEL/AudioEthernet_MIGUEL.ino
2025-02-25 21:29:42 +01:00

254 lines
6 KiB
C++

#include <SD.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OSCBoards.h>
#include <OSCMessage.h>
#include <ArduinoSound.h>
#include "MCP23008.h"
#include <Adafruit_NeoPixel.h>
#include <Adafruit_CAP1188.h>
#include "mcp_can.h"
// Reset Pin is used for I2C
#define CAP1188_RESET 7
#define CAP1188_IRQ A5
#define CAP1188_WAKE 6
Adafruit_CAP1188 cap = Adafruit_CAP1188();
//const int SPI_CS_PIN = 9;
//MCP_CAN CAN(SPI_CS_PIN);
// filename of wave file to play
const char filename[] = "music.wav";
#define CS_MMC 4
// variable representing the Wave File
SDWaveFile waveFile;
//Definicion de los pines de la tira de led
#define D1 8
#define D2 3
//Definicion de los pines de laS tiraS de led PIXELS
#define SDO1 A1
#define SDO2 A2
const int numLeds = 300; //
Adafruit_NeoPixel leds = Adafruit_NeoPixel(numLeds, SDO1, NEO_GRB + NEO_KHZ800);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEA
};
IPAddress ip(192, 168, 1, 3); // rango gaspar
IPAddress destIp(192, 168, 1, 2); // mac pero no se usa
unsigned int localPort = 8888; // local port to listen on
unsigned int destPort = 9999; // TO SET SENDING PORT
#define ETH_IRQ 5
EthernetUDP Udp;
OSCErrorCode error;
#define UDP_RX_PACKET_MAX_SIZE 8
MCP23008 id;
void setup() {
// Open SerialUSB communications and wait for port to open:
SerialUSB.begin(9600);
while (!SerialUSB) {
; // wait for SerialUSB port to connect. Needed for native USB port only
}
leds.begin();
Serial.println("LEDs initialization test.");
initTest();
// setup the SD card, depending on your shield of breakout board
// you may need to pass a pin number in begin for SS
SerialUSB.print("Initializing SD card...");
if (!SD.begin(CS_MMC)) { // feather CS pin
SerialUSB.println("initialization failed!");
//return;
}
SerialUSB.println("initialization done.");
// create a SDWaveFile
waveFile = SDWaveFile(filename);
// check if the WaveFile is valid
if (!waveFile) {
SerialUSB.println("wave file is invalid!");
//while(1); // do nothing
}
// print out some info. about the wave file
SerialUSB.print("Bits per sample = ");
SerialUSB.println(waveFile.bitsPerSample());
long channels = waveFile.channels();
SerialUSB.print("Channels = ");
SerialUSB.println(channels);
long sampleRate = waveFile.sampleRate();
SerialUSB.print("Sample rate = ");
SerialUSB.print(sampleRate);
SerialUSB.println(" Hz");
long duration = waveFile.duration();
SerialUSB.print("Duration = ");
SerialUSB.print(duration);
SerialUSB.println(" seconds");
// adjust the playback volume
AudioOutI2S.volume(10);
// check if the I2S output can play the wave file
if (!AudioOutI2S.canPlay(waveFile)) {
SerialUSB.println("unable to play wave file using I2S!");
//while (1); // do nothing
}
// start playback
SerialUSB.println("starting playback");
AudioOutI2S.play(waveFile);
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
SerialUSB.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
SerialUSB.println("Ethernet cable is not connected.");
}
Udp.begin(localPort);
if (!cap.begin()) {
SerialUSB.println("CAP1188 not found");
while (1);
}
SerialUSB.println("CAP1188 found!");
// configure MCP23008 component
id.begin(0x20);
// inicializar pin a pin
id.pinMode(0xFF);
id.setPullup(0xFF); // esto o se que hace
SerialUSB.print("ID: ");
SerialUSB.println(id.read());
attachInterrupt(ETH_IRQ, ISR_ETH, LOW); //FALLING //RISING
// while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
// {
// Serial.println("CAN BUS Shield init fail");
// Serial.println("Init CAN BUS Shield again");
// delay(100);
// }
// Serial.println("CAN BUS Shield init ok!");
}
void test(OSCMessage &msg)
{
SerialUSB.println("test");
}
void initTest()
{
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 127, 0, 0);
leds.show();
delay(500);
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 0, 127, 0);
leds.show();
delay(500);
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 0, 0, 127);
leds.show();
delay(1000);
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 0, 0, 0);
leds.show();
delay(500);
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 0, 0, 0);
leds.show();
delay(500);
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 0, 0, 0);
leds.show();
delay(500);
}
unsigned long time_eth = millis();
void loop() {
// check if playback is still going on
// if (!AudioOutI2S.isPlaying()) {
// // playback has stopped
// SerialUSB.println("playback stopped");
// //while (1); // do nothing
// }
if ((millis()-time_eth)>50)
{
ISR_ETH();
time_eth = millis();
}
}
void ISR_ETH()
{
AudioOutI2S.pause();
OSCMessage msg;
int size;
if ( (size = Udp.parsePacket()) > 0)
{
SerialUSB.print("mensaje recibido: ");
//SerialUSB.println(size);
while (size--)
{
// msg.fill(Udp.read());
uint8_t packetBuffer[UDP_RX_PACKET_MAX_SIZE];
Udp.read(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
msg.fill(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
}
if (!msg.hasError())
{
msg.dispatch("/test", test ); // this is how it is€ marked on the silkscreen
}
else
{
error = msg.getError();
//SerialUSB.print("error: ");
//SerialUSB.println(error);
}
}
AudioOutI2S.resume();
}