/* Dot matrix control Scrolling
Tutorial: https://www.electronoobs.com/eng_arduino_tut56.php
Schematic: https://www.electronoobs.com/eng_arduino_tut56_sch1.php
LedControl library: https://www.electronoobs.com/eng_arduino_ledcontrol.php
*/
#include <LedControl.h> //LedControl library: https://www.electronoobs.com/ledcontrol.php
#include "charLUT.h"
const int numDevices = 4; // number of MAX7219s used
const long scrollDelay = 40; // adjust scrolling speed
unsigned long bufferLong [14] = {0};
const unsigned char scrollText[] PROGMEM = {" Hello World !!! "};
LedControl lc = LedControl(11, 13, 10, numDevices); // DATA | CLK | CS/LOAD | number of matrices
// for test, scrolls entire font list
void scrollFont() {
for (int counter = 0x20; counter < 0x80; counter++) {
loadBufferLong(counter);
delay(50);
}
}
// Scroll Message
void scrollMessage(const unsigned char * messageString) {
int counter = 0;
int myChar = 0;
do {
// read back a char
myChar = pgm_read_byte_near(messageString + counter);
if (myChar != 0) {
loadBufferLong(myChar);
}
counter++;
}
while (myChar != 0);
}
// Load character into scroll buffer
void loadBufferLong(int ascii) {
if (ascii >= 0x20 && ascii <= 0x7F) {
for (int a = 0; a < 7; a++) { // Loop 7 times for a 5x7 font
unsigned long c = pgm_read_byte_near(font5x7 + ((ascii - 0x20) * 8) + a); // Index into character table to get row data
unsigned long x = bufferLong [a * 2]; // Load current scroll buffer
x = x | c; // OR the new character onto end of current
bufferLong [a * 2] = x; // Store in buffer
}
byte count = pgm_read_byte_near(font5x7 + ((ascii - 0x20) * 8) + 7); // Index into character table for kerning data
for (byte x = 0; x < count; x++) {
rotateBufferLong();
printBufferLong();
delay(scrollDelay);
}
}
}
// Rotate the buffer
void rotateBufferLong() {
for (int a = 0; a < 7; a++) { // Loop 7 times for a 5x7 font
unsigned long x = bufferLong [a * 2]; // Get low buffer entry
byte b = bitRead(x, 31); // Copy high order bit that gets lost in rotation
x = x << 1; // Rotate left one bit
bufferLong [a * 2] = x; // Store new low buffer
x = bufferLong [a * 2 + 1]; // Get high buffer entry
x = x << 1; // Rotate left one bit
bitWrite(x, 0, b); // Store saved bit
bufferLong [a * 2 + 1] = x; // Store new high buffer
}
}
// Display Buffer on LED matrix
void printBufferLong() {
for (int a = 0; a < 7; a++) { // Loop 7 times for a 5x7 font
unsigned long x = bufferLong [a * 2 + 1]; // Get high buffer entry
byte y = x; // Mask off first character
lc.setRow(3, a, y); // Send row to relevent MAX7219 chip
x = bufferLong [a * 2]; // Get low buffer entry
y = (x >> 24); // Mask off second character
lc.setRow(2, a, y); // Send row to relevent MAX7219 chip
y = (x >> 16); // Mask off third character
lc.setRow(1, a, y); // Send row to relevent MAX7219 chip
y = (x >> 8); // Mask off forth character
lc.setRow(0, a, y); // Send row to relevent MAX7219 chip
}
}
void setup() {
for (int x = 0; x < numDevices; x++) {
lc.shutdown(x, false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(x, 8); // Set the brightness to default value
lc.clearDisplay(x); // and clear the display
}
}
void loop() {
scrollMessage(scrollText);
//scrollFont();
}