Skip to content

Instantly share code, notes, and snippets.

@cassc
Created October 9, 2021 07:08
Show Gist options
  • Save cassc/9c111bc5e871cd443849ae8d1fef3123 to your computer and use it in GitHub Desktop.
Save cassc/9c111bc5e871cd443849ae8d1fef3123 to your computer and use it in GitHub Desktop.
ST7735S on Raspberry Pi
# Connections:
# LED-K : GND
# LED-A : GPIO2
# RESET : GPIO25
# DCX : GPIO24
# SDA : GPIO10
# SCL : GPIO11
# VDDI : 3.3V
# VDD : 3.3V
# CS : GPIO8
# GND : GND
#
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import time
from random import choice
import textwrap
from subprocess import check_output
import ST7735
speed = 18000000
disp = ST7735.ST7735(port=0, cs=0, dc=24, backlight=2, rst=25, width=128, height=160, rotation=90, invert=False, spi_speed_hz=speed)
WIDTH = disp.width
HEIGHT = disp.height
font = ImageFont.truetype("LiberationMono-Regular.ttf", 12, encoding="unic") # 设置字体
img = Image.new('RGB', (disp.width, disp.height), 'black')
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype("DejaVuSans.ttf", 12, encoding="UNIX") # 设置字体
# font = ImageFont.truetype("wqy-microhei.ttc", 12, encoding="unic") # 设置字体
# img = Image.open('/home/pi/img.jpg')
def text(x, y, text, color, font=font):
draw.text((x, y), text, color, font)
def show():
disp.display(img)
def reset():
disp.reset()
def blank(color='black'):
draw.rectangle((0, 0, WIDTH, HEIGHT), fill=color)
def show_quote():
text = check_output(['fortune']).decode('utf8')
img = Image.new('RGB', (disp.width, disp.height), 'black')
draw = ImageDraw.Draw(img)
color = choice(['white', 'green', 'yellow', 'cyan'])
y = 0
for line in text.splitlines():
for seg in textwrap.wrap(line, width=24):
(t_width, t_height) = font.getsize(seg)
print(f'seg size {t_width} / {t_height}')
draw.text((0, y), seg, color, font)
y += 16
disp.display(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment