2592|16

10

帖子

1

TA的资源

一粒金砂(中级)

楼主
 

【得捷电子Follow me第2期】CircuitPython入门到完成任务1-5详细教程 [复制链接]

 
  本帖最后由 ttxiaotaoge 于 2023-8-24 11:19 编辑

 本帖记录我这两天使用CircuitPython固件完成任务1-5的过程

可完成任务:

任务1:控制屏幕显示中文(必做任务)----- 完成屏幕的控制,并且能显示中文

任务2:网络功能使用(必做任务)----- 完成网络功能的使用,能够创建热点和连接到WiFi

任务3:控制WS2812B(必做任务)---- 使用按键控制板载Neopixel LED的显示和颜色切换

任务4:从下方5个分任务中选择1个感兴趣的完成即可(必做任务)

■  分任务1:日历&时钟——完成一个可通过互联网更新的万年历时钟,并显示当地的天气信息

■  分任务2:WS2812B效果控制——完成一个Neopixel(12灯珠或以上)控制器,通过按键和屏幕切换展示效果

任务5:通过网络控制WS2812B(可选任务,非必做)----- 结合123,在手机上通过网络控制板载Neopixel LED的显示和颜色切换,屏幕同步显示状态

 

先贴上实验现象给大伙看下,由于活动提供的板子还没到,只能用手头上现有的板子给大家演示,实际代码区别仅是部分IO的定义不同和显示的坐标不同,可以轻松移植

  本套固件支持功能:

1. 代码运行后将自动联网,如联网成功会自动同步网络时间和获取天气信息并显示在LCD屏幕上,时间显示为经过同步的RTC内部时钟,天气地区配置在代码中设定,使用知心天气API

若未成功联网将会开启AP模式,默认为SSID为ESP32无密码。显示屏会显示当前网络模式和访问ip,同时显示本地时间(RTC内部时钟)

2. 电脑or手机在同一网络下(esp32连接同一个wifi or 连接esp32发出的wifi)访问‘ip’:1080/client 可进入WEB控制中心,该界面下可控制板载的RGB灯,控制灯光效果,LCD屏将同步显示当前RGB颜色配置。

3. 板载按键也可以控制灯光效果,同时将状态上报WEB控制中心

4. 支持web编辑界面,可直接访问IP进入相关circuitpyhon菜单。

相关演示如下:

 

 

 

播放器加载失败: 未检测到Flash Player,请到安装
演示

---

代码解析

先看下代码结构,主要为lib,fonts, code.py, settings.toml         

其中lib里存放了我们需要的一些外部库(来源adafruit-circuitpython-bundle-8.x-mpy-20230815)

fonts中存放了我们用到的中文字体库(格式为pcf,通过fontforge和bdftopcf font converter (adafruit.github.io)进行转换),字体生成具体方法可参考官方教程Use FontForge | Custom Fonts for CircuitPython Displays | Adafruit Learning System

code.py就是我们的实际工作代码了。

settings.toml 将宏定义我们需要连接的wifi名称和密码(注意esp32只能连接2.4G频段的wifi),以及使用web编辑器的连接密码

 

