Merge pull request #130 from dev-ng/master

Significantly increase speed of displaying images (for 7in5 and 7in_HD)
This commit is contained in:
SSYYL 2021-01-19 17:07:49 +08:00 committed by GitHub
commit 49260094e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 113 deletions

View file

@ -65,6 +65,12 @@ class EPD:
epdconfig.spi_writebyte([data]) epdconfig.spi_writebyte([data])
epdconfig.digital_write(self.cs_pin, 1) epdconfig.digital_write(self.cs_pin, 1)
def send_data2(self, data):
epdconfig.digital_write(self.dc_pin, 1)
epdconfig.digital_write(self.cs_pin, 0)
epdconfig.spi_writebyte2(data)
epdconfig.digital_write(self.cs_pin, 1)
def ReadBusy(self): def ReadBusy(self):
logging.debug("e-Paper busy") logging.debug("e-Paper busy")
while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
@ -78,17 +84,13 @@ class EPD:
self.reset() self.reset()
self.send_command(0x01) # POWER_SETTING self.send_command(0x01) # POWER_SETTING
self.send_data(0x37) self.send_data2([0x37, 0x00])
self.send_data(0x00)
self.send_command(0x00) # PANEL_SETTING self.send_command(0x00) # PANEL_SETTING
self.send_data(0xCF) self.send_data2([0xCF, 0x08])
self.send_data(0x08)
self.send_command(0x06) # BOOSTER_SOFT_START self.send_command(0x06) # BOOSTER_SOFT_START
self.send_data(0xc7) self.send_data2([0xc7, 0xcc, 0x28])
self.send_data(0xcc)
self.send_data(0x28)
self.send_command(0x04) # POWER_ON self.send_command(0x04) # POWER_ON
self.ReadBusy() self.ReadBusy()
@ -121,72 +123,50 @@ class EPD:
return 0 return 0
def getbuffer(self, image): def getbuffer(self, image):
logging.debug("1234") img = image
buf = [0x00] * int(self.width * self.height / 4) imwidth, imheight = img.size
image_monocolor = image.convert('1') halfwidth = int(self.width / 2)
imwidth, imheight = image_monocolor.size buf = [0x33] * halfwidth * self.height
pixels = image_monocolor.load()
logging.debug('imwidth = %d imheight = %d ',imwidth, imheight)
if(imwidth == self.width and imheight == self.height): if(imwidth == self.width and imheight == self.height):
for y in range(imheight): img = img.convert('1')
for x in range(imwidth):
# Set the bits for the column of pixels at the current position.
if pixels[x, y] < 64: # black
buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2))
elif pixels[x, y] < 192: # convert gray to red
buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2))
buf[int((x + y * self.width) / 4)] |= 0x40 >> (x % 4 * 2)
else: # white
buf[int((x + y * self.width) / 4)] |= 0xC0 >> (x % 4 * 2)
elif(imwidth == self.height and imheight == self.width): elif(imwidth == self.height and imheight == self.width):
img = img.rotate(90, expand=True).convert('1')
imwidth, imheight = img.size
else:
logging.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
# return a blank buffer
return buf
pixels = img.load()
for y in range(imheight): for y in range(imheight):
for x in range(imwidth): offset = y * halfwidth
newx = y for x in range(1, imwidth, 2):
newy = self.height - x - 1 i = offset + x // 2
if pixels[x, y] < 64: # black if(pixels[x-1, y] > 191):
buf[int((newx + newy*self.width) / 4)] &= ~(0xC0 >> (y % 4 * 2)) if(pixels[x, y] > 191):
elif pixels[x, y] < 192: # convert gray to red buf[i] = 0x33
buf[int((newx + newy*self.width) / 4)] &= ~(0xC0 >> (y % 4 * 2)) else:
buf[int((newx + newy*self.width) / 4)] |= 0x40 >> (y % 4 * 2) buf[i] = 0x30
else: # white else:
buf[int((newx + newy*self.width) / 4)] |= 0xC0 >> (y % 4 * 2) if(pixels[x, y] > 191):
buf[i] = 0x03
else:
buf[i] = 0x00
return buf return buf
def display(self, image): def display(self, image):
self.send_command(0x10) self.send_command(0x10)
for i in range(0, int(self.width / 4 * self.height)): self.send_data2(image)
temp1 = image[i]
j = 0
while (j < 4):
if ((temp1 & 0xC0) == 0xC0):
temp2 = 0x03
elif ((temp1 & 0xC0) == 0x00):
temp2 = 0x00
else:
temp2 = 0x04
temp2 = (temp2 << 4) & 0xFF
temp1 = (temp1 << 2) & 0xFF
j += 1
if((temp1 & 0xC0) == 0xC0):
temp2 |= 0x03
elif ((temp1 & 0xC0) == 0x00):
temp2 |= 0x00
else:
temp2 |= 0x04
temp1 = (temp1 << 2) & 0xFF
self.send_data(temp2)
j += 1
self.send_command(0x12) self.send_command(0x12)
epdconfig.delay_ms(100) epdconfig.delay_ms(100)
self.ReadBusy() self.ReadBusy()
def Clear(self): def Clear(self):
buf = [0x33] * int(self.width * self.height / 2)
self.send_command(0x10) self.send_command(0x10)
for i in range(0, int(self.width / 4 * self.height)): self.send_data2(buf)
for j in range(0, 4):
self.send_data(0x33)
self.send_command(0x12) self.send_command(0x12)
self.ReadBusy() self.ReadBusy()

View file

@ -65,6 +65,12 @@ class EPD:
epdconfig.spi_writebyte([data]) epdconfig.spi_writebyte([data])
epdconfig.digital_write(self.cs_pin, 1) epdconfig.digital_write(self.cs_pin, 1)
def send_data2(self, data):
epdconfig.digital_write(self.dc_pin, 1)
epdconfig.digital_write(self.cs_pin, 0)
epdconfig.spi_writebyte2(data)
epdconfig.digital_write(self.cs_pin, 1)
def ReadBusy(self): def ReadBusy(self):
logging.debug("e-Paper busy") logging.debug("e-Paper busy")
busy = epdconfig.digital_read(self.busy_pin) busy = epdconfig.digital_read(self.busy_pin)
@ -90,31 +96,18 @@ class EPD:
self.ReadBusy(); self.ReadBusy();
self.send_command(0x0C); # Soft start setting self.send_command(0x0C); # Soft start setting
self.send_data(0xAE); self.send_data2([0xAE, 0xC7, 0xC3, 0xC0, 0x40])
self.send_data(0xC7);
self.send_data(0xC3);
self.send_data(0xC0);
self.send_data(0x40);
self.send_command(0x01); # Set MUX as 527 self.send_command(0x01); # Set MUX as 527
self.send_data(0xAF); self.send_data2([0xAF, 0x02, 0x01])
self.send_data(0x02);
self.send_data(0x01);#0x01
self.send_command(0x11); # Data entry mode self.send_command(0x11); # Data entry mode
self.send_data(0x01); self.send_data(0x01);
self.send_command(0x44); self.send_command(0x44);
self.send_data(0x00); # RAM x address start at 0 self.send_data2([0x00, 0x00, 0x6F, 0x03]) # RAM x address start at 0
self.send_data(0x00);
self.send_data(0x6F);
self.send_data(0x03);
self.send_command(0x45); self.send_command(0x45);
self.send_data(0xAF); self.send_data2([0xAF, 0x02, 0x00, 0x00])
self.send_data(0x02);
self.send_data(0x00);
self.send_data(0x00);
self.send_command(0x3C); # VBD self.send_command(0x3C); # VBD
self.send_data(0x05); # LUT1, for white self.send_data(0x05); # LUT1, for white
@ -129,46 +122,32 @@ class EPD:
self.ReadBusy(); self.ReadBusy();
self.send_command(0x4E); # set RAM x address count to 0; self.send_command(0x4E); # set RAM x address count to 0;
self.send_data(0x00); self.send_data2([0x00, 0x00])
self.send_data(0x00);
self.send_command(0x4F); self.send_command(0x4F);
self.send_data(0x00); self.send_data2([0x00, 0x00])
self.send_data(0x00);
# EPD hardware init end # EPD hardware init end
return 0 return 0
def getbuffer(self, image): def getbuffer(self, image):
# logging.debug("bufsiz = ",int(self.width/8) * self.height) img = image
buf = [0xFF] * (int(self.width/8) * self.height) imwidth, imheight = img.size
image_monocolor = image.convert('1')
imwidth, imheight = image_monocolor.size
pixels = image_monocolor.load()
# logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
if(imwidth == self.width and imheight == self.height): if(imwidth == self.width and imheight == self.height):
logging.debug("Vertical") img = img.convert('1')
for y in range(imheight):
for x in range(imwidth):
# Set the bits for the column of pixels at the current position.
if pixels[x, y] == 0:
buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
elif(imwidth == self.height and imheight == self.width): elif(imwidth == self.height and imheight == self.width):
logging.debug("Horizontal") img = img.rotate(90, expand=True).convert('1')
for y in range(imheight): else:
for x in range(imwidth): logging.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
newx = y # return a blank buffer
newy = self.height - x - 1 return [0xff] * int(self.width * self.height / 8)
if pixels[x, y] == 0:
buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8)) buf = bytearray(img.tobytes('raw'))
return buf return buf
def display(self, image): def display(self, image):
self.send_command(0x4F); self.send_command(0x4F);
self.send_data(0x00); self.send_data2([0x00, 0x00])
self.send_data(0x00);
self.send_command(0x24); self.send_command(0x24);
for i in range(0, int(self.width * self.height / 8)): self.send_data2(image)
self.send_data(image[i]);
self.send_command(0x22); self.send_command(0x22);
self.send_data(0xF7);#Load LUT from MCU(0x32) self.send_data(0xF7);#Load LUT from MCU(0x32)
self.send_command(0x20); self.send_command(0x20);
@ -176,16 +155,14 @@ class EPD:
self.ReadBusy(); self.ReadBusy();
def Clear(self): def Clear(self):
buf = [0xff] * int(self.width * self.height / 8)
self.send_command(0x4F); self.send_command(0x4F);
self.send_data(0x00); self.send_data2([0x00, 0x00])
self.send_data(0x00);
self.send_command(0x24) self.send_command(0x24)
for i in range(0, int(self.width * self.height / 8)): self.send_data2(buf)
self.send_data(0xff)
self.send_command(0x26) self.send_command(0x26)
for i in range(0, int(self.width * self.height / 8)): self.send_data2(buf)
self.send_data(0xff)
self.send_command(0x22); self.send_command(0x22);
self.send_data(0xF7);#Load LUT from MCU(0x32) self.send_data(0xF7);#Load LUT from MCU(0x32)

View file

@ -61,6 +61,9 @@ class RaspberryPi:
def spi_writebyte(self, data): def spi_writebyte(self, data):
self.SPI.writebytes(data) self.SPI.writebytes(data)
def spi_writebyte2(self, data):
self.SPI.writebytes2(data)
def module_init(self): def module_init(self):
self.GPIO.setmode(self.GPIO.BCM) self.GPIO.setmode(self.GPIO.BCM)
self.GPIO.setwarnings(False) self.GPIO.setwarnings(False)