代码拉取完成,页面将自动刷新
"""
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。