Skip to content

Commit

Permalink
chore: Add setDisplayRotation function and update display rotation in…
Browse files Browse the repository at this point in the history
… CO2_Gadget_EINK.h AND CO2_Gadget_OLED.h
  • Loading branch information
melkati committed May 21, 2024
1 parent 86f85c5 commit 870bea9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
46 changes: 39 additions & 7 deletions CO2_Gadget_EINK.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ void displaySleep(bool value = true) // https://github.com/Bodmer/TFT_eSPI/issu
}
}

// Function to set the display rotation
void setDisplayRotation(int rotation) {
if (rotation == 1) {
display.setRotation(displayReverse ? 3 : 1);
} else if (rotation == 4) {
display.setRotation(displayReverse ? 2 : 4);
} else {
display.setRotation(rotation); // Default behavior for other values
}
// shouldRedrawDisplay = true;
}

/**
* Draws text on the display with alignment options.
*
Expand Down Expand Up @@ -372,6 +384,17 @@ void busyHighPerformanceCallback(const void* p) {
menuLoop();
}

void setDisplayReverse(bool reverse) {
if (reverse) {
Serial.printf("-->[EINK] Set display reverse\n");
setDisplayRotation(3);
} else {
Serial.printf("-->[EINK] Set display normal\n");
setDisplayRotation(1);
}
shouldRedrawDisplay = true;
}

void initDisplayFromDeepSleep(bool forceRedraw = false) {
RTC_DATA_ATTR static bool firstBoot = true;
SPI.begin(EPD_SCLK, EPD_MISO, EPD_MOSI);
Expand All @@ -386,7 +409,8 @@ void initDisplayFromDeepSleep(bool forceRedraw = false) {
}

// Set default options to draw
display.setRotation(1);
setDisplayRotation(1);
// display.setRotation(1);
display.setFont(&SmallFont);
// display.setTextColor(GxEPD_BLACK);
// display.setFullWindow();
Expand Down Expand Up @@ -440,7 +464,7 @@ void initDisplay(bool fastMode = false) {
#endif

// Set default options to draw
display.setRotation(1);
setDisplayRotation(1);
display.setFont(&SmallFont);
display.setTextColor(GxEPD_BLACK);
// display.setFullWindow();
Expand Down Expand Up @@ -479,17 +503,17 @@ void showCO2(uint16_t co2, int32_t posX, int32_t posY, bool forceRedraw) {
if (!forceRedraw && (co2 == oldCO2Value)) return;
if ((co2 == 0) || (co2 > 9999)) return;

display.setRotation(1);
setDisplayRotation(1);
display.setPartialWindow(0, 0, display.width(), display.height());

display.fillRoundRect(0, elementPosition.co2Y, display.width(), elementPosition.bifFontDigitsHeight + 10, 6, GxEPD_WHITE); // 10 = 2px for top and bottom rectangle borders + 8px for top and bottom margin
display.drawRoundRect(0, elementPosition.co2Y, display.width(), elementPosition.bifFontDigitsHeight + 10, 6, GxEPD_BLACK);

display.setRotation(4);
setDisplayRotation(4);
display.setFont(&SmallFont);
drawTextAligned(elementPosition.co2XUnits, elementPosition.co2YUnits, elementPosition.co2H - 5, elementPosition.smallFontDigitsHeight + 3, "PPM", 'c', 'b');

display.setRotation(1);
setDisplayRotation(1);
display.setFont(&BigFont);
display.setTextColor(GxEPD_BLACK);
drawTextAligned(elementPosition.co2X, elementPosition.co2Y + 2, elementPosition.co2W, elementPosition.bifFontDigitsHeight, String(co2), 'c', 'b');
Expand All @@ -505,7 +529,7 @@ void showHumidity(float hum, int32_t posX, int32_t posY, bool forceRedraw) {
String humidityStr;
if (!forceRedraw && (hum == oldHumiValue)) return;
if ((hum == 0) && (temp == 0)) return;
display.setRotation(1);
setDisplayRotation(1);
display.setPartialWindow(0, 0, display.width(), display.height());
display.drawBitmap(elementPosition.humidityXIcon, elementPosition.humidityYUnits, iconHumidityBW, 16, 16, GxEPD_BLACK);
display.fillRect(elementPosition.humidityXValue, elementPosition.humidityYValue, elementPosition.humidityWValue, elementPosition.humidityHValue, GxEPD_WHITE); // Clear previous humidity value
Expand All @@ -525,7 +549,7 @@ void showTemperature(float temp, int32_t posX, int32_t posY, bool forceRedraw) {
String tempStr;
if (!forceRedraw && (temp == oldTempValue)) return;
if ((temp == 0) && (hum == 0)) return;
display.setRotation(1);
setDisplayRotation(1);
display.setPartialWindow(0, 0, display.width(), display.height());
display.drawBitmap(elementPosition.tempXUnits, elementPosition.tempYUnits, iconTempBW, 16, 16, GxEPD_BLACK);
display.fillRect(elementPosition.tempXValue, elementPosition.tempYValue, elementPosition.tempWValue, elementPosition.tempHValue, GxEPD_WHITE); // Clear previous temperature value
Expand Down Expand Up @@ -656,6 +680,10 @@ void testRedrawValues(bool randomNumbers = false) {
void displayShowValues(bool forceRedraw = false) {
static uint32_t lastDisplayUpdate = 0;
if (isDownloadingBLE) return; // Do not update display while downloading BLE data to MyAmbiance
if (shouldRedrawDisplay) {
forceRedraw = true;
shouldRedrawDisplay = false;
}
// Return if last update less than 15 seconds ago
if (!forceRedraw && (millis() - lastDisplayUpdate < 10000)) {
return;
Expand Down Expand Up @@ -712,6 +740,10 @@ void displayShowValues(bool forceRedraw = false) {
void displayShowValues(bool forceRedraw = false) {
static uint32_t lastDisplayUpdate = 0;
if (isDownloadingBLE) return; // Do not update display while downloading BLE data to MyAmbiance
if (shouldRedrawDisplay) {
forceRedraw = true;
shouldRedrawDisplay = false;
}
// Return if last update less than 15 seconds ago
if (!forceRedraw && (millis() - lastDisplayUpdate < 15000)) {
return;
Expand Down
11 changes: 11 additions & 0 deletions CO2_Gadget_OLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ int displayHeight = 64;
// #define MENUFONT u8g2_font_6x10_mf
#define MENUFONT u8g2_font_5x8_mf

void setDisplayReverse(bool reverse) {
if (reverse) {
Serial.printf("-->[OLED] Set display reversed\n");
u8g2.setDisplayRotation(U8G2_R2);
} else {
Serial.printf("-->[OLED] Set display normal\n");
u8g2.setDisplayRotation(U8G2_R0);
}
shouldRedrawDisplay = true;
}

void setDisplayBrightness(uint32_t newBrightness) {
Serial.printf("-->[OLED] Setting display brightness value at %d\n", newBrightness);
u8g2.setContrast(newBrightness);
Expand Down

0 comments on commit 870bea9

Please sign in to comment.