Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorcelli authored Oct 3, 2024
2 parents fd4083f + 1952233 commit 422559a
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 131 deletions.
145 changes: 122 additions & 23 deletions src/core/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void displayRedStripe(String text, uint16_t fgcolor, uint16_t bgcolor) {
tft.println(text);
}

void displayError(String txt) {
void displayError(String txt) {
#ifndef HAS_SCREEN
Serial.println("ERR: " + txt);
return;
Expand Down Expand Up @@ -351,7 +351,7 @@ void drawOptions(int index,std::vector<Option>& options, uint16_t fgcolor, uint1
if(index>=MAX_MENU_SIZE) init=index-MAX_MENU_SIZE+1;
for(i=0;i<menuSize;i++) {
if(i>=init) {
if(options[i].selected) tft.setTextColor(fgcolor-0x1111,bgcolor); // if selected, change Text color
if(options[i].selected) tft.setTextColor(getColorVariation(fgcolor),bgcolor); // if selected, change Text color
else tft.setTextColor(fgcolor,bgcolor);

String text="";
Expand Down Expand Up @@ -396,9 +396,16 @@ void drawSubmenu(int index,std::vector<Option>& options, String system) {
tft.setTextColor(FGCOLOR-0x2000);
tft.drawCentreString(options[menuSize-1].label.c_str(),WIDTH/2, 42+(HEIGHT-134)/2,SMOOTH_FONT);
}
tft.setTextSize(FG);
tft.setTextColor(FGCOLOR);
tft.drawCentreString(options[index].label.c_str(),WIDTH/2, 67+(HEIGHT-134)/2,SMOOTH_FONT);

int selectedTextSize = options[index].label.length() <= WIDTH/(LW*FG)-1 ? FG : FM;
tft.setTextSize(selectedTextSize);
tft.setTextColor(FGCOLOR);
tft.drawCentreString(
options[index].label.c_str(),
WIDTH/2,
67+(HEIGHT-134)/2+((selectedTextSize-1)%2)*LH/2,
SMOOTH_FONT
);

if (index+1<menuSize) {
tft.setTextSize(FM);
Expand All @@ -409,7 +416,13 @@ void drawSubmenu(int index,std::vector<Option>& options, String system) {
tft.setTextColor(FGCOLOR-0x2000);
tft.drawCentreString(options[0].label.c_str(),WIDTH/2, 102+(HEIGHT-134)/2,SMOOTH_FONT);
}
tft.drawFastHLine(WIDTH/2 - options[index].label.size()*FG*LW/2, 67+FG*LH+(HEIGHT-134)/2,options[index].label.size()*FG*LW,FGCOLOR);

tft.drawFastHLine(
WIDTH/2 - options[index].label.size()*selectedTextSize*LW/2,
67+(HEIGHT-134)/2+((selectedTextSize-1)%2)*LH/2+selectedTextSize*LH,
options[index].label.size()*selectedTextSize*LW,
FGCOLOR
);
tft.fillRect(WIDTH-5,0,5,HEIGHT,BGCOLOR);
tft.fillRect(WIDTH-5,index*HEIGHT/menuSize,5,HEIGHT/menuSize,FGCOLOR);

Expand Down Expand Up @@ -577,7 +590,7 @@ void listFiles(int index, std::vector<FileList> fileList) {
while(i<arraySize) {
if(i>=start) {
tft.setCursor(10,tft.getCursorY());
if(fileList[i].folder==true) tft.setTextColor(FGCOLOR-0x1111, BGCOLOR);
if(fileList[i].folder==true) tft.setTextColor(getColorVariation(FGCOLOR), BGCOLOR);
else if(fileList[i].operation==true) tft.setTextColor(ALCOLOR, BGCOLOR);
else { tft.setTextColor(FGCOLOR,BGCOLOR); }

Expand Down Expand Up @@ -659,7 +672,7 @@ void jpegRender(int xpos, int ypos) {

bool swapBytes = tft.getSwapBytes();
tft.setSwapBytes(true);

// Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
// Typically these MCUs are 16x16 pixel blocks
// Determine the width and height of the right and bottom edge image blocks
Expand Down Expand Up @@ -736,13 +749,13 @@ void jpegRender(int xpos, int ypos) {

bool showJpeg(FS fs, String filename, int x, int y) {
File picture;
if(fs.exists(filename))
if(fs.exists(filename))
picture = fs.open(filename, FILE_READ);
else
else
return false;

const size_t data_size = picture.size();

// Alloc memory into heap
uint8_t* data_array = new uint8_t[data_size];
if (data_array == nullptr) {
Expand All @@ -754,46 +767,132 @@ bool showJpeg(FS fs, String filename, int x, int y) {
uint8_t data;
int i = 0;
byte line_len = 0;

while (picture.available()) {
data = picture.read();
data_array[i] = data;
data_array[i] = data;
i++;

//print array on Serial
/*
Serial.print("0x");
Serial.print("0x");
if (abs(data) < 16) {
Serial.print("0");
Serial.print("0");
}
Serial.print(data, HEX);
Serial.print(data, HEX);
Serial.print(","); // Add value and comma
line_len++;
if (line_len >= 32) {
line_len = 0;
Serial.println();
}
*/
}
}

picture.close();

bool decoded = false;
if (data_array) {
decoded = JpegDec.decodeArray(data_array, data_size);
} else {
} else {
displayError(filename + " Fail");
delay(2500);
delete[] data_array; // free heap before leaving
return false;
}

if (decoded) {
jpegRender(x, y);
}

delete[] data_array; // free heap before leaving
return true;
return true;
}


/***************************************************************************************
** Function name: getComplementaryColor
** Description: Get complementary color in RGB565 format
***************************************************************************************/
uint16_t getComplementaryColor(uint16_t color) {
double r = ((color >> 11) & 0x1F) / 31.0;
double g = ((color >> 5) & 0x3F) / 63.0;
double b = (color & 0x1F) / 31.0;

double cmax = fmax(r, fmax(g, b));
double cmin = fmin(r, fmin(g, b));
double delta = cmax - cmin;

double hue = 0.0;
if (delta == 0) hue = 0.0;
else if (cmax == r) hue = 60 * fmod((g-b) / delta, 6);
else if (cmax == g) hue = 60 * ((b-r) / delta + 2);
else hue = 60 * ((r-g) / delta + 4);

if (hue < 0) hue += 360;

double lightness = (cmax + cmin) / 2;
double saturation = (delta == 0) ? 0 : delta / (1 - std::abs(2 * lightness - 1));

double compHue = fmod(hue + 180, 360);

double c = (1 - std::abs(2 * lightness - 1)) * saturation;
double x = c * (1 - std::abs(fmod(compHue / 60, 2) - 1));
double m = lightness - c / 2;

double compR = 0, compG = 0, compB = 0;
if (compHue >= 0 && compHue < 60) {
compR = c;
compG = x;
} else if (compHue >= 60 && compHue < 120) {
compR = x;
compG = c;
} else if (compHue >= 120 && compHue < 180) {
compG = c;
compB = x;
} else if (compHue >= 180 && compHue < 240) {
compG = x;
compB = c;
} else if (compHue >= 240 && compHue < 300) {
compB = c;
compR = x;
} else {
compB = x;
compR = c;
}

uint16_t compl_color = uint8_t(compR * 31) << 11 | uint8_t(compG * 63) << 5 | uint8_t(compB * 31);

// change black color
if (compl_color == 0) compl_color = color - 0x1111;

return compl_color;
}


/***************************************************************************************
** Function name: getColorVariation
** Description: Get a variation of color in RGB565 format
***************************************************************************************/
uint16_t getColorVariation(uint16_t color, int delta, int direction) {
uint8_t r = ((color >> 11) & 0x1F);
uint8_t g = ((color >> 5) & 0x3F);
uint8_t b = (color & 0x1F);

float brightness = 0.299*r/31 + 0.587*g/63 + 0.114*b/31;

if (direction < 0 || (direction == 0 && brightness >= 0.5)) {
r = max(0, r-delta);
g = max(0, g-2*delta);
b = max(0, b-delta);
} else {
r = min(31, r+delta);
g = min(63, g+2*delta);
b = min(31, b+delta);
}

uint16_t compl_color = r << 11 | g << 5 | b;

return compl_color;
}
3 changes: 3 additions & 0 deletions src/core/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

bool showJpeg(FS fs,String filename, int x=0, int y=0);

uint16_t getComplementaryColor(uint16_t color);
uint16_t getColorVariation(uint16_t color, int delta = 10, int direction = 0);

void resetTftDisplay(int x = 0, int y = 0, uint16_t fc = FGCOLOR, int size = FM, uint16_t bg = BGCOLOR, uint16_t screen = BGCOLOR);
void setTftDisplay(int x = 0, int y = 0, uint16_t fc = tft.textcolor, int size = tft.textsize, uint16_t bg = tft.textbgcolor);

Expand Down
2 changes: 1 addition & 1 deletion src/core/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void load_eeprom() {
IrRx = GROVE_SCL;
RfTx = GROVE_SDA;
RfRx = GROVE_SCL;
FGCOLOR = 0xA80F;
FGCOLOR = DEFAULTFGCOLOR;
tmz = 0;
RfModule = M5_RF_MODULE;
RfidModule = M5_RFID2_MODULE;
Expand Down
1 change: 1 addition & 0 deletions src/core/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//#define FGCOLOR TFT_PURPLE+0x3000
extern char16_t FGCOLOR;
#define DEFAULTFGCOLOR 0xA80F
#define ALCOLOR TFT_RED
#define BGCOLOR TFT_BLACK

Expand Down
10 changes: 5 additions & 5 deletions src/core/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ MainMenu::MainMenu() {
&rfidMenu,
&irMenu,
&fmMenu,
&othersMenu,
&clockMenu,
#if !defined(CORE) && !defined(CORE2)
&scriptsMenu,
#endif
#if defined(USE_NRF24_VIA_SPI)
&nrf24Menu,
#endif
#if !defined(CORE) && !defined(CORE2)
&scriptsMenu,
#endif
&othersMenu,
&clockMenu,
&configMenu,
};

Expand Down
86 changes: 58 additions & 28 deletions src/core/menu_items/ScriptsMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,62 @@
#include "modules/bjs_interpreter/interpreter.h" // for JavaScript interpreter


void ScriptsMenu::optionsMenu() {

String Folder = "/scripts";
FS* fs = NULL;
if(SD.exists(Folder)) fs = &SD;
if(LittleFS.exists(Folder)) fs = &LittleFS;
if(!fs) return; // dir not found

//String fileList[MAXFILES][3];
//readFs(fs, Folder, fileList, "bjs");

options = { };

File root = fs->open(Folder);
if (!root || !root.isDirectory()) return; // not a dir
String getScriptsFolder(FS *&fs) {
String folder;
String possibleFolders[] = {"/scripts", "/BruceScripts", "/BruceJS"};
int listSize = sizeof(possibleFolders) / sizeof(possibleFolders[0]);

for (int i = 0; i < listSize; i++) {
if(SD.exists(possibleFolders[i])) {
fs = &SD;
return possibleFolders[i];
}
if(LittleFS.exists(possibleFolders[i])) {
fs = &LittleFS;
return possibleFolders[i];
}
}
return "";
}


std::vector<Option> getScriptsOptionsList() {
std::vector<Option> opt = {};
FS* fs;
String folder = getScriptsFolder(fs);
if(folder == "") return opt; // did not find


File root = fs->open(folder);
if (!root || !root.isDirectory()) return opt; // not a dir
File file2 = root.openNextFile();

while (file2) {
if (file2.isDirectory()) continue;

String fileName = String(file2.name());
if( ! fileName.endsWith(".js") && ! fileName.endsWith(".bjs")) continue;
// else append to the choices
String entry_title = String(file2.name()); entry_title = entry_title.substring(0, entry_title.lastIndexOf(".")); // remove the extension
options.push_back({entry_title.c_str(), [=]() { run_bjs_script_headless(*fs, file2.path()); }});

String entry_title = String(file2.name());
entry_title = entry_title.substring(0, entry_title.lastIndexOf(".")); // remove the extension
opt.push_back(
{entry_title.c_str(), [=]() { run_bjs_script_headless(*fs, file2.path()); }}
);

file2 = root.openNextFile();
}
file2.close();
root.close();

options.push_back({"Load...", [=]() { run_bjs_script(); }});
options.push_back({"Main Menu", [=]() { backToMenu(); }});

return opt;
}


void ScriptsMenu::optionsMenu() {
options = getScriptsOptionsList();

options.push_back({"Load...", [=]() { run_bjs_script(); }});
options.push_back({"Main Menu", [=]() { backToMenu(); }});

delay(200);
loopOptions(options,false,true,"Scripts");
Expand All @@ -46,11 +71,16 @@ String ScriptsMenu::getName() {
}

void ScriptsMenu::draw() {
// draw the icon
tft.fillRect(iconX,iconY,80,80,BGCOLOR);
int i=0;
for(i=0;i<6;i++) {
tft.drawArc(40+iconX,40+iconY,30,20,15+60*i,45+60*i,FGCOLOR,BGCOLOR,true);
}
tft.drawArc(40+iconX,40+iconY,22,8,0,360,FGCOLOR,BGCOLOR,false);

tft.drawRect(15+iconX, 5+iconY, 50, 70, FGCOLOR);
tft.fillRect(50+iconX, 5+iconY, 15, 15, BGCOLOR);
tft.drawTriangle(50+iconX, 5+iconY, 50+iconX, 19+iconY, 64+iconX, 19+iconY, FGCOLOR);

tft.drawLine(25+iconX, 45+iconY, 30+iconX, 50+iconY, FGCOLOR);
tft.drawLine(25+iconX, 45+iconY, 30+iconX, 40+iconY, FGCOLOR);
tft.drawLine(35+iconX, 50+iconY, 45+iconX, 40+iconY, FGCOLOR);
tft.drawLine(55+iconX, 45+iconY, 50+iconX, 50+iconY, FGCOLOR);
tft.drawLine(55+iconX, 45+iconY, 50+iconX, 40+iconY, FGCOLOR);

}
Loading

0 comments on commit 422559a

Please sign in to comment.