Skip to content

Instantly share code, notes, and snippets.

@hekras
Created February 10, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hekras/e43c4033732349e9e4dce731fc0c7a63 to your computer and use it in GitHub Desktop.
Save hekras/e43c4033732349e9e4dce731fc0c7a63 to your computer and use it in GitHub Desktop.
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif
// NeoMatrix pin
#define PIN 7
//LED pin
int LED = 13;
// MATRIX DECLARATION:
// Parameter 1 = width of NeoPixel matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
// Position of the FIRST LED in the matrix; pick two, e.g.
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
// rows or in vertical columns, respectively; pick one or the other.
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
// in the same order, or alternate lines reverse direction; pick one.
// See example below for these values in action.
// Parameter 5 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_GRBW Pixels are wired for GRBW bitstream (RGB+W NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// Example for NeoPixel Shield. In this application we'd like to use it
// as a 5x8 tall matrix, with the USB port positioned at the top of the
// Arduino. When held that way, the first pixel is at the top right, and
// lines are arranged in columns, progressive order. The shield uses
// 800 KHz (v2) pixels that expect GRB color data.
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
NEO_GRB + NEO_KHZ800);
// This the Mega's code UART MAster UART3
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
pinMode(LED, OUTPUT);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
pinMode(29, OUTPUT);
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
pinMode(36, OUTPUT);
pinMode(37, OUTPUT);
pinMode(38, OUTPUT);
pinMode(39, OUTPUT);
pinMode(40, OUTPUT);
pinMode(41, OUTPUT);
pinMode(42, OUTPUT);
pinMode(43, OUTPUT);
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
pinMode(47, OUTPUT);
pinMode(48, OUTPUT);
pinMode(49, OUTPUT);
pinMode(50, OUTPUT);
pinMode(51, OUTPUT);
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
matrix.begin();
matrix.setBrightness(16);
matrix.fill(0);
matrix.show();
// Serial.println("begin");
}
int port=0;
bool stat;
int numbert;
int statemachine = -1;
char trace[200];
void loop() {
// Check if there is anything in the Serial Buffer
// Serial.print("Serial.available = ");
// Serial.println(Serial.available());
bool refresh = false;
while (Serial.available())
{
// Read one value from the Serial buffer and store it in the variable com
int com = Serial.read();
/* Serial.print("state = ");
Serial.print(statemachine);
Serial.print(", com=");
Serial.println(com);
*/ switch(statemachine){
case -1:
if (com == '$'){
statemachine = 0; // next read port number
port = -1;
stat = false;
numbert = -1;
}
break;
case 0: // read port
if (isDigit(com)) {
if (numbert == -1) {
numbert = 0;
}
numbert = numbert*10 + (com-'0');
}
else if ((com == ' ')&&(numbert != -1)){
port = numbert;
statemachine = 1; // next read stat
numbert = -1;
}
else{
statemachine = -1;
}
break;
case 1: // read stat
if (isDigit(com)) {
if (numbert == -1) {
numbert = 0;
}
numbert = numbert*10 + (com-'0');
}
else if ((com == '!')&&(numbert != -1)){
stat = (numbert != 0);
digitalWrite(port, stat);
/* Serial.print("numbert = ");
Serial.print(numbert);
Serial.print("port = ");
Serial.print(port);
Serial.print(", stat=");
Serial.println(stat);
*/ if ((port >= 22)&&(port <= 53)){
int num = port - 22;
int x = num % 8;
int y = (num/8) * 2;
uint16_t col = (stat) ? matrix.Color(255,0,0) : matrix.Color(0,255,0);
/* Serial.print("matrix(");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(") = ");
Serial.println(col);
*/ matrix.drawPixel(x,y,col);
matrix.drawPixel(x,y+1,col);
refresh = true;
}
statemachine = -1;
}
else{
statemachine = -1;
}
break;
}
}
while (Serial3.available())
{
// Read one value from the Serial buffer and store it in the variable com
int com = Serial3.read();
/* Serial.print("state = ");
Serial.print(statemachine);
Serial.print(", com=");
Serial.println(com);
*/ switch(statemachine){
case -1:
if (com == '$'){
statemachine = 0; // next read port number
port = -1;
stat = false;
numbert = -1;
}
break;
case 0: // read port
if (isDigit(com)) {
if (numbert == -1) {
numbert = 0;
}
numbert = numbert*10 + (com-'0');
}
else if ((com == ' ')&&(numbert != -1)){
port = numbert;
statemachine = 1; // next read stat
numbert = -1;
}
else{
statemachine = -1;
}
break;
case 1: // read stat
if (isDigit(com)) {
if (numbert == -1) {
numbert = 0;
}
numbert = numbert*10 + (com-'0');
}
else if ((com == '!')&&(numbert != -1)){
stat = (numbert != 0);
digitalWrite(port, stat);
/* Serial.print("numbert = ");
Serial.print(numbert);
Serial.print("port = ");
Serial.print(port);
Serial.print(", stat=");
Serial.println(stat);
*/ if ((port >= 22)&&(port <= 53)){
int num = port - 22;
int x = num % 8;
int y = (num/8) * 2;
uint16_t col = (stat) ? matrix.Color(255,0,0) : matrix.Color(0,255,0);
/* Serial.print("matrix(");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(") = ");
Serial.println(col);
*/ matrix.drawPixel(x,y,col);
matrix.drawPixel(x,y+1,col);
refresh = true;
}
statemachine = -1;
}
else{
statemachine = -1;
}
break;
}
}
if (refresh){
matrix.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment