powerLD_ML_2040_matrix.ino
//powerLD_ML_2040_matrix
#include <max7219.h>
#define SELECT_PIN 3
#define CLOCK_PIN 4
#define DATA_PIN 1
const byte interruptPin = 2;
const byte on = 7;
int stringDat1[112];
int binTab[8] = {0, 32, 16, 8, 4, 2, 1};
int lastSource[5];
bool onR = false;
char stringRadio[8] = "RADIO ";
char stringAtape[8] = "ATAPE ";
char stringCd[8] = "CD ";
char stringPhono[8] = "PHONO ";
char stringAtape2[8] = "A.TP2 ";
char stringBallance[10] = "BL";
char stringBass[10] = "BA";
char stringTreble[10] = "TR";
char stringLoudOff[10] = "LO OF";
char stringLoudOn[10] = "LO ON";
char vol[5] = " V ";
char stringNeg[10] = "- ";
char stringPlus[10] = "+ ";
char stringNev[10] = "><";
char stringRight[10] = " >";
char stringLeft[10] = "< ";
char stringMute[10] = "--";
char stringDatD[10];
char stringDatD2[10];
char stringDatD3[10];
char stringDatPrint[20];
char stringDatPrint2[10];
char str[20];
char str2[10] = " ";
char clearDat[7] = " ";
volatile int res = 0;
volatile int pos1 = 0;
volatile int pos2 = 0;
volatile int pos3 = 0;
volatile int count = 0;
volatile byte last = 1;
volatile byte print = 0;
volatile byte printMatr = 0;
volatile byte printMatr2 = 0;
volatile byte sound = 0;
volatile byte source = 0;
volatile byte number = 0;
volatile unsigned long startMillis;
volatile unsigned long endMillis;
volatile unsigned long diffMillis;
volatile unsigned long currentMillis;
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);// initialisering af LCD
void setup() {
Serial.begin(115200);
maxInit(4, 0, CLOCK_PIN, DATA_PIN, SELECT_PIN); // initialize (turn on) the controllers
pinMode(interruptPin, INPUT);
startMillis = millis();
attachInterrupt(digitalPinToInterrupt(interruptPin), powerL, FALLING);
}
void setup2() {
}
void printMatrix (char stringDatPrint[20]) {
int i, j, iPitch;
char cTemp[32];
byte bImg[24 * 8];
iPitch = 12;
memset(bImg, 0, iPitch*8);
{
sprintf(cTemp, stringDatPrint);
maxDrawString(cTemp, bImg, iPitch, 1); // draw digits into "image" buffer
for (j=0; j<iPitch*8; j++)
{
maxSendImage(bImg, iPitch);
maxScrollBitmap(bImg, iPitch, 1);
delay(40);
} // for j
}
}
void printMatrix2 (char stringDatPrint[20]) {
int i, j, iPitch;
char cTemp[32];
byte bImg[24 * 8];
iPitch = 8;
memset(bImg, 0, iPitch*8);
{
sprintf(cTemp, stringDatPrint);
maxDrawString(cTemp, bImg, iPitch, 1); // draw digits into "image" buffer
for (j=0; j<iPitch*8; j++)
{
maxSendImage(bImg, iPitch);
//maxScrollBitmap(bImg, iPitch, 1);
delay(10);
} // for j
}
}
int decode (int pos1, int pos2, int pos3)
{
switch (pos1)
{
case 1 : {//RADIO
strcpy (stringDatD, stringRadio);
}
break;
case 17 : {//A.TAPE
strcpy (stringDatD, stringAtape);
}
break;
case 18 : {//CD
strcpy (stringDatD, stringCd);
}
break;
case 19 : {//PHONO
strcpy (stringDatD, stringPhono);
}
break;
case 20 : {//A.TP2
strcpy (stringDatD, stringAtape2);
}
break;
case 2 : {//BALL
strcpy (stringDatD, stringBallance);
}
break;
case 4 : {//BASS
strcpy (stringDatD, stringBass);
}
break;
case 6 : {//TREBLE
strcpy (stringDatD, stringTreble);
}
break;
case 32 : {//LOUD
strcpy (stringDatD, stringLoudOff);
}
break;
case 33 : {//LOUD
strcpy (stringDatD, stringLoudOn);
}
break;
default: {
return 0;
}
}
if (pos2 >33)
{
switch (pos2)
{
case 35 : {//Neg
strcpy (stringDatD2, stringNeg);
}
break;
case 36 : {//Plus
strcpy (stringDatD2, stringPlus);
}
break;
case 37 : {//><
strcpy (stringDatD2, stringNev);
}
break;
case 38 : {//>
strcpy (stringDatD2, stringRight);
}
break;
case 39 : {//<
strcpy (stringDatD2, stringLeft);
}
break;
case 50 : {//MUTE
strcpy (stringDatD3, stringMute);
//lastP = pos2;
}
break;
default: {
return 0;
}
}
}
if (pos1 == 1 || pos1 == 17 || pos1 == 18 || pos1 == 19 || pos1 == 20)
{
if (pos1 > 1 && pos1 != 18)
{
strcpy (stringDatPrint, stringDatD);
if (pos3 < 33)
sprintf (str, "%d", pos3);
strcat (stringDatPrint, vol);
strcat (stringDatPrint, str);
printMatr = 1;
Serial.print (stringDatPrint);
}
else if (pos1 == 18)
{
Serial.println (pos2);
strcpy (stringDatPrint,
stringDatD);
sprintf (str, "%d", pos2);
sprintf (str2, " %d", pos3);
strcat (stringDatPrint, str);
strcat (stringDatPrint, vol);
strcat (stringDatPrint, str2);
printMatr = 1;
Serial.print (stringDatPrint);
}
else if (pos2 > 0)
{
strcpy (stringDatPrint, stringDatD);
sprintf (str, "%d", pos2);
if (pos3 < 33)
sprintf (str2, " %d", pos3);
strcat (stringDatPrint, str);
strcat (stringDatPrint, vol);
strcat (stringDatPrint, str2);
printMatr = 1;
Serial.print (stringDatPrint);
pos2 = 0;
}
}
if (pos1 == 32 || pos1 == 33)
{
strcpy (stringDatPrint2, stringDatD);
printMatr2 = 1;
Serial.print (stringDatPrint2);
}
if (pos1 == 2)
{
strcpy (stringDatPrint2, stringDatD);
//printMatr2 = 1;
strcat (stringDatPrint2, stringDatD2);
sprintf (str, "%d", pos3);
strcat (stringDatPrint2, str);
//delay (900);
printMatr2 = 1;
Serial.print (stringDatPrint2);
}
if (pos1 == 4)
{
strcpy (stringDatPrint2, stringDatD);
strcat (stringDatPrint2, stringDatD2);
sprintf (str, "%d", pos3);
strcat (stringDatPrint2, str);
printMatr2 = 1;
Serial.print (stringDatPrint2);
}
if (pos1 == 6)
{
strcpy (stringDatPrint2, stringDatD);
strcat (stringDatPrint2, stringDatD2);
sprintf (str, "%d", pos3);
strcat (stringDatPrint2, str);
printMatr2 = 1;
Serial.print (stringDatPrint2);
}
/*if (pos3 == 40)
{
strcpy (stringDatPrint, stringMute);
printMatr = 1;
Serial.println(stringDatPrint);//Skriver til serial monitor
}*/
return 0;
}
void powerL() {
int i = 0;
endMillis = millis();
diffMillis = endMillis - startMillis;
if (diffMillis > 11 && diffMillis < 14 && count == 49)//source + Nummer
{
//stop = 1;
res = 0;
for (i = 12 ; i < 18; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 11];
}
pos1 = res;
source = res;
print = 1;
res = 0;
for (i = 36 ; i < 42; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 35];
}
pos2 = res;
number = res;
sound = 0;
}
if (diffMillis > 11 && diffMillis < 14 && count == 48)//Volume
{
res = 0;
for (i = 38 ; i < 44; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 37];
}
pos3 = res * 2;
print = 1;
}
if (diffMillis > 11 && diffMillis < 14 && count == 48)//LOUD ON OFF
{
if (sound)
{
if (stringDat1[45] == 1)//LOUD ON
pos1 = 33;
else
pos1 = 32;
print = 1;
}
}
if (diffMillis > 11 && diffMillis < 14 && count == 48)//return from sound
{
if (sound)
{
if (stringDat1[26] == 0)
{
pos1 = lastSource[1];
pos2 = lastSource[2];
}
}
if (stringDat1[24] == 1)
{
pos3 = 40;
}
else
//pos2 = lastP2;
print = 1;
}
if (diffMillis > 11 && diffMillis < 14 && count == 36)//Sound Bass Treble
{
res = 0;
for (i = 24 ; i < 29; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 23];
}
//Serial.println(res);
pos1 = res;
print = 1;
res = 0;
for (i = 31 ; i < 37; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 30];
}
if (res > 56)
{
res = 64 - res;
pos2 = 35;
}
else
pos2 = 36;
pos3 = res;
print = 1;
sound = 1;
}
if (diffMillis > 11 && diffMillis < 14 && count == 44)//Sound Ball
{
lastSource[1] = source;
lastSource[2] = number;
res = 0;
for (i = 24 ; i < 30; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 23];
}
pos1 = res;
print = 1;
res = 0;
for (i = 39 ; i < 45; i++)
{
if (stringDat1[i] == 1)
res = res + binTab[i - 38];
}
if (res == 0)
pos2 = 37;
if (res > 56)
{
res = 64 - res;
pos2 = 39;
}
else
pos2 = 38;
if (res == 0)
pos2 = 37;
pos3 = res;
print = 1;
sound = 1;
}
startMillis = millis();
if (diffMillis > 13 && diffMillis < 17)
count = 0;
count ++;
if (last == 1 && diffMillis > 4 && diffMillis < 8)
{
stringDat1[count] = 1;
last = 1;
}
else if (last == 0 && diffMillis > 8)
{
stringDat1[count] = 1;
last = 1;
}
else
{
stringDat1[count] = 0;
last = 0;
}
if (print == 1)
decode (pos1, pos2, pos3);
print = 0;
}
void loop() {
}
void loop1() {
if (printMatr)
{
printMatrix (stringDatPrint);
printMatr = 0;
}
if (printMatr2)
{
printMatrix2 (stringDatPrint2);
printMatr2 = 0;
}
}
Compile med: Arduino ide se www.arduino.cc
Bemærk:
dette er eksperimentel software der er ingen garanti for brugbarhed, det
kan derimod sandsynligvis være skadeligt, kun til brug i Danmark.