E-Paper-Calendar/Calendar/Epd7in5bAdapter.py

93 lines
3.5 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
from math import sqrt, pow
2019-04-14 17:41:42 +02:00
import numpy as np
2019-02-19 21:43:53 +01:00
2019-07-13 08:05:35 +02:00
2019-02-19 21:43:53 +01:00
class Epd7in5bAdapter (EpdAdapter):
2019-07-13 08:05:35 +02:00
def __init__(self):
super(Epd7in5bAdapter, self).__init__(384, 640)
2019-02-19 21:43:53 +01:00
2019-07-13 08:05:35 +02:00
def display_frame(self, frame_buffer):
2019-02-19 21:43:53 +01:00
self.send_command(DATA_START_TRANSMISSION_1)
2019-02-28 21:58:38 +01:00
for i in range(0, int(self.height / 4 * self.width)):
2019-07-13 08:05:35 +02:00
# the above line had to be modified due to python2 -> python3
# the issue lies in division, which returns integers in python2
# but floats in python3
2019-02-19 21:43:53 +01:00
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
self.send_command(DISPLAY_REFRESH)
self.delay_ms(100)
self.wait_until_idle()
2019-07-13 08:05:35 +02:00
def get_frame_buffer(self, image):
buf = [0x00] * int(self.height * self.width / 4)
2019-04-14 17:41:42 +02:00
imwidth, imheight = image.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
2019-04-14 17:41:42 +02:00
image_buf = self.__prepare_image__(image)
2019-03-23 12:22:02 +01:00
for x in range(self.width):
for y in range(self.height):
2019-02-19 21:49:50 +01:00
# Set the bits for the column of pixels at the current
# position.
2019-07-13 08:05:35 +02:00
if image_buf[x, y, 1] == 255: # White
2019-03-23 12:22:02 +01:00
buf[int((y + x * self.height) / 4)] |= 0xC0 >> (y % 4 * 2)
2019-07-13 08:05:35 +02:00
elif image_buf[x, y, 0] == 0: # Black
buf[int((y + x * self.height) / 4)
] &= ~(0xC0 >> (y % 4 * 2))
else: # Red
buf[int((y + x * self.height) / 4)
] &= ~(0xC0 >> (y % 4 * 2))
2019-03-23 12:22:02 +01:00
buf[int((y + x * self.height) / 4)] |= 0x40 >> (y % 4 * 2)
2019-04-14 17:41:42 +02:00
return buf
2019-02-19 21:49:50 +01:00
2019-04-14 17:41:42 +02:00
def __prepare_image__(self, image):
buffer = np.array(image)
2019-07-13 08:05:35 +02:00
r, g = buffer[:, :, 0], buffer[:, :, 1]
buffer[np.logical_and(r > 220, g > 220)] = [255, 255, 255]
buffer[r > g] = [255, 0, 0]
buffer[r != 255] = [0, 0, 0]
2019-04-14 17:41:42 +02:00
return buffer
2019-07-13 08:05:35 +02:00
def calibrate(self):
2019-02-19 21:49:50 +01:00
for _ in range(2):
self.init_render()
black = Image.new('RGB', (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))
red = Image.new('RGB', (self.height, self.width), 'red')
2019-02-19 21:49:50 +01:00
ImageDraw.Draw(red)
print('calibrating red...')
self.display_frame(self.get_frame_buffer(red))
white = Image.new('RGB', (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()
2019-07-13 08:05:35 +02:00
print('Calibration complete')