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

305 lines
8 KiB
C++

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "DFRobotDFPlayerMini.h"
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
//HARDWARE
byte mac[] = { 144, 162, 218, 00, 16, 96 };//the mac adress of ethernet shield or uno shield board
byte ip[] = { 10, 0, 0, 10 };// the IP adress of your device, that should be in same universe of the network you are using, here: 192.168.1.x
byte destination_Ip[] = { 10, 0, 0, 11 }; // the ip to send data, 255,255,255,255 is broadcast sending
// art net parameters
unsigned int localPort = 6454; // artnet UDP port is by default 6454
const int DMX_Universe = 1 ;//universe is from 0 to 15, subnet is not used
const int number_of_channels = 512; //512 for 512 channels, MAX=512
EthernetUDP Udp;
//ART-NET variables
char ArtNetHead[8] = "Art-Net";
const int art_net_header_size = 17;
short OpOutput = 0x5000 ; //output
byte buffer_dmx[number_of_channels]; //buffer used for DMX data
//Artnet PACKET
byte ArtDmxBuffer[(art_net_header_size + number_of_channels) + 8 + 1];
int temp_val = 0;
void setup() {
// Open Serial communications and wait for port to open:
Serial.begin(115200);
Serial1.begin(9600);
while (!Serial) {
; // wait for Serial port to connect. Needed for native USB port only
}
construct_arnet_packet(); // constructor de paquete ARTNET
Ethernet.begin(mac, ip);
Udp.begin(localPort);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.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) {
Serial.println("Ethernet cable is not connected.");
}
Serial.println("Initialization Ethernet done.");
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(Serial1)) { //Use softwareSerial to communicate with myDFPlayer.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
}
void loop() {
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
/*
check_arduino_inputs();
construct_arnet_packet();
//Udp.sendPacket( ArtDmxBuffer,(art_net_header_size+number_of_channels+1), destination_Ip, localPort);
//Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.beginPacket( destination_Ip , localPort );
Udp.write(ArtDmxBuffer, (art_net_header_size + number_of_channels + 1));
Udp.endPacket();
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
//NVIC_SystemReset(); // RESET SAMD21
//digitalWrite(11, LOW); // Reset Ethernet Chip
//delay(100);
//digitalWrite(11, HIGH);
delay(1000);
}
*/
if (Serial.available())
{
char c = Serial.read();
//Serial.print(c);
if (c == 'r') // RESET
{
Serial.println("Reset CPU");
NVIC_SystemReset(); // RESET SAMD21
}
else if (c == 's') // pista 1
{
Serial.println("Stop");
myDFPlayer.stop(); //Play next myDFPlayer
}
else if (c == '1') // pista 1
{
Serial.println("Play 0001.mp3");
myDFPlayer.play(1); //Play next myDFPlayer
}
else if (c == '2') // pista 2
{
Serial.println("Play 0002.mp3");
myDFPlayer.play(2); //Play next myDFPlayer
}
else if (c == '3') // pista 3
{
Serial.println("Play 0003.mp3");
myDFPlayer.play(3); //Play next myDFPlayer
}
else if (c == '4') // pista 4
{
Serial.println("Play 0004.mp3");
myDFPlayer.play(4); //Play next myDFPlayer
}
else if (c == '5') // pista 5
{
Serial.println("Play 0005.mp3");
myDFPlayer.play(5); //Play next myDFPlayer
}
else if (c == '6') // pista 6
{
Serial.println("Play 0006.mp3");
myDFPlayer.play(6); //Play next myDFPlayer
}
else if (c == '+') // RESET
{
//Serial.println("stopping...");
//myDFPlayer.stop(); // stop
//readmyDFPlayerstate();
//delay(1000);
//readmyDFPlayerstate();
myDFPlayer.next(); //Play next myDFPlayer
Serial.print("Play ");
Serial.println(myDFPlayer.readCurrentFileNumber());
//readmyDFPlayerstate();
}
else if (c == '-') // RESET
{
myDFPlayer.previous(); //Play next myDFPlayer
Serial.print("Play ");
Serial.println(myDFPlayer.readCurrentFileNumber());
Serial.println(myDFPlayer.readState()); //read mp3 state
}
else if (c == 'h') // HELP inos
{
Serial.println(myDFPlayer.readState()); //read mp3 state
Serial.println(myDFPlayer.readVolume()); //read current volume
Serial.println(myDFPlayer.readEQ()); //read EQ setting
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03 }
}
}
delay(30); // 33fps
}
void check_arduino_inputs()
{
//data from arduino aquisition
for (int i = 0; i < 6; i++) //reads the 6 analogic inputs and set the data from 1023 steps to 255 steps (dmx)
{
temp_val++;
if (temp_val > 255)
temp_val = 0;
//Serial.println(temp_val);
buffer_dmx[i] = byte(temp_val);
}
}
void construct_arnet_packet()
{
//preparation pour tests
for (int i = 0; i < 7; i++)
{
ArtDmxBuffer[i] = ArtNetHead[i];
}
//Operator code low byte first
ArtDmxBuffer[8] = OpOutput;
ArtDmxBuffer[9] = OpOutput >> 8;
//protocole
ArtDmxBuffer[10] = 0;
ArtDmxBuffer[11] = 14;
//sequence
ArtDmxBuffer[12] = 0;
//physical
ArtDmxBuffer[13] = 0;
// universe
ArtDmxBuffer[14] = DMX_Universe; //or 0
ArtDmxBuffer[15] = DMX_Universe >> 8;
//data length
ArtDmxBuffer[16] = number_of_channels >> 8;
ArtDmxBuffer[17] = number_of_channels;
for (int t = 0; t < number_of_channels; t++)
{
ArtDmxBuffer[t + art_net_header_size + 1] = buffer_dmx[t];
}
}
void printDetail(uint8_t type, int value) {
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}