使用 python 的 LED 追逐
LED chase using python
我正在使用一个简单的 while 循环和一个数组来追踪条带上的 LED。
while True:
for i in range(nLEDs):
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = [ 0 ] * nLEDs
intensity[i] = 1
setLEDs(R, G, B, intensity)
time.sleep(0.05)
追到底追回来(有点像弹跳球)最优雅的方法是什么?
非常简单,您可以复制 for
循环。使它第二次反转。
似乎没有必要一遍又一遍地重新定义 R、G、B,因此可以将它们移出循环,但也许您正打算更改它们,所以我暂时将它们保留
while True:
for i in range(nLEDs):
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = [ 0 ] * nLEDs
intensity[i] = 1
setLEDs(R, G, B, intensity)
time.sleep(0.05)
for i in reversed(range(nLEDs)):
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = [ 0 ] * nLEDs
intensity[i] = 1
setLEDs(R, G, B, intensity)
time.sleep(0.05)
理想情况下,你的 API 有一个你可以调用的 setLED
函数,那么当一次只有 2 个 LED 发生变化时,你不需要设置所有 LED 的状态。
与其将强度定义为列表,不如将其定义为 collections.deque
,然后是 rotate
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = collections.deque([ 0 ] * nLEDs)
intensity[0] = 1
while True:
for i in range(nLEDs):
intensity.rotate(1)
setLEDs(R, G, B, list(intensity))
time.sleep(0.05)
然后添加一个额外的 for 循环返回
我正在使用一个简单的 while 循环和一个数组来追踪条带上的 LED。
while True:
for i in range(nLEDs):
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = [ 0 ] * nLEDs
intensity[i] = 1
setLEDs(R, G, B, intensity)
time.sleep(0.05)
追到底追回来(有点像弹跳球)最优雅的方法是什么?
非常简单,您可以复制 for
循环。使它第二次反转。
似乎没有必要一遍又一遍地重新定义 R、G、B,因此可以将它们移出循环,但也许您正打算更改它们,所以我暂时将它们保留
while True:
for i in range(nLEDs):
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = [ 0 ] * nLEDs
intensity[i] = 1
setLEDs(R, G, B, intensity)
time.sleep(0.05)
for i in reversed(range(nLEDs)):
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = [ 0 ] * nLEDs
intensity[i] = 1
setLEDs(R, G, B, intensity)
time.sleep(0.05)
理想情况下,你的 API 有一个你可以调用的 setLED
函数,那么当一次只有 2 个 LED 发生变化时,你不需要设置所有 LED 的状态。
与其将强度定义为列表,不如将其定义为 collections.deque
,然后是 rotate
R = [ 255 ] * nLEDs
G = [ 255 ] * nLEDs
B = [ 255 ] * nLEDs
intensity = collections.deque([ 0 ] * nLEDs)
intensity[0] = 1
while True:
for i in range(nLEDs):
intensity.rotate(1)
setLEDs(R, G, B, list(intensity))
time.sleep(0.05)
然后添加一个额外的 for 循环返回