254 lines
5.9 KiB
C++
254 lines
5.9 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 <Adafruit_NeoPixel.h>
|
|
#include <Adafruit_CAP1188.h>
|
|
#include "mcp_can.h"
|
|
#include "MCP23008.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
|
|
|
|
// 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
|
|
|
|
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(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 board
|
|
// 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;
|
|
}
|
|
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.");
|
|
|
|
//
|
|
// // create a SDWaveFile
|
|
// waveFile = SDWaveFile(filename);
|
|
// // check if the WaveFile is valid
|
|
// if (!waveFile) {
|
|
// SerialUSB.println("wave file is invalid!");
|
|
// //while(1); // do nothing
|
|
// }
|
|
// else
|
|
// {
|
|
// // 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(80);
|
|
//
|
|
// // 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);
|
|
// }
|
|
|
|
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());
|
|
|
|
// 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() {
|
|
|
|
// // check if playback is still going on
|
|
// if (!AudioOutI2S.isPlaying()) {
|
|
// // playback has stopped
|
|
//
|
|
// SerialUSB.println("playback stopped");
|
|
// //while (1); // do nothing
|
|
// }
|
|
|
|
|
|
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(15);
|
|
|
|
}
|