下面贴下我们code.py中的源代码,并对他进行初步讲解。

  • import board
  • import digitalio
  • import terminalio
  • import busio
  • import neopixel
  • import rtc
  • import time
  • #网络相关库
  • import wifi
  • import socketpool
  • import ssl
  • import adafruit_requests
  • from adafruit_httpserver import Server, Request, Response, Websocket, GET
  • #显示相关库
  • import displayio
  • from adafruit_st7735r import ST7735R
  • from adafruit_bitmap_font import bitmap_font
  • from adafruit_display_text.label import Label
  • # displayDriver_Begin
  • displayio.release_displays()
  • spi = busio.SPI(board.GPIO18, board.GPIO17)
  • tft_cs = board.GPIO14
  • tft_dc = board.GPIO15
  • display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.GPIO16)
  • display = ST7735R(display_bus, width=128, height=160,rowstart=0,colstart=0,rotation=0,bgr = 1)#160 * 128
  • # display = ST7735R(display_bus, width=160, height=80,rowstart=0,colstart=24,rotation=270)#160 * 80
  • my_display_group = displayio.Group()
  • print("Display Init Success\n")
  • # displayDriver_End
  • # WiFi状态查询,未连接WIFI则不联网更新数据,开启AP模式
  • wificonnect_State = wifi.radio.connected
  • print("wificonnect_State",wificonnect_State)
  • if(wificonnect_State == False):
  • wifi.radio.start_ap(ssid = 'Esp32', password = '')
  • Wifi_Mode = 'STA' if(wificonnect_State == True) else 'AP'
  • Wifi_ip = wifi.radio.ipv4_address if(wificonnect_State == True) else wifi.radio.ipv4_address_ap
  • #构造函数——按键控制RGB
  • i = 0
  • Enter_flag = False
  • def Button0_Work():
  • global i
  • global Enter_flag
  • global RGBr, RGBg, RGBb
  • if(Button0.value == False and Enter_flag == False):
  • time.sleep(0.05)#延时消抖
  • if(Button0.value == False and Enter_flag == False):
  • i+=1
  • RGBr, RGBg, RGBb = 8*i, 8*i, 8*i
  • pixels.fill((RGBr, RGBg, RGBb))
  • Enter_flag = True
  • print("pixels change\n",i)
  • if(i>20):
  • i = 0
  • elif(Button0.value == True and Enter_flag == True):
  • time.sleep(0.05)#延时消抖
  • if(Button0.value == True and Enter_flag == True):
  • Enter_flag = False
  • #构造函数——解码天气
  • def parse_weather(weather_str):
  • # Extract the time part from the datetime string
  • area = weather_str['results'][0]['location']['name']
  • temp = weather_str['results'][0]['now']['temperature']
  • weather = weather_str['results'][0]['now']['text']
  • return area, temp, weather
  • #构造函数——解码时间
  • def parse_time(datetime_str):
  • # Extract the time part from the datetime string
  • date_str = datetime_str.split("T")[0]
  • time_str = datetime_str.split("T")[1].split(".")[0]
  • hour, minute, second = map(int, time_str.split(":"))
  • date = date_str
  • return hour, minute, second, date
  • #构造函数——更新网络数据
  • def Update_Data():
  • global r
  • global current_area, current_temp, current_weather
  • current_area = current_temp = current_weather = None
  • r = rtc.RTC()
  • if(wificonnect_State == True):
  • #WeatherUpdate Begin
  • requests = adafruit_requests.Session(pool, ssl.create_default_context())
  • response_Weather = requests.get(WEATHER_DATA_SOURCE)
  • data_Weather = response_Weather.json()
  • response_Weather= requests.get(WEATHER_DATA_SOURCE)
  • response_Weather.socket.close()
  • current_area, current_temp, current_weather = parse_weather(data_Weather)
  • print("WeatherUpdate Success\n")
  • #WeatherUpdate End
  • #ClockTimeUpdate Begin
  • requests = adafruit_requests.Session(pool, ssl.create_default_context())
  • response_clock = requests.get(TIME_DATA_SOURCE)
  • data_clock = response_clock.json()
  • response_clock= requests.get(WEATHER_DATA_SOURCE)
  • response_clock.socket.close()
  • current_hour, current_minute, current_second, current_date = parse_time(data_clock["datetime"])
  • r.datetime = time.struct_time((int(current_date[0:4]), int(current_date[5:7]), int(current_date[8:10]), \
  • current_hour, current_minute, current_second, 0, -1, -1))
  • print("ClockTimeUpdate Success\n")
  • #ClockTimeUpdate Begin
  • # RGBDriver_Begin
  • pixel_pin = board.GPIO10
  • num_pixels = 16
  • pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=True)
  • pixels.fill((8, 8, 8))
  • print("RGBDriver Init Success\n")
  • # RGBDriver_End
  • # ButtonDriver_Begin
  • Button0 = digitalio.DigitalInOut(board.GPIO0)
  • Button0.direction = digitalio.Direction.INPUT
  • print("Button0 Init Success\n")
  • # ButtonDriver_End
  • # 添加中文字体
  • font_file = "fonts/test16-16.pcf"
  • # font_file = "fonts/test16-16.bdf"
  • # font_file = "fonts/test24-24.bdf"
  • # font_file = "fonts/opposans_m_12.pcf"
  • # font_file = "fonts/sanjiruosixingkai_m_16.pcf"
  • font_Chinese = bitmap_font.load_font(font_file)
  • print("fonts Init Success\n")
  • # 显示WiFi IP 和 Mode
  • text_Wifi = "WiFi:{}\n{}".format(Wifi_Mode,Wifi_ip)#更新RGB状态
  • text_Wifi_title = Label(terminalio.FONT, text=text_Wifi, color = 0xffffff)
  • text_Wifi_title.x = 5
  • text_Wifi_title.y = 5
  • my_display_group.append(text_Wifi_title)
  • # 显示RGB状态,和按键状态,RGB文字同RGB灯颜色
  • RGBr, RGBg, RGBb = 255,255,255
  • text_RGB = "RGB:{:03}:{:03}:{:03}\nKEY:{:02}".format(RGBr, RGBg, RGBb, i)#更新RGB状态
  • text_RGB_title = Label(terminalio.FONT, text=text_RGB, color = (RGBr, RGBg, RGBb))
  • text_RGB_title.x = 5
  • text_RGB_title.y = 30
  • my_display_group.append(text_RGB_title)
  • # ShowChinese_End
  • # GetandSetHttp_Begin
  • TIME_DATA_SOURCE = "http://worldtimeapi.org/api/timezone/Asia/Shanghai"
  • WEATHER_DATA_SOURCE = "http://api.seniverse.com/v3/weather/now.json?key=SBmmNRnXdiTFi8UQw&location=nanjing&language=zh-Hans&unit=c"
  • pool = socketpool.SocketPool(wifi.radio) # 网络管理,触发http操作
  • server = Server(pool, debug=True)
  • websocket: Websocket = None
  • next_message_time = time.monotonic()#数据更新时钟,以秒为单位变化
  • HTML_TEMPLATE = """
  • <html lang="en">
  • <head>
  • <!--注释:强制html使用utf-8编码-->
  • <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  • <title>Web控制中心</title>
  • </head>
  • <body>
  • <p>按键状态: <strong>-</strong></p>
  • <p>RGB 颜色: <input type="color"></p>
  • <script>
  • const Button_count = document.querySelector('strong');
  • const colorPicker = document.querySelector('input[type="color"]');
  • let ws = new WebSocket('ws://' + location.host + '/connect-websocket');
  • ws.onopen = () => console.log('WebSocket connection opened');
  • ws.onclose = () => console.log('WebSocket connection closed');
  • ws.onmessage = event => Button_count.textContent = event.data;
  • ws.onerror = error => Button_count.textContent = error;
  • colorPicker.oninput = debounce(() => ws.send(colorPicker.value), 200);
  • <!--注释:创建一个延时函数,消除RGB调整抖动-->
  • function debounce(callback, delay = 1000) {
  • let timeout
  • return (...args) => {
  • clearTimeout(timeout)
  • timeout = setTimeout(() => {
  • callback(...args)
  • }, delay)
  • }
  • }
  • </script>
  • </body>
  • </html>
  • """
  • #创建Http服务器
  • @server.route("/client", GET)
  • def client(request: Request):
  • return Response(request, HTML_TEMPLATE, content_type="text/html")
  • #创建websocket服务
  • @server.route("/connect-websocket", GET)
  • def connect_client(request: Request):
  • global websocket # pylint: disable=global-statement
  • if websocket is not None:
  • print("websocket.close")
  • websocket.close() # Close any existing connection
  • websocket = Websocket(request)
  • return websocket
  • # GetTimeandTemp_Beign
  • Update_Data()
  • # GetTimeandTemp_End
  • # DrawTimeandTemp_Beign
  • time_label_text = "{}-{:02}-{:02}\n"\
  • "时间:{:02}:{:02}:{:02}\n"\
  • "地区:{}\n"\
  • "温度:{}\n"\
  • "天气:{}\n".format(r.datetime.tm_year, r.datetime.tm_mon, r.datetime.tm_mday, \
  • r.datetime.tm_hour, r.datetime.tm_min, r.datetime.tm_sec, \
  • current_area, current_temp, current_weather)
  • time_label = Label(font = font_Chinese, text=time_label_text, color = 0xff00ff)
  • time_label.x = 10
  • time_label.y = 60
  • my_display_group.append(time_label)
  • # DrawTimeandTemp_End
  • #UI show Begin
  • display.show(my_display_group)
  • print("display show Success\n")
  • #UI show End
  • #判断网络模式,设定location.host
  • if(wificonnect_State == True):
  • server.start(str(wifi.radio.ipv4_address), port = 1080)
  • else:
  • server.start(str(wifi.radio.ipv4_address_ap), port = 1080)
  • # GetandSetHttp_Begin
  • time.sleep(2)#等待2秒进入循环
  • while True:
  • #httpSeverLoop_Begin
  • server.poll()
  • # Check for incoming messages from client
  • if websocket is not None:
  • if (data := websocket.receive(True)) is not None:
  • RGBr, RGBg, RGBb = int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16)
  • pixels.fill((RGBr, RGBg, RGBb))
  • # Send a message every second
  • if websocket is not None and next_message_time < time.monotonic():
  • time.sleep(0.2)
  • if websocket is not None:
  • Button_count = i
  • websocket.send_message(str(Button_count))
  • next_message_time = time.monotonic() + 1
  • #httpSeverLoop_End
  • #Other_Loop
  • text_RGB_title.color = (RGBr, RGBg, RGBb)
  • text_RGB_title.text = "RGB:{:03}:{:03}:{:03}\nKEY:{:02}".format(RGBr, RGBg, RGBb, i)#更新RGB状态
  • time_label.text = "{}-{:02}-{:02}\n"\
  • "时间:{:02}:{:02}:{:02}\n"\
  • "地区:{}\n"\
  • "温度:{}\n"\
  • "天气:{}\n".format(r.datetime.tm_year, r.datetime.tm_mon, r.datetime.tm_mday, \
  • r.datetime.tm_hour, r.datetime.tm_min, r.datetime.tm_sec, \
  • current_area, current_temp, current_weather)
  • Button0_Work()

