#include <LiquidCrystal.h>
#include <LedControl.h>
#include <SD.h>
#include "dmx.h"
#define DEBUG_ENABLED
#ifdef DEBUG_ENABLED
#define DEBUG_PRINT(value) Serial.print(value)
#define DEBUG_PRINTLN(value) Serial.println(value)
#else
#define DEBUG_PRINT(value)
#define DEBUG_PRINTLN(value)
#endif
// DMX Universe ports
#define U1 Serial1
#define U2 Serial2
#define U3 Serial3
#ifndef DEBUG_ENABLED
#define U4 Serial
#endif
// LCD
#define lcd_rs 13
#define lcd_enable 12
#define lcd_d4 7
#define lcd_d5 6
#define lcd_d6 5
#define lcd_d7 4
LiquidCrystal lcd(lcd_rs, lcd_enable, lcd_d4, lcd_d5, lcd_d6, lcd_d7);
// Joystick
#define VERT_PIN A3
#define HORZ_PIN A4
#define SEL_PIN 2
// SD Card
#define CS_PIN 53
File root;
char version[4] = "0.01";
// DMX Config variables
int currentUniverse = 1;
#ifndef DEBUG_ENABLED
int maxUniverse = 4;
#else
int maxUniverse = 3;
#endif
void enableUniverse(int n)
{
U1.end();
DEBUG_PRINTLN("DMX_CTRL: Stopping UART 1.");
U2.end();
DEBUG_PRINTLN("DMX_CTRL: Stopping UART 2.");
U3.end();
DEBUG_PRINTLN("DMX_CTRL: Stopping UART 3.");
#ifndef DEBUG_ENABLED
U4.end();
#endif
while (n > 0)
{
switch (n)
{
case 1:
U1.begin(250000);
DEBUG_PRINTLN("DMX_CTRL: UART 1 initialized to DMX speed.");
break;
case 2:
U2.begin(250000);
DEBUG_PRINTLN("DMX_CTRL: UART 2 initialized to DMX speed.");
break;
case 3:
U3.begin(250000);
DEBUG_PRINTLN("DMX_CTRL: UART 3 initialized to DMX speed.");
break;
#ifndef DEBUG_ENABLED
case 4:
U4.begin(250000);
break;
#endif
}
n--;
}
}
void printUniverse(int n)
{
lcd.setCursor(0, 0);
lcd.print("UNIVERSE");
lcd.setCursor(9, 0);
lcd.print(n);
DEBUG_PRINT("LCD: Printing UNIVERSE ");
DEBUG_PRINT(n);
DEBUG_PRINTLN(".");
}
void printPreset(char preset[], bool active = false)
{
lcd.setCursor(0, 1);
lcd.print("PRESET:");
lcd.setCursor(8, 1);
lcd.print(preset);
DEBUG_PRINT("LCD: Printing \"");
DEBUG_PRINT(preset);
DEBUG_PRINTLN("\" to the LCD.");
if (active)
{
lcd.setCursor(15, 1);
lcd.print("*");
DEBUG_PRINTLN("LCD: This preset is an active preset.");
}
}
#ifdef DEBUG_ENABLED
LedControl U1_test = LedControl(28, 24, 26, 8);
LedControl U2_test = LedControl(44, 40, 42, 8);
LedControl U3_test = LedControl(36, 32, 34, 8);
void initMatrix(LedControl matrix)
{
for (int i = 0; i < 16; i++)
{
matrix.shutdown(i, false);
matrix.setIntensity(i, 15);
matrix.clearDisplay(i);
}
}
int* getLedPosition(int channel)
{
int *values = new int[3];
int module = 0;
int row = 0;
int column = 0;
int match = 0;
while (true)
{
match++;
if (match == channel)
{
values[0] = module;
values[1] = column;
values[2] = row;
return values;
}
column++;
if (column > 7)
{
column = 0;
row++;
}
if (row > 7)
{
row = 0;
module++;
}
}
}
void processLeds()
{
for (int m = 0; m < 8; m++) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
U1_test.setLed(m, j, i, true);
U2_test.setLed(m, j, i, true);
U3_test.setLed(m, j, i, true);
}
}
}
}
#endif
void initializeSD()
{
if (!SD.begin(CS_PIN))
{
DEBUG_PRINTLN("Card initialization failed!");
while (true);
}
}
File mainConfig;
void setup() {
lcd.begin(16, 2);
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
#ifdef DEBUG_ENABLED
Serial.begin(9600);
DEBUG_PRINT("COLOR DMX CONTROLER VERSION:");
DEBUG_PRINTLN(version);
initMatrix(U1_test);
DEBUG_PRINTLN("Matrix 1 initialized.");
initMatrix(U2_test);
DEBUG_PRINTLN("Matrix 2 initialized.");
initMatrix(U3_test);
DEBUG_PRINTLN("Matrix 3 initialized.");
//processLeds();
#endif
initializeSD();
root = SD.open("/");
mainConfig = SD.open("config.txt");
if (mainConfig) {
DEBUG_PRINTLN("SD: config.txt");
while (mainConfig.available())
{
Serial.write(mainConfig.read());
}
DEBUG_PRINTLN();
mainConfig.close();
}
else
{
DEBUG_PRINTLN("SD: Can't read config.txt.");
}
printUniverse(currentUniverse);
printPreset("PRESET", true);
enableUniverse(3);
}
int joystickState = 0;
// 0 - nullState
// 1 - TOP
// 2 - BOTTOM
// 3 - RIGHT
// 4 - LEFT
int buttonState = 0;
// 0 - nullState
// 1 - button is pressed
void changeUniverse(bool direction = true)
{
DEBUG_PRINT("DMX_CTRL: Switching universe from ");
DEBUG_PRINT(currentUniverse);
if (direction)
{
currentUniverse++;
}
else
{
currentUniverse--;
}
DEBUG_PRINT(" to ");
DEBUG_PRINTLN(currentUniverse);
printUniverse(currentUniverse);
}
void processJoystick()
{
int horz = analogRead(HORZ_PIN);
int vert = analogRead(VERT_PIN);
bool selPressed = digitalRead(SEL_PIN) == LOW;
if (horz > 1019 && vert < 515 && vert > 509)
{
joystickState = 4;
}
else if (horz < 32 && vert < 515 && vert > 509)
{
joystickState = 3;
}
else if (horz < 515 && horz > 509 && vert > 1019)
{
joystickState = 1;
}
else if (horz > 509 && horz < 515 && vert < 32)
{
joystickState = 2;
}
else
{
switch (joystickState)
{
case 3:
DEBUG_PRINTLN("JOYSTICK: RIGHT");
if (currentUniverse < maxUniverse)
{
changeUniverse();
}
break;
case 4:
DEBUG_PRINTLN("JOYSTICK: LEFT");
if (currentUniverse > 1)
{
changeUniverse(false);
}
break;
case 1:
DEBUG_PRINTLN("JOYSTICK: UP");
break;
case 2:
DEBUG_PRINTLN("JOYSTICK: DOWN");
break;
}
joystickState = 0;
}
if (selPressed)
{
buttonState = 1;
DEBUG_PRINTLN("JOYSTICK: Button is pressed.");
}
else
{
if(buttonState == 1)
{
DEBUG_PRINTLN("JOYSTICK: Button was released");
}
buttonState = 0;
}
}
void loop() {
processJoystick();
}