This is what I run on the Arduino. Some of it's a bit sloppy.
#include "MatrixDisplay.h"
#include "DisplayToolbox.h"
#include "betterfont.h"
const byte pinCS1 = 10;
const byte pinCS2 = 9;
const byte pinWR = 11;
const byte pinData = 12;
const byte nDisplays = 2;
char command = 'm';
char param[2];
char env[1] = {7};
// Macro to make it the initDisplay function a little easier to understand
#define setMaster(dispNum, CSPin) initDisplay(dispNum,CSPin, true)
#define setSlave(dispNum, CSPin) initDisplay(dispNum,CSPin,false)
// Init Matrix
MatrixDisplay disp(nDisplays,pinWR,pinData, true);
// Pass a copy of the display into the toolbox
DisplayToolbox toolbox(&disp);
// Prepare boundaries
uint8_t X_MAX = 0;
uint8_t Y_MAX = 0;
const int maxString = 1000;
char banner[maxString];
long lastUpdate = 0;
const int timeout = 5000; //ms
void setup() {
Serial.begin(9600);
// Fetch bounds (dynamically work out how large this display is)
X_MAX = disp.getDisplayCount() * (disp.getDisplayWidth()-1)+1;
Y_MAX = disp.getDisplayHeight();
// Prepare displays
disp.setMaster(0,pinCS1);
disp.setSlave(1,pinCS2);
// banner[0] = 'k';
// banner[1] = '\0';
// scrollText();
// disp.syncDisplays();
}
void loop() {
getSerial();
while(banner[0] == '/' && banner[1] == '/'){
command = banner[2];
param[0] = banner[3];
param[1] = banner[4];
for (int i = 0; i < maxString - 5; i++){
banner[i] = banner[i + 5];
}
if(command == 'b'){
toolbox.setBrightness((param[0] - 48) * 10 + (param[1] - 48));
}
//stationary text
else if(command == 's'){
disp.clear();
drawString(2, 0, env);
drawString(15, 0, banner);
disp.syncDisplays();
}
}
if (command == 'm'){
scrollText();
}
}
/*
* Copy a character glyph from the myfont data structure to
* display memory, with its upper left at the given coordinate
* This is unoptimized and simply uses setPixel() to draw each dot.
*/
void drawChar(uint8_t x, uint8_t y, int c)
{
uint8_t dots;
// just draw visible chars
if(c >= ' ' && c <= '~' + 2){
c -= 32;
}
for (char col=0; col< 5; col++) {
dots = pgm_read_byte_near(&myfont[c][col]);
for (char row=0; row < 8; row++) {
if (dots & (128>>row)) // 128 for 8 rows.
toolbox.setPixel(x+col, y+row, 1);
// else
// toolbox.setPixel(x+col, y+row, 0);
}
}
}
// Write out an entire string (Null terminated)
void drawString(int x, int y, char* c)
{
for(char i=0; i< strlen(c); i++)
{
if(x > -6 && x < X_MAX)
if(c[i] == 7){
drawChar(x, y, '~' + 1);
x+=5;
drawChar(x, y, '~' + 2);
x+=8;
}
else{
drawChar(x, y, c[i]);
x+=6; // Width of each glyph
}
}
}
void scrollText(){
int x = X_MAX;
int charsFinished = 0;
char visible[11];
int i = 0;
for (int n = 0; n < 11; n++){
visible[n] = banner[n];
}
while(charsFinished < strlen(banner)){
if(millis() - lastUpdate > 25){
lastUpdate = millis();
drawString(x, 0, visible);
disp.syncDisplays();
x--;
if (x <= -6){
charsFinished++;
x = 0;
i = 0;
while(i < 11 && banner[i + charsFinished] != '\0'){
visible[i] = banner[i + charsFinished];
i++;
}
visible[i] = banner[i + charsFinished];
}
disp.clear();
}
}
}
void getSerial(){
boolean finished = false;
long tLast = millis();
int iChar = 0;
if (Serial.available()){
while(!finished){
for (int i = 0; i < 126;){
if(Serial.available()){
banner[iChar] = Serial.read();
tLast = millis();
iChar++;
i++;
//if we get the terminating char or we get too big of a string
if(banner[iChar - 1] == 13 || iChar == 1000){
finished = true;
break;
}
}
}
if (!finished){
toolbox.setPixel(1, 1, 1);
disp.syncDisplays();
Serial.print("ready.");
long tSend = millis();
tLast = millis();
while(!Serial.available()){
if(millis() - tSend > 500){
Serial.print("ready.");
tSend = millis();
}
toolbox.setPixel(1, 1, 0);
disp.syncDisplays();
}// while(!Serial.available())
}// if not finished
}// while(!finished)
banner[iChar - 1] = '\0';
}
//if we haven't received anything, let the control box know the buffer is clear
//and arduino is ready
else
Serial.print("ready.");
}