我坚持使用Python和LED灯条。 LED灯条具有WS2801芯片并可通过SPI寻址,其排列方式如下:
---- ---- ---- ----
140 | | | | | | | | 15
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
155 | | | | | | | | 0
---- ---- ----
每个破折号代表一个LED(一个像素)。一列中有16个像素,占8行。
编号从右下角开始。因此,最右边的列以索引0开始,以索引15结束。行之间的四个像素将不会被点亮。所以第二个最右边的列从顶部开始,索引20下降到底部,索引为35。 最左边的列的范围从顶部的140到底部的155。
我想要做的是在播放时想象一首歌曲的频谱。较低的频率应显示在左列中,较高的频率显示在右列中。
我的代码基于PixelPi(https://github.com/scottjgibson/PixelPi)。我将FFT部分省略掉,因为那部分不是问题。
# Each pixel consumes 3 bytes
PIXEL_SIZE = 3
# Lots of colors
BLACK = bytearray(b'\x00\x00\x00')
AQUA = bytearray(b'\x00\xff\xff')
AQUAMARINE = bytearray(b'\x7f\xff\xd4')
def filter_pixel(input_pixel, brightness):
output_pixel = bytearray(PIXEL_SIZE)
input_pixel[0] = int(brightness * input_pixel[0])
input_pixel[1] = int(brightness * input_pixel[1])
input_pixel[2] = int(brightness * input_pixel[2])
output_pixel[0] = gamma[input_pixel[0]]
output_pixel[1] = gamma[input_pixel[1]]
output_pixel[2] = gamma[input_pixel[2]]
return output_pixel
# Initialize LED strip matrix
height = 16
base = [155, 120, 115, 80, 75, 40, 35, 0]
# Do the indexes of this column go from bottom to top?
up = [False, True, False, True, False, True, False, True]
color = [NAVY, RED, MAROON, DARKBLUE, DARKCYAN, PALEGREEN, YELLOWGREEN, YELLOW]
# Output matrix, filled with black pixels
empty_output = bytearray(args.num_leds * PIXEL_SIZE + 3)
for led in range(args.num_leds):
empty_output[led * PIXEL_SIZE:] = filter_pixel(BLACK, 1)
current_color = bytearray(PIXEL_SIZE)
corrected_color = bytearray(PIXEL_SIZE)
while True: # (Actually while song is playing)
# Returns an array of length 8 with values between 0 and 4095
matrix = calculate_levels(matrix, weighting, data, CHUNK_SIZE, sample_rate)
# Copy the matrix with only black pixels. Copying seems to be faster than resetting all not needed pixels to black
pixel_output[:] = empty_output
for col in range(len(base)):
current_color[:] = color[col][:]
# Do some gamma correction
corrected_color[:] = filter_pixel(current_color[:], 1)
# Each column is 16 pixels high. The maximum value of the FFT to be returned for each column is 4095. 4096 / 256 = 16
lighted_height = round(matrix[col]/float(1 << 8), 2)
for row in range(max(16, int(lighted_height) + 1)):
pixel_index = base[col] + row if up[col] == True else base[col] - row
pixel_index = pixel_index * PIXEL_SIZE
if (row < int(lighted_height)):
# Pixel's brightness in 100%
pixel_output[pixel_index:] = corrected_color[:]
elif (row <= int(lighted_height) and row + 1 > int(lighted_height)):
# Pixel's brightness is between 0 and 1
pixel_output[pixel_index:] = filter_pixel(current_color[:], lighted_height - int(lighted_height))
#print "[col:", col, ", row:", row, "] : ", pixel_index, "lighted_height:", lighted_height, "int(lighted_height)", int(lighted_height), "lighted:", lighted
# As I uncomment these two lines, at least all pixels on the other columns are displayed.
#spidev.write(pixel_output)
#spidev.flush()
spidev.write(pixel_output)
spidev.flush()
问题是此代码仅点亮最右侧的列(0到15)。所有其他列似乎都是黑色的。
当我将spidev.write(pixel_output)
和spidev.flush()
放在col循环中,以便为每列写入pixel_output时,其他列中的至少一些灯会亮起。然而,它们以某种方式随机出现,声音不再平滑。
顺便说一句,LED条带在PixelPi示例中表现得很好,如褪色和追逐。 可能这是由于我不知道的WS2801芯片的一些特性?或者我没有正确计算pixel_output矩阵?
更新:还有一件奇怪的事情:
i = 0
x = 0
while x < 160:
if i != 0 and i % 16 == 0:
x = x + 4
pixel_index = x * PIXEL_SIZE
pixel_output[pixel_index:] = filter_pixel(WHITE, 1)
i = i + 1
x = x + 1
print "i, x", i, x
time.sleep(0.1)
spidev.write(pixel_output)
spidev.flush()
这应该实际上将像素0点亮到最后一个并且在循环执行16次之后省略4个像素。但是,它不会遗漏像素,因此会在到达最后一个像素之前停止。
答案 0 :(得分:0)
想出来了!
pixel_output[pixel_index:] = filter_pixel(WHITE, 1)
不仅复制了我预期的3个数组元素。它将filter_pixel返回的值从pixel_index复制到整个缓冲区。
设置复制的上限解决了我的问题。
pixel_output[pixel_index:(pixel_index + PIXEL_SIZE)] = filter_pixel(WHITE, 1)