E-Paper-Calendar/Calendar/Epd7in5Adapter.py

64 lines
2.4 KiB
Python
Raw Normal View History

2019-02-19 21:43:53 +01:00
from EpdAdapter import EpdAdapter, DISPLAY_REFRESH, DATA_START_TRANSMISSION_1
2019-02-19 21:49:50 +01:00
from settings import display_colours
from PIL import Image, ImageDraw
2019-02-19 21:43:53 +01:00
class Epd7in5Adapter (EpdAdapter):
2019-02-19 22:07:23 +01:00
def __init__ (self):
super(Epd7in5Adapter, self).__init__(384, 640)
2019-02-19 21:43:53 +01:00
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
self.send_command(DISPLAY_REFRESH)
self.delay_ms(100)
self.wait_until_idle()
def get_frame_buffer (self, image):
2019-02-28 21:58:38 +01:00
buf = [0x00] * int(self.height * self.width / 8)
2019-02-19 21:43:53 +01:00
# 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
2019-02-28 21:58:38 +01:00
if imwidth != self.height or imheight != self.width:
2019-02-19 21:43:53 +01:00
raise ValueError('Image must be same dimensions as display \
2019-02-28 21:58:38 +01:00
({0}x{1}).' .format(self.height, self.width))
2019-02-19 21:43:53 +01:00
pixels = image_monocolor.load()
2019-02-28 21:58:38 +01:00
for y in range(self.width):
for x in range(self.height):
2019-02-19 21:43:53 +01:00
# Set the bits for the column of pixels at the current position.
if pixels[x, y] != 0:
2019-02-28 21:58:38 +01:00
buf[int((x + y * self.height) / 8)] |= 0x80 >> (x % 8)
2019-02-19 21:49:50 +01:00
return buf
2019-02-28 21:58:38 +01:00
def calibrate (self):
2019-02-19 21:49:50 +01:00
for _ in range(2):
self.init_render()
2019-02-28 21:58:38 +01:00
black = Image.new('1', (self.height, self.width), 'black')
2019-02-19 21:49:50 +01:00
print('calibrating black...')
ImageDraw.Draw(black)
self.display_frame(self.get_frame_buffer(black))
2019-02-28 21:58:38 +01:00
white = Image.new('1', (self.height, self.width), 'white')
2019-02-19 21:49:50 +01:00
ImageDraw.Draw(white)
print('calibrating white...')
self.display_frame(self.get_frame_buffer(white))
self.sleep()
print('Calibration complete')