本帖最后由 未见 于 2023-10-16 13:58 编辑
第一部分:视频简介
视频地址:https://training.eeworld.com.cn/course/68150
第二部分:任务提交
任务一】控制屏幕显示中文:
实现代码如下:
import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
# 设置要显示的字符串
dis_str = "你好,CircuitPython\n你好,EEWORLD!"
def screen_dispstr(str):
# 根据屏幕旋转角度确定每行显示的字符数
if board.DISPLAY.rotation % 180 == 0:
char_num = 50 # 横屏
else:
char_num = 20 # 竖屏
strbuf = ""
for i in range(len(str) / char_num):
# 将字符串按每行字符数拆分,并添加换行符
strbuf = strbuf + str[i * char_num:(i + 1) * char_num] + "\n"
return strbuf
# 初始化显示屏
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
# 加载字体和设置颜色
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
color = 0x00FFFF
# 创建显示文本的组和标签
text_group = displayio.Group()
text_area = label.Label(font, text=screen_dispstr(dis_str), color=color)
text_area.x = 70
text_area.y = 55
text_area.line_spacing = 0.8
text_area.scale = 1
text_group.append(text_area)
display.show(text_group)
# 持续显示
while True:
pass
实现效果:
任务二】使用网络功能:
2.1创建AP热点
实现代码如下:
import os
import wifi
import adafruit_requests
print("ESP32-S3 Access Point Test")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
AP_SSID = "CircuitPythonAP"
AP_PASSWD = "12345678"
wifi.radio.start_ap(AP_SSID , AP_PASSWD )
print(f"My SSID: {AP_SSID}")
print(f"My PASSWORD: {AP_PASSWD}")
while True:
pass
2.2连接外部网络
实现代码如下:
import board
import displayio
import terminalio
import wifi
from adafruit_display_text import label
# 配置屏幕显示
display = board.DISPLAY
# 填写WIFI名和密码
SSID = "MIX4_Phone"
PASSWD = "12345678"
# 连接WIFI
wifi.radio.connect(SSID, PASSWD)
# 打印连接信息
text="WiFi:"+SSID+"\n"+"IP:"+str(wifi.radio.ipv4_address)
text_group = displayio.Group(scale=2,x=0,y=0,)
text_area = label.Label(terminalio.FONT, text=text, color=0x00FF00, x=10, y=25)
text_group.append(text_area) # Subgroup for text scaling
display.show(text_group)
while True:
pass
连接成功后屏幕显示效果:
任务三】控制板载WS2812B
控制代码如下:
import time
import board
import neopixel
import digitalio
from adafruit_display_text import bitmap_label
from adafruit_bitmap_font import bitmap_font
# 加载字体文件
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
# 定义颜色配置
colors = [(0x00, 0x00, 0x00), # 黑色
(0xFF, 0x00, 0x00), # 红色
(0x00, 0xFF, 0x00), # 绿色
(0x00, 0x00, 0xFF), # 蓝色
(0xFF, 0xFF, 0xFF)] # 白色
# 初始化WS2812B
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.5
# 按键初始化
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
# 初始化文本显示
text_area = bitmap_label.Label(font, scale=7)
text_area.x = 35
text_area.y = 60
current_color_index = -1
while True:
# 获取按键状态
button_state = not button.value
if button_state:
# Cycle through colors
current_color_index = (current_color_index + 1) % len(colors)
pixel.fill(colors[current_color_index])
# Set the text and color
text2 = ["黑色", "红色", "绿色", "蓝色", "白色"][current_color_index]
text_area.text = text2
text_area.color = colors[current_color_index]
board.DISPLAY.show(text_area)
time.sleep(0.2)
显示效果如下:
任务四】时钟和天气显示
import board, os, wifi
import time,ssl,socketpool,adafruit_requests
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
# 设置显示颜色
color = 0x00FF00
# 配置中文字体文件
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
# 显示初始化
display = board.DISPLAY
# 配置天气API
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
JSON_weather_URL = "http://t.weather.sojson.com/api/weather/city/{}"
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def refresh_screen(print_text):
text_area = label.Label(font, text=print_text, scale=1)
text_area.x = 5
text_area.y = 15
text_area.scale = 1
text_area.color = color
display.show(text_area)
# 获取并解析天气数据
def get_data():
try:
response = requests.get(JSON_TIME_URL)
except ConnectionError as e:
print("Connection Error:", e)
print("Retrying in 60 seconds")
time_data = response.json()['sysTime2'][:-3]
try:
city_code = "101190401"
url = JSON_weather_URL.format(city_code)
response = requests.get(url)
except ConnectionError as e:
print("connect error:{},retry in 60s".format(e))
weather_data = response.json()
cityInfo = weather_data['cityInfo']
city_weather = weather_data['data']
forecast = city_weather['forecast']
dis_str = cityInfo['parent'] + ' '+ cityInfo['city'] + ' ' + forecast[0]['week']
dis_str += "\n时间:"+time_data
dis_str += "\n温度:"+city_weather['wendu']+"℃"
dis_str += " 湿度:" + city_weather['shidu']
dis_str += '\n空气质量:' + city_weather['quality']
return dis_str
while not wifi.radio.ipv4_address:
try:
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
except ConnectionError as e:
print("connecti error:{},retry in 2s".format(e))
time.sleep(2)
print("get ip:", wifi.radio.ipv4_address)
while True:
info = get_data()
refresh_screen(info)
time.sleep(60)
实际显示效果:
任务五】网络控制WS2812B
第三部分:代码提交
点此下载。
|