[英]LED strip matrix for FFT with Python and Raspberry Pi
我被Python和LED燈條卡住了。 具有WS2801芯片且可通過SPI尋址的LED燈帶已安排為如下矩陣:
---- ---- ---- ----
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)。 所有其他列似乎都是黑色的。
當我在col循環中放置spidev.write(pixel_output)
和spidev.flush()
以便為每一列寫入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個像素。 但是,它不會遺漏一個像素,因此會在到達最后一個像素之前停止。
弄清楚了!
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.