Lab_interaccio/2022/CABALGATA/pixelplayer_SD_MASTER/pixelplayer_SD_MASTER.ino

382 lines
8.9 KiB
Arduino
Raw Normal View History

2025-02-25 21:29:42 +01:00
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
#include <SD.h>
#include "nRF24L01MOD.h"
#include "RF24MOD.h"
#include "printfMOD.h"
#include "MCP23008.h"
#define DEBUG 0
//#define ALAS
//define ESTRELLA
#define AROS
//#define VERONAL
//#define GUIRNALDA
//Found device at: 0x20
//Found device at: 0xA0
// Radio Settings
#define CE_PIN 7
#define CSN_PIN 6
int radioId;
uint32_t CMD[ 8 ]; // Comando Radio
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
#ifdef ALAS
int ch = 0x20 ; // 76 por defecto es el canal 76
#endif
#ifdef ESTRELLA
int ch = 0x4C ;
#endif
#ifdef AROS
int ch = 0x76 ; // 76 por defecto es el canal 76
#endif
#ifdef VERONAL
int ch = 0x76 ; // 76 por defecto es el canal 76
#endif
#ifdef GUIRNALDA
int ch = 0x4C ;
#endif
//Definicion de los pines de la tira de led
#define RED 8
#define GREEN 9
#define BLUE 10
#define WHITE 5
//Definicion de los pines de laS tiraS de led PIXELS
#define SDO1 A5
#define SDO2 A2
#define SDO3 A3
#ifdef ALAS
// ALAS
#define NUMPIXELS1 512 // han de ser 576
#define NUMPIXELS2 0
#define NUMPIXELS3 0
#endif
#ifdef AROS
// AROS
#define NUMPIXELS1 297 // ponemos un par mas para corregir errores
#define NUMPIXELS2 0
#define NUMPIXELS3 16 // 16 y GRB al reves que los otros
#endif
#ifdef ESTRELLA
// ESTRELLA
#define NUMPIXELS1 409
#define NUMPIXELS2 0
#define NUMPIXELS3 30
#endif
#ifdef VERONAL
// VERONAL
#define NUMPIXELS1 100
#define NUMPIXELS2 100
#define NUMPIXELS3 100
#endif
#ifdef GUIRNALDA
// VERONAL
#define NUMPIXELS1 120
#define NUMPIXELS2 120
#define NUMPIXELS3 120
#endif
Adafruit_NeoPixel pixels_1(NUMPIXELS1, SDO1, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel pixels_2(NUMPIXELS2, SDO2, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel pixels_3(NUMPIXELS3, SDO3, NEO_RGB + NEO_KHZ800);
MCP23008 id;
// Artnet settings
const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as zero.
const int numberOfChannels = (NUMPIXELS1 + NUMPIXELS2 + NUMPIXELS3) * 3; // Total number of channels you want to receive (1 led = 3 channels)
byte channelBuffer[numberOfChannels]; // Combined universes into a single array
// SD card
File datafile;
char fileName[] = "data.txt";
const int chipSelect = 4;
bool playback = 0;
// Check if we got all universes
const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0);
bool universesReceived[maxUniverses];
bool sendFrame = 1;
unsigned long previousMillis = 0;
const long interval = 1000; // originalmente son 15s
void setup()
{
Serial.begin(115200);
//while (!Serial) {
//; // wait for serial port to connect. Needed for Leonardo only
//}
pixels_1.begin();
pixels_2.begin();
pixels_3.begin();
#ifdef ALAS
pixels_1.setBrightness(100); // Set BRIGHTNESS to about 1/5 (max = 255)
pixels_2.setBrightness(100); // Set BRIGHTNESS to about 1/5 (max = 255)
pixels_3.setBrightness(100); // Set BRIGHTNESS to about 1/5 (max = 255)
#endif
Serial.println("LEDs initialization test.");
initTest();
if (!SD.begin(chipSelect)) {
Serial.println("SD initialization failed!");
}
else
Serial.println("SD initialization done.");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setPALevel( RF24_PA_MAX );
radio.setAutoAck(0);
radio.setPayloadSize(8);
radio.setChannel(ch);
//radio.setChannel(0x4c);
radio.openWritingPipe(pipe);
radio.printDetails();
Serial.println("RADIO initialization done.");
// configure MCP23008 component
id.begin(0x20);
id.pinMode(0xFF);
id.setPullup(0x00);
Serial.print("ID: ");
Serial.println(id.read());
Serial.print("Num Pixels: ");
Serial.println(NUMPIXELS1 + NUMPIXELS2 + NUMPIXELS3);
CMD[0] = 1; //sync
}
void loop()
{
/*
if (Serial.available() > 0)
{
char c = Serial.read();
//Serial.println(c);
if (c == 'p')
{
playback = 1;
datafile = SD.open(fileName, FILE_READ);
Serial.print("datafile play: ");
}
else if ( c == 's')
{
playback = 0;
datafile.close();
Serial.print("datafile stop: ");
}
}
if ( radio.available() )
{
while (radio.available())
{
//done = radio.read( CMD, sizeof(CMD) );
radio.read( CMD, sizeof(CMD) );
if (DEBUG)
{
Serial.println("Got command");
for (int i = 0 ; i < 8 ; i++)
{
Serial.print(CMD[i]);
Serial.print(",");
}
Serial.println();
}
if (CMD[0] == 1) // play
{
playback = 1;
datafile = SD.open(fileName, FILE_READ);
Serial.print("datafile Radio play: ");
}
else if (CMD[0] == 0) // stop
{
playback = 0;
datafile.close();
Serial.print("datafile Radio stop: ");
}
}
}
*/
// we call the read function inside the loop
if (playback)
{
//while (datafile.available() && playback) // bloqueante hasta que termine de leer
if ( datafile.available() && playback ) // NO BLOQUEANTE hasta que termine de leer
{
//Serial.println("playback_while");
datafile.read(channelBuffer, numberOfChannels);
if (NUMPIXELS1)
{
for (int i = 0; i < NUMPIXELS1; i++)
{
pixels_1.setPixelColor(i, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
//Serial.println(channelBuffer[(i) * 3]);
}
pixels_1.show();
}
#ifdef VERONAL
if (NUMPIXELS2)
{
for (int i = NUMPIXELS1; i < NUMPIXELS1 + NUMPIXELS2; i++)
{
pixels_2.setPixelColor(i - NUMPIXELS1, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
//Serial.println(channelBuffer[(i) * 3]);
}
pixels_2.show();
}
#endif
#ifdef GUIRNALDA
if (NUMPIXELS2)
{
for (int i = NUMPIXELS1; i < NUMPIXELS1 + NUMPIXELS2; i++)
{
pixels_2.setPixelColor(i - NUMPIXELS1, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
//Serial.println(channelBuffer[(i) * 3]);
}
pixels_2.show();
}
#endif
if (NUMPIXELS3)
{
for (int i = NUMPIXELS1 + NUMPIXELS2; i < NUMPIXELS1 + NUMPIXELS2 + NUMPIXELS3; i++)
{
pixels_3.setPixelColor(i - NUMPIXELS1 - NUMPIXELS2, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
//Serial.println(channelBuffer[(i) * 3]);
}
pixels_3.show();
}
#ifdef AROS
//delayMicroseconds(19390); // 30 fps y 300 pixels) -> AROSx
delayMicroseconds(17900); // 30 fps y 313 pixels) -> AROS
#endif
#ifdef ALAS
//delayMicroseconds(9475); // 30 fps y 576 pixels ) -> ALAS grabadas a 512 pixels temporalmente
delayMicroseconds(9475); // 30 fps y 576 pixels ) -> ALAS grabadas a 512 pixels temporalmente
#endif
#ifdef ESTRELLA
delayMicroseconds(13185); // 30 fps y 409 pixels + 30 pixels) -> ESTRELLA grabada a 439 pixels
#endif
#ifdef VERONAL
delayMicroseconds(19150); // 30 fps y 409 pixels + 30 pixels) -> ESTRELLA grabada a 439 pixels
#endif
#ifdef GUIRNALDA
delayMicroseconds(16000); // 30 fps y 409 pixels + 30 pixels) -> ESTRELLA grabada a 439 pixels
#endif
}
else if ( !datafile.available() && playback ) // Cuando termina de leer el file cierra y para. NO BLOQUEANTE
{
playback = 0;
datafile.close();
}
}
else // ENTRA CUANDO SE ACABA LA PELI Y RESETEA
{
unsigned long currentMillis = millis(); // INTERVALO DE 1S DESPUES DEL FINAL DEL MASTER
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
playback = 1;
datafile = SD.open(fileName, FILE_READ);
Serial.print("datafile timer play: ");
//ENVIAR COMANDO RADIO DE SYNC PARA TODOS
radio.write( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
delay(1);
// radio.write( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
// delay(1);
// radio.write( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
// delay(1);
}
}
}
void initTest()
{
for (int i = 0 ; i < NUMPIXELS1 ; i++)
pixels_1.setPixelColor(i, 127, 0, 0);
pixels_1.show();
delay(500);
for (int i = 0 ; i < NUMPIXELS2 ; i++)
pixels_2.setPixelColor(i, 0, 127, 0);
pixels_2.show();
delay(500);
for (int i = 0 ; i < NUMPIXELS3 ; i++)
pixels_3.setPixelColor(i, 0, 0, 127);
pixels_3.show();
delay(500);
for (int i = 0 ; i < NUMPIXELS1 ; i++)
pixels_1.setPixelColor(i, 0, 0, 0);
pixels_1.show();
delay(500);
for (int i = 0 ; i < NUMPIXELS2 ; i++)
pixels_2.setPixelColor(i, 0, 0, 0);
pixels_2.show();
delay(500);
for (int i = 0 ; i < NUMPIXELS3 ; i++)
pixels_3.setPixelColor(i, 0, 0, 0);
pixels_3.show();
}