pimoroni / blinkt

Python Library for Blinkt; 8 APA102 LEDs for your Raspberry Pi
https://shop.pimoroni.com/products/blinkt
MIT License
314 stars 103 forks source link

Bug in larson.py #56

Closed bablokb closed 7 years ago

bablokb commented 7 years ago

In examples/larson.py you will run into an IndexError if delta is an exact multiple of 16:

offset = int(abs((delta % 16) - 8))
for i in range(8):
  blinkt.set_pixel(i , REDS[offset + i], 0, 0)

The list REDS only has 15 values (max index is 14), but if offset is 8 you are requesting index 15.

Bernhard

dglaude commented 7 years ago

I may have discover this bug while porting larson to mote stick. My souvenir is indeed that you need to grow the array a little bit.

2017-07-15 11:30 GMT+02:00 bablokb notifications@github.com:

In examples/larson.py you will run into an IndexError if delta is an exact multiple of 16:

offset = int(abs((delta % 16) - 8)) for i in range(8): blinkt.set_pixel(i , REDS[offset + i], 0, 0)

The list REDS only has 15 values (max index is 14), but if offset is 8 you are requesting index 15.

Bernhard

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pimoroni/blinkt/issues/56, or mute the thread https://github.com/notifications/unsubscribe-auth/ASiRnBm8zNiJdpzT3ZA4aGZEtc-QvVWfks5sOIakgaJpZM4OY99n .

Gadgetoid commented 7 years ago

Yikes. Good spot. It should be delta % len(REDS) ideally.

Edit: And the array padded, and the magic numbers replaced with blinkt.NUM_PIXELS to be clear about intent, and the commented-out Sine wave example fixed because it depends upon the missing math import!

bablokb commented 7 years ago

Hm, just a bit of nitpicking: you still have a magic number in your code:

delta = (time.time() - start_time) * 16
    offset = int(abs((delta % len(REDS)) - blinkt.NUM_PIXELS))

couldn't you also replace the 16 with len(REDS)?

Anyhow, it's just an example and I think you are doing a wonderful job giving us users a headstart on implementing our own solutions.

Bernhard