Ai
2 Star 0 Fork 0

mirrors_tad-lispy/circuit-python-playground

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.py 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
import board
import neopixel
from digitalio import DigitalInOut, Direction, Pull
import time
brightness = 0.0
target = 0.5
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=brightness)
pixels.fill((255, 255, 255))
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
button_left = DigitalInOut(board.BUTTON_A)
button_left.direction = Direction.INPUT
button_left.pull = Pull.DOWN
button_right = DigitalInOut(board.BUTTON_B)
button_right.direction = Direction.INPUT
button_right.pull = Pull.DOWN
press = DigitalInOut(board.A1)
press.direction = Direction.INPUT
press.pull = Pull.UP
rotary_left = DigitalInOut(board.A2)
rotary_left.direction = Direction.INPUT
rotary_left.pull = Pull.DOWN
rotary_right = DigitalInOut(board.A3)
rotary_right.direction = Direction.INPUT
rotary_right.pull = Pull.DOWN
previous = (False, False)
while True:
# An amount and direction of brightness change will be stored here
vector = 0
# Light up the red LED when any button is pressedself.
# It's an easy way to see if the program is running on a board.
led.value = button_left.value or button_right.value or not press.value
# Read the rotary encoder state and detect direction of rotation
# See https://en.wikipedia.org/wiki/Rotary_encoder#Incremental_rotary_encoder
rotary = (not rotary_left.value, not rotary_right.value)
if previous != rotary and previous == (False, False):
if rotary[0]:
# Turning counter-clockwise
vector = -10
else:
# Turning clockwise
vector = 10
previous = rotary
# The brightness is always "pulled" toward the target value
diff = target - brightness
vector = vector + (diff / 5)
# Read on-board buttons and apply the input to the vector
if button_left.value:
# Decrease brightness
vector = vector - 1
if button_right.value:
# Increase brightness
vector = vector + 1
# Change brightness according to the vector
step = vector / 1000
brightness = max(min(brightness + step, 1), 0)
pixels.brightness = brightness
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_tad-lispy/circuit-python-playground.git
git@gitee.com:mirrors_tad-lispy/circuit-python-playground.git
mirrors_tad-lispy
circuit-python-playground
circuit-python-playground
master

搜索帮助