我正在使用一个简单的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)
反复追逐结束和反击的最优雅方式是什么(有点像弹跳球)?
答案 0 :(得分:1)
很简单,您可以复制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的状态。
答案 1 :(得分:0)
您可以将其定义为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循环返回