2023/12/28
CircuitPython 為 micropython 的另一個分支,主要由 Adafruit 公司所做的開發平台
- 要用 CircuitPython 來開發,請先到 https://circuitpython.org/board/raspberry_pi_pico_w/ 下載專用的韌體 xxx.uf2
上傳方式和之前一樣,按住 boot 鈕,插拔 usb 線後,把 xxx.uf2 拉至新磁碟槽即可
- CircuitPython 官網有針對 Pico_W 使用方式做說明,可參考下面文章
https://learn.adafruit.com/pico-w-wifi-with-circuitpython/overview
使用 Thonny IDE 寫程式時,需注意將直譯器 -> 選擇 "CircuitPython" 如下圖
- 本文以下面的 github 程式範例說明如何套用至 Pico_Game 的螢幕上
https://github.com/adafruit/Adafruit_CircuitPython_ILI9341
ex- slideshow.py
-> 會顯示三張 bmp 照片至螢幕上
1-把螢幕相關腳位,設定為 Pico_Game 腳位(紅色字體處),其它不變
# This script supports the Raspberry Pi Pico board and the Lilygo ESP32-S2 board
# Raspberry Pi Pico: http://educ8s.tv/part/RaspberryPiPico
# ESP32-S2 Board: http://educ8s.tv/part/esp32s2
import board,busio, os
from time import sleep
import adafruit_ili9341
import displayio
import time
board_type = os.uname().machine
print(f"Board: {board_type}")
if 'Pico' in board_type:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.GP19, board.GP18, board.GP21, board.GP17, board.GP20
elif 'ESP32-S2' in board_type:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.IO35, board.IO36, board.IO38, board.IO34, board.IO37
else:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.GP11, board.GP10, board.GP17, board.GP18, board.GP16
print("This board is not supported. Change the pin definitions above.")
displayio.release_displays()
spi = busio.SPI(clock=clk_pin, MOSI=mosi_pin)
display_bus = displayio.FourWire(spi, command=dc_pin, chip_select=cs_pin, reset=reset_pin)
display = adafruit_ili9341.ILI9341(display_bus, width=240, height=320, rotation=270)
bitmap = displayio.OnDiskBitmap("/0.bmp")
bitmap1 = displayio.OnDiskBitmap("/1.bmp")
bitmap2 = displayio.OnDiskBitmap("/2.bmp")
group = displayio.Group()
display.show(group)
while True:
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
group.append(tile_grid)
sleep(8)
tile_grid = displayio.TileGrid(bitmap1, pixel_shader=bitmap1.pixel_shader)
group.append(tile_grid)
sleep(8)
tile_grid = displayio.TileGrid(bitmap2, pixel_shader=bitmap2.pixel_shader)
group.pop()
group.append(tile_grid)
sleep(8)
2- 會用到 adafruit_ili9341 library,請用以下的修正檔,不然顏色會反相
# SPDX-FileCopyrightText: 2019 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_ili9341`
====================================================
Display driver for ILI9341
* Author(s): Scott Shawcroft
Implementation Notes
--------------------
**Hardware:**
* Adafruit PiTFT 2.2" HAT Mini Kit - 320x240 2.2" TFT - No Touch
<https://www.adafruit.com/product/2315>
* Adafruit PiTFT 2.4" HAT Mini Kit - 320x240 TFT Touchscreen
<https://www.adafruit.com/product/2455>
* Adafruit PiTFT - 320x240 2.8" TFT+Touchscreen for Raspberry Pi
<https://www.adafruit.com/product/1601>
* PiTFT 2.8" TFT 320x240 + Capacitive Touchscreen for Raspberry Pi
<https://www.adafruit.com/product/1983>
* Adafruit PiTFT Plus 320x240 2.8" TFT + Capacitive Touchscreen
<https://www.adafruit.com/product/2423>
* PiTFT Plus Assembled 320x240 2.8" TFT + Resistive Touchscreen
<https://www.adafruit.com/product/2298>
* PiTFT Plus 320x240 3.2" TFT + Resistive Touchscreen
<https://www.adafruit.com/product/2616>
* 2.2" 18-bit color TFT LCD display with microSD card breakout
<https://www.adafruit.com/product/1480>
* 2.4" TFT LCD with Touchscreen Breakout Board w/MicroSD Socket
<https://www.adafruit.com/product/2478>
* 2.8" TFT LCD with Touchscreen Breakout Board w/MicroSD Socket
<https://www.adafruit.com/product/1770>
* 3.2" TFT LCD with Touchscreen Breakout Board w/MicroSD Socket
<https://www.adafruit.com/product/1743>
* TFT FeatherWing - 2.4" 320x240 Touchscreen For All Feathers
<https://www.adafruit.com/product/3315>
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
try:
# used for typing only
from typing import Any
except ImportError:
pass
import displayio
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ILI9341.git"
_INIT_SEQUENCE = (
b"\x01\x80\x80" # Software reset then delay 0x80 (128ms)
b"\xEF\x03\x03\x80\x02"
b"\xCF\x03\x00\xC1\x30"
b"\xED\x04\x64\x03\x12\x81"
b"\xE8\x03\x85\x00\x78"
b"\xCB\x05\x39\x2C\x00\x34\x02"
b"\xF7\x01\x20"
b"\xEA\x02\x00\x00"
b"\xc0\x01\x23" # Power control VRH[5:0]
b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0]
b"\xc5\x02\x3e\x28" # VCM control
b"\xc7\x01\x86" # VCM control2
b"\x36\x01\x38" # Memory Access Control
b"\x37\x01\x00" # Vertical scroll zero
b"\x3a\x01\x55" # COLMOD: Pixel Format Set
b"\x21\x00" # Add by Mason
b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors)
b"\xb6\x03\x08\x82\x27" # Display Function Control
b"\xF2\x01\x00" # 3Gamma Function Disable
b"\x26\x01\x01" # Gamma curve selected
b"\xe0\x0f\x0F\x31\x2B\x0C\x0E\x08\x4E\xF1\x37\x07\x10\x03\x0E\x09\x00" # Set Gamma
b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F" # Set Gamma
b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms)
b"\x29\x80\x78" # Display on then delay 0x78 (120ms)
)
# pylint: disable=too-few-public-methods
class ILI9341(displayio.Display):
"""
ILI9341 display driver
:param displayio.FourWire bus: bus that the display is connected to
"""
def __init__(self, bus: displayio.FourWire, **kwargs: Any):
super().__init__(bus, _INIT_SEQUENCE, **kwargs)
ex-simpletest.py
1-一樣先把螢幕相關腳位,設定為 Pico_Game 腳位(紅色字體處),其它不變
# This script supports the Raspberry Pi Pico board and the Lilygo ESP32-S2 board
# Raspberry Pi Pico: http://educ8s.tv/part/RaspberryPiPico
# ESP32-S2 Board: http://educ8s.tv/part/esp32s2
import board, busio, displayio, os
import terminalio #Just a font
import adafruit_ili9341
from adafruit_display_text import label
displayio.release_displays()
board_type = os.uname().machine
print(f"Board: {board_type}")
if 'Pico' in board_type:
cs_pin, reset_pin, dc_pin, mosi_pin, clk_pin = board.GP17, board.GP21, board.GP20, board.GP19, board.GP18
elif 'ESP32-S2' in board_type:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.IO35, board.IO36, board.IO38, board.IO34, board.IO37
else:
mosi_pin, clk_pin, reset_pin, cs_pin, dc_pin = board.GP11, board.GP10, board.GP17, board.GP18, board.GP16
print("This board is not supported. Change the pin definitions above.")
spi = busio.SPI(clock=clk_pin, MOSI=mosi_pin)
display_bus = displayio.FourWire(spi, command=dc_pin, chip_select=cs_pin, reset=reset_pin)
display = adafruit_ili9341.ILI9341(display_bus, width=240, height=320, rotation=270)
splash = displayio.Group()
display.show(splash)
color_bitmap = displayio.Bitmap(240, 320, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x00FF00 # Bright Green
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(230, 310, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000 # Black
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=5, y=5)
splash.append(inner_sprite)
# Draw a label
text_group = displayio.Group(scale=1, x=11, y=24)
text = "Hello World!\n\nThis is a sample\ntext!\n\nEverything is \nworking fine."
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
text_group.append(text_area) # Subgroup for text scaling
splash.append(text_group)
2- 會用到 adafruit_display_text library,請至 https://github.com/adafruit/Adafruit_CircuitPython_Display_Text 下載後,將 "adafruit_display_text" 整個目錄上傳至 Pico_W 板子上