adafruit / Adafruit_CircuitPython_NeoPixel

CircuitPython drivers for neopixels.
MIT License
302 stars 98 forks source link

How to clear the NeoPixel's buffer data? #153

Closed xgqfrms closed 1 year ago

xgqfrms commented 1 year ago

How to clear the NeoPixel's buffer data?

Why is this code not working as I expect?

I think it might be a buffer data problem.

I had read the API docs, but did not find any method that can clear the buffer.

So, how to clear the buffer data?

./led-strip-test.py

#!/usr/bin/env python3
# coding: utf8

import board
import neopixel
from time import sleep

PIN = board.D18
# 0.3W/LED (30mA ~ 60mA)
LEDs = 30
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(PIN, LEDs, brightness=1.0, auto_write=True, pixel_order=ORDER)

while True:
  pixels[29] = (255, 0, 0)
  sleep(0.3)
  pixels[29] = (0, 255, 0)
  sleep(0.3)
  pixels[29] = (0, 0, 255)
  sleep(0.3)
# test
$ sudo ./led-strip-test.py

error / bug

I'm just confused about what's wrong with this.

The order in which the LEDs light up is very random and doesn't seem to be under my control.

Please See the video for the test results.

https://www.youtube.com/watch?v=KZjyptMkATs

wanted result

Only the 30th LED blinks.

xgqfrms commented 1 year ago

./led-strip-test.py

#!/usr/bin/env python3
# coding: utf8

import board
import neopixel
from time import sleep

PIN = board.D18
# 0.3W/LED (30mA ~ 60mA)
LEDs = 30
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(PIN, LEDs, brightness=1.0, auto_write=True, pixel_order=ORDER)

def clear():
  # pixels[29] = (0, 0, 0)
  for i in range(LEDs):
    pixels[i] = (0, 0, 0)

def rgb_test():
  while True:
    # the 30th LED
    pixels[29] = (255, 0, 0)
    sleep(0.3)
    pixels[29] = (0, 255, 0)
    sleep(0.3)
    pixels[29] = (0, 0, 255)
    sleep(0.3)

try:
  rgb_test()
except KeyboardInterrupt:
  print('Ctrl + C exit ✅')
  clear()
except RuntimeError as error:
  print("error =", error, error.args[0])
  clear()
  pass
except Exception as error:
  print("exception =", error)
  clear()
  raise error
# finally:
#   print("after one second, auto clear buffer! 👻")
#   sleep(1.0)
#   clear()
#   print('clear 🚀')
# test
$ sudo ./led-strip-test.py