// Setup for LCD screen
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <avr/pgmspace.h>
#define TFT_CS 10
#define TFT_RST 6
#define TFT_DC 7
#define TFT_SCLK 13
#define TFT_MOSI 11
// Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
// Setup for PROGMEM
const char string_0[] PROGMEM = "104002013";
const char string_1[] PROGMEM = "Azelea Isip";
const char string_2[] PROGMEM = "ENG20009 Engineering Technology Inquiry Project";
const char string_3[] PROGMEM = "Semester 2, 2023";
// list of all the strings in PROGMMEM
const char *const string_table[] PROGMEM = {string_0, string_1, string_2, string_3};
char buffer[50];
int listSize = 4;
int longestStr = 0; // Var for determining which string is the longest (List index)
char comp[50], longestLength[50], displayText[50];
void setup() {
Serial.begin(9600);
// LCD Setup
tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.fillScreen(ST7735_BLACK);
// Read from PROGMEM
for (int i = 0; i < listSize; i++) {
/* Using the string table in program memory requires the use of special functions to retrieve the data.
The strcpy_P function copies a string from program space to a string in RAM. */
strcpy_P(comp, (char *)pgm_read_word(&(string_table[i])));
strcpy_P(longestLength, (char *)pgm_read_word(&(string_table[longestStr])));
if (strlen(comp) > strlen(longestLength)) { // Compare length of each string in the table to determine the longest one
longestStr = i;
}
}
strcpy_P(longestLength, (char *)pgm_read_word(&(string_table[longestStr])));
}
int x = 0, y = 10, dw = 3;
void loop()
{
int16_t x1, y1, wid = tft.width();
uint16_t w, h; // Width and height of boundary of longest string
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
tft.setTextWrap(false);
tft.getTextBounds(longestLength, x, y, &x1, &y1, &w, &h); // Helper to determine size of a string (longestLength) with current font/size, returns boundary W H
// Scroll the text
for (int steps = wid + w; steps >= 0; steps -= dw) {
for (int i = 0; i < listSize; i++) {
strcpy_P(displayText, (char *)pgm_read_word(&(string_table[i])));
x = steps - w;
x -= dw;
tft.setCursor(x, y+10*i);
tft.print(displayText);
tft.print(" ");
}
}
}