小实验目录
树莓派科学小实验
LTE灯 —001 点亮第一盏LED灯
LTE灯 —002 点亮LED灯组
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
查询管脚定义
17,27,22,5 为这4个LED灯的管脚
提示:以下是本篇文章正文内容,下面案例可供参考
一、 写python代码:
第一种方法,通过无线循环来实现亮灭
# -*- coding: utf-8 -*-
"""
#类说明文件
author = "Derek Tian"
version = '0.0.1'
make day=2022-01-22
"""
__docformat__ = "restructuredtext en"
__all__ = []
__license__ = "MIT license"
from gpiozero import LED
from time import sleep
red=LED(17)
yellow=LED(27)
green=LED(22)
blue=LED(5)
while True:
red.on()
yellow.on()
green.on()
blue.on()
sleep(1)
red.on()
yellow.off()
green.off()
blue.off()
sleep(1)
red.off()
yellow.on()
green.off()
blue.off()
sleep(1)
red.off()
yellow.off()
green.on()
blue.off()
sleep(1)
red.off()
yellow.off()
green.off()
blue.on()
sleep(1)
red.off()
yellow.off()
green.off()
blue.off()
sleep(1)
第二种方法,调用系统中的RGB函数来显示
由于这个函数是给RGB的三原色LED准备的所以在这里没有黄色灯点亮
查询系统说明可以看到这个函数实际上是同时初始化了3个管脚
Extends :class:
Device
and represents a full color LED component (composed
of red, green, and blue LEDs).
the other legs (representing the red, green, and blue anodes) to any GPIO
pins. You should use three limiting resistors (one per anode).
The following code will make the LED yellow::
from gpiozero import RGBLED
led = RGBLED(2, 3, 4)
led.color = (1, 1, 0)
Thecolorzero
_ library is also supported::
from gpiozero import RGBLED
from colorzero import Color
led = RGBLED(2, 3, 4)
led.color = Color(‘yellow’)
# -*- coding: utf-8 -*-
"""
#这个类里将借助系统的RGB三原色灯来实现同时和顺序点亮三个颜色的LED灯
author = "Derek Tian"
version = '0.0.1'
make day=2022-01-22
"""
__docformat__ = "restructuredtext en"
__all__ = []
__license__ = "MIT license"
from gpiozero import RGBLED
from time import sleep
led = RGBLED(red=17, green=22, blue=5) # 设定红、绿、蓝 三色灯的GPIO管脚
led.color = (1, 0, 0) # 红色高电平点亮
sleep(1)
led.color = (0, 1, 0) # 绿色高电平点亮
sleep(1)
led.color = (0, 0, 1) # 蓝色高电平点亮
sleep(1)
led.color = (0, 0, 0) # 全部颜色低电平熄灭
sleep(1)
led.color = (1, 1, 1) # 全部高电平点亮
sleep(1)
led.color = (0, 0, 0) # off
#sleep(1)
for n in range(100): # 设定100的循环
led.red = n / 100
sleep(0.1)
led.blue = n / 100
sleep(0.1)
led.green= n / 100
sleep(0.1)
print(n)
led.color = (0, 0, 0, 0) # off
第三种方法,仿照系统的RGBLED函数来重新写一个函数,用来点亮测试版上的4个LED
# -*- coding: utf-8 -*-
"""
#在这个模块中添加了对yellow针脚的支持
author = "Derek Tian"
version = '0.0.1'
make day=2022-01-22
"""
__docformat__ = "restructuredtext en"
__all__ = []
__license__ = "MIT license"
from itertools import cycle, chain, repeat
from colorzero import Color
from gpiozero import LED, SourceMixin, Device, GPIOPinMissing, PWMLED, OutputDeviceBadValue
from gpiozero.threads import GPIOThread
class RGBYLED(SourceMixin, Device):
def __init__(
self, red=None, green=None, blue=None,yellow=None, active_high=True,
initial_value=(0, 0,0, 0), pwm=True, pin_factory=None):
self._leds = ()
self._blink_thread = None
if not all(p is not None for p in [red, green, blue,yellow]):
raise GPIOPinMissing('red, green, and blue,yellow pins must be provided')
LEDClass = PWMLED if pwm else LED
super(RGBYLED, self).__init__(pin_factory=pin_factory)
self._leds = tuple(
LEDClass(pin, active_high, pin_factory=pin_factory)
for pin in (red, green, blue,yellow)
)
self.value = initial_value
def close(self):
if getattr(self, '_leds', None):
self._stop_blink()
for led in self._leds:
led.close()
self._leds = ()
super(RGBYLED, self).close()
@property
def closed(self):
return len(self._leds) == 0
@property
def value(self):
return tuple(led.value for led in self._leds)
@value.setter
def value(self, value):
for component in value:
if not 0 <= component <= 1:
raise OutputDeviceBadValue(
'each RGBY color component must be between 0 and 1')
if isinstance(self._leds[0], LED):
if component not in (0, 1):
raise OutputDeviceBadValue(
'each RGBY color component must be 0 or 1 with non-PWM '
'RGBYLEDs')
self._stop_blink()
for led, v in zip(self._leds, value):
led.value = v
@property
def is_active(self):
"""
Returns :data:`True` if the LED is currently active (not black) and
:data:`False` otherwise.
"""
return self.value != (0, 0,0, 0)
is_lit = is_active
@property
def color(self):
"""
Represents the color of the LED as a :class:`~colorzero.Color` object.
"""
return Color(*self.value)
@color.setter
def color(self, value):
self.value = value
@property
def red(self):
"""
Represents the red element of the LED as a :class:`~colorzero.Red`
object.
"""
return self.color.red
@red.setter
def red(self, value):
self._stop_blink()
r, g, b,y = self.value
self.value = value, g, b,y
@property
def green(self):
"""
Represents the green element of the LED as a :class:`~colorzero.Green`
object.
"""
return self.color.green
@green.setter
def green(self, value):
self._stop_blink()
r, g, b,y = self.value
self.value = r, value, b,y
@property
def blue(self):
"""
Represents the blue element of the LED as a :class:`~colorzero.Blue`
object.
"""
return self.color.blue
@blue.setter
def blue(self, value):
self._stop_blink()
r, g, b,y = self.value
self.value = r, g, value,y
@property
def yellow(self):
"""
Represents the blue element of the LED as a :class:`~colorzero.Blue`
object.
"""
return self.color.yellow
@yellow.setter
def yellow(self, value):
self._stop_blink()
r, g, b, y = self.value
self.value = r, g, b, value
def on(self):
"""
Turn the LED on. This equivalent to setting the LED color to white
``(1, 1,1, 1)``.
"""
self.value = (1, 1, 1,1)
def off(self):
"""
Turn the LED off. This is equivalent to setting the LED color to black
``(0, 0,0, 0)``.
"""
self.value = (0, 0,0, 0)
def toggle(self):
"""
Toggle the state of the device. If the device is currently off
(:attr:`value` is ``(0, 0, 0)``), this changes it to "fully" on
(:attr:`value` is ``(1, 1, 1)``). If the device has a specific color,
this method inverts the color.
"""
r, g, b,y = self.value
self.value = (1 - r, 1 - g, 1 - b,1-y)
def blink(
self, on_time=1, off_time=1, fade_in_time=0, fade_out_time=0,
on_color=(1, 1,1, 1), off_color=(0, 0,0, 0), n=None, background=True):
if isinstance(self._leds[0], LED):
if fade_in_time:
raise ValueError('fade_in_time must be 0 with non-PWM RGBLEDs')
if fade_out_time:
raise ValueError('fade_out_time must be 0 with non-PWM RGBLEDs')
self._stop_blink()
self._blink_thread = GPIOThread(
self._blink_device,
(
on_time, off_time, fade_in_time, fade_out_time,
on_color, off_color, n
)
)
self._blink_thread.start()
if not background:
self._blink_thread.join()
self._blink_thread = None
def pulse(
self, fade_in_time=1, fade_out_time=1,
on_color=(1, 1, 1,1), off_color=(0, 0,0, 0), n=None, background=True):
on_time = off_time = 0
self.blink(
on_time, off_time, fade_in_time, fade_out_time,
on_color, off_color, n, background
)
def _stop_blink(self, led=None):
# If this is called with a single led, we stop all blinking anyway
if self._blink_thread:
self._blink_thread.stop()
self._blink_thread = None
def _blink_device(
self, on_time, off_time, fade_in_time, fade_out_time, on_color,
off_color, n, fps=25):
# Define a simple lambda to perform linear interpolation between
# off_color and on_color
lerp = lambda t, fade_in: tuple(
(1 - t) * off + t * on
if fade_in else
(1 - t) * on + t * off
for off, on in zip(off_color, on_color)
)
sequence = []
if fade_in_time > 0:
sequence += [
(lerp(i * (1 / fps) / fade_in_time, True), 1 / fps)
for i in range(int(fps * fade_in_time))
]
sequence.append((on_color, on_time))
if fade_out_time > 0:
sequence += [
(lerp(i * (1 / fps) / fade_out_time, False), 1 / fps)
for i in range(int(fps * fade_out_time))
]
sequence.append((off_color, off_time))
sequence = (
cycle(sequence) if n is None else
chain.from_iterable(repeat(sequence, n))
)
for l in self._leds:
l._controller = self
for value, delay in sequence:
for l, v in zip(self._leds, value):
l._write(v)
if self._blink_thread.stopping.wait(delay):
break
重新编写主显示程序
# -*- coding: utf-8 -*-
"""
#在这个类中将调用改写的RGB程序用来显示4个LED灯
author = "Derek Tian"
version = '0.0.1'
make day=2022-01-22
"""
__docformat__ = "restructuredtext en"
__all__ = []
__license__ = "MIT license"
from gpiozero import LED
#from gpiozero import RGBLED
from LEDGROUP import RGBYLED as LEDGROUP
from time import sleep
led = LEDGROUP(red=17, green=22, blue=5, yellow=27)
led.red = 1 # full red
sleep(1)
led.yellow= 0.5 # half yellow
sleep(1)
led.color = (1, 0, 0,0) # 红色高电平点亮
sleep(1)
led.color = (0, 0, 1,0) # 黄色高电平点亮
sleep(1)
led.color = (0, 1, 0,0) # 绿色高电平点亮
sleep(1)
led.color = (0, 0, 0,1) # 蓝色高电平点亮
sleep(1)
led.color = (1, 1, 1,1) # 全部高电平点亮
sleep(1)
led.color = (0, 0, 0, 0) # 红色高低平
sleep(1)
print("call blink")
led.blink(3,1) # 设定显示3秒,关闭1秒
print("Main Thread sleep 30 sec ")
sleep(30)
led.toggle() # 常亮
sleep(20)
led.color = (0, 0, 0, 0) # off
第四种方法, 调用系统函数来实现多个灯的统一控制
# -*- coding: utf-8 -*-
"""
#在这个类中将调用改写的RGB程序用来显示4个LED灯
author = "Derek Tian"
version = '0.0.1'
make day=2022-01-22
"""
__docformat__ = "restructuredtext en"
__all__ = []
__license__ = "MIT license"
from gpiozero import LEDBoard
from time import sleep
leds=LEDBoard(17,27,22,5)
for b in range(100):
for a in range(len(leds)):
leds[a].on()
sleep(0.5)
leds[a].off()