Skip to content

Commit

Permalink
Scrolling text
Browse files Browse the repository at this point in the history
Thanks @Hosseios
  • Loading branch information
bmorcelli committed Nov 26, 2024
1 parent 82e7e6f commit 630460e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"iomanip": "cpp"
},
"cmake.sourceDirectory": "C:/Projetos/M5Stick-Launcher/lib/M5GFX"
}
1 change: 1 addition & 0 deletions ports/CYD-2432S028/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ bool checkPrevPress() {
** Verifies if Select or OK was pressed
**********************************************************************/
bool checkSelPress() {
checkPowerSaveTime();
if(menuPress(SEL)) {
if(wakeUpScreen()) {
delay(200);
Expand Down
57 changes: 51 additions & 6 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#endif


/***************************************************************************************
** Function name: displayScrollingText
** Description: Scroll large texts into screen
***************************************************************************************/
void displayScrollingText(const String& text, Opt_Coord coord) {
int len = text.length();
String displayText = text + " "; // Add spaces for smooth looping
int scrollLen = len + 3; // Full text plus space buffer
static int i=0;
static long _lastmillis=0;
if (len < coord.size) {
// Text fits within limit, no scrolling needed
return;
} else if(millis()>_lastmillis+200) {
String scrollingPart = displayText.substring(i, i + (coord.size - 1)); // Display charLimit characters at a time
tft.fillRect(coord.x, coord.y, (coord.size-1) * LW * tft.textsize, LH * tft.textsize, BGCOLOR); // Clear display area
tft.setCursor(coord.x, coord.y);
tft.print(scrollingPart);
if (i >= scrollLen - coord.size) i = -1; // Loop back
_lastmillis=millis();
i++;
if(i==1) _lastmillis=millis()+1000;
}
}

/***************************************************************************************
** Function name: resetTftDisplay
** Description: set cursor to 0,0, screen and text to default color
Expand Down Expand Up @@ -333,12 +358,15 @@ void progressHandler(int progress, size_t total) {

}



/***************************************************************************************
** Function name: drawOptions
** Description: Função para desenhar e mostrar as opçoes de contexto
***************************************************************************************/
#define MAX_MENU_SIZE (int)(HEIGHT/25)
void drawOptions(int index,const std::vector<std::pair<std::string, std::function<void()>>>& options, uint16_t fgcolor, uint16_t bgcolor) {
Opt_Coord drawOptions(int index,const std::vector<std::pair<std::string, std::function<void()>>>& options, uint16_t fgcolor, uint16_t bgcolor) {
Opt_Coord coord;
int menuSize = options.size();
if(options.size()>MAX_MENU_SIZE) {
menuSize = MAX_MENU_SIZE;
Expand All @@ -359,7 +387,12 @@ void drawOptions(int index,const std::vector<std::pair<std::string, std::functio
for(i=0;i<menuSize;i++) {
if(i>=init) {
String text="";
if(i==index) text+=">";
if(i==index) {
text+=">";
coord.x=tft.getCursorX();
coord.y=tft.getCursorY();
coord.size=(WIDTH*0.8 - 10)/(LW*FONT_M) - 1;
}
else text +=" ";
text += String(options[i].first.c_str()) + " ";
tft.setCursor(WIDTH*0.10+5,tft.getCursorY()+4);
Expand All @@ -371,6 +404,7 @@ void drawOptions(int index,const std::vector<std::pair<std::string, std::functio
Exit:
if(options.size()>MAX_MENU_SIZE) menuSize = MAX_MENU_SIZE;
tft.drawRoundRect(WIDTH*0.10,HEIGHT/2-menuSize*(FONT_M*8+4)/2 -5,WIDTH*0.8,(FONT_M*8+4)*menuSize+10,5,fgcolor);
return coord;
}

/***************************************************************************************
Expand Down Expand Up @@ -467,7 +501,8 @@ void drawBatteryStatus() {
** Description: Função para desenhar e mostrar o menu principal
***************************************************************************************/
#define MAX_ITEMS (int)(HEIGHT-20)/(LH*2)
void listFiles(int index, String fileList[][3]) {
Opt_Coord listFiles(int index, String fileList[][3]) {
Opt_Coord coord;
tft.setCursor(10,10);
tft.setTextSize(FONT_M);
int i=0;
Expand All @@ -480,17 +515,24 @@ void listFiles(int index, String fileList[][3]) {
}
int nchars = (WIDTH-20)/(6*tft.textsize);
String txt=">";
int j=0;
while(i<arraySize) {
if(i>=start && fileList[i][2]!="") {
tft.setCursor(10,tft.getCursorY());
if(fileList[i][2]=="folder") tft.setTextColor(FGCOLOR-0x1111, BGCOLOR);
else if(fileList[i][2]=="operator") tft.setTextColor(ALCOLOR, BGCOLOR);
else { tft.setTextColor(FGCOLOR,BGCOLOR); }

if (index==i) txt=">";
if (index==i) {
txt=">";
coord.x=10+FM*LW;
coord.y=tft.getCursorY() + j*FM*LH;
coord.size=nchars;
}
else txt=" ";
txt+=fileList[i][0] + " ";
tft.println(txt.substring(0,nchars));
j++;
}
i++;
if (i==(start+MAX_ITEMS) || fileList[i][2]=="") break;
Expand All @@ -499,10 +541,10 @@ void listFiles(int index, String fileList[][3]) {
#if defined(HAS_TOUCH)
TouchFooter();
#endif
return coord;
}



/*********************************************************************
** Function: loopOptions
** Where you choose among the options in menu
Expand All @@ -513,16 +555,19 @@ void loopOptions(const std::vector<std::pair<std::string, std::function<void()>>
log_i("Number of options: %d", options.size());
int numOpt = options.size()-1;
drawOptions(0,options, ALCOLOR, BGCOLOR);
Opt_Coord coord;
while(1){
if (redraw) {
drawOptions(index,options, ALCOLOR, BGCOLOR);
coord=drawOptions(index,options, ALCOLOR, BGCOLOR);
if(bright){
setBrightness(100*(numOpt-index)/numOpt,false);
}
redraw=false;
delay(REDRAW_DELAY);
}

displayScrollingText(options[index].first.c_str(), coord);

if(checkPrevPress()) {
#if defined(ESC_LOGIC)
if(index==0) index = options.size() - 1;
Expand Down
12 changes: 10 additions & 2 deletions src/display.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
// display.h
#ifndef DISPLAY_H
#define DISPLAY_H
Expand Down Expand Up @@ -33,7 +34,14 @@ void displayRedStripe(String text, uint16_t fgcolor = TFT_WHITE, uint16_t bgcolo

void progressHandler(int progress, size_t total);

void drawOptions(int index,const std::vector<std::pair<std::string, std::function<void()>>>& options, uint16_t fgcolor, uint16_t bgcolor);
struct Opt_Coord {
uint16_t x=0;
uint16_t y=0;
uint16_t size=10;
};
void displayScrollingText(const String& text, Opt_Coord coord);

Opt_Coord drawOptions(int index,const std::vector<std::pair<std::string, std::function<void()>>>& options, uint16_t fgcolor, uint16_t bgcolor);

void drawSection(int x, int y, int w, int h, uint16_t color, const char* text, bool isSelected);

Expand All @@ -43,7 +51,7 @@ void drawBatteryStatus();

void drawMainMenu(int index = 0);

void listFiles(int index, String fileList[][3]);
Opt_Coord listFiles(int index, String fileList[][3]);

void TouchFooter(uint16_t color = FGCOLOR);

Expand Down
5 changes: 4 additions & 1 deletion src/sd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ void readFs(String folder, String result[][3]) {
** Where you choose what to do wuth your SD Files
**********************************************************************/
String loopSD(bool filePicker) {
Opt_Coord coord;
prog_handler = 0;
String result = "";
bool reload=false;
Expand All @@ -391,7 +392,7 @@ String loopSD(bool filePicker) {
tft.drawRoundRect(5,5,WIDTH-10,HEIGHT-10,5,FGCOLOR);

readFs(Folder, fileList);
listFiles(0, fileList);
coord=listFiles(0, fileList);

for(int i=0; i<MAXFILES; i++) if(fileList[i][2]!="") maxFiles++; else break;
while(1){
Expand All @@ -414,6 +415,8 @@ String loopSD(bool filePicker) {
redraw = false;
}

displayScrollingText(fileList[index][0], coord);

if(checkPrevPress()) {
if(index==0) index = maxFiles - 1;
else if(index>0) index--;
Expand Down

0 comments on commit 630460e

Please sign in to comment.