change layout during runtime

This commit is contained in:
Stephan Mühl
2023-04-03 13:05:38 +02:00
parent 22970218f3
commit 5484439f75
2 changed files with 100 additions and 70 deletions

View File

@@ -31,13 +31,9 @@ GifPlayer gif;
bool showGif; bool showGif;
CRGB leds[MATRIX_WIDTH * MATRIX_HEIGHT]; CRGB leds[MATRIX_WIDTH * MATRIX_HEIGHT];
// Awtrix Big / Ulanzi // Awtrix Big / Ulanzi
FastLED_NeoMatrix matrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG);
// Awtrid Midi
//FastLED_NeoMatrix matrix(leds, 8, 8, 4, 1, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE);
// Awtrix Mini?
//FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG);
MatrixDisplayUi ui(&matrix); FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(leds, 8, 8, 4, 1, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE);
MatrixDisplayUi *ui = new MatrixDisplayUi(matrix);
DisplayManager_ &DisplayManager_::getInstance() DisplayManager_ &DisplayManager_::getInstance()
{ {
@@ -51,22 +47,22 @@ void DisplayManager_::setBrightness(uint8_t bri)
{ {
if (MATRIX_OFF && !ALARM_ACTIVE) if (MATRIX_OFF && !ALARM_ACTIVE)
{ {
matrix.setBrightness(0); matrix->setBrightness(0);
} }
else else
{ {
matrix.setBrightness(bri); matrix->setBrightness(bri);
} }
} }
void DisplayManager_::setFPS(uint8_t fps) void DisplayManager_::setFPS(uint8_t fps)
{ {
ui.setTargetFPS(fps); ui->setTargetFPS(fps);
} }
void DisplayManager_::setTextColor(uint16_t color) void DisplayManager_::setTextColor(uint16_t color)
{ {
matrix.setTextColor(color); matrix->setTextColor(color);
} }
void DisplayManager_::MatrixState(bool on) void DisplayManager_::MatrixState(bool on)
@@ -78,19 +74,19 @@ void DisplayManager_::MatrixState(bool on)
bool DisplayManager_::setAutoTransition(bool active) bool DisplayManager_::setAutoTransition(bool active)
{ {
if (ui.AppCount < 2) if (ui->AppCount < 2)
{ {
ui.disablesetAutoTransition(); ui->disablesetAutoTransition();
return false; return false;
} }
if (active && AUTO_TRANSITION) if (active && AUTO_TRANSITION)
{ {
ui.enablesetAutoTransition(); ui->enablesetAutoTransition();
return true; return true;
} }
else else
{ {
ui.disablesetAutoTransition(); ui->disablesetAutoTransition();
return false; return false;
} }
} }
@@ -109,14 +105,14 @@ void DisplayManager_::drawJPG(uint16_t x, uint16_t y, fs::File jpgFile)
void DisplayManager_::drawBMP(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w, int16_t h) void DisplayManager_::drawBMP(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w, int16_t h)
{ {
matrix.drawRGBBitmap(y, x, bitmap, w, h); matrix->drawRGBBitmap(y, x, bitmap, w, h);
} }
void DisplayManager_::applyAllSettings() void DisplayManager_::applyAllSettings()
{ {
ui.setTargetFPS(MATRIX_FPS); ui->setTargetFPS(MATRIX_FPS);
ui.setTimePerApp(TIME_PER_APP); ui->setTimePerApp(TIME_PER_APP);
ui.setTimePerTransition(TIME_PER_TRANSITION); ui->setTimePerTransition(TIME_PER_TRANSITION);
setBrightness(BRIGHTNESS); setBrightness(BRIGHTNESS);
setTextColor(TEXTCOLOR_565); setTextColor(TEXTCOLOR_565);
setAutoTransition(AUTO_TRANSITION); setAutoTransition(AUTO_TRANSITION);
@@ -124,13 +120,13 @@ void DisplayManager_::applyAllSettings()
void DisplayManager_::resetTextColor() void DisplayManager_::resetTextColor()
{ {
matrix.setTextColor(TEXTCOLOR_565); matrix->setTextColor(TEXTCOLOR_565);
} }
void DisplayManager_::clearMatrix() void DisplayManager_::clearMatrix()
{ {
matrix.clear(); matrix->clear();
matrix.show(); matrix->show();
} }
bool jpg_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap) bool jpg_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap)
@@ -146,7 +142,7 @@ bool jpg_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap)
uint8_t r = ((color & 0xF800) >> 11) << 3; uint8_t r = ((color & 0xF800) >> 11) << 3;
uint8_t g = ((color & 0x07E0) >> 5) << 2; uint8_t g = ((color & 0x07E0) >> 5) << 2;
uint8_t b = (color & 0x001F) << 3; uint8_t b = (color & 0x001F) << 3;
matrix.drawPixel(x + col, y + row, matrix.Color(r, g, b)); matrix->drawPixel(x + col, y + row, matrix->Color(r, g, b));
} }
} }
return 0; return 0;
@@ -158,11 +154,11 @@ void DisplayManager_::printText(int16_t x, int16_t y, const char *text, bool cen
{ {
uint16_t textWidth = getTextWidth(text, ignoreUppercase); uint16_t textWidth = getTextWidth(text, ignoreUppercase);
int16_t textX = ((32 - textWidth) / 2); int16_t textX = ((32 - textWidth) / 2);
matrix.setCursor(textX, y); matrix->setCursor(textX, y);
} }
else else
{ {
matrix.setCursor(x, y); matrix->setCursor(x, y);
} }
if (UPPERCASE_LETTERS && !ignoreUppercase) if (UPPERCASE_LETTERS && !ignoreUppercase)
@@ -176,35 +172,35 @@ void DisplayManager_::printText(int16_t x, int16_t y, const char *text, bool cen
} }
upperText[length] = '\0'; // Null terminator upperText[length] = '\0'; // Null terminator
matrix.print(upperText); matrix->print(upperText);
} }
else else
{ {
matrix.print(text); matrix->print(text);
} }
} }
void DisplayManager_::HSVtext(int16_t x, int16_t y, const char *text, bool clear) void DisplayManager_::HSVtext(int16_t x, int16_t y, const char *text, bool clear)
{ {
if (clear) if (clear)
matrix.clear(); matrix->clear();
static uint8_t hueOffset = 0; static uint8_t hueOffset = 0;
uint16_t xpos = 0; uint16_t xpos = 0;
for (uint16_t i = 0; i < strlen(text); i++) for (uint16_t i = 0; i < strlen(text); i++)
{ {
uint8_t hue = map(i, 0, strlen(text), 0, 255) + hueOffset; uint8_t hue = map(i, 0, strlen(text), 0, 255) + hueOffset;
uint32_t textColor = hsvToRgb(hue, 255, 255); uint32_t textColor = hsvToRgb(hue, 255, 255);
matrix.setTextColor(textColor); matrix->setTextColor(textColor);
const char *myChar = &text[i]; const char *myChar = &text[i];
matrix.setCursor(xpos + x, y); matrix->setCursor(xpos + x, y);
if (UPPERCASE_LETTERS) if (UPPERCASE_LETTERS)
{ {
matrix.print((char)toupper(text[i])); matrix->print((char)toupper(text[i]));
} }
else else
{ {
matrix.print(&text[i]); matrix->print(&text[i]);
} }
char temp_str[2] = {'\0', '\0'}; char temp_str[2] = {'\0', '\0'};
temp_str[0] = text[i]; temp_str[0] = text[i];
@@ -212,7 +208,7 @@ void DisplayManager_::HSVtext(int16_t x, int16_t y, const char *text, bool clear
} }
hueOffset++; hueOffset++;
if (clear) if (clear)
matrix.show(); matrix->show();
} }
void pushCustomApp(String name, int position) void pushCustomApp(String name, int position)
@@ -235,7 +231,7 @@ void pushCustomApp(String name, int position)
Apps.push_back(std::make_pair(name, customApps[customPagesCount])); Apps.push_back(std::make_pair(name, customApps[customPagesCount]));
} }
ui.setApps(Apps); // Add Apps ui->setApps(Apps); // Add Apps
DisplayManager.getInstance().setAutoTransition(true); DisplayManager.getInstance().setAutoTransition(true);
} }
} }
@@ -246,7 +242,7 @@ void removeCustomApp(const String &name)
{ return appPair.first == name; }); { return appPair.first == name; });
Apps.erase(it); Apps.erase(it);
ui.setApps(Apps); ui->setApps(Apps);
} }
void DisplayManager_::generateCustomPage(String name, const char *json) void DisplayManager_::generateCustomPage(String name, const char *json)
@@ -525,22 +521,22 @@ void DisplayManager_::loadNativeApps()
updateApp("bat", BatApp, SHOW_BAT, 4); updateApp("bat", BatApp, SHOW_BAT, 4);
#endif #endif
ui.setApps(Apps); ui->setApps(Apps);
setAutoTransition(true); setAutoTransition(true);
} }
void DisplayManager_::setup() void DisplayManager_::setup()
{ {
TJpgDec.setCallback(jpg_output); TJpgDec.setCallback(jpg_output);
TJpgDec.setJpgScale(1); TJpgDec.setJpgScale(1);
FastLED.addLeds<NEOPIXEL, MATRIX_PIN>(leds, MATRIX_WIDTH * MATRIX_HEIGHT); FastLED.addLeds<NEOPIXEL, MATRIX_PIN>(leds, MATRIX_WIDTH * MATRIX_HEIGHT);
gif.setMatrix(&matrix); setMatrixLayout(MATRIX_LAYOUT);
ui.setAppAnimation(SLIDE_DOWN); gif.setMatrix(matrix);
ui.setOverlays(overlays, 4); ui->setAppAnimation(SLIDE_DOWN);
ui->setOverlays(overlays, 4);
setAutoTransition(AUTO_TRANSITION); setAutoTransition(AUTO_TRANSITION);
ui.init(); ui->init();
} }
void ShowGif() void ShowGif()
@@ -556,57 +552,57 @@ void DisplayManager_::tick()
else else
{ {
if (ui.getUiState()->appState == IN_TRANSITION && !appIsSwitching) if (ui->getUiState()->appState == IN_TRANSITION && !appIsSwitching)
{ {
appIsSwitching = true; appIsSwitching = true;
showGif = false; showGif = false;
} }
else if (ui.getUiState()->appState == FIXED && appIsSwitching) else if (ui->getUiState()->appState == FIXED && appIsSwitching)
{ {
appIsSwitching = false; appIsSwitching = false;
MQTTManager.setCurrentApp(CURRENT_APP); MQTTManager.setCurrentApp(CURRENT_APP);
setAppTime(TIME_PER_APP); setAppTime(TIME_PER_APP);
} }
int remainingTimeBudget = ui.update(); int remainingTimeBudget = ui->update();
if (showGif) if (showGif)
gif.drawFrame(0, 0); gif.drawFrame(0, 0);
matrix.show(); matrix->show();
} }
} }
void DisplayManager_::clear() void DisplayManager_::clear()
{ {
matrix.clear(); matrix->clear();
} }
void DisplayManager_::show() void DisplayManager_::show()
{ {
matrix.show(); matrix->show();
} }
void DisplayManager_::leftButton() void DisplayManager_::leftButton()
{ {
if (!MenuManager.inMenu) if (!MenuManager.inMenu)
ui.previousApp(); ui->previousApp();
} }
void DisplayManager_::rightButton() void DisplayManager_::rightButton()
{ {
if (!MenuManager.inMenu) if (!MenuManager.inMenu)
ui.nextApp(); ui->nextApp();
} }
void DisplayManager_::nextApp() void DisplayManager_::nextApp()
{ {
if (!MenuManager.inMenu) if (!MenuManager.inMenu)
ui.nextApp(); ui->nextApp();
} }
void DisplayManager_::previousApp() void DisplayManager_::previousApp()
{ {
if (!MenuManager.inMenu) if (!MenuManager.inMenu)
ui.previousApp(); ui->previousApp();
} }
void snozzeTimerCallback() void snozzeTimerCallback()
@@ -689,7 +685,7 @@ void DisplayManager_::switchToApp(const char *json)
bool fast = doc["fast"] | false; bool fast = doc["fast"] | false;
int index = findAppIndexByName(name); int index = findAppIndexByName(name);
if (index > -1) if (index > -1)
ui.transitionToApp(index); ui->transitionToApp(index);
} }
void DisplayManager_::setNewSettings(const char *json) void DisplayManager_::setNewSettings(const char *json)
@@ -711,15 +707,15 @@ void DisplayManager_::setNewSettings(const char *json)
void DisplayManager_::drawProgressBar(int cur, int total) void DisplayManager_::drawProgressBar(int cur, int total)
{ {
matrix.clear(); matrix->clear();
int progress = (cur * 100) / total; int progress = (cur * 100) / total;
char progressStr[5]; char progressStr[5];
snprintf(progressStr, 5, "%d%%", progress); // Formatieren des Prozentzeichens snprintf(progressStr, 5, "%d%%", progress); // Formatieren des Prozentzeichens
printText(0, 6, progressStr, true, false); printText(0, 6, progressStr, true, false);
int leds_for_progress = (progress * MATRIX_WIDTH * MATRIX_HEIGHT) / 100; int leds_for_progress = (progress * MATRIX_WIDTH * MATRIX_HEIGHT) / 100;
matrix.drawFastHLine(0, 7, MATRIX_WIDTH, matrix.Color(100, 100, 100)); matrix->drawFastHLine(0, 7, MATRIX_WIDTH, matrix->Color(100, 100, 100));
matrix.drawFastHLine(0, 7, leds_for_progress / MATRIX_HEIGHT, matrix.Color(0, 255, 0)); matrix->drawFastHLine(0, 7, leds_for_progress / MATRIX_HEIGHT, matrix->Color(0, 255, 0));
matrix.show(); matrix->show();
} }
void DisplayManager_::drawMenuIndicator(int cur, int total, uint16_t color) void DisplayManager_::drawMenuIndicator(int cur, int total, uint16_t color)
@@ -733,11 +729,11 @@ void DisplayManager_::drawMenuIndicator(int cur, int total, uint16_t color)
int x = leftMargin + i * (menuItemWidth + pixelSpacing); int x = leftMargin + i * (menuItemWidth + pixelSpacing);
if (i == cur) if (i == cur)
{ {
matrix.drawLine(x, 7, x + menuItemWidth - 1, 7, color); matrix->drawLine(x, 7, x + menuItemWidth - 1, 7, color);
} }
else else
{ {
matrix.drawLine(x, 7, x + menuItemWidth - 1, 7, matrix.Color(100, 100, 100)); matrix->drawLine(x, 7, x + menuItemWidth - 1, 7, matrix->Color(100, 100, 100));
} }
} }
} }
@@ -784,7 +780,7 @@ void DisplayManager_::drawBarChart(int16_t x, int16_t y, const int data[], byte
int x1 = x + startX + (barWidth + 1) * i; int x1 = x + startX + (barWidth + 1) * i;
int barHeight = newData[i]; int barHeight = newData[i];
int y1 = min(8 - barHeight, 7); int y1 = min(8 - barHeight, 7);
matrix.fillRect(x1, y1 + y, barWidth, barHeight, color); matrix->fillRect(x1, y1 + y, barWidth, barHeight, color);
} }
} }
@@ -908,7 +904,7 @@ void DisplayManager_::updateAppVector(const char *json)
// Update the apps vector, set it in the UI, and save settings // Update the apps vector, set it in the UI, and save settings
Apps = std::move(newApps); Apps = std::move(newApps);
ui.setApps(Apps); ui->setApps(Apps);
saveSettings(); saveSettings();
} }
@@ -939,5 +935,27 @@ String DisplayManager_::getStat()
void DisplayManager_::setAppTime(uint16_t duration) void DisplayManager_::setAppTime(uint16_t duration)
{ {
ui.setTimePerApp(duration); ui->setTimePerApp(duration);
}
void DisplayManager_::setMatrixLayout(int layout)
{
delete matrix; // Free memory from the current matrix object
switch (layout)
{
case 0:
matrix = new FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG);
break;
case 1:
matrix = new FastLED_NeoMatrix(leds, 8, 8, 4, 1, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE);
break;
case 2:
matrix = new FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG);
break;
default:
break;
}
delete ui; // Free memory from the current ui object
ui = new MatrixDisplayUi(matrix); // Create a new ui object with the new matrix
} }

