From d08d55f701f87a9f317a9207ed5aeb6812732a20 Mon Sep 17 00:00:00 2001 From: Ron Holmes Date: Mon, 27 May 2024 14:32:30 +0800 Subject: [PATCH] Fix off by 1 error in Displaypart There was an off by one error requiring all pattern buffers to have an additional byte at the start. The error has been updated and the function has been documented for other users. Also added the inversion "~" of the white bytes to keep with the same format in the pattern buffer. --- Arduino/epd7in5_V2/epd7in5_V2.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Arduino/epd7in5_V2/epd7in5_V2.cpp b/Arduino/epd7in5_V2/epd7in5_V2.cpp index 242ed006a..64cd8f304 100644 --- a/Arduino/epd7in5_V2/epd7in5_V2.cpp +++ b/Arduino/epd7in5_V2/epd7in5_V2.cpp @@ -238,20 +238,26 @@ void Epd::DisplayFrame(const unsigned char* frame_buffer) { WaitUntilIdle(); } -void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) { +void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart, unsigned long Picture_Width, unsigned long Picture_Height) { SendCommand(0x13); - // xStart = xStart/8; - // xStart = xStart*8; + + // Ensure xStart is byte-aligned + unsigned long xStart_byte = xStart / 8; + + // Iterate over the entire display area and see if the bytes fit within the starts and width/height bounds for (unsigned long j = 0; j < height; j++) { - for (unsigned long i = 0; i < width/8; i++) { - if( (j>=yStart) && (j=xStart) && (i*8= yStart) && (j < yStart + Picture_Height) && (i >= xStart_byte) && (i < xStart_byte + Picture_Width / 8)) { + // Correctly calculate the buffer index + unsigned long buffer_index = (i - xStart_byte) + (Picture_Width / 8) * (j - yStart); + SendData(~(pgm_read_byte(&(pbuffer[buffer_index])))); + } else { + SendData(~0xFF); // ~ inverts the bytes } } } + SendCommand(0x12); DelayMs(100); WaitUntilIdle();