from machine import Pin import time
BLACK=0x0000
WHITE=0xFFFF
RED=0xF800
GREEN=0x07E0
BLUE=0x001F
YELLOW=0xFFE0
CYAN=0x07FF
MAGENTA=0xF81F
class ST7789:
def __init__(self,spi,width,height,reset,dc,cs):
self.spi=spi
self.width=width
self.height=height
self.reset=reset
self.dc=dc
self.cs=cs
self.cs.init(Pin.OUT,value=1)
self.dc.init(Pin.OUT,value=0)
self.reset.init(Pin.OUT,value=1)
def cmd(self,c):
self.cs(0)
self.dc(0)
self.spi.write(bytearray([c]))
self.cs(1)
def data(self,d):
self.cs(0)
self.dc(1)
self.spi.write(bytearray([d]))
self.cs(1)
def reset_display(self):
self.reset(0)
time.sleep_ms(50)
self.reset(1)
time.sleep_ms(50)
def init(self):
self.reset_display()
self.cmd(0x11)
time.sleep_ms(120)
self.cmd(0x36)
self.data(0x00)
self.cmd(0x3A)
self.data(0x55)
self.cmd(0x21)
self.cmd(0x29)
time.sleep_ms(20)
def window(self,x0,y0,x1,y1):
self.cmd(0x2A)
self.data(x0>>8)
self.data(x0&255)
self.data(x1>>8)
self.data(x1&255)
self.cmd(0x2B)
self.data(y0>>8)
self.data(y0&255)
self.data(y1>>8)
self.data(y1&255)
self.cmd(0x2C)
def pixel(self,x,y,color):
self.window(x,y,x,y)
hi=color>>8
lo=color&255
self.cs(0)
self.dc(1)
self.spi.write(bytearray([hi,lo]))
self.cs(1)
def fill(self,color):
self.window(0,0,self.width-1,self.height-1)
hi=color>>8
lo=color&255
buf = bytearray(512)
for i in range(0,512,2):
buf[i]=hi
buf[i+1]=lo
self.cs(0)
self.dc(1)
pixels=self.width*self.height
for _ in range(pixels//256):
self.spi.write(buf)
self.cs(1)
def fill_rect(self,x,y,w,h,color):
self.window(x,y,x+w-1,y+h-1)
hi=color>>8
lo=color&255
buf=bytearray(w*2)
for i in range(0,w*2,2):
buf[i]=hi
buf[i+1]=lo
self.cs(0)
self.dc(1)
for _ in range(h):
self.spi.write(buf)
self.cs(1)
def hline(self,x,y,w,color):
self.fill_rect(x,y,w,1,color)
def vline(self,x,y,h,color):
self.fill_rect(x,y,1,h,color)
def rect(self,x,y,w,h,color):
self.hline(x,y,w,color)
self.hline(x,y+h-1,w,color)
self.vline(x,y,h,color)
self.vline(x+w-1,y,h,color)
def line(self,x0,y0,x1,y1,color):
dx=abs(x1-x0)
dy=abs(y1-y0)
sx=1 if x0 <x1 else -1
sy=1 if y0 <y1 else -1
err=dx-dy
while True:
self.pixel(x0,y0,color)
if x0==x1 and y0==y1:
reak
e2=2*err
if e2>-dy:
err-=dy
x0+=sx
if e2<dx:
err+=dx
y0+=sy
# ---------- フォント ----------
font = {
"A":[0x18,0x24,0x42,0x7E,0x42,0x42,0x42,0],
"B":[0x7C,0x42,0x42,0x7C,0x42,0x42,0x7C,0],
"C":[0x3C,0x42,0x40,0x40,0x40,0x42,0x3C,0],
"D":[0x78,0x44,0x42,0x42,0x42,0x44,0x78,0],
"E":[0x7E,0x40,0x40,0x7C,0x40,0x40,0x7E,0],
"F":[0x7E,0x40,0x40,0x7C,0x40,0x40,0x40,0],
"G":[0x3C,0x42,0x40,0x4E,0x42,0x42,0x3C,0],
"H":[0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0],
"I":[0x3C,0x08,0x08,0x08,0x08,0x08,0x3C,0],
"J":[0x1E,0x04,0x04,0x04,0x44,0x44,0x38,0],
"K":[0x42,0x44,0x48,0x70,0x48,0x44,0x42,0],
"L":[0x40,0x40,0x40,0x40,0x40,0x40,0x7E,0],
"M":[0x42,0x66,0x5A,0x5A,0x42,0x42,0x42,0],
"N":[0x42,0x62,0x52,0x4A,0x46,0x42,0x42,0],
"O":[0x3C,0x42,0x42,0x42,0x42,0x42,0x3C,0],
"P":[0x7C,0x42,0x42,0x7C,0x40,0x40,0x40,0],
"Q":[0x3C,0x42,0x42,0x42,0x4A,0x44,0x3A,0],
"R":[0x7C,0x42,0x42,0x7C,0x48,0x44,0x42,0],
"S":[0x3C,0x42,0x40,0x3C,0x02,0x42,0x3C,0],
"T":[0x7F,0x08,0x08,0x08,0x08,0x08,0x08,0],
"U":[0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0],
"V":[0x42,0x42,0x42,0x42,0x42,0x24,0x18,0],
"W":[0x42,0x42,0x42,0x5A,0x5A,0x66,0x42,0],
"X":[0x42,0x42,0x24,0x18,0x24,0x42,0x42,0],
"Y":[0x42,0x42,0x24,0x18,0x08,0x08,0x08,0],
"Z":[0x7E,0x02,0x04,0x18,0x20,0x40,0x7E,0],
"0":[0x3C,0x46,0x4A,0x52,0x62,0x46,0x3C,0],
"1":[0x08,0x18,0x28,0x08,0x08,0x08,0x3E,0],
"2":[0x3C,0x42,0x02,0x0C,0x30,0x40,0x7E,0],
"3":[0x3C,0x42,0x02,0x1C,0x02,0x42,0x3C,0],
"4":[0x0C,0x14,0x24,0x44,0x7E,0x04,0x04,0],
"5":[0x7E,0x40,0x7C,0x02,0x02,0x42,0x3C,0],
"6":[0x1C,0x20,0x40,0x7C,0x42,0x42,0x3C,0],
"7":[0x7E,0x02,0x04,0x08,0x10,0x20,0x20,0],
"8":[0x3C,0x42,0x42,0x3C,0x42,0x42,0x3C,0],
"9":[0x3C,0x42,0x42,0x3E,0x02,0x04,0x38,0]
}
def char(self,x,y,ch,color):
if ch not in self.font:
return
data=self.font[ch]
for row in range(8):
line=data[row]
for col in range(8):
if line&(1<<(7-col)):
self.pixel(x+col,y+row,color)
def text(self,x,y,string,color):
for i,ch in enumerate(string):
self.char(x+i*8,y,ch,color) |
|