# code for micropython 1.10 on esp8266
# https://github.com/devbis/st7789py_mpy/
# raspberry_st7789
# https://wokwi.com/projects/441002263613549569
from machine import Pin, SPI, I2C
import utime
import sys,os
import math
import random
import st7789py as st7789
import vga1_8x16 as font1
# import vga8x8 as font2
# import vga2_8x8 as font3
DISP_WIDTH = 240
DISP_HEIGHT = 240
# 配置 SPI 與 控制 GPIO
if(sys.platform=="esp8266"):
led_pin = 2 # GPIO2(D4)
# disp_miso = 12 # GPIO12(D6), not use
disp_mosi = 13 # GPIO13(D7), default MOSI of SPI(1)
disp_sck = 14 # GPIO14(D5), default SCK of SPI(1)
# disp_cs = 15 # GPIO15(D8)
disp_dc = 0 # GPIO0(D3)
disp_rst = 16 # GPIO16(D0)
# disp_blk =
# Initialize SPI communication, ESP8266 baudrate between 20MHz to 40MHz
spi = SPI(1, baudrate=40000000, polarity=1)
I2C_SDA =4 # GPIO4(D2)
I2C_SCL =5 # GIPO5(D1)
i2c = I2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=100000) #initializing the I2C method for ESP8266
elif(sys.platform=="esp32"):
machine_info = os.uname().machine.upper()
if "S3" in machine_info:
print("正在初始化 ESP32-S3 的 SPI 腳位...")
spi_id = 1 # S3 通常使用 SPI2 (FSPI) 或 SPI3
# disp_miso = 13 # not use
disp_mosi = 11
disp_sck = 12
# disp_cs = 10
disp_dc = 8
disp_rst = 9
# disp_blk =
I2C_SDA =1 # GPIO1
I2C_SCL =0 # GPIO0
else:
led = Pin(2, Pin.OUT)
button_pin = 35
print("正在初始化傳統 ESP32 的 SPI 腳位...")
# VSPI: SCK=18, MOSI=23, MOSI=19
spi_id = 2
# disp_miso = 19 # not use
disp_mosi = 23 # default MOSI of SPI(2)
disp_sck = 18 # default SCK of SPI(2)
# disp_cs = 5
disp_dc = 17 # TX2
disp_rst = 16 # RX2
# disp_blk = 4
I2C_SDA =21 # GPIO21
I2C_SCL =22 # GPIO22
# Initialize SPI communication
spi = SPI(spi_id, baudrate=40000000, polarity=1, phase=0, bits=8, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=None)
# Initialize I2C
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000) #initializing the I2C method for ESP32
elif(sys.platform=="rp2"):
led = Pin("LED", Pin.OUT)
button_pin = 21
# disp_miso = 4 # not use
disp_mosi = 7 # default MOSI of SPI(0)
disp_sck = 6 # default SCK of SPI(0)
# disp_cs = 5
disp_dc = 1
disp_rst = 2
# disp_blk = 3
# Initialize SPI communication
spi = SPI(0, baudrate=24000000, polarity=1, phase=0, bits=8, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=None)
I2C_SDA =16 # GPIO16
I2C_SCL =17 # GPIO17
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000)
# Colors
BLACK = st7789.BLACK
WHITE = st7789.WHITE
YELLOW = st7789.YELLOW
BLUE = st7789.BLUE
GREEN = st7789.GREEN
RED = st7789.RED
# Constants
HOR = 240
XC = 120
YC = 120
DEG2RAD = math.pi / 180
last_roll = 0
last_pitch = 0
# Draw the horizon
def draw_horizon(roll, pitch):
global last_roll, last_pitch
sx = math.cos(roll * DEG2RAD)
sy = math.sin(roll * DEG2RAD)
x0 = int(sx * HOR)
y0 = int(sy * HOR)
# Center line
tft.line(XC - x0, YC - y0 - pitch, XC + x0, YC + y0 - pitch, WHITE)
last_roll = roll
last_pitch = pitch
# Update the horizon
def update_horizon(roll, pitch):
global last_roll, last_pitch
while last_pitch != pitch or last_roll != roll:
delta_pitch = 1 if last_pitch < pitch else -1 if last_pitch > pitch else 0
delta_roll = 1 if last_roll < roll else -1 if last_roll > roll else 0
draw_horizon(last_roll + delta_roll, last_pitch + delta_pitch)
# Roll test
def test_roll():
for _ in range(360):
roll = int(180 * math.sin(utime.ticks_ms() * DEG2RAD / 10))
update_horizon(roll, 0)
utime.sleep_ms(10)
# Pitch test
def test_pitch():
for p in range(-80, 80):
update_horizon(0, p)
utime.sleep_ms(10)
for p in range(80, -1, -1):
update_horizon(0, p)
utime.sleep_ms(10)
# Start Function
if __name__ == '__main__':
# 初始化 ST7789 LCD (解析度為240x240)
tft = st7789.ST7789(spi,
DISP_WIDTH, DISP_HEIGHT,
reset=Pin(disp_rst, Pin.OUT),
dc=Pin(disp_dc, Pin.OUT),
cs=None, # 模組沒有 CS 腳位
# backlight=Pin(disp_blk, Pin.OUT),
)
# 初始化
tft.init()
# 點亮背光
if tft.backlight:
tft.backlight.on()
# 清屏(填滿黑色)
tft.fill(BLACK)
# 畫矩形(顏色選用 16-bit RGB565)
tft.rect(20, 20, 200, 200, RED)
tft.fill_rect(40, 40, 160, 160, BLUE)
# 參數依序為:字型, 字串, X座標, Y座標, 文字顏色, 背景顏色
tft.text(font1, "Hello, World!", 45, 100, WHITE, BLACK)
tft.text(font1, "MicroPython ST7789", 45, 130, GREEN, BLACK)
utime.sleep(2) # Pause 2 seconds.
tft.fill(BLACK)
tft.fill_rect(0, 0, 240, 120, BLUE)
tft.fill_rect(0, 120, 240, 120, YELLOW)
draw_horizon(0, 0)
utime.sleep(2)
test_roll()
test_pitch()
while True:
try:
roll = random.randint(-180, 180)
pitch = random.randint(-80, 80)
update_horizon(roll, pitch)
utime.sleep_ms(100)
"""
color = st7789.color565(
random.getrandbits(8),
random.getrandbits(8),
random.getrandbits(8))
tft.fill(color)
utime.sleep(2) # Pause 2 seconds.
"""
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
print("Stop")
break
Loading
pi-pico-w
pi-pico-w
Loading
st7789
st7789