Skip to content

Commit 5bf0532

Browse files
committed
fix: resolve flickering issue when updating
closes #179
1 parent 947b281 commit 5bf0532

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

include/constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ enum SYSTEM_STATUS
6666
LOADING,
6767
};
6868

69-
extern SYSTEM_STATUS currentStatus;
69+
extern volatile SYSTEM_STATUS currentStatus;

include/screen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Screen_
3333
0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff};
3434

3535
static void onScreenTimer();
36-
ICACHE_RAM_ATTR void _render();
36+
void _render();
3737
void rotate();
3838
uint8_t *getRotatedRenderBuffer();
3939

src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ unsigned long previousMillis = 0;
5454
unsigned long interval = 30000;
5555

5656
PluginManager pluginManager;
57-
SYSTEM_STATUS currentStatus = NONE;
57+
#ifdef ESP32
58+
DRAM_ATTR volatile SYSTEM_STATUS currentStatus = NONE;
59+
#else
60+
volatile SYSTEM_STATUS currentStatus = NONE;
61+
#endif
5862
WiFiManager wifiManager;
5963

6064
unsigned long lastConnectionAttempt = 0;

src/ota.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ void onOTAStart()
1212
Serial.println("OTA update started!");
1313
currentStatus = UPDATE;
1414

15+
Screen.clear();
1516
std::vector<int> bits = Screen.readBytes(letterU);
1617

1718
for (int i = 0; i < bits.size(); i++)
1819
{
19-
Screen.setPixelAtIndex(i, bits[i]);
20+
Screen.setPixelAtIndex(i, bits[i], MAX_BRIGHTNESS);
2021
}
2122
}
2223

@@ -41,6 +42,7 @@ void onOTAEnd(bool success)
4142
{
4243
Serial.println("There was an error during OTA update!");
4344
}
45+
4446
std::vector<int> bits = Screen.readBytes(letterR);
4547

4648
for (int i = 0; i < bits.size(); i++)
@@ -53,6 +55,7 @@ void onOTAEnd(bool success)
5355
#else
5456
delay(1000);
5557
#endif
58+
5659
currentStatus = NONE;
5760
Screen.loadFromStorage();
5861
}

src/screen.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void Screen_::setCurrentRotation(int rotation, bool shouldPersist)
187187
#endif
188188
}
189189

190-
uint8_t *Screen_::getRotatedRenderBuffer()
190+
IRAM_ATTR uint8_t *Screen_::getRotatedRenderBuffer()
191191
{
192192
// No rotation needed - return original buffer directly
193193
if (currentRotation == 0)
@@ -206,7 +206,7 @@ uint8_t *Screen_::getRotatedRenderBuffer()
206206
return rotatedRenderBuffer_;
207207
}
208208

209-
void Screen_::rotate()
209+
IRAM_ATTR void Screen_::rotate()
210210
{
211211
if (currentRotation == 1)
212212
{
@@ -244,14 +244,14 @@ void Screen_::rotate()
244244
}
245245
}
246246

247-
void Screen_::onScreenTimer()
247+
IRAM_ATTR void Screen_::onScreenTimer()
248248
{
249249
Screen._render();
250250
}
251251

252-
ICACHE_RAM_ATTR void Screen_::_render()
252+
IRAM_ATTR void Screen_::_render()
253253
{
254-
const auto buf = getRotatedRenderBuffer();
254+
const auto buf = (currentStatus == UPDATE) ? renderBuffer_ : getRotatedRenderBuffer();
255255

256256
// SPI data needs to be 32-bit aligned, round up before divide
257257
static unsigned long
@@ -261,13 +261,26 @@ ICACHE_RAM_ATTR void Screen_::_render()
261261

262262
static unsigned char counter = 0;
263263

264-
for (int idx = 0; idx < ROWS * COLS; idx++)
264+
if (currentStatus == UPDATE)
265265
{
266-
uint16_t scaledValue = ((uint16_t)buf[positions[idx]] * brightness_) / MAX_BRIGHTNESS;
267-
bits[idx >> 3] |= (scaledValue > counter ? 0x80 : 0) >> (idx & 7);
266+
for (int idx = 0; idx < ROWS * COLS; idx++)
267+
{
268+
if (buf[positions[idx]] > 0)
269+
{
270+
bits[idx >> 3] |= (0x80 >> (idx & 7));
271+
}
272+
}
273+
}
274+
else
275+
{
276+
// Normal rendering with PWM for grayscale
277+
for (int idx = 0; idx < ROWS * COLS; idx++)
278+
{
279+
uint16_t scaledValue = ((uint16_t)buf[positions[idx]] * brightness_) / MAX_BRIGHTNESS;
280+
bits[idx >> 3] |= (scaledValue > counter ? 0x80 : 0) >> (idx & 7);
281+
}
282+
counter += ((MAX_BRIGHTNESS + 1) / GRAY_LEVELS);
268283
}
269-
270-
counter += ((MAX_BRIGHTNESS + 1) / GRAY_LEVELS);
271284

272285
digitalWrite(PIN_LATCH, LOW);
273286
SPI.writeBytes(bits, sizeof(spi_bits));

0 commit comments

Comments
 (0)