第一部分,添加需要的库文件和初始屏幕,如果是使用活动开发板的同学,可以直接在初始化的地方输入 display = board.DISPLAY即可

由于我使用的ST3375驱动的160x128的屏幕,所以需要添加如下代码

  • import board
  • import digitalio
  • import terminalio
  • import busio
  • import neopixel
  • import rtc
  • import time
  • #网络相关库
  • import wifi
  • import socketpool
  • import ssl
  • import adafruit_requests
  • from adafruit_httpserver import Server, Request, Response, Websocket, GET
  • #显示相关库
  • import displayio
  • from adafruit_st7735r import ST7735R
  • from adafruit_bitmap_font import bitmap_font
  • from adafruit_display_text.label import Label
  • # displayDriver_Begin
  • displayio.release_displays()
  • spi = busio.SPI(board.GPIO18, board.GPIO17)
  • tft_cs = board.GPIO14
  • tft_dc = board.GPIO15
  • display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.GPIO16)
  • display = ST7735R(display_bus, width=128, height=160,rowstart=0,colstart=0,rotation=0,bgr = 1)#160 * 128
  • # display = ST7735R(display_bus, width=160, height=80,rowstart=0,colstart=24,rotation=270)#160 * 80
  • my_display_group = displayio.Group()
  • print("Display Init Success\n")
  • # displayDriver_End

