251125
This commit is contained in:
parent
33647fdfae
commit
30457645c3
|
|
@ -1,14 +1,9 @@
|
|||
import utime
|
||||
from machine import Pin
|
||||
from utils import MTS102
|
||||
from utils import WS2812
|
||||
|
||||
# 配置引脚
|
||||
led = Pin(0, Pin.OUT)
|
||||
toggle_switch = MTS102(1)
|
||||
rgb = WS2812()
|
||||
|
||||
while True:
|
||||
if toggle_switch.switched:
|
||||
led.on()
|
||||
else:
|
||||
led.off()
|
||||
utime.sleep_ms(200)
|
||||
rgb.iridesce()
|
||||
|
||||
utime.sleep_ms(20)
|
||||
|
|
@ -1,3 +1,9 @@
|
|||
import utime
|
||||
from machine import Pin, PWM
|
||||
from neopixel import NeoPixel
|
||||
|
||||
|
||||
|
||||
class MTS102:
|
||||
"""MTS102(三脚二档钮子开关)"""
|
||||
|
||||
|
|
@ -6,14 +12,14 @@ class MTS102:
|
|||
:param pin: 引脚编号
|
||||
:param pull_down: 引脚下拉电阻(断开为低电平,接通为高电平),默认为False。若使用下拉电阻,因钮子接通后断开可能存留电荷/漏电故需在引脚和GND并联1K~10K欧电阻
|
||||
"""
|
||||
import utime
|
||||
from machine import Pin
|
||||
|
||||
if not isinstance(pull_down, bool):
|
||||
raise TypeError("pull_down数据类型应为布尔")
|
||||
|
||||
self.pull_down = pull_down
|
||||
|
||||
import utime
|
||||
from machine import Pin
|
||||
|
||||
# 尝试初始化引脚
|
||||
try:
|
||||
self.pin = Pin(
|
||||
|
|
@ -50,6 +56,29 @@ class MTS102:
|
|||
return self._debounce() == 1
|
||||
|
||||
|
||||
"""
|
||||
使用示例
|
||||
|
||||
引脚0→LED→220R→GND
|
||||
引脚1→MST102→GND
|
||||
|
||||
import utime
|
||||
from machine import Pin
|
||||
from utils import MTS102
|
||||
|
||||
led = Pin(0, Pin.OUT)
|
||||
toggle = MTS102(pin=1)
|
||||
|
||||
while True:
|
||||
if toggle.switched:
|
||||
led.on()
|
||||
else:
|
||||
led.off()
|
||||
|
||||
utime.sleep_ms(200)
|
||||
"""
|
||||
|
||||
|
||||
class WS2812:
|
||||
"""WS2812(可编程 RGB LED)"""
|
||||
|
||||
|
|
@ -58,6 +87,10 @@ class WS2812:
|
|||
:param pin: 引脚编号,RP2350ZERO板载WS2812使用引脚16
|
||||
:param led_beads: LED灯珠数
|
||||
"""
|
||||
import utime
|
||||
from machine import Pin
|
||||
from neopixel import NeoPixel
|
||||
|
||||
if not isinstance(led_beads, int):
|
||||
raise TypeError("led_beads数据类型应为整数")
|
||||
|
||||
|
|
@ -66,10 +99,6 @@ class WS2812:
|
|||
|
||||
self.led_beads = led_beads
|
||||
|
||||
import utime
|
||||
from machine import Pin
|
||||
from neopixel import NeoPixel
|
||||
|
||||
# 尝试初始化LED
|
||||
try:
|
||||
self.led = NeoPixel(Pin(pin, Pin.OUT), self.led_beads)
|
||||
|
|
@ -161,6 +190,23 @@ class WS2812:
|
|||
utime.sleep_ms(20)
|
||||
|
||||
|
||||
"""
|
||||
使用示例
|
||||
|
||||
RP2350ZERP 使用板载WS2812,其占用引脚16
|
||||
|
||||
import utime
|
||||
from utils import WS2812
|
||||
|
||||
rgb = WS2812()
|
||||
|
||||
while True:
|
||||
rgb.iridesce()
|
||||
|
||||
utime.sleep_ms(20)
|
||||
"""
|
||||
|
||||
|
||||
class Servo:
|
||||
"""舵机基类"""
|
||||
|
||||
|
|
@ -204,7 +250,6 @@ class Servo:
|
|||
:param dead_zone: 死区,单位为微秒
|
||||
:param duration_per_degree: 转动单位角度对应的时长,单位为秒/六十度
|
||||
"""
|
||||
|
||||
import utime
|
||||
from machine import Pin, PWM
|
||||
|
||||
|
|
@ -348,7 +393,7 @@ class Servo:
|
|||
|
||||
|
||||
class SG90(Servo):
|
||||
"""SG90(9G舵机)"""
|
||||
"""SG90(适用于SG90和MG90S等舵机)"""
|
||||
def __init__(self, pin):
|
||||
super().__init__(
|
||||
pin=pin,
|
||||
|
|
|
|||
Loading…
Reference in New Issue