一:帖子汇总
【得捷电子Follow me第2期】 任务1:控制屏幕显示中文 https://bbs.eeworld.com.cn/thread-1258723-1-1.html
【得捷电子Follow me第2期】 任务2:网络功能使用 https://bbs.eeworld.com.cn/thread-1258724-1-1.html
【得捷电子Follow me第2期】 任务3:控制WS2812B https://bbs.eeworld.com.cn/thread-1258725-1-1.html
【得捷电子Follow me第2期】 任务4:分任务1:日历&时钟 https://bbs.eeworld.com.cn/thread-1258726-1-1.html
视频链接:
https://training.eeworld.com.cn/video/37981
源码及库的下载链接如下:
https://download.eeworld.com.cn/detail/knv/629533
二:任务1:控制屏幕显示中文
终于等到了板子发货,顺丰包邮,速度很快
就是这一个小板子用一个大箱子装,也太豪华了哈哈
收到果断上电启动,demo程序正确加载,运气不错
0x0:刷机
板子到手只是demo状态,还需要先进行刷机
下载乐鑫官方的刷机工具
从circuitpython.org 下载板子对应的bin文件,进行刷机
使用官方工具
配置固件地址,选择对应 com接口,配置波特率为115200
点击“erase”
等待清空完成后,点击“start”
刷机完毕后,点击reset按钮,显示如下界面,固件已经刷好了
电脑成功显示u盘
0x1:显示中文前的准备工作
显示中文需要先创建字库文件。
我使用的方式是 “FontSmaller 2.0.exe”软件进行字体文件精简,然后使用“u8g2_fontmaker” 进行字体文件创建
步骤如下:
生成的BDF文件非常小
将字体文件复制倒开发板中
0x2:显示中文
经过观看了直播,了解想使用 Adafruit ESP32-S3 TFT Feather 显示中文还需要使用一个adafruit_display_text 库,所以下载该库到开发板的lib中
代码也很简单
import board
import displayio
from adafruit_display_text import label
import adafruit_bitmap_font.bitmap_font
display = board.DISPLAY
board.DISPLAY.brightness = 1
font = adafruit_bitmap_font.bitmap_font.load_font("mfyh-16.bdf")
text_group = displayio.Group()
text_area = label.Label(font, text="北京市房山区晴天", color=(255,255,255))
text_area.x = 0
text_area.y = 20
text_group.append(text_area)
display.show(text_group)
显示效果:
三: 任务2:网络功能使用
0x0:创建一个 secrets.py文件用于保存wifi信息
内容以下
secrets = {
"ssid": "这是ssid",
"password": "这是密码",
"timezone": "Asia/Shanghai", # Check http://worldtimeapi.org/timezones
}
0x1 :链接wifi
链接wifi代码如下,其中使用 wifi.radio.connect 进行链接wifi。
import board
import displayio
from adafruit_display_text import label
import adafruit_bitmap_font.bitmap_font
import wifi
import time
import ssl
import socketpool
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
display = board.DISPLAY
board.DISPLAY.brightness = 1
font = adafruit_bitmap_font.bitmap_font.load_font("mfyh-16.bdf")
text_group = displayio.Group()
text_area = label.Label(font, color=(255,255,255))
text_area.text=str(wifi.radio.ipv4_address)
text_area.x = 0
text_area.y = 20
text_group.append(text_area)
display.show(text_group)
运行结果如下:
0x1:创建一个AP,其中关键点就是使用 wifi.radio.start_ap 函数即可创建成功,非常简单
代码如下:
import board
import digitalio
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
import wifi
import os
import time
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
font = bitmap_font.load_font("font/DingTalk_ncn_60.pcf")
color = 0xffffff
text_change = 0
text_group = displayio.Group()
text_area = label.Label(font, text="test", color=color)
text_area.x = 2
text_area.y = 50
text_area.line_spacing = 0.8
text_area.scale = 1
wifi.radio.start_ap('wifitest', '12345678')
text_area.text="running"
text_group.append(text_area)
display.show(text_group)
while True:
time.sleep(2)
运行结果:
四:任务3:控制WS2812B
0x0:准备好板卡链接。导入控制RGB灯的库 adafruit_ws2801.py
板子的RGB灯为
board.NEOPIXEL
只需要使用neopixel库,进行RGB控制该设备即可
按键切换灯颜色,首先创建一个颜色列表,将 颜色RGB值定义
colors = [(0x00, 0x00, 0x00), # black
(0xFF, 0x00, 0x00), # red
(0x00, 0xFF, 0x00), # green
(0x00, 0x00, 0xFF), # blue
(0xFF, 0xFF, 0xFF)] # white
创建一个循环,监听按键事件,若按下则颜色+1,采用color 的颜色,这样缩减了代码的大小,使程序逻辑更清晰
屏幕同时显示对应的颜色文字,与之前的显示中文代码类似,不再赘述
0x1:其中代码如下
import time
import board
import neopixel
import digitalio
from adafruit_display_text import bitmap_label
from adafruit_bitmap_font import bitmap_font
# Load the font
font = bitmap_font.load_font("font/DingTalk_ncn_60.pcf")
# Define colors
colors = [(0x00, 0x00, 0x00), # black
(0xFF, 0x00, 0x00), # red
(0x00, 0xFF, 0x00), # green
(0x00, 0x00, 0xFF), # blue
(0xFF, 0xFF, 0xFF)] # white
# Initialize NeoPixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.5
# Initialize the button
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
# Initialize text label
text_area = bitmap_label.Label(font, scale=1)
text_area.x = 0
text_area.y = 60
current_color_index = -1
while True:
# Read the button state
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 = ["black", "red", "green", "blue", "white"][current_color_index]
text_area.text = text2
text_area.color = colors[current_color_index]
board.DISPLAY.show(text_area)
time.sleep(0.1)
显示效果如下:
五:任务4:分任务1:日历&时钟——完成一个可通过互联网更新的万年历时钟,并显示当地的天气信息
0x0: 相关API
本次使用高德地图的根据IP访问获取地区信息
使用高德地图的API根据地区信息获取天气信息
使用http://worldtimeapi.org/api/ip 获取准确的时间信息
创建secrets.py
secrets = {
"ssid": "这是ssid",
"password": "这是密码",
"timezone": "Asia/Shanghai", # Check http://worldtimeapi.org/timezones
"token":"高德api token"
}
0x1:使用到的库
创建wifi链接信息文件 secrets.py
0x2:编写代码
import board
import wifi
import time
import ssl
import socketpool
import adafruit_requests
import displayio
import terminalio
import adafruit_imageload
from adafruit_display_text import label
import adafruit_bitmap_font.bitmap_font
import rtc
the_rtc = rtc.RTC()
基本库导入完毕后,配置WIFI链接
token = "高德API的token"
### connecte to a WiFi AP (AP name and password in secrets.py) ###
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
获得屏幕信息,设置屏幕方向
screen = board.DISPLAY
screen.rotation = 90
设置一个背景图片
bitmap, palette = adafruit_imageload.load("/imgs/purple.bmp",bitmap=displayio.Bitmap,palette=displayio.Palette)
# Create a TileGrid to hold the bitmap
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette,tile_width=160, tile_height=240,# pixels number
width=1, height=1) # tiles number
创造一些显示信息
FONT = adafruit_bitmap_font.bitmap_font.load_font("mfyh-16.bdf")
arealabel = label.Label(FONT, x=0, y=32, text="北京市", scale=1, color=(255,255,255))
wtlabel = label.Label(FONT, x=0, y=62, text="晴", scale=1, color=(255,255,255))
wdlabel = label.Label(FONT, x=0, y=92, text="温度:", scale=1, color=(255,255,255))
sdlabel = label.Label(FONT, x=0, y=122, text="湿度:", scale=1, color=(255,255,255))
yearlabel = label.Label(FONT, x=0, y=152, text="时间:", scale=1, color=(255,255,255))
monthlabel = label.Label(FONT, x=0, y=182, text="", scale=1, color=(255,255,255))
daylabel = label.Label(FONT, x=0, y=212, text="", scale=1, color=(255,255,255))
# Create a Group to hold the TileGrid
group = displayio.Group()
# Add the TileGrid to the Group
group.append(tile_grid)
group.append(arealabel)
group.append(wtlabel)
group.append(wdlabel)
group.append(sdlabel)
group.append(yearlabel)
group.append(monthlabel)
group.append(daylabel)
screen.show(group)
tile_grid[0] = 0
time.sleep(1.0)
打印WIFI信息,创造SOKCET连接池
print("IP addr:", wifi.radio.ipv4_address)
response = None
pool = socketpool.SocketPool(wifi.radio)
# 建立连接
session = adafruit_requests.Session(pool, ssl.create_default_context())
使用高德API获取ip的区域代码
while True:
response = session.get("https://restapi.amap.com/v3/ip?key="+token)
data = response.json()
#print("data:",data["adcode"])
adcode = data["adcode"]
time.sleep(1)
break
使用worldtimeapi 获得地区时间
while True:
response = session.get("http://worldtimeapi.org/api/ip")
json = response.json()
current_time = json["datetime"]
the_date, the_time = current_time.split("T")
year, month, mday = [int(x) for x in the_date.split("-")]
the_time = the_time.split(".")[0]
hours, minutes, seconds = [int(x) for x in the_time.split(":")]
# We can also fill in these extra nice things
year_day = json["day_of_year"]
week_day = json["day_of_week"]
# Daylight Saving Time (夏令时)?
is_dst = json["dst"]
now = time.struct_time(
(year, month, mday, hours, minutes, seconds+1, week_day, year_day, is_dst) )
the_rtc.datetime = now
time.sleep(1)
break
loopdata = 600
使用高德API定时刷新数据信息
while True:
#yearlabel.text = "时间:"+str(the_rtc.datetime.tm_year)
monthlabel.text = ""+str(the_rtc.datetime.tm_mon)+"-"+str(the_rtc.datetime.tm_mday)+""
daylabel.text = "" + str(the_rtc.datetime.tm_hour) + ":" + str(the_rtc.datetime.tm_min) + ":"+str(the_rtc.datetime.tm_sec)+""
try:
if loopdata==600:
response = session.get("https://restapi.amap.com/v3/weather/weatherInfo?key="+token+"&city="+adcode)
data = response.json()
#print("data:",data["adcode"])
wd = data["lives"][0]["temperature_float"]
sd = data["lives"][0]["humidity_float"]
wdlabel.text ="温度: "+ wd+"°"
sdlabel.text = "湿度: "+sd+"%"
arealabel.text = data["lives"][0]["province"]
wtlabel.text = data["lives"][0]["weather"]
loopdata=0
else:
loopdata = loopdata +1
time.sleep(1)
except:
pass
效果如下:
https://training.eeworld.com.cn/video/37981
源码及库的下载链接如下:
https://download.eeworld.com.cn/detail/knv/629533
经过参加得捷电子Follow Me第2期的学习,我掌握了更现代化的单片机开发技巧。使用Python编程语言,我能够更迅速地开发程序,实现项目的顺利落地。这个学习经历让我受益匪浅,为我的职业发展和技能提升带来了重要的帮助。
本帖最后由 knv 于 2023-10-10 16:45 编辑