第二部分,判断WiFi状态并设定WiFi状态

  • # WiFi状态查询,未连接WIFI则不联网更新数据,开启AP模式
  • wificonnect_State = wifi.radio.connected
  • print("wificonnect_State",wificonnect_State)
  • if(wificonnect_State == False):
  • wifi.radio.start_ap(ssid = 'Esp32', password = '')
  • Wifi_Mode = 'STA' if(wificonnect_State == True) else 'AP'
  • Wifi_ip = wifi.radio.ipv4_address if(wificonnect_State == True) else wifi.radio.ipv4_address_ap

第三部分,构造封装一些我们需要用到的函数,为了让代码更加清晰

  • #构造函数——按键控制RGB
  • i = 0
  • Enter_flag = False
  • def Button0_Work():
  • global i
  • global Enter_flag
  • global RGBr, RGBg, RGBb
  • if(Button0.value == False and Enter_flag == False):
  • time.sleep(0.05)#延时消抖
  • if(Button0.value == False and Enter_flag == False):
  • i+=1
  • RGBr, RGBg, RGBb = 8*i, 8*i, 8*i
  • pixels.fill((RGBr, RGBg, RGBb))
  • Enter_flag = True
  • print("pixels change\n",i)
  • if(i>20):
  • i = 0
  • elif(Button0.value == True and Enter_flag == True):
  • time.sleep(0.05)#延时消抖
  • if(Button0.value == True and Enter_flag == True):
  • Enter_flag = False
  • #构造函数——解码天气
  • def parse_weather(weather_str):
  • # Extract the time part from the datetime string
  • area = weather_str['results'][0]['location']['name']
  • temp = weather_str['results'][0]['now']['temperature']
  • weather = weather_str['results'][0]['now']['text']
  • return area, temp, weather
  • #构造函数——解码时间
  • def parse_time(datetime_str):
  • # Extract the time part from the datetime string
  • date_str = datetime_str.split("T")[0]
  • time_str = datetime_str.split("T")[1].split(".")[0]
  • hour, minute, second = map(int, time_str.split(":"))
  • date = date_str
  • return hour, minute, second, date
  • #构造函数——更新网络数据
  • def Update_Data():
  • global r
  • global current_area, current_temp, current_weather
  • current_area = current_temp = current_weather = None
  • r = rtc.RTC()
  • if(wificonnect_State == True):
  • #WeatherUpdate Begin
  • requests = adafruit_requests.Session(pool, ssl.create_default_context())
  • response_Weather = requests.get(WEATHER_DATA_SOURCE)
  • data_Weather = response_Weather.json()
  • response_Weather= requests.get(WEATHER_DATA_SOURCE)
  • response_Weather.socket.close()
  • current_area, current_temp, current_weather = parse_weather(data_Weather)
  • print("WeatherUpdate Success\n")
  • #WeatherUpdate End
  • #ClockTimeUpdate Begin
  • requests = adafruit_requests.Session(pool, ssl.create_default_context())
  • response_clock = requests.get(TIME_DATA_SOURCE)
  • data_clock = response_clock.json()
  • response_clock= requests.get(WEATHER_DATA_SOURCE)
  • response_clock.socket.close()
  • current_hour, current_minute, current_second, current_date = parse_time(data_clock["datetime"])
  • r.datetime = time.struct_time((int(current_date[0:4]), int(current_date[5:7]), int(current_date[8:10]), \
  • current_hour, current_minute, current_second, 0, -1, -1))
  • print("ClockTimeUpdate Success\n")
  • #ClockTimeUpdate Begin

