【得捷电子Follow me第2期】任务4:日历&时钟
[复制链接]
本帖最后由 walker2048 于 2023-10-5 21:38 编辑
上上一期视频,我们实现了基本的天气时钟界面,界面的代码还是可以复用的。
由于导入了time库,原有的time标签需要更名成timeL。
- import board
-
- import displayio
-
-
- import adafruit_imageload
-
- from adafruit_display_text import label
-
- from adafruit_bitmap_font import bitmap_font,用于显示字体
-
-
- display = board.DISPLAY
-
-
- group = displayio.Group()
-
-
- image, palette = adafruit_imageload.load("/pic/tqbg.png")
-
- palette.make_transparent(0)
-
-
- grid = displayio.TileGrid(image, pixel_shader=palette)
-
-
- group.append(grid)
-
-
- display.show(group)
-
-
- font = bitmap_font.load_font("/font/sytq_16.pcf")
- nun_font = bitmap_font.load_font("/font/DingTalk_ncn_60.pcf")
- color = 0x000000
-
-
- date = label.Label(font, text="10月3日", color=color)
- date.x = 50
- date.y = 30
- group.append(date)
-
-
- week = label.Label(font, text="周二", color=color)
- week.x = 150
- week.y = 30
- group.append(week)
-
-
- time = label.Label(nun_font, text="18:20", color=color)
- time.x = 20
- time.y = 80
- group.append(time)
-
-
- font = bitmap_font.load_font("/font/sytq_16.pcf")
- nun_font = bitmap_font.load_font("/font/DingTalk_ncn_60.pcf")
- color = 0x000000
-
-
- date = label.Label(font, text="10月3日", color=color)
- date.x = 50
- date.y = 30
- group.append(date)
-
-
- week = label.Label(font, text="周二", color=color)
- week.x = 150
- week.y = 30
- group.append(week)
-
-
- time = label.Label(nun_font, text="18:20", color=color)
- time.x = 20
- time.y = 80
- group.append(time)
-
-
- temp = label.Label(font, text="30°", color=color)
- temp.x = 70
- temp.y = 140
- group.append(temp)
-
-
- tempzh = label.Label(font, text="晴", color=color)
- tempzh.x = 110
- tempzh.y = 140
- group.append(tempzh)
-
-
- display.show(group)
-
- while True:
- pass
在实现了界面的基础上,只需要添加ntp网络校时、天气信息获取和更新的功能,就可以把这个天气时钟做好了。
第一步、导入需要用到的库
-
- import os
-
- import rtc
-
- import wifi
- import time
-
-
- import ssl
- import socketpool
- import adafruit_ntp
- import adafruit_requests
第二步、连接wifi
使用os.getenv函数,从setting.toml文件里获取wifi ssid和密码,然后连接到wifi即可
-
- ssid = os.getenv("CIRCUITPY_WIFI_SSID")
- password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
-
-
- print("Connecting to", ssid)
- wifi.radio.connect(ssid, password)
- print("Connected to", ssid)
第三步、初始化ntp服务
使用adafruit_ntp.NTP函数初始化ntp服务,第一个函数是确定网络连接端口,
第二个函数设置时区,中国是+8时区,第三个函数用来指定ntp服务器地址。
然后使用ntp时间更新系统时间。
-
-
- pool = socketpool.SocketPool(wifi.radio)
- ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com")
-
-
- rtc.RTC().datetime = ntp.datetime
第四步、编写星期数据转换函数
这个函数没啥复杂的,根据时间结构体获取到的tm_wday属性,判断是星期几
- def get_wday(wday):
- if (wday == 0):
- return "周一"
- elif (wday == 1):
- return "周二"
- elif (wday == 2):
- return "周三"
- elif (wday == 3):
- return "周四"
- elif (wday == 4):
- return "周五"
- elif (wday == 5):
- return "周六"
- elif (wday == 6):
- return "周日"
第五步、编写天气获取函数
这个函数使用的是高德API,使用该API需要先去注册相关账户,申请key。
获取到相关json数据后,就从json数据里解析温度和天气信息,并返回
-
- pool = socketpool.SocketPool(wifi.radio)
- requests = adafruit_requests.Session(pool, ssl.create_default_context())
-
- def get_weather():
-
- city = "450900"
-
- key = "1xxx"
-
- getweather_url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + city + "&key=" + key
-
- response = requests.get(getweather_url)
- json_resp = response.json()
-
- response.close()
-
- for da in json_resp["lives"]:
-
- return da["temperature"], da["weather"]
最后、编写主循环代码
先创建一个status变量,用来在设备启动时获取天气信息。
然后在主循环里不断更新timeL的时间属性,每秒更换一下颜色,用来指示程序还在运行。
也提供一些动态效果,免得大家觉得过于单调。
然后每小时更新一下天气信息。详细的大家可以看注释,代码没啥难度的。等过段时间学习了gifio库,再考虑增加一些动画效果,例如任务眨眼睛什么的。
-
- status = "boot"
-
-
- while True:
-
- t = time.localtime()
-
- if (status == "boot" or t.tm_min == 0):
-
- date.text = "%d月%d日" % (t.tm_mon, t.tm_mday)
- week.text = get_wday(t.tm_wday)
-
- str_t, str_tz = get_weather()
-
- temp.text = "%s°" % (str_t)
-
- tempzh.text = str_tz
- status = "updated"
-
- if (t.tm_sec % 2 == 0):
- timeL.text = "%02d:%02d" % ( t.tm_hour, t.tm_min)
- timeL.color = 0x000000
- else:
- timeL.text = "%02d:%02d" % ( t.tm_hour, t.tm_min)
- timeL.color = 0xD9D7C9
-
- display.show(group)
-
- time.sleep(1)
|