From 0a6437bf103e9f697ff0560bd8956fd823fbb90c Mon Sep 17 00:00:00 2001 From: Wusaweki Date: Sat, 25 Nov 2023 14:55:24 +0100 Subject: [PATCH] improve pixelshift screensaver (cherry picked from commit 9eaa369147430c7d8c94830f325192e8f2e81853) --- src/plugins/Display/Display_Mono.h | 6 ++ src/plugins/Display/Display_Mono_128X32.h | 9 +-- src/plugins/Display/Display_Mono_128X64.h | 71 ++++++++++++----------- src/plugins/Display/Display_Mono_64X48.h | 11 ++-- 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/plugins/Display/Display_Mono.h b/src/plugins/Display/Display_Mono.h index a38be58f..a360f796 100644 --- a/src/plugins/Display/Display_Mono.h +++ b/src/plugins/Display/Display_Mono.h @@ -78,6 +78,7 @@ class DisplayMono { uint8_t mLineYOffsets[5] = {}; uint8_t mExtra; + int8_t mPixelshift=0; uint32_t mStarttime = millis(); bool mDisplayActive = true; // always start with display on char mFmtText[DISP_FMT_TEXT_LEN]; @@ -94,6 +95,11 @@ class DisplayMono { mDispWidth = mDisplay->getDisplayWidth(); mDispHeight = mDisplay->getDisplayHeight(); } + + void calcPixelShift(int range) { + int8_t mod = (millis() / 10000) % ((range >> 1) << 2); + mPixelshift = mScreenSaver == 1 ? ((mod < range) ? mod - (range >> 1) : -(mod - range - (range >> 1) + 1)) : 0; + } }; /* adapted 5x8 Font for low-res displays with symbols diff --git a/src/plugins/Display/Display_Mono_128X32.h b/src/plugins/Display/Display_Mono_128X32.h index ffb96384..fa0cacdf 100644 --- a/src/plugins/Display/Display_Mono_128X32.h +++ b/src/plugins/Display/Display_Mono_128X32.h @@ -31,9 +31,8 @@ class DisplayMono128X32 : public DisplayMono { void disp(void) { mDisplay->clearBuffer(); - // set Contrast of the Display to raise the lifetime - if (3 != mType) - mDisplay->setContrast(mLuminance); + // calculate current pixelshift for pixelshift screensaver + calcPixelShift(pixelShiftRange); if ((mDisplayData->totalPower > 0) && (mDisplayData->nrProducing > 0)) { if (mDisplayData->totalPower > 999) @@ -67,6 +66,8 @@ class DisplayMono128X32 : public DisplayMono { } private: + const uint8_t pixelShiftRange = 7; // number of pixels to shift from left to right (centered -> must be odd!) + void calcLinePositions() { uint8_t yOff[] = {0, 0}; for (uint8_t i = 0; i < 4; i++) { @@ -108,7 +109,7 @@ class DisplayMono128X32 : public DisplayMono { void printText(const char *text, uint8_t line) { setFont(line); - uint8_t dispX = mLineXOffsets[line] + ((mScreenSaver==1) ? (mExtra % 7) : 0); + uint8_t dispX = mLineXOffsets[line] + pixelShiftRange / 2 + mPixelshift; if (isTwoRowLine(line)) { String stringText = String(text); diff --git a/src/plugins/Display/Display_Mono_128X64.h b/src/plugins/Display/Display_Mono_128X64.h index dae49ac0..78a18db6 100644 --- a/src/plugins/Display/Display_Mono_128X64.h +++ b/src/plugins/Display/Display_Mono_128X64.h @@ -58,8 +58,8 @@ class DisplayMono128X64 : public DisplayMono { mDisplay->drawPixel(mDispWidth-1, mDispHeight-1); */ - // set Contrast of the Display to raise the lifetime - mDisplay->setContrast(mLuminance); + // calculate current pixelshift for pixelshift screensaver + calcPixelShift(pixelShiftRange); // print total power if (mDisplayData->nrProducing > 0) { @@ -109,44 +109,48 @@ class DisplayMono128X64 : public DisplayMono { pos = (mDispWidth - mDisplay->getStrWidth(mFmtText)) / 2; mDisplay->setFont(u8g2_font_ncenB08_symbols8_ahoy); if (sun_pos!=-1) - mDisplay->drawStr(pos + sun_pos + pixelshift(), mLineYOffsets[l_Status], "G"); // sun + mDisplay->drawStr(pos + sun_pos + mPixelshift, mLineYOffsets[l_Status], "G"); // sun if (moon_pos!=-1) - mDisplay->drawStr(pos + moon_pos + pixelshift(), mLineYOffsets[l_Status], "H"); // moon + mDisplay->drawStr(pos + moon_pos + mPixelshift, mLineYOffsets[l_Status], "H"); // moon } // print yields mDisplay->setFont(u8g2_font_ncenB10_symbols10_ahoy); - mDisplay->drawStr(15 + pixelshift(), mLineYOffsets[l_YieldDay], "I"); // day - mDisplay->drawStr(15 + pixelshift(), mLineYOffsets[l_YieldTotal], "D"); // total + mDisplay->drawStr(17 + mPixelshift, mLineYOffsets[l_YieldDay], "I"); // day + mDisplay->drawStr(17 + mPixelshift, mLineYOffsets[l_YieldTotal], "D"); // total snprintf(mFmtText, DISP_FMT_TEXT_LEN, "%7.0f Wh", mDisplayData->totalYieldDay); printText(mFmtText, l_YieldDay, 25); snprintf(mFmtText, DISP_FMT_TEXT_LEN, "%7.1f kWh", mDisplayData->totalYieldTotal); printText(mFmtText, l_YieldTotal, 25); - if (mScreenSaver != 1 ) { // not practical for pixel shift screensaver - // draw dynamic RSSI bars - int rssi_bar_height = 9; - for (int i=0; i<4;i++) { - int radio_rssi_threshold = -60 - i*10; - int wifi_rssi_threshold = -60 - i*10; - if (mDisplayData->RadioRSSI > radio_rssi_threshold) - mDisplay->drawBox(0, 8+(rssi_bar_height+1)*i, 4-i,rssi_bar_height); - if (mDisplayData->WifiRSSI > wifi_rssi_threshold) - mDisplay->drawBox(mDispWidth-4+i, 8+(rssi_bar_height+1)*i, 4-i,rssi_bar_height); - } - // draw dynamic antenna and WiFi symbols - mDisplay->setFont(u8g2_font_ncenB10_symbols10_ahoy); - char sym[]=" "; - sym[0] = mDisplayData->RadioSymbol?'A':'E'; // NRF - mDisplay->drawStr(0, mLineYOffsets[l_RSSI], sym); - - if (mDisplayData->MQTTSymbol) - sym[0] = 'J'; // MQTT - else - sym[0] = mDisplayData->WifiSymbol?'B':'F'; // Wifi - mDisplay->drawStr(mDispWidth - mDisplay->getStrWidth(sym), mLineYOffsets[l_RSSI], sym); - mDisplay->sendBuffer(); + // draw dynamic RSSI bars + int xoffs; + if (mScreenSaver == 1) // shrink screenwidth for pixelshift screensaver + xoffs = pixelShiftRange/2; + else + xoffs = 0; + int rssi_bar_height = 9; + for (int i = 0; i < 4; i++) { + int radio_rssi_threshold = -60 - i * 10; + int wifi_rssi_threshold = -60 - i * 10; + if (mDisplayData->RadioRSSI > radio_rssi_threshold) + mDisplay->drawBox(xoffs + mPixelshift, 8 + (rssi_bar_height + 1) * i, 4 - i, rssi_bar_height); + if (mDisplayData->WifiRSSI > wifi_rssi_threshold) + mDisplay->drawBox(mDispWidth - 4 - xoffs + mPixelshift + i, 8 + (rssi_bar_height + 1) * i, 4 - i, rssi_bar_height); } + // draw dynamic antenna and WiFi symbols + mDisplay->setFont(u8g2_font_ncenB10_symbols10_ahoy); + char sym[]=" "; + sym[0] = mDisplayData->RadioSymbol?'A':'E'; // NRF + mDisplay->drawStr(xoffs + mPixelshift, mLineYOffsets[l_RSSI], sym); + + if (mDisplayData->MQTTSymbol) + sym[0] = 'J'; // MQTT + else + sym[0] = mDisplayData->WifiSymbol?'B':'F'; // Wifi + mDisplay->drawStr(mDispWidth - mDisplay->getStrWidth(sym) - xoffs + mPixelshift, mLineYOffsets[l_RSSI], sym); + mDisplay->sendBuffer(); + mDisplay->sendBuffer(); @@ -171,6 +175,8 @@ class DisplayMono128X64 : public DisplayMono { l_MAX_LINES = 5, }; + const uint8_t pixelShiftRange = 11; // number of pixels to shift from left to right (centered -> must be odd!) + void calcLinePositions() { uint8_t yOff = 0; uint8_t i = 0; @@ -189,11 +195,6 @@ class DisplayMono128X64 : public DisplayMono { } while(l_MAX_LINES>i); } - inline int8_t pixelshift(void) { - int8_t range = 11; // number of pixels to shift from left to right (centered) - return(mScreenSaver == 1 ? (mExtra % range - range/2) : 0); - } - inline void setLineFont(uint8_t line) { if ((line == l_TotalPower) || (line == l_Ahoy)) @@ -213,7 +214,7 @@ class DisplayMono128X64 : public DisplayMono { dispX = (mDispWidth - mDisplay->getStrWidth(text)) / 2; // center text else dispX = col; - dispX += pixelshift(); + dispX += mPixelshift; mDisplay->drawStr(dispX, mLineYOffsets[line], text); } }; diff --git a/src/plugins/Display/Display_Mono_64X48.h b/src/plugins/Display/Display_Mono_64X48.h index 9edaf5e4..68cac96f 100644 --- a/src/plugins/Display/Display_Mono_64X48.h +++ b/src/plugins/Display/Display_Mono_64X48.h @@ -22,6 +22,7 @@ class DisplayMono64X48 : public DisplayMono { u8g2_cb_t *rot = (u8g2_cb_t *)((rotation != 0x00) ? U8G2_R2 : U8G2_R0); // Wemos OLed Shield is not defined in u8 lib -> use nearest compatible monoInit(new U8G2_SSD1306_64X48_ER_F_HW_I2C(rot, reset, clock, data), type, displayData); + calcLinePositions(); printText("Ahoy!", 0); printText("ahoydtu.de", 1); @@ -32,8 +33,8 @@ class DisplayMono64X48 : public DisplayMono { void disp(void) { mDisplay->clearBuffer(); - // set Contrast of the Display to raise the lifetime - mDisplay->setContrast(mLuminance); + // calculate current pixelshift for pixelshift screensaver + calcPixelShift(pixelShiftRange); if ((mDisplayData->totalPower > 0) && (mDisplayData->nrProducing > 0)) { if (mDisplayData->totalPower > 999) @@ -67,6 +68,8 @@ class DisplayMono64X48 : public DisplayMono { } private: + const uint8_t pixelShiftRange = 4; // number of pixels to shift from left to right + void calcLinePositions() { uint8_t yOff = 0; for (uint8_t i = 0; i < 4; i++) { @@ -95,8 +98,8 @@ class DisplayMono64X48 : public DisplayMono { } void printText(const char *text, uint8_t line) { - uint8_t dispX = 0; //small display, use all we have - dispX += (mScreenSaver==1) ? (mExtra % 4) : 0; + uint8_t dispX = mLineXOffsets[line] + pixelShiftRange/2 + mPixelshift; + setFont(line); mDisplay->drawStr(dispX, mLineYOffsets[line], text); }