第四部分,相关模块的初始化,具体操作可以看代码中的相关注释

其中API获取网络天气和时间,需要根据自身的需求进行修改,包含知心天气的KEY和地区

TIME_DATA_SOURCE = "http://worldtimeapi.org/api/timezone/Asia/Shanghai"
WEATHER_DATA_SOURCE = "http://api.seniverse.com/v3/weather/now.json?key=SBmmNRnXdiTFi8UQw&location=nanjing&language=zh-Hans&unit=c"

  • # RGBDriver_Begin
  • pixel_pin = board.GPIO10
  • num_pixels = 16
  • pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=True)
  • pixels.fill((8, 8, 8))
  • print("RGBDriver Init Success\n")
  • # RGBDriver_End
  • # ButtonDriver_Begin
  • Button0 = digitalio.DigitalInOut(board.GPIO0)
  • Button0.direction = digitalio.Direction.INPUT
  • print("Button0 Init Success\n")
  • # ButtonDriver_End
  • # 添加中文字体
  • font_file = "fonts/test16-16.pcf"
  • # font_file = "fonts/test16-16.bdf"
  • # font_file = "fonts/test24-24.bdf"
  • # font_file = "fonts/opposans_m_12.pcf"
  • # font_file = "fonts/sanjiruosixingkai_m_16.pcf"
  • font_Chinese = bitmap_font.load_font(font_file)
  • print("fonts Init Success\n")
  • # 显示WiFi IP 和 Mode
  • text_Wifi = "WiFi:{}\n{}".format(Wifi_Mode,Wifi_ip)#更新RGB状态
  • text_Wifi_title = Label(terminalio.FONT, text=text_Wifi, color = 0xffffff)
  • text_Wifi_title.x = 5
  • text_Wifi_title.y = 5
  • my_display_group.append(text_Wifi_title)
  • # 显示RGB状态,和按键状态,RGB文字同RGB灯颜色
  • RGBr, RGBg, RGBb = 255,255,255
  • text_RGB = "RGB:{:03}:{:03}:{:03}\nKEY:{:02}".format(RGBr, RGBg, RGBb, i)#更新RGB状态
  • text_RGB_title = Label(terminalio.FONT, text=text_RGB, color = (RGBr, RGBg, RGBb))
  • text_RGB_title.x = 5
  • text_RGB_title.y = 30
  • my_display_group.append(text_RGB_title)
  • # ShowChinese_End
  • # GetandSetHttp_Begin
  • TIME_DATA_SOURCE = "http://worldtimeapi.org/api/timezone/Asia/Shanghai"
  • WEATHER_DATA_SOURCE = "http://api.seniverse.com/v3/weather/now.json?key=SBmmNRnXdiTFi8UQw&location=nanjing&language=zh-Hans&unit=c"
  • pool = socketpool.SocketPool(wifi.radio) # 网络管理,触发http操作
  • server = Server(pool, debug=True)
  • websocket: Websocket = None
  • next_message_time = time.monotonic()#数据更新时钟,以秒为单位变化
  • HTML_TEMPLATE = """
  • <html lang="en">
  • <head>
  • <!--注释:强制html使用utf-8编码-->
  • <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  • <title>Web控制中心</title>
  • </head>
  • <body>
  • <p>按键状态: <strong>-</strong></p>
  • <p>RGB 颜色: <input type="color"></p>
  • <script>
  • const Button_count = document.querySelector('strong');
  • const colorPicker = document.querySelector('input[type="color"]');
  • let ws = new WebSocket('ws://' + location.host + '/connect-websocket');
  • ws.onopen = () => console.log('WebSocket connection opened');
  • ws.onclose = () => console.log('WebSocket connection closed');
  • ws.onmessage = event => Button_count.textContent = event.data;
  • ws.onerror = error => Button_count.textContent = error;
  • colorPicker.oninput = debounce(() => ws.send(colorPicker.value), 200);
  • <!--注释:创建一个延时函数,消除RGB调整抖动-->
  • function debounce(callback, delay = 1000) {
  • let timeout
  • return (...args) => {
  • clearTimeout(timeout)
  • timeout = setTimeout(() => {
  • callback(...args)
  • }, delay)
  • }
  • }
  • </script>
  • </body>
  • </html>
  • """
  • #创建Http服务器
  • @server.route("/client", GET)
  • def client(request: Request):
  • return Response(request, HTML_TEMPLATE, content_type="text/html")
  • #创建websocket服务
  • @server.route("/connect-websocket", GET)
  • def connect_client(request: Request):
  • global websocket # pylint: disable=global-statement
  • if websocket is not None:
  • print("websocket.close")
  • websocket.close() # Close any existing connection
  • websocket = Websocket(request)
  • return websocket
  • # GetTimeandTemp_Beign
  • Update_Data()
  • # GetTimeandTemp_End
  • # DrawTimeandTemp_Beign
  • time_label_text = "{}-{:02}-{:02}\n"\
  • "时间:{:02}:{:02}:{:02}\n"\
  • "地区:{}\n"\
  • "温度:{}\n"\
  • "天气:{}\n".format(r.datetime.tm_year, r.datetime.tm_mon, r.datetime.tm_mday, \
  • r.datetime.tm_hour, r.datetime.tm_min, r.datetime.tm_sec, \
  • current_area, current_temp, current_weather)
  • time_label = Label(font = font_Chinese, text=time_label_text, color = 0xff00ff)
  • time_label.x = 10
  • time_label.y = 60
  • my_display_group.append(time_label)
  • # DrawTimeandTemp_End
  • #UI show Begin
  • display.show(my_display_group)
  • print("display show Success\n")
  • #UI show End