View File

@@ -51,7 +51,9 @@ EasyButton button_select(BUTTON_SELECT_PIN);
#ifdef ULANZI #ifdef ULANZI
MelodyPlayer player(BUZZER_PIN, LOW); MelodyPlayer player(BUZZER_PIN, LOW);
#else #else
class Mp3Notify{}; class Mp3Notify
{
};
SoftwareSerial mySoftwareSerial(DFPLAYER_RX, DFPLAYER_TX); // RX, TX SoftwareSerial mySoftwareSerial(DFPLAYER_RX, DFPLAYER_TX); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> dfmp3(mySoftwareSerial); DFMiniMp3<SoftwareSerial, Mp3Notify> dfmp3(mySoftwareSerial);
#endif #endif
@@ -91,14 +93,19 @@ PeripheryManager_ &PeripheryManager = PeripheryManager.getInstance();
void left_button_pressed() void left_button_pressed()
{ {
#ifdef AWTRIX_UPGRADE
PeripheryManager.playFromFile(DFMINI_MP3_CLICK); PeripheryManager.playFromFile(DFMINI_MP3_CLICK);
#endif
if (AP_MODE) if (AP_MODE)
{ {
#ifndef ULANZI
--MATRIX_LAYOUT; --MATRIX_LAYOUT;
if (MATRIX_LAYOUT < 0) if (MATRIX_LAYOUT < 0)
MATRIX_LAYOUT = 2; MATRIX_LAYOUT = 2;
saveSettings(); saveSettings();
ESP.restart(); ESP.restart();
#endif
} }
else else
{ {
@@ -109,14 +116,18 @@ void left_button_pressed()
void right_button_pressed() void right_button_pressed()
{ {
#ifdef AWTRIX_UPGRADE
PeripheryManager.playFromFile(DFMINI_MP3_CLICK); PeripheryManager.playFromFile(DFMINI_MP3_CLICK);
#endif
if (AP_MODE) if (AP_MODE)
{ {
#ifndef ULANZI
++MATRIX_LAYOUT; ++MATRIX_LAYOUT;
if (MATRIX_LAYOUT > 2) if (MATRIX_LAYOUT > 2)
MATRIX_LAYOUT = 0; MATRIX_LAYOUT = 0;
saveSettings(); saveSettings();
ESP.restart(); ESP.restart();
#endif
} }
else else
{ {
@@ -127,21 +138,22 @@ void right_button_pressed()
void select_button_pressed() void select_button_pressed()
{ {
#ifdef AWTRIX_UPGRADE
PeripheryManager.playFromFile(DFMINI_MP3_CLICK); PeripheryManager.playFromFile(DFMINI_MP3_CLICK);
#endif
DisplayManager.selectButton(); DisplayManager.selectButton();
MenuManager.selectButton(); MenuManager.selectButton();
} }
void select_button_pressed_long() void select_button_pressed_long()
{ {
PeripheryManager.playFromFile(DFMINI_MP3_CLICK);
DisplayManager.selectButtonLong(); DisplayManager.selectButtonLong();
MenuManager.selectButtonLong(); MenuManager.selectButtonLong();
} }
void select_button_tripple() void select_button_double()
{ {
PeripheryManager.playFromFile(DFMINI_MP3_CLICK_ON);
if (MATRIX_OFF) if (MATRIX_OFF)
{ {
DisplayManager.MatrixState(true); DisplayManager.MatrixState(true);
@@ -184,9 +196,9 @@ void PeripheryManager_::stopSound()
#ifdef ULANZI #ifdef ULANZI
player.stop(); player.stop();
#else #else
dfmp3.stopAdvertisement(); dfmp3.stopAdvertisement();
delay(50); delay(50);
dfmp3.stop(); dfmp3.stop();
#endif #endif
} }
@@ -261,7 +273,7 @@ void PeripheryManager_::setup()
button_right.onPressed(right_button_pressed); button_right.onPressed(right_button_pressed);
button_select.onPressed(select_button_pressed); button_select.onPressed(select_button_pressed);
button_select.onPressedFor(1000, select_button_pressed_long); button_select.onPressedFor(1000, select_button_pressed_long);
button_select.onSequence(2, 300, select_button_tripple); button_select.onSequence(2, 300, select_button_double);
#ifdef ULANZI #ifdef ULANZI
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
@@ -295,7 +307,7 @@ void PeripheryManager_::tick()
CURRENT_TEMP = bme280.readTemperature(); CURRENT_TEMP = bme280.readTemperature();
CURRENT_HUM = bme280.readHumidity(); CURRENT_HUM = bme280.readHumidity();
#endif #endif
//checkAlarms(); // checkAlarms();
MQTTManager.sendStats(); MQTTManager.sendStats();
} }
@@ -383,7 +395,7 @@ void PeripheryManager_::checkAlarms()
SNOOZE_TIME = 0; SNOOZE_TIME = 0;
} }
} }
} }
} }
} }