【Follow me第二季第1期】基础任务一(必做):控制板载炫彩LED,跑马灯点亮和颜色...
[复制链接]
板载的炫彩LED是WS2812的rgb灯珠在其中我们可以通过调用neopixel库
来实现这个目标可以参考这个官方网站Adafruit CircuitPython NeoPixel — Adafruit CircuitPython NeoPixel Library 1.0 documentation
我们首先需要对灯珠进行初始化
初始化函数定义如下
classneopixel.NeoPixel(pin: Pin, n: int, *, bpp: int = 3, brightness: float = 1.0, auto_write: bool = True, pixel_order: str = None)[source]¶
A sequence of neopixels.
Parameters:
pin (Pin) – The pin to output neopixel data on.
n (int) – The number of neopixels in the chain
bpp (int) – Bytes per pixel. 3 for RGB and 4 for RGBW pixels.
brightness (float) – Brightness of the pixels between 0.0 and 1.0 where 1.0 is full brightness
auto_write (bool) – True if the neopixels should immediately change when set. If False, show must be called explicitly.
pixel_order (str) – Set the pixel color channel order. GRBW is set by default.
但经过查阅资料了解到python已经封装好了无需自己写驱动直接调用即可这里附上详细代码
- import time
- from adafruit_circuitplayground import cp
-
-
- color_mapping = {
- "black": (0, 0, 0),
- "white": (255, 255, 255),
- "green": (0, 255, 0),
- "red": (255, 0, 0),
- "blue": (0, 0, 255),
- "cyan": (0, 255, 255),
- "magenta": (228, 0, 127),
- "yellow": (255, 150, 0),
- }
- a = 0
- cp.detect_taps = 1
- cp.pixels.brightness = 0.05
-
- pixel_count = 10
-
- def scale_range(value):
-
- return round(value / 320 * 9)
-
-
- def color_wheel(position):
-
- if (position < 0) or (position > 255):
- return color_mapping["black"]
-
-
- if position < 85:
- return (int(255 - position * 3), int(position * 3), 0)
- elif position < 170:
- position -= 85
- return (0, int(255 - (position * 3)), int(position * 3))
- else:
- position -= 170
- return (int(position * 3), 0, int(255 - position * 3))
-
- current_pixel = 0
-
-
- while True:
-
-
- if cp.tapped:
- cp.red_led = 1
- print("Tapped!")
- a=a+0.01
-
-
-
- if cp.switch:
- current_pixel += 1
- if current_pixel > pixel_count - 1:
- current_pixel = 0
- else:
- current_pixel -= 1
- if current_pixel < 0:
- current_pixel = pixel_count - 1
-
- if cp.button_b:
- print("lingh+!")
- cp.pixels.brightness = cp.pixels.brightness +0.01
- if cp.button_a:
- cp.pixels.brightness = cp.pixels.brightness -0.01
- print("lingh-!")
-
-
- time.sleep(a)
- cp.red_led = 0
- print(cp.light)
- for index in range(pixel_count):
-
- rainbow_color = color_wheel(255 // pixel_count * ((current_pixel + index) % pixel_count))
-
-
- cp.pixels[index] = tuple(
- int(c * ((pixel_count - (current_pixel + index) % pixel_count)) / pixel_count) for c in rainbow_color
- )
-
-
如视频中展示按下a按键增加亮度 按下b按键减小亮度中间拨码开关可以控制RGB轮换方向,敲击板子使能加速度传感器会让旋转速度变慢
|