第五部分,在wile循环中更新屏幕的显示状态,和相应web控制中心的操作指令

  • while True:
  • #httpSeverLoop_Begin
  • server.poll()
  • # Check for incoming messages from client
  • if websocket is not None:
  • if (data := websocket.receive(True)) is not None:
  • RGBr, RGBg, RGBb = int(data[1:3], 16), int(data[3:5], 16), int(data[5:7], 16)
  • pixels.fill((RGBr, RGBg, RGBb))
  • # Send a message every second
  • if websocket is not None and next_message_time < time.monotonic():
  • time.sleep(0.2)
  • if websocket is not None:
  • Button_count = i
  • websocket.send_message(str(Button_count))
  • next_message_time = time.monotonic() + 1
  • #httpSeverLoop_End
  • #Other_Loop
  • text_RGB_title.color = (RGBr, RGBg, RGBb)
  • text_RGB_title.text = "RGB:{:03}:{:03}:{:03}\nKEY:{:02}".format(RGBr, RGBg, RGBb, i)#更新RGB状态
  • time_label.text = "{}-{:02}-{:02}\n"\
  • "时间:{:02}:{:02}:{:02}\n"\
  • "地区:{}\n"\
  • "温度:{}\n"\
  • "天气:{}\n".format(r.datetime.tm_year, r.datetime.tm_mon, r.datetime.tm_mday, \
  • r.datetime.tm_hour, r.datetime.tm_min, r.datetime.tm_sec, \
  • current_area, current_temp, current_weather)
  • Button0_Work()

