525|1

1

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【得捷电子Follow me第2期】+任务合集(选择任务:WS2812B效果控制) [复制链接]

 

大家好,很高兴参加这次得捷Follow me第2期活动,经过这次活动,了解了circuit python的基本操作,也用开发板Adafruit ESP32-S3 TFT Feather实现了一些功能,在此也向大家分享一下学习过程。

此次任务简介:

1.汉字显示

2.wifi热点链接和wifi热点广播

3.板载Neopixel LED的控制

4.可选任务:WS2812B效果控制(12颗灯珠以上)

一、视频内容

二、总结报告

本程序嫁接在多线程任务上,由于难度较高,解说不是很利索,代码结构可能有部分相似,但是具体执行还是非常有个性化思路的

1、程序框架

async def rainbow_cycle(controls):

async def monitor_button(button, controls):#此函数中经过改造,其中包含对于汉字和屏幕其他字符的显示,还有控制流水灯和板载RGBLED的具体程序


async def main():
    animation_controls = AnimationControls()
    button_task = asyncio.create_task(monitor_button(button_pin, animation_controls))
    animation_task = asyncio.create_task(rainbow_cycle(animation_controls))

    # This will run forever, because no tasks ever finish.
    await asyncio.gather(button_task, animation_task)

asyncio.run(main())


2、网络功能使用

wifi.radio.start_ap('FOLLOWMETWO', '00000000') #网络wifi ap广播功能(code.py文件夹内)


#下面两行 在settings.toml文件夹内,负责wiff连接,仅限2.4G
CIRCUITPY_WIFI_SSID = "your wifi ssid"
CIRCUITPY_WIFI_PASSWORD = "your wifi password"

首先找到CircuitPython Internet Test的介绍页面,学习如何使用网络,地址为:

https://learn.adafruit.com/adafruit-esp32-s3-tft-feather/circuitpython-internet-test

2、汉字显示功能

#显示模块初始化和字体文件加载
display = board.DISPLAY#初始化屏幕
group = displayio.Group() #定义显示组别
font = bitmap_font.load_font("/font/sytq_16.pcf")#字体文件导入

#汉字内容显示
text1="风雪晴"
date = label.Label(font, text=text1, color=0xFFFFFF, x=10, y=40,scale=3)
group.append(date)


#汉字内容显示(放在多线程async def rainbow_cycle函数中加载)
async def rainbow_cycle(controls):
  display.show(group)

此处的显示程序,前面的初始化和内容都在外主程序前部,具体的屏幕显示放在了

多线程async def rainbow_cycle函数中,因为这个函数会直接执行,所以放在这里,顺便就初始化屏幕了。

3.板载RGBLED的驱动

async def monitor_button(button, controls):
    """Monitor button that reverses rainbow direction and changes blink speed.
    Assume button is active low.
    """     
    i=1
    with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
        while True:
            key_event = key.events.get()
            if key_event:
                print('key_event')
                if key_event.pressed:
                    controls.reverse = not controls.reverse
                    if(i%3==0):
                        pixel.fill((255, 0, 0))
                        time.sleep(0.5)
                    
                    if(i%3==1):
                        pixel.fill((0, 255, 0))
                        time.sleep(0.5)
                    
                    if(i%3==2):
                        pixel.fill((0, 0, 255))
                        time.sleep(0.5)

这里的板载RGBLED初始化,会在按键按下时,系统做出反馈,而且会根据按下的次序,依次显示三个颜色。

4.WS2812B效果控制(24粒,只用了22粒)

这个功能可以参考开发板的文档,网址为:

https://learn.adafruit.com/adafruit-esp32-s3-tft-feather/multitasking-with-asyncio

class AnimationControls:
    """The controls to allow you to vary the rainbow and blink animations."""
    def __init__(self):
        self.reverse = False
        self.wait = 0.0
        self.delay = 0.5


async def rainbow_cycle(controls):
    """Rainbow cycle animation on ring one."""
    print('rainbow_cycle')
    display.show(group)
    while True:
        if(controls.reverse == False):            
            for j in range(255, -1, -1) if controls.reverse else range(0, 256, 1):
                for i in range(num_pixels):
                    rc_index = (i * 256 // num_pixels) + j
                    ring_pin[i] = colorwheel(rc_index & 255)
                ring_pin.show()
                await asyncio.sleep(controls.wait)
        await asyncio.sleep(controls.wait)

async def monitor_button(button, controls):
    """Monitor button that reverses rainbow direction and changes blink speed.
    Assume button is active low.
    """     
    i=1
    with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
        while True:
            key_event = key.events.get()
            if key_event:
                print('key_event')
                if key_event.pressed:
                    controls.reverse = not controls.reverse

                    if (i%2==1):
                        try:
                            group.remove(date)
                        except:
                            pass
                        try:
                            group.remove(date2)
                        except:
                            pass
                        date1 = label.Label(font, text="1", color=0xFFFFFF, x=10, y=40,scale=3)
                        group.append(date1)
                        display.show(group)
                      #
                    if (i%2==0):
                        try:
                            group.remove(date)
                        except:
                            pass
                        try:
                            group.remove(date1)
                        except:
                            pass
                        date2 = label.Label(font, text="0", color=0xFFFFFF, x=40, y=40,scale=3)
                       # group.append(date)
                        group.append(date2) 
                        display.show(group)
                    i=i+1
                    controls.delay = 0.6              
            await asyncio.sleep(0)

 

 


这里面主要是三个函数,流水灯彩虹效果函数,和动画控制的类,第二个就是需要重点讲解的按键检测。

按键检测,里面包含屏幕的具体显示控制和异常捕捉,group.append()显示会造成显示效果重叠,所以需要导入  group.remove()用于删除特定的层,但是由于如果该层不存在,则会报错,所以分别使用两个try except来捕捉两个group.remove()使程序正常运行。按键每次按下,都会使得屏幕显示不同的流水灯状态(0是运行,1是停止)按键检测也会吧按键状态导入到 class AnimationControls()类中用于控制动画的启停。

三、项目总结

此次学习收获颇丰,稍微了解了一下circuitpython的多线程知识,能力有限,理解也十分初级。但是确实让程序要我我的设想如愿完成(但愿不是依靠bug运行)

四、源码附件

code.zip (41.96 KB, 下载次数: 2)

 

最新回复

啊   详情 回复 发表于 2023-10-25 13:10
点赞(1) 关注
 
 

回复
举报

3

帖子

0

TA的资源

一粒金砂(中级)

沙发
 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

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

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

北京市海淀区中关村大街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
快速回复 返回顶部 返回列表