Ai
1 Star 0 Fork 11

Admin/MicroPython WS2812 Led Clock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hardware_test.py 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
Walkline 提交于 2023-07-19 14:04 +08:00 . hardware_test.py: 删除 v1 相关代码
"""
Copyright © 2023 Walkline Wang (https://walkline.wang)
Gitee: https://gitee.com/walkline/micropython-ws2812-led-clock
"""
from machine import Pin, ADC, SoftI2C
from random import randint
from neopixel import NeoPixel
from drivers.button import Button
from utils.utilities import Utilities
try:
from dispatcher import Dispatcher
except ImportError:
from utils.dispatcher import Dispatcher
CONFIG = Utilities.import_config()
class HardwareTest(object):
def __init__(self):
self.__neopixel = NeoPixel(Pin(CONFIG.PINS.DIN_MATRIX), CONFIG.WS2812_MATRIX.WIDTH * CONFIG.WS2812_MATRIX.HEIGHT)
self.__tasks = Dispatcher()
self.__buttons = Button(
CONFIG.KEYS.KEY_LIST,
click_cb=self.__buttons_click_cb,
press_cb=self.__buttons_press_cb,
timeout=1000,
behavior=Button.BEHAVIOR_HOLD,
timer_id=None
)
adc_pin = CONFIG.PINS.BRIGHTNESS_ADC[0]
self.__adc = ADC(Pin(adc_pin))
self.__adc.atten(ADC.ATTN_11DB)
self.__iic = SoftI2C(scl=Pin(CONFIG.PINS.IIC_SCL), sda=Pin(CONFIG.PINS.IIC_SDA))
print(f'scanned iic devices: {self.__iic.scan()}')
self.__colors = self.__color_generator()
self.__task = lambda: self.__buttons_press_task()
self.__tasks.add_work(self.__buttons.timer_callback, 20)
self.__tasks.add_work(self.__task, 100)
self.clean()
def __color_generator(self):
while True:
yield randint(0, 30)
def __buttons_click_cb(self, pin):
self.__tasks.del_work(self.__task)
print(f'Key {CONFIG.KEYS.KEY_MAP[pin]} clicked, adc value: {self.__adc.read()}')
self.__neopixel.fill((next(self.__colors), next(self.__colors), next(self.__colors)))
self.__neopixel.write()
def __buttons_press_cb(self, time, pin):
print(f'Key {CONFIG.KEYS.KEY_MAP[pin]} pressed {time} ms')
self.__tasks.add_work(self.__task, 100)
def __buttons_press_task(self):
for _ in range(self.__neopixel.n):
self.__neopixel[_] = (next(self.__colors), next(self.__colors), next(self.__colors))
self.__neopixel.write()
def clean(self):
self.__neopixel.fill((0, 0, 0))
self.__neopixel.write()
if __name__ == '__main__':
print('Test running...\r\n- Click or press keys on board\r\n- Press Ctrl+D to terminate')
test = HardwareTest()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/fci_0/micropython-ws2812-led-clock.git
git@gitee.com:fci_0/micropython-ws2812-led-clock.git
fci_0
micropython-ws2812-led-clock
MicroPython WS2812 Led Clock
master

搜索帮助