至此,该代码就实现了我们follow me第二期的1到5的全部功能啦,是不是非常简单呢

在这两天的学习中还是遇到了很多小bug的,建议大家从以下几个官方网站获取API的相关教程

Adafruit Learning System 在这个网站中有大量的实战案例和api的使用教程,建议直接搜索需要用到的模块来参考示例,如直接搜索fonts来了解如何生成字体库

CircuitPython — Adafruit CircuitPython 8.2.0 documentation 这个是circuitpyhon固件的使用文档,可以在里面找到所有api的使用说明,了解其传入传出的参数及配置先后顺序

链接已隐藏,如需查看请登录或者注册
 这个官方仓库中包含了我们需要用到的所有库文件以及对应的例程,可以参考其中的内容进行代码的编写和集成。

这次分享就到这里了,在最后附上我们本次工程的全部代码供大家参考。(活动提供的板子,板载内存可以使用的空间只有2M左右,请按照需求添加lib和fonts文件)

code.zip (1.86 MB, 下载次数: 22)

最新回复

谢谢,我去学习一下   详情 回复 发表于 2023-9-20 16:12

赞赏

1

查看全部赞赏

点赞(2) 关注
 
 

回复
举报

5

帖子

0

TA的资源

一粒金砂(初级)

沙发
 

大佬,这个任务有教程吗?■  分任务4:音乐播放功能——实现音乐播放器功能,可以在屏幕上显示列表信息和音乐信息,使用按键进行切换,使用扬声器进行播放



建议搭配器件:Adafruit ESP32-S3 TFT Feather、音频放大器、扬声器


 
 
 

回复

10

帖子

1

TA的资源

一粒金砂(中级)

板凳
 
felix456 发表于 2023-8-24 14:54 大佬,这个任务有教程吗?■  分任务4:音乐播放功能——实现音乐播放器功能,可以在屏幕上显示 ...

这个没有,我没买那些设备

 
 
 

回复

10

帖子

1

TA的资源

一粒金砂(中级)

4
 
felix456 发表于 2023-8-24 14:54 大佬,这个任务有教程吗?■  分任务4:音乐播放功能——实现音乐播放器功能,可以在屏幕上显示 ...

