#https://docs.wokwi.com/zh-CN/parts/wokwi-ili9341
'''
https://jimirobot.tw/esp32-micropython-tutorial-i2c-lcd1602-207/
Micropython I2C 跟 SPI 類似,都有硬體與軟體 I2C 的區別,
硬體的 I2C 就是直接使 mcu 內部 I2C 控制器進行通訊,
軟體的 I2C 則可以搭配任意的 GPIO,利用軟體模擬出通訊協定,
雖然軟體 I2C 使用彈性較大,但建議直接使用硬體 I2C 即可,
因為在 ESP32 內,硬體 I2C 已可以搭配任意 Output 的 GPIO
'''
from machine import Pin, SPI, SoftI2C,I2C
from ili9341 import Display, color565
from ft6206 import Touch # capacitive touch
import time
from time import sleep
CYAN = color565(0, 255, 255)
PURPLE = color565(255, 0, 255)
WHITE = color565(255, 255, 255)
GREEN = color565(0, 255, 0)
RED = color565(255, 05, 0)
BACK_COLOR = color565(0, 0, 0)
RECT_COLOR = color565(255, 0, 0)
POLY_COLOR = color565(0, 64, 255)
CIRC_COLOR = color565(0, 255, 0)
ELLP_COLOR = color565(255, 0, 0)
ROTATION = 180
#TFT Display
spi = SPI(1, baudrate=1000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
display = Display(spi, cs=Pin(5), dc=Pin(2), rst=Pin(4), rotation=ROTATION)
#touch
'''
Hardware I2C 的第一個參數為 id 可以填入 0 或 1,第 2 個參數指定頻率,
如果各位使用的是預設腳位去操作 I2C 通訊,就可以省略腳位的設定,
每個 id 都有一個預設的腳位
'''
#i2c = I2C(scl=Pin(22), sda=Pin(21))
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
touch = Touch(i2c, display)
print('w=',display.width,'h=',display.height)
print('Execution time:start')
#記錄開始時間
#https://diyprojectslab.com/raspberry-pi-pico-tft-lcd-touch-screen-tutorial/#google_vignette
start_time = time.ticks_us()
#######你的程式碼在這裡
# Draw rectangles
def draw_rectangles():
for x in range(0, 225, 15):
display.fill_rectangle(x, 0, 15, 227, RECT_COLOR)
# Draw polygons
def draw_polygons():
display.fill_polygon(7, 120, 120, 100, POLY_COLOR)
sleep(1)
display.draw_polygon(3, 120, 286, 30, POLY_COLOR, rotate=15)
sleep(3)
# Draw circles
def draw_circles():
display.fill_circle(132, 132, 70, CIRC_COLOR)
sleep(1)
display.draw_circle(132, 96, 70, color565(0, 0, 255))
sleep(1)
# Draw ellipses
def draw_ellipses():
display.fill_ellipse(96, 96, 30, 16, ELLP_COLOR)
sleep(1)
display.draw_ellipse(96, 256, 16, 30, color565(255, 255, 0))
# Clear screen and draw shapes
display.clear(BACK_COLOR)
draw_rectangles()
display.clear(BACK_COLOR)
draw_polygons()
display.clear(BACK_COLOR)
draw_circles()
display.clear(BACK_COLOR)
draw_ellipses()
# 記錄結束時間
end_time = time.ticks_us()
# Calculate and print the execution time in seconds
execution_time_seconds = (end_time - start_time) / 1000000.0
print("Execution time: {} seconds".format(execution_time_seconds))