Working version to load 24bit and display BW.

This commit is contained in:
Jonas Diemer 2025-12-17 12:07:39 +01:00
parent e7785003af
commit 9dcdcb02ba

View File

@ -29,8 +29,11 @@ struct BMPHeader {
};
#pragma pack(pop)
// Load BMP file from SD card
// Load BMP file from SD card and rotate 90 degrees clockwise
// This rotation matches what we need for the e-ink display
uint8_t* loadBMP(const char* filename, int& width, int& height) {
// Using rotation type 1: 90 degrees clockwise
const int rotationType = 1;
Serial.printf("[SleepScreen] Trying to load BMP: %s\n", filename);
if (!SD.exists(filename)) {
@ -69,9 +72,27 @@ uint8_t* loadBMP(const char* filename, int& width, int& height) {
Serial.printf("[SleepScreen] BMP dimensions: %dx%d, %d bits/pixel\n", width, height, header.bitsPerPixel);
// E-ink display uses 1 bit per pixel (8 pixels per byte)
int bytesPerRow = (width + 7) / 8; // Calculate bytes needed, rounding up
int bufferSize = bytesPerRow * height;
// Calculate destination dimensions based on rotation type
int destWidth, destHeight;
if (rotationType == 0 || rotationType == 2) {
// No rotation or 180 degree rotation: keep same dimensions
destWidth = width;
destHeight = height;
} else {
// 90 degree rotations (clockwise or counterclockwise): swap width and height
destWidth = height;
destHeight = width;
}
Serial.printf("[SleepScreen] Output dimensions: %dx%d (rotation type: %d)\n",
destWidth, destHeight, rotationType);
// E-ink display: 1 bit per pixel (8 pixels per byte), MSB first format
int bytesPerRow = (destWidth + 7) / 8; // Round up to nearest byte
int bufferSize = bytesPerRow * destHeight;
Serial.printf("[SleepScreen] Buffer dimensions: %d bytes per row, %d total bytes\n", bytesPerRow, bufferSize);
Serial.printf("[SleepScreen] Buffer size: %d bytes (%d bytes per row)\n", bufferSize, bytesPerRow);
@ -120,34 +141,41 @@ uint8_t* loadBMP(const char* filename, int& width, int& height) {
if (header.bitsPerPixel == 1) {
// For 1-bit BMPs, read the bit directly
int byteIndex = x / 8;
int bitIndex = 7 - (x % 8); // MSB first
int bitIndex = 7 - (x % 8); // MSB first in BMP file format
// In BMPs, 1 typically means black and 0 means white
// In 1-bit BMPs, bit value 1 typically means black and 0 means white
// Check if the bit is set (1) at the specified position
isBlack = (rowBuffer[byteIndex] & (1 << bitIndex)) != 0;
} else { // 24-bit
// For 24-bit BMPs, convert RGB to grayscale
// BMP stores colors as BGR (Blue, Green, Red)
int byteIndex = x * 3;
uint8_t blue = rowBuffer[byteIndex];
uint8_t green = rowBuffer[byteIndex + 1];
uint8_t red = rowBuffer[byteIndex + 2];
// Convert to grayscale using standard weights
// Convert to grayscale using standard luminance formula
uint8_t gray = (red * 30 + green * 59 + blue * 11) / 100;
// If below threshold, consider it black
// If below threshold (128), consider it black
isBlack = (gray < 128);
}
// Calculate destination position in our display buffer (8 pixels per byte)
int destByteIndex = y * bytesPerRow + (x / 8);
int destBitIndex = 7 - (x % 8); // MSB first (leftmost pixel is highest bit)
// Apply 90 degree clockwise rotation
// For rotation type 1: destX = y, destY = width - 1 - x
int destX = y;
int destY = width - 1 - x;
// Set pixel in the display buffer (for e-ink: 0=black, 1=white)
// Calculate byte and bit position (1 bit per pixel)
int destByteIndex = destY * bytesPerRow + (destX / 8);
int destBitIndex = 7 - (destX % 8); // MSB first (leftmost pixel in highest bit)
// For e-ink display: 0=black, 1=white
if (isBlack) {
// Set to black (0) - clear bit
// Set to black (0) by clearing the corresponding bit
displayImage[destByteIndex] &= ~(1 << destBitIndex);
}
// White pixels (1) are already set by the memset(0xFF) above
// White pixels (1) are already set to 1 by the memset(0xFF) initialization
}
}
@ -173,7 +201,9 @@ void SleepScreen::onEnter() {
uint8_t* imageData = nullptr;
// Try different possible paths
const char* bmpPaths[] = {"sleep.bmp", "/sleep.bmp"};
const char* bmpPaths[] = {"sleep.bmp", "/sleep.bmp", "/SD/sleep.bmp"};
// Try loading from different paths
for (const char* path : bmpPaths) {
imageData = loadBMP(path, imageWidth, imageHeight);
if (imageData) {
@ -185,6 +215,14 @@ void SleepScreen::onEnter() {
if (imageData) {
// Image loaded successfully
Serial.printf("[SleepScreen] Drawing image: %dx%d\n", imageWidth, imageHeight);
Serial.printf("[SleepScreen] Screen dimensions: %dx%d\n", pageWidth, pageHeight);
// Print the first 16 bytes of image data (for debugging)
Serial.print("[SleepScreen] Image data sample: ");
for (int i = 0; i < 16 && i < (imageWidth+7)/8 * imageHeight; i++) {
Serial.printf("%02X ", imageData[i]);
}
Serial.println();
// Calculate position to center the image
int xPos = (pageWidth - imageWidth) / 2;
@ -192,8 +230,12 @@ void SleepScreen::onEnter() {
if (xPos < 0) xPos = 0;
if (yPos < 0) yPos = 0;
// Draw the image - this sends the 1-bit bitmap data to the e-ink display
Serial.printf("[SleepScreen] Drawing image at position: %d, %d\n", xPos, yPos);
// Draw the image - this sends the bitmap data to the e-ink display
// Note: We've applied 90-degree clockwise rotation to compensate for
// the renderer's behavior and ensure the image appears correctly
// on the e-ink display.
Serial.printf("[SleepScreen] Drawing at position: %d,%d (dimensions: %dx%d)\n",
xPos, yPos, imageWidth, imageHeight);
renderer.drawImage(imageData, xPos, yPos, imageWidth, imageHeight);
// Free the image data