可以学习下这个ESP32迷你墨水屏MP3收音机多功能 - 嘉立创EDA开源硬件平台 (oshwhub.com)

 
 
 

回复

10

帖子

6

TA的资源

一粒金砂(中级)

5
 

大佬,这个函数调用老是报错:extra positional arguments given

知不知道怎么搞。

wifi.radio.set_ipv4_address_ap(ip, mask, gateway)

 
 
 

回复

10

帖子

1

TA的资源

一粒金砂(中级)

6
 
青蛙2009 发表于 2023-8-24 22:27 大佬,这个函数调用老是报错:extra positional arguments given 知不知道怎么搞。 wifi.radio.set_i ...

输入参数多了

 
 
 

回复

68

帖子

0

TA的资源

一粒金砂(中级)

7
 
谢谢楼主,学习了
 
 
 

回复

555

帖子

3

TA的资源

纯净的硅(初级)

8
 

感谢分享

 
 
 

回复

2

帖子

1

TA的资源

一粒金砂(中级)

9
 
谢谢啊!!!
 
 
 

回复

19

帖子

3

TA的资源

一粒金砂(中级)

10
 
ttxiaotaoge 发表于 2023-8-24 16:35 这个没有,我没买那些设备

Adafruit有music maker模块的,资料例程也都有,去官网搜下看下资料然后国内找类似的就行

 
 
 

回复

283

帖子

7

TA的资源

一粒金砂(高级)

11
 

大佬厉害,这个网页端做的太有技巧了,我要抄作业了,顺路提一嘴,貌似其中的key没有隐藏

 
 
 

回复

7188

帖子

11

TA的资源

版主

12
 

非常全面的教程,感谢大佬的分享,等板子到了,我们一一学习!

 
 
 

回复

6859

帖子

10

TA的资源

版主

13
 

楼主你好,在使用append(text_text)时,例如时间刷新,时间变化相当于直接在1上写2,然后不断叠加,只认识什么原因,看您这个是刷新的

个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复

10

帖子

1

TA的资源

一粒金砂(中级)

14
 
秦天qintian0303 发表于 2023-9-17 14:44 楼主你好,在使用append(text_text)时,例如时间刷新,时间变化相当于直接在1上写2,然后不断叠加,只认识 ...

这个函数只需要初始化一次,后续直接修改xx.text文本内容即可,后台会自己更新数据

 
 
 

回复

10

帖子

1

TA的资源

一粒金砂(中级)

15
 
秦天qintian0303 发表于 2023-9-17 14:44 楼主你好,在使用append(text_text)时,例如时间刷新,时间变化相当于直接在1上写2,然后不断叠加,只认识 ...

append本质上应该是在这个区间填充内容,反复调用的话就变成了叠加。举个不恰当的例子,先画了一个圆,然后这个程序后台会一直更新这个圆的显示。你这个时候想把圆改成正方形,但是你却直接调用了append,等于重新画了一个正方形,本身的圆还在,最终表现出来就是圆和正方形同时叠加显示

 
 
 

回复

6859

帖子

10

TA的资源

版主

16
 
ttxiaotaoge 发表于 2023-9-18 15:22 append本质上应该是在这个区间填充内容,反复调用的话就变成了叠加。举个不恰当的例子,先画了一个圆,然 ...

那如何更新比较好呢?这个就相当于局部刷新,对于全局的来说就是一直在上边覆盖,如果这里是个存储空间能够直接替换就好了

个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复

5

帖子

0

TA的资源

一粒金砂(初级)

17
 
ttxiaotaoge 发表于 2023-8-24 16:38 可以学习下这个ESP32迷你墨水屏MP3收音机多功能 - 嘉立创EDA开源硬件平台 (oshwhub.com)

谢谢,我去学习一下

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
有奖直播报名| TI 面向楼宇和工厂自动化行业的毫米波雷达解决方案
【内容简介】TI 60GHz IWRL6432和 IWRL1432毫米波雷达传感器如何帮助解决楼宇和工厂自动化应用中的感应难题
【直播时间】5月28日(周三)上午10:00
【直播礼品】小米双肩包、contigo水杯、胶囊伞、安克充电器

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 8

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表