使用Python和树莓派的LED条矩阵进行FFT
我在用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()
放在列循环里,这样每列的像素输出都能写入时,其他列的某些灯亮了。但是它们的亮起方式有点随机,而且声音也不再流畅了。
顺便说一下,LED灯带在PixelPi的示例中,比如渐变和追逐效果,都表现得很好。
这可能是WS2801芯片的一些特性我不知道吗?还是我没有正确计算像素输出矩阵?
更新:还有一件奇怪的事:
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个像素。然而,它并没有跳过一个像素,因此在到达最后一个像素之前就停止了。
1 个回答
0
我搞明白了!
pixel_output[pixel_index:] = filter_pixel(WHITE, 1)
它不仅仅复制了我预期的3个数组元素。实际上,它把从pixel_index得到的值,复制到了整个缓冲区的末尾。
给复制设置一个上限,解决了我的问题。
pixel_output[pixel_index:(pixel_index + PIXEL_SIZE)] = filter_pixel(WHITE, 1)