pico-epaper/test.py

102 line
3.1 KiB
Python

import epaper
import microbmp
import time
import random
import gc
epd_resolution = [600, 448]
epd_colormap = [
[0x00, 0x00, 0x00], # black
[0xff, 0xff, 0xff], # white
[0x00, 0xdd, 0x00], # green
[0x00, 0x00, 0xee], # blue
[0x00, 0xdd, 0x00], # red
[0xff, 0xdd, 0x00], # yellow
[0xff, 0x88, 0x00], # orange
]
images = ["lambo.bmp", "fallout.bmp", "nasa.bmp"]
gc.enable()
def init_display():
#print("ePaper init ", str(time.localtime()))
epd = epaper.EPD_5in65()
epd.fill(epd.White)
return epd
def draw_image(filename):
global epd
global epd_colormap
offset_x = 0
offset_y = 0
color_map = {}
def header_callback(header):
print("header callback: ", str(header))
global epd_resolution
w = header[0]
h = header[1]
offset_x = (epd_resolution[0] - w)//2
offset_y = (epd_resolution[1] - h)//2
print("offset: ", offset_x, offset_y)
def pixel_callback(x, y, color):
global epd
global epd_colormap
# translate color to color index
color_index = 0
color_key = color[0] + color[1] << 8 + color[2] << 16
if color_key in color_map:
color_index = color_map[color_key]
else:
# search for the best color
best_index = 0
best_score = 1000
for index in range(len(epd_colormap)):
c1 = epd_colormap[index]
c2 = color
score = abs(c1[0] - c2[0]) + abs(c1[1] - c2[1]) + abs(c1[2] - c2[2])
if score < best_score:
best_score = score
best_index = index
if score < 20:
break
color_index = best_index
color_map[color_key] = color_index
# hack directly into the 4-bit color buffer instead of:
# epd.pixel(offset_x + x, offset_y + y, color_to_index(color))
buffer_pos = (offset_x + x + (offset_y + y) * epd_resolution[0])
buffer_index = buffer_pos // 2
if buffer_pos % 2 == 1:
buffer_pos = buffer_pos << 4
epd.buffer[buffer_index] = color_index
def pixel_row_callback(x, y, colors):
pass
#global epd
#print("PXL ", str([color, r,g,b, pixel]))
#for i in range(len(colors)):
# epd.pixel(offset_x + x + i, offset_y + y, color_to_index(colors[i]))
print(str(time.localtime()), " BMP ", filename, " loading")
epd.fill(epd.White)
microbmp.MicroBMP(header_callback=header_callback, data_callback=pixel_callback).load(filename)
print(str(time.localtime()), " BMP loaded")
epd.EPD_5IN65F_Display(epd.buffer)
print(str(time.localtime()), " ePaper printed")
# MAIN
epd = init_display()
while True:
for filename in images:
epd.EPD_5IN65F_Init()
print("TV loading image ", filename)
draw_image(filename)
epd.Sleep()
print("TV showing ", filename)
gc.collect()
epd.delay_ms(10000)