// scrolltext demo for Adafruit RGBmatrixPanel library.
// Demonstrates double-buffered animation on our 16x32 RGB LED matrix:
// http://www.adafruit.com/products/420
// DOUBLE-BUFFERED ANIMATION DOES NOT WORK WITH ARDUINO UNO or METRO 328.

// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.

#include <RGBmatrixPanel.h>
#include <Fonts/FreeSansBold12pt7b.h> // too tall
#include <Fonts/FreeMono9pt7b.h> // good: matrix.setCursor(textX, 13)
#include <Fonts/FreeMonoBoldOblique9pt7b.h> //oblique too hard to read
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeSans9pt7b.h> // best: matrix.setCursor(textX, 12) cuts of fragmented pxls at top
#include <Fonts/FreeSansBold9pt7b.h> 
#include <Fonts/Tiny3x3a2pt7b.h> // totally illegible

/* Most of the signal pins/private/var/folders/kk/3bkp3ss93nn_cqz69wbhpqjr0000gn/T/.arduinoIDE-unsaved20241027-14405-1y9i06q.zdzk/scrolltext_16x32/scrolltext_16x32.ino are configurable, but the CLK pin has some
special constraints.  On 8-bit AVR boards it must be on PORTB...
Pin 11 works on the Arduino Mega.  On 32-bit SAMD boards it must be
on the same PORT as the RGB data pins (D2-D7)...
Pin 8 works on the Adafruit Metro M0 or Arduino Zero,
Pin A4 works on the Adafruit Metro M4 (if using the Adafruit RGB
Matrix Shield, cut trace between CLK pads and run a wire to A4).
*/

#define CLK  8 
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2

// Last parameter = 'true' enables double-buffering, for flicker-free,
// buttery smooth animation.  Note that NOTHING WILL SHOW ON THE DISPLAY
// until the first call to swapBuffers().  This is normal.
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

/* 
To create a color, use the helper function Color333() which will take
three 3-bit numbers and combine them into a single packed integer. For example,
the first argument, r can range from 0 to 7. Likewise for g and b. To make a 
pixel that is pure red, r would be 7 and g, b would be 0. To make a white pixel,
 set all to 7. To make a black (off) pixel, set the colors to 0.
 */

// Similar to F(), but for PROGMEM string pointers rather than literals
#define F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr

const char str[] PROGMEM = "ER S at N WBURG in 9min...17 min...29 min...";
int16_t textX = matrix.width();
int16_t textMin = (int16_t)sizeof(str) * -12;
uint16_t hue = 0;

// const uint8_t MESG_SIZE = 255;
const uint8_t CHAR_SPACING = 1;
const uint8_t SCROLL_DELAY = 5; // Adjust for speed control


void setup() {
  matrix.begin();
  matrix.setTextWrap(false); // Allow text to run off edges
  matrix.setFont(&FreeSans9pt7b); // Set the custom font
  // matrix.setTextColor(matrix.Color333(7, 0, 0)); // Set text color to red while debuggin
  matrix.setTextColor(matrix.Color333(0, 0, 7)); // Set text color to blue 
}

// void loop() with slow down
void loop() {
  // Clear background
  matrix.fillScreen(0);

  // Set the text cursor + print the string
  matrix.setCursor(textX, 12); // Position text (for use with custom font size 9pt)
  matrix.print(F2(str));

  // Move text left (w/wrap)
  if ((--textX) < textMin) textX = matrix.width();

  // Update display
  matrix.swapBuffers(false);

  // Add delay to control scroll speed
  delay(SCROLL_DELAY); // Adjust this value to control the speed. Larger value = slower scroll
}
