fix: prevent Serial.printf from blocking when USB disconnected
All checks were successful
CI / build (push) Successful in 2m23s
All checks were successful
CI / build (push) Successful in 2m23s
On ESP32-C3 with USB CDC, Serial.printf() blocks indefinitely when USB is not connected. This caused device freezes when booted without USB. Solution: Call Serial.setTxTimeoutMs(0) after Serial.begin() to make all Serial output non-blocking. Also added if (Serial) guards to high-traffic logging paths in EpubReaderActivity as belt-and-suspenders protection. Includes documentation of the debugging process and Serial call inventory. Also applies clang-format to fix pre-existing formatting issues.
This commit is contained in:
@@ -184,7 +184,8 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap, const std::str
|
||||
drawHeight = 0;
|
||||
fillWidth = static_cast<int>(bitmap.getWidth());
|
||||
fillHeight = static_cast<int>(bitmap.getHeight());
|
||||
Serial.printf("[%lu] [SLP] ACTUAL mode: centering at %d, %d (fill: %dx%d)\n", millis(), x, y, fillWidth, fillHeight);
|
||||
Serial.printf("[%lu] [SLP] ACTUAL mode: centering at %d, %d (fill: %dx%d)\n", millis(), x, y, fillWidth,
|
||||
fillHeight);
|
||||
} else if (coverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP) {
|
||||
// CROP mode: Scale to fill screen completely (may crop edges)
|
||||
// Calculate crop values to fill the screen while maintaining aspect ratio
|
||||
@@ -221,8 +222,8 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap, const std::str
|
||||
// Center the scaled image
|
||||
x = (pageWidth - fillWidth) / 2;
|
||||
y = (pageHeight - fillHeight) / 2;
|
||||
Serial.printf("[%lu] [SLP] FIT mode: scale %f, scaled size %d x %d, position %d, %d\n", millis(), scale,
|
||||
fillWidth, fillHeight, x, y);
|
||||
Serial.printf("[%lu] [SLP] FIT mode: scale %f, scaled size %d x %d, position %d, %d\n", millis(), scale, fillWidth,
|
||||
fillHeight, x, y);
|
||||
}
|
||||
|
||||
// Get edge luminance values (from cache or calculate)
|
||||
@@ -232,9 +233,8 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap, const std::str
|
||||
const uint8_t leftGray = quantizeGray(edges.left);
|
||||
const uint8_t rightGray = quantizeGray(edges.right);
|
||||
|
||||
Serial.printf("[%lu] [SLP] Edge luminance: T=%d B=%d L=%d R=%d -> gray levels T=%d B=%d L=%d R=%d\n",
|
||||
millis(), edges.top, edges.bottom, edges.left, edges.right,
|
||||
topGray, bottomGray, leftGray, rightGray);
|
||||
Serial.printf("[%lu] [SLP] Edge luminance: T=%d B=%d L=%d R=%d -> gray levels T=%d B=%d L=%d R=%d\n", millis(),
|
||||
edges.top, edges.bottom, edges.left, edges.right, topGray, bottomGray, leftGray, rightGray);
|
||||
|
||||
// Check if greyscale pass should be used (PR #476: skip if filter is applied)
|
||||
const bool hasGreyscale = bitmap.hasGreyscale() &&
|
||||
@@ -418,10 +418,10 @@ std::string SleepActivity::getEdgeCachePath(const std::string& bmpPath) {
|
||||
uint8_t SleepActivity::quantizeGray(uint8_t lum) {
|
||||
// Quantize luminance (0-255) to 4-level grayscale (0-3)
|
||||
// Thresholds tuned for X4 display gray levels
|
||||
if (lum < 43) return 0; // black
|
||||
if (lum < 128) return 1; // dark gray
|
||||
if (lum < 213) return 2; // light gray
|
||||
return 3; // white
|
||||
if (lum < 43) return 0; // black
|
||||
if (lum < 128) return 1; // dark gray
|
||||
if (lum < 213) return 2; // light gray
|
||||
return 3; // white
|
||||
}
|
||||
|
||||
EdgeLuminance SleepActivity::getEdgeLuminance(const Bitmap& bitmap, const std::string& bmpPath) const {
|
||||
@@ -434,8 +434,7 @@ EdgeLuminance SleepActivity::getEdgeLuminance(const Bitmap& bitmap, const std::s
|
||||
uint8_t cacheData[EDGE_CACHE_SIZE];
|
||||
if (cacheFile.read(cacheData, EDGE_CACHE_SIZE) == EDGE_CACHE_SIZE) {
|
||||
// Extract cached file size
|
||||
const uint32_t cachedSize = static_cast<uint32_t>(cacheData[0]) |
|
||||
(static_cast<uint32_t>(cacheData[1]) << 8) |
|
||||
const uint32_t cachedSize = static_cast<uint32_t>(cacheData[0]) | (static_cast<uint32_t>(cacheData[1]) << 8) |
|
||||
(static_cast<uint32_t>(cacheData[2]) << 16) |
|
||||
(static_cast<uint32_t>(cacheData[3]) << 24);
|
||||
|
||||
@@ -448,8 +447,8 @@ EdgeLuminance SleepActivity::getEdgeLuminance(const Bitmap& bitmap, const std::s
|
||||
result.bottom = cacheData[5];
|
||||
result.left = cacheData[6];
|
||||
result.right = cacheData[7];
|
||||
Serial.printf("[%lu] [SLP] Edge cache hit for %s: T=%d B=%d L=%d R=%d\n", millis(), bmpPath.c_str(),
|
||||
result.top, result.bottom, result.left, result.right);
|
||||
Serial.printf("[%lu] [SLP] Edge cache hit for %s: T=%d B=%d L=%d R=%d\n", millis(), bmpPath.c_str(), result.top,
|
||||
result.bottom, result.left, result.right);
|
||||
cacheFile.close();
|
||||
return result;
|
||||
}
|
||||
@@ -462,8 +461,8 @@ EdgeLuminance SleepActivity::getEdgeLuminance(const Bitmap& bitmap, const std::s
|
||||
// Cache miss - calculate edge luminance
|
||||
Serial.printf("[%lu] [SLP] Calculating edge luminance for %s\n", millis(), bmpPath.c_str());
|
||||
result = bitmap.detectEdgeLuminance(2); // Sample 2 pixels deep for stability
|
||||
Serial.printf("[%lu] [SLP] Edge luminance detected: T=%d B=%d L=%d R=%d\n", millis(),
|
||||
result.top, result.bottom, result.left, result.right);
|
||||
Serial.printf("[%lu] [SLP] Edge luminance detected: T=%d B=%d L=%d R=%d\n", millis(), result.top, result.bottom,
|
||||
result.left, result.right);
|
||||
|
||||
// Get BMP file size from already-opened bitmap for cache
|
||||
const uint32_t fileSize = bitmap.getFileSize();
|
||||
@@ -534,8 +533,7 @@ bool SleepActivity::tryRenderCachedCoverSleep(const std::string& bookPath, bool
|
||||
cacheFile.close();
|
||||
|
||||
// Extract cached values
|
||||
const uint32_t cachedBmpSize = static_cast<uint32_t>(cacheData[0]) |
|
||||
(static_cast<uint32_t>(cacheData[1]) << 8) |
|
||||
const uint32_t cachedBmpSize = static_cast<uint32_t>(cacheData[0]) | (static_cast<uint32_t>(cacheData[1]) << 8) |
|
||||
(static_cast<uint32_t>(cacheData[2]) << 16) |
|
||||
(static_cast<uint32_t>(cacheData[3]) << 24);
|
||||
EdgeLuminance cachedEdges;
|
||||
@@ -548,8 +546,8 @@ bool SleepActivity::tryRenderCachedCoverSleep(const std::string& bookPath, bool
|
||||
// Check if cover mode matches (for EPUB)
|
||||
const uint8_t currentCoverMode = cropped ? 1 : 0;
|
||||
if (StringUtils::checkFileExtension(bookPath, ".epub") && cachedCoverMode != currentCoverMode) {
|
||||
Serial.printf("[SLP] Cover mode changed (cached=%d, current=%d), invalidating cache\n",
|
||||
cachedCoverMode, currentCoverMode);
|
||||
Serial.printf("[SLP] Cover mode changed (cached=%d, current=%d), invalidating cache\n", cachedCoverMode,
|
||||
currentCoverMode);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -571,8 +569,8 @@ bool SleepActivity::tryRenderCachedCoverSleep(const std::string& bookPath, bool
|
||||
// Check if BMP file size matches cache
|
||||
const uint32_t currentBmpSize = bmpFile.size();
|
||||
if (currentBmpSize != cachedBmpSize || currentBmpSize == 0) {
|
||||
Serial.printf("[SLP] BMP size mismatch (cached=%lu, current=%lu)\n",
|
||||
static_cast<unsigned long>(cachedBmpSize), static_cast<unsigned long>(currentBmpSize));
|
||||
Serial.printf("[SLP] BMP size mismatch (cached=%lu, current=%lu)\n", static_cast<unsigned long>(cachedBmpSize),
|
||||
static_cast<unsigned long>(currentBmpSize));
|
||||
bmpFile.close();
|
||||
return false;
|
||||
}
|
||||
@@ -585,8 +583,8 @@ bool SleepActivity::tryRenderCachedCoverSleep(const std::string& bookPath, bool
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [SLP] Using cached cover sleep: %s (T=%d B=%d L=%d R=%d)\n", millis(),
|
||||
coverBmpPath.c_str(), cachedEdges.top, cachedEdges.bottom, cachedEdges.left, cachedEdges.right);
|
||||
Serial.printf("[%lu] [SLP] Using cached cover sleep: %s (T=%d B=%d L=%d R=%d)\n", millis(), coverBmpPath.c_str(),
|
||||
cachedEdges.top, cachedEdges.bottom, cachedEdges.left, cachedEdges.right);
|
||||
|
||||
// Render the bitmap with cached edge values
|
||||
// We call renderBitmapSleepScreen which will use getEdgeLuminance internally,
|
||||
|
||||
Reference in New Issue
Block a user