From a038818050dd0ed33761e9a8a23268d3ca64f882 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 2 May 2019 19:08:30 +0200 Subject: [PATCH] Merging e-paper drivers for bw and bwr display --- test.py | 119 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 38 deletions(-) diff --git a/test.py b/test.py index 65cd327..81254cb 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,7 @@ import epdif from PIL import Image import RPi.GPIO as GPIO +from settings import display_colours # Display resolution EPD_WIDTH = 640 @@ -78,7 +79,6 @@ class EPD: if (epdif.epd_init() != 0): return -1 self.reset() - self.send_command(POWER_SETTING) self.send_data(0x37) self.send_data(0x00) @@ -117,46 +117,91 @@ class EPD: self.digital_write(self.reset_pin, GPIO.LOW) # module reset self.delay_ms(200) self.digital_write(self.reset_pin, GPIO.HIGH) - self.delay_ms(200) + self.delay_ms(200) + def get_frame_buffer(self, image): - buf = [0x00] * int(self.width * self.height / 8) - # Set buffer to value of Python Imaging Library image. - # Image must be in mode 1. - image_monocolor = image.convert('1') #with ot withour dithering? - imwidth, imheight = image_monocolor.size - if imwidth != self.width or imheight != self.height: - raise ValueError('Image must be same dimensions as display \ - ({0}x{1}).' .format(self.width, self.height)) - - pixels = image_monocolor.load() - for y in range(self.height): - for x in range(self.width): - # 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) - return buf + if display_colours is 'bwr': + buf = [0x00] * int(self.width * self.height / 4) + image_grayscale = image.convert('L', dither=None) + imwidth, imheight = image_grayscale.size + if imwidth != self.width or imheight != self.height: + raise ValueError('Image must be same dimensions as display \ + ({0}x{1}).' .format(self.width, self.height)) + pixels = image_grayscale.load() + + for y in range(self.height): + for x in range(self.width): + # Set the bits for the column of pixels at the current position. + if pixels[x, y] == 0: # black + buf[int((x + y * self.width) / 4)] &= ~(0xC0 >> (x % 4 * 2)) + elif pixels[x, y] == 76: # 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) + return buf + if display_colours is 'bw': + buf = [0x00] * int(self.width * self.height / 8) + image_monocolor = image.convert('1') + imwidth, imheight = image_monocolor.size + if imwidth != self.width or imheight != self.height: + raise ValueError('Image must be same dimensions as display \ + ({0}x{1}).' .format(self.width, self.height)) + pixels = image_monocolor.load() + for y in range(self.height): + for x in range(self.width): + # 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) + return buf + + def display_frame(self, frame_buffer): self.send_command(DATA_START_TRANSMISSION_1) - for i in range(0, 30720): - temp1 = frame_buffer[i] - j = 0 - while (j < 8): - if(temp1 & 0x80): - temp2 = 0x03 - else: - temp2 = 0x00 - temp2 = (temp2 << 4) & 0xFF - temp1 = (temp1 << 1) & 0xFF - j += 1 - if(temp1 & 0x80): - temp2 |= 0x03 - else: - temp2 |= 0x00 - temp1 = (temp1 << 1) & 0xFF - self.send_data(temp2) - j += 1 + if display_colours is 'bwr': + for i in range(0, int(self.width / 4 * self.height)): + temp1 = frame_buffer[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 + if display_colours is 'bw': + for i in range(0, 30720): + temp1 = frame_buffer[i] + j = 0 + while (j < 8): + if(temp1 & 0x80): + temp2 = 0x03 + else: + temp2 = 0x00 + temp2 = (temp2 << 4) & 0xFF + temp1 = (temp1 << 1) & 0xFF + j += 1 + if(temp1 & 0x80): + temp2 |= 0x03 + else: + temp2 |= 0x00 + temp1 = (temp1 << 1) & 0xFF + self.send_data(temp2) + j += 1 self.send_command(DISPLAY_REFRESH) self.delay_ms(100) self.wait_until_idle() @@ -166,5 +211,3 @@ class EPD: self.wait_until_idle() self.send_command(DEEP_SLEEP) self.send_data(0xa5) - -### END OF FILE ###