视频【得捷电子Follow me第2期】任务提交-【得捷电子Follow me第2期】任务提交 -EEWORLD大学堂
代码下载地址得捷电子Follow me第2期 任务源码2-嵌入式开发相关资料下载-EEWORLD下载中心
【任务一】
要求在屏幕上显示中文。
完整的方案:【得捷电子Follow me第2期】+ 【任务一】控制屏幕显示中文
- import board
- # 导入displayio库(内置的)
- import displayio
-
- # 导入外部库adafruit_imageload,如果没有就在教程附件下载
- import adafruit_imageload
- # 导入外部库adafruit_display_text里的lable,用于显示标签
- from adafruit_display_text import label
- # 导入外部库adafruit_bitmap_font里的lable
- 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
-
- # 初始化日期标签并设置x,y轴绘图坐标,然后将标签添加到图像组
- date = label.Label(font, text="10月3日", color=color)
- date.x = 50
- date.y = 30
- group.append(date)
-
- # 初始化星期标签并设置x,y轴绘图坐标,然后将标签添加到图像组
- week = label.Label(font, text="周二", color=color)
- week.x = 150
- week.y = 30
- group.append(week)
-
- # 初始化时间标签并设置x,y轴绘图坐标,然后将标签添加到图像组
- time = label.Label(nun_font, text="18:20", color=color)
- time.x = 20
- time.y = 80
- group.append(time)
-
- # 初始化温度标签并设置x,y轴绘图坐标,然后将标签添加到图像组
- temp = label.Label(font, text="30°", color=color)
- temp.x = 70
- temp.y = 140
- group.append(temp)
-
- # 初始化天气标签并设置x,y轴绘图坐标,然后将标签添加到图像组
- tempzh = label.Label(font, text="晴", color=color)
- tempzh.x = 110
- tempzh.y = 140
- group.append(tempzh)
-
- # 显示修改后的图像组
- display.show(group)
-
- while True:
- pass
-
-
displayio 是 CircuitPython 中用于管理和控制显示设备(通常是液晶屏或 OLED 屏幕)的库。它提供了一种方式来创建和管理图形元素,以便在显示设备上绘制各种图形和文本。 简单来说,就是可以用display系列库来制作一些简单的界面。而最简单的莫过于使用背景图片加文字的形式。第一步、创建显示对象,第二步、创建图像组,第三步、创建背景图片,第四步、创建文本标签以显示文字,背景图片是用pixso制作的,240*135尺寸
【任务二】网络功能使用
连接wifi的话,直接修改settings.toml文件
- CIRCUITPY_WIFI_SSID = "Xiaomi_9FC2"
-
-
,将网络名称和密码输入实现联网创建热点使用如下代码
- 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('wifi01', 'lesfd231')
- text_area.text="running"
- text_group.append(text_area)
- display.show(text_group)
- while True:
- time.sleep(2)
任务3:控制WS2812B(必做任务)
adafruit_led_animation库是一个用于控制LED动态效果的库,可以使用它进行点灯
导入blink,time,board库等
接下来初始化引脚和像素灯实例
使用board.NEOPIXEL来给引脚赋值
接下来配置按键
最后就是主循环代码
-
- import board
-
- import time
-
- from digitalio import DigitalInOut, Direction, Pull
-
- import neopixel
-
- from adafruit_led_animation.animation.blink import Blink
-
- from adafruit_led_animation.color import JADE, BLACK, ORANGE, GOLD, OLD_LACE
-
- pixel_pin = board.NEOPIXEL
-
- num_pixels = 1
-
- pixels = neopixel.NeoPixel(
- pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=neopixel.GRB
- )
-
- blink = Blink(pixels, speed=0.5, color=BLACK)
-
- btn = DigitalInOut(board.BOOT0)
-
- btn.direction = Direction.INPUT
-
- btn.pull = Pull.UP
-
- led_color = 0
-
-
- while True:
-
- if not btn.value:
-
- if led_color % 5 == 0:
- blink = Blink(pixels, speed=0.5, color=JADE)
- print("LED is JADE")
- elif led_color % 5 == 1:
- blink = Blink(pixels, speed=0.5, color=ORANGE)
- print("LED is ORANGE")
- elif led_color % 5 == 2:
- blink = Blink(pixels, speed=0.5, color=GOLD)
- print("LED is GOLD")
- elif led_color % 5 == 3:
- blink = Blink(pixels, speed=0.5, color=OLD_LACE)
- print("LED is OLD_LACE")
- elif led_color % 5 == 4:
- blink = Blink(pixels, speed=0.5, color=BLACK)
- print("LED is BLACK")
-
- led_color = led_color + 1
- else:
-
- pass
-
- time.sleep(0.2)
-
- blink.animate()
-
任务4:日历&时钟——完成一个可通过互联网更新的万年历时钟,并显示当地的天气信息
在实现了界面的基础上,只需要添加ntp网络校时、天气信息获取和更新的功能,就可以把这个天气时钟做好了
第一步、导入需要用到的库
第二步、连接wifi
第三步、初始化ntp服务
第四步、编写星期数据转换函数
第五步、编写天气获取函数 (还要申请天气api的key)
最后、编写主循环代码
- 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(1)
-
-
- 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)
-
-
- timeL = label.Label(nun_font, text="18:20", color=color)
- timeL.x = 20
- timeL.y = 80
- group.append(timeL)
-
-
- temp = label.Label(font, text="30°", color=color)
- temp.x = 200
- temp.y = 20
- group.append(temp)
-
-
- tempzh = label.Label(font, text="晴", color=color)
- tempzh.x = 10
- tempzh.y = 20
- group.append(tempzh)
-
-
- import os
-
- import rtc
-
- import wifi
- import time
-
-
- import ssl
- import socketpool
- import adafruit_ntp
- import adafruit_requests
-
- 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)
-
-
- pool = socketpool.SocketPool(wifi.radio)
- ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com")
-
-
- rtc.RTC().datetime = ntp.datetime
- 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 "周日"
-
- pool = socketpool.SocketPool(wifi.radio)
- requests = adafruit_requests.Session(pool, ssl.create_default_context())
-
- def get_weather():
-
- city = "321100"
-
- key = "8027d9b28a93e1fdc1bd9f8076041e0e"
-
- 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 = "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)
-
我一直对电子和计算机科学抱有浓厚的兴趣,我相信电路板是实现我创新想法的重要工具。
在网友的帮助下,我参加了Follow me 第2期任务的活动。我们通过讨论和合作,尝试使用电路板来实现各种不同的功能。在这个过程中,我遇到了许多挑战,但我都设法一一克服了。我深刻体会到团队合作的重要性,也学到了许多实践经验和理论知识。
电路板对我来说并不陌生,我在高中时就对电子技术有一定的了解。通过这次活动,我更深入地理解了电路板的工作原理和应用场景。我发现了电路板能够为我们提供一种高效、可靠且灵活的方式来实现我们的创意。对于未来,我将继续深入研究电路板,希望能够将其运用到我所学专业中的各种项目中。
在此,我要感谢那些在活动中帮助过我的网友们。他们的热心帮助和指导使我有机会在这次活动中收获颇丰。我期待未来能有机会参加更多类似的活动,与更多志同道合的朋友们一起学习、一起进步。
|