adafruit / Adafruit_EPD

e-paper display driver for Arduino
140 stars 56 forks source link

Fix rotation calculation for EPD modules #64

Closed trapperhoney closed 2 years ago

trapperhoney commented 2 years ago

This code was conflating the need to adjust address offset with the rotation calculation. The rotation calculation should be done in a coordinate system equal to the display height and width. The address offset must be done using a 'height' that represents the total number of addressable bytes of memory configured.


I noticed this problem using the Adafruit 2.13" Monochrome eInk board, but I believe the issue will exist for most boards in this library. When using the setRotation function with values of 2 or 3 (180 or 270 degrees of rotation), the image is shifted off of the panel by a few pixels (6 pixels in the case of this board). Any board whose height is not a multiple of 8 will have this problem.

Below is a test program and an image that shows it running at rotations 0, 1, 2, and 3. The last two are shifted down too far. This patch fixes the issue. I don't have another board to test this on, but if I understand the problem correctly, this should work with all of the other boards as well.

#include "Adafruit_ThinkInk.h"

#define EPD_CS      A0
#define EPD_DC      A1
#define SRAM_CS     A2
#define SD_CS       A3
#define EPD_RESET   A4
#define EPD_BUSY    A5

ThinkInk_213_Mono_BN display(EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);

void setup() {
  display.begin();
  display.setTextColor(EPD_BLACK);
}

void pattern(uint8_t rotation) {
  display.clearBuffer();
  display.setRotation(rotation);
  display.setCursor(2, 2);
  display.print("rotate: ");
  display.print(rotation);
  display.drawRect(0, 0, display.width(), display.height(), EPD_BLACK);
  display.drawLine(0, 0, display.width()-1, display.height()-1, EPD_BLACK);
  display.display();
}

void loop() {
  pattern(0);
  delay(1000);

  pattern(1);
  delay(1000);

  pattern(2);
  delay(1000);

  pattern(3);
  delay(1000);
}

rotate-bug