【问题标题】:LED strip matrix for FFT with Python and Raspberry Pi使用 Python 和 Raspberry Pi 进行 FFT 的 LED 条矩阵
【发布时间】:2014-06-03 22:16:57
【问题描述】:

我被 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)。所有其他列似乎都是黑色的。

当我将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 个像素。但是,它不会遗漏一个像素,因此会在到达最后一个像素之前停止。

【问题讨论】:

  • 您需要退后一步,简化您的测试用例。摆脱对音乐文件的依赖,只需生成具有已知值的测试模式。你会这样找到你的错误。

标签: python arrays numpy raspberry-pi led


【解决方案1】:

想通了!

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)

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多