//
// ESP32 64x8 Scrolling Message - this script uses two 32x8 panels
// to create a single 64x8 maxtrix.
//

#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>

#define SCROLLING_MESSAGE "Titanium Electrical Solutions"
#define SCROLL_ENABLED true
// #define SCROLL_ENABLED false
#define SCROLL_DELAY 20

#define PIN 2

// Two matrix matrix
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, 2, 1, PIN,
  NEO_MATRIX_TOP   + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE +
  NEO_TILE_TOP     + NEO_TILE_LEFT   + NEO_TILE_ROWS   + NEO_TILE_PROGRESSIVE,
  NEO_GRB          + NEO_KHZ800);

// Other declarations

const int displayWidth = matrix.width();

const char myMessage[] = SCROLLING_MESSAGE;
const int myMessageCharacterCount = (sizeof(myMessage)/sizeof(myMessage[0])) - 1;

const uint16_t red    = matrix.Color(255, 0, 0);
const uint16_t green  = matrix.Color(0, 255, 0);
const uint16_t blue   = matrix.Color(0, 0, 255);
const uint16_t yellow = matrix.Color(255, 255, 0);
const uint16_t black  = matrix.Color(0, 0, 0);

const uint16_t rotatingColors[] = {red, green, blue, yellow};
const int rotatingColorsCount = (sizeof(rotatingColors)/sizeof(rotatingColors[0]));

//
// Setup function
//

void setup() {
  Serial.begin(115200);
  Serial.print("myMessage has ");
  Serial.print(myMessageCharacterCount);
  Serial.print(" characters and ");
  Serial.print(rotatingColorsCount);
  Serial.print(" rotating colors. Matrix panel is ");
  Serial.print(displayWidth);
  Serial.println(" pixels in width.");

  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(255);
  matrix.setTextColor(rotatingColors[0]);
}

//
// Loop function
//

int colorIterator = 0;

// Starting charPosition is the rightmost pixel on the display which is 
// where the start of the first character is initially displayed.
int charPosition = displayWidth;

// restartPosition is the "virtual" position of the first character when   
// the last character scrolls of the leftmost pixel of the display.  This
// position assumes each character is 6 pixels wide.
int restartPosition = -((myMessageCharacterCount) * 6);

void loop() {
  matrix.fillScreen(black);
  
  if (SCROLL_ENABLED) {
    matrix.setCursor(charPosition, 0);
  } else {
    matrix.setCursor(1, 0);    
  }
 
  matrix.print(myMessage);
  
  // If last character has scrolled off of the display, change the text to
  // the next color and reset the cursor position to the rightmost pixel
  if (--charPosition <= restartPosition) {
    if (++colorIterator >= rotatingColorsCount) colorIterator = 0;
    matrix.setTextColor(rotatingColors[colorIterator]);
    charPosition = displayWidth;
  } 
  
  matrix.show();
  delay(SCROLL_DELAY);
}
X Coordinate -->
Y Coordinate -->