Merging e-paper drivers for bw and bwr display

This commit is contained in:
Ace 2019-05-02 19:08:30 +02:00 committed by GitHub
parent 28c76b6d86
commit a038818050
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

117
test.py
View File

@ -1,6 +1,7 @@
import epdif import epdif
from PIL import Image from PIL import Image
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from settings import display_colours
# Display resolution # Display resolution
EPD_WIDTH = 640 EPD_WIDTH = 640
@ -78,7 +79,6 @@ class EPD:
if (epdif.epd_init() != 0): if (epdif.epd_init() != 0):
return -1 return -1
self.reset() self.reset()
self.send_command(POWER_SETTING) self.send_command(POWER_SETTING)
self.send_data(0x37) self.send_data(0x37)
self.send_data(0x00) self.send_data(0x00)
@ -119,44 +119,89 @@ class EPD:
self.digital_write(self.reset_pin, GPIO.HIGH) 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() def get_frame_buffer(self, image):
for y in range(self.height): if display_colours is 'bwr':
for x in range(self.width): buf = [0x00] * int(self.width * self.height / 4)
# Set the bits for the column of pixels at the current position. image_grayscale = image.convert('L', dither=None)
if pixels[x, y] != 0: imwidth, imheight = image_grayscale.size
buf[int((x + y * self.width) / 8)] |= 0x80 >> (x % 8) if imwidth != self.width or imheight != self.height:
return buf 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): def display_frame(self, frame_buffer):
self.send_command(DATA_START_TRANSMISSION_1) self.send_command(DATA_START_TRANSMISSION_1)
for i in range(0, 30720): if display_colours is 'bwr':
temp1 = frame_buffer[i] for i in range(0, int(self.width / 4 * self.height)):
j = 0 temp1 = frame_buffer[i]
while (j < 8): j = 0
if(temp1 & 0x80): while (j < 4):
temp2 = 0x03 if ((temp1 & 0xC0) == 0xC0):
else: temp2 = 0x03
temp2 = 0x00 elif ((temp1 & 0xC0) == 0x00):
temp2 = (temp2 << 4) & 0xFF temp2 = 0x00
temp1 = (temp1 << 1) & 0xFF else:
j += 1 temp2 = 0x04
if(temp1 & 0x80): temp2 = (temp2 << 4) & 0xFF
temp2 |= 0x03 temp1 = (temp1 << 2) & 0xFF
else: j += 1
temp2 |= 0x00 if((temp1 & 0xC0) == 0xC0):
temp1 = (temp1 << 1) & 0xFF temp2 |= 0x03
self.send_data(temp2) elif ((temp1 & 0xC0) == 0x00):
j += 1 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.send_command(DISPLAY_REFRESH)
self.delay_ms(100) self.delay_ms(100)
self.wait_until_idle() self.wait_until_idle()
@ -166,5 +211,3 @@ class EPD:
self.wait_until_idle() self.wait_until_idle()
self.send_command(DEEP_SLEEP) self.send_command(DEEP_SLEEP)
self.send_data(0xa5) self.send_data(0xa5)
### END OF FILE ###