Lab_interaccio/2020/Audio-Ethernet (iguzzini)/DF-Ethernet/DF-Ethernet.ino

298 lines
6.7 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
#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 <Adafruit_NeoPixel.h>
#include <Adafruit_CAP1188.h>
#include "mcp_can.h"
#include "MCP23008.h"
#include <DFMiniMp3.h>
// Reset Pin is used for I2C or SPI
#define CAP1188_RESET 7
#define CAP1188_CS 12
#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 cardSelect 4
#define ethSelect 10
//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
EthernetUDP Udp;
OSCErrorCode error;
#define UDP_RX_PACKET_MAX_SIZE 8
MCP23008 id;
// implement a notification class,
// its member methods will get called
//
class Mp3Notify
{
public:
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(uint16_t globalTrack)
{
Serial.println();
Serial.print("Play finished for #");
Serial.println(globalTrack);
}
static void OnCardOnline(uint16_t code)
{
Serial.println();
Serial.print("Card online ");
Serial.println(code);
}
static void OnUsbOnline(uint16_t code)
{
Serial.println();
Serial.print("USB Disk online ");
Serial.println(code);
}
static void OnCardInserted(uint16_t code)
{
Serial.println();
Serial.print("Card inserted ");
Serial.println(code);
}
static void OnUsbInserted(uint16_t code)
{
Serial.println();
Serial.print("USB Disk inserted ");
Serial.println(code);
}
static void OnCardRemoved(uint16_t code)
{
Serial.println();
Serial.print("Card removed ");
Serial.println(code);
}
static void OnUsbRemoved(uint16_t code)
{
Serial.println();
Serial.print("USB Disk removed ");
Serial.println(code);
}
};
// instance a DFMiniMp3 object,
// defined with the above notification class and the hardware serial class
//
DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);
// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
//SoftwareSerial secondarySerial(10, 11); // RX, TX
//DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
void setup() {
// Open SerialUSB communications and wait for port to open:
SerialUSB.begin(115200);
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 boardv
// you may need to pass a pin number in begin for SS
SerialUSB.print("Initializing SD card...");
if (!SD.begin(cardSelect)) { // feather CS pin
SerialUSB.println("initialization failed!");
//return;
}
else
SerialUSB.println("Initialization done.");
Ethernet.begin(mac, ip);
//Ethernet.init(ethSelect);
// 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);
SerialUSB.println("Initialization Ethernet done.");
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(0x00); // creo que hay que poner este modo
id.setPullup(0xFF); // esto o se que hace
SerialUSB.print("ID: ");
SerialUSB.println(id.read());
mp3.begin();
uint16_t volume = mp3.getVolume();
SerialUSB.print("volume ");
SerialUSB.println(volume);
mp3.setVolume(24);
uint16_t count = mp3.getTotalTrackCount();
SerialUSB.print("files ");
SerialUSB.println(count);
SerialUSB.println("starting...");
SerialUSB.println("track 1");
mp3.playMp3FolderTrack(1); // sd:/mp3/0001.mp3
// 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(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);
for (int i = 0 ; i < numLeds ; i++)
leds.setPixelColor(i, 0, 0, 0);
leds.show();
delay(500);
}
void loop() {
// calling mp3.loop() periodically allows for notifications
// to be handled without interrupts
mp3.loop();
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);
}
}
//delay(10);
}