1 Star 1 Fork 0

Walkline/MicroPython 迷你电子台历

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hardware_test.py 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
"""
Copyright © 2023 Walkline Wang (https://walkline.wang)
Gitee: https://gitee.com/walkline/micropython-mini-calendar
"""
from machine import Pin, ADC
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.__np_days = NeoPixel(Pin(Config.PINS.DIN_DAYS), Config.WS2812.DAYS_COUNT)
self.__np_week_month = NeoPixel(Pin(Config.PINS.DIN_WEEK_MONTH), Config.WS2812.WEEK_COUNT + Config.WS2812.MONTH_COUNT)
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
)
self.__adc = ADC(Pin(Config.PINS.BRIGHTNESS_ADC))
self.__adc.atten(ADC.ATTN_0DB)
self.__colors = self.__color_generator()
self.__task = lambda: self.__buttons_press_task()
self.__tasks = Dispatcher()
self.__tasks.add_work(self.__buttons.timer_callback, 20)
self.clean()
def __color_generator(self):
while True:
yield randint(0, 50)
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.__np_days.fill((next(self.__colors), next(self.__colors), next(self.__colors)))
self.__np_days.write()
self.__np_week_month.fill((next(self.__colors), next(self.__colors), next(self.__colors)))
self.__np_week_month.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.__np_days.n):
self.__np_days[_] = (next(self.__colors), next(self.__colors), next(self.__colors))
for _ in range(self.__np_week_month.n):
self.__np_week_month[_] = (next(self.__colors), next(self.__colors), next(self.__colors))
self.__np_days.write()
self.__np_week_month.write()
def clean(self):
self.__np_days.fill((0, 0, 0))
self.__np_days.write()
self.__np_week_month.fill((0, 0, 0))
self.__np_week_month.write()
if __name__ == '__main__':
print('Test running...\r\n- Click or press keys on board\r\n- Press Ctrl+D to terminate')
test = HardwareTest()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/walkline/micropython-mini-calendar.git
git@gitee.com:walkline/micropython-mini-calendar.git
walkline
micropython-mini-calendar
MicroPython 迷你电子台历
master

搜索帮助