#include <EEPROM.h>
#include <Wire.h>
/*
typedef unsigned char u8;
struct routing
{
u8 dst;
u8 src;
u8 null;
};
struct sysex
{
u8 start;
u8 vendor;
u8 null;
u8 null2;
u8 matrixsize;
u8 null3;
routing r[12];
u8 null4;
u8 end;
};
*/
// sysex message is HDR 2 + preamble 5, 12*3 + end 1 byte = 2 + 5 + 36 + 1 = 44
uint8_t Code[44] = {0xF0, 0x3F, 0x0,
0x0, 0x10, 0x0,
0x0, 0x1, 0x0,
0x1, 0x1, 0x0,
0x2, 0x1, 0x0,
0x3, 0x1, 0x0,
0x4, 0x1, 0x0,
0x5, 0x1, 0x0,
0x6, 0x1, 0x0,
0x7, 0x1, 0x0,
0x8, 0x1, 0x0,
0x9, 0x1, 0x0,
0xA, 0x1, 0x0,
0xB, 0x1, 0x0,
0x0, 0xF7};
int x, y;
void(* resetFunc) (void) = 0;
void senddmx(int port, int eingang, int ausgang);
void setup ()
{
Serial.begin (115200); // input from Nextion
senddmx(4,0,0);
}
int e;
void
loop ()
{
senddmx(0, e, 0);
senddmx(1, e, 0);
senddmx(2, e, 0);
senddmx(5, 0, 0);
delay (1000);
e = (e + 1) % 13;
}
#define OFFSET_EINGANG (6)
void
serialEvent ()
{
int mode;
int port;
int ausgang;
int eingang;
// if more than 4 bytes available, read the values
// otherwise early return
if (Serial.available() >= 4)
{
mode= Serial.read();
port = Serial.read ();
ausgang = Serial.read ();
eingang = Serial.read ();
if (mode==1 && port==3)
{
resetFunc();
}
}
else
return;
// sanity checks
// port needs to be between 0 and 5
if (port < 0 || port > 5)
return;
// if port is 4 we want ausgang and eingang to be 0...
if (port == 4 && (ausgang != 0 || eingang != 0))
return;
// for all other port values, we check ausgang and eingang are in bounds of 0-11 + 12 (mute)
if (port < 4 && (eingang < 0 || eingang > 12 || ausgang < 0 || ausgang > 11))
return;
senddmx(port, eingang, ausgang);
}
void senddmx(int port, int eingang, int ausgang)
{
if (port == 4)
{
for (int i = 0; i < 36; i++)
{
EEPROM.write (i, Code[OFFSET_EINGANG + i]);
EEPROM.write (i + 36, Code[OFFSET_EINGANG + i]);
EEPROM.write (i + 36 * 2, Code[OFFSET_EINGANG + i]);
}
return;
}
if (port < 4)
{
// read init values from eprom for port
// and write into array to send
// 36 is size of 12 outputs x 3 matrix units
for (int i = 0; i < 36; i++)
Code[OFFSET_EINGANG + i] = EEPROM.read (port * 36 + i);
// select eingang for ausgang
Code[OFFSET_EINGANG + (ausgang * 3) + 1] = eingang;
// save this in the EPROM
EEPROM.write (port * 36 + (ausgang * 3) + 1, eingang);
}
if (port == 5)
{
Serial1.begin (31250, SERIAL_8N1);
Serial2.begin (31250, SERIAL_8N1);
Serial3.begin (31250, SERIAL_8N1);
for(port = 0; port < 3; port++)
{
// read init values from eprom for port
// and write into array to send
// 36 is size of 12 outputs x 3 matrix units
for (int i = 0; i < 36; i++)
Code[OFFSET_EINGANG + i] = EEPROM.read (port * 36 + i);
// write the whole array to serial port for matrix port
switch (port)
{
case 0:
Serial1.write (Code, sizeof(Code));
Serial1.flush();
break;
case 1:
Serial2.write (Code, sizeof(Code));
Serial2.flush();
break;
case 2:
Serial3.write (Code, sizeof(Code));
Serial3.flush();
break;
}
}
Serial1.end();
Serial2.end();
Serial3.end();
}
}