Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality for using WS2801 LED Controller #51

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions python/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"""Set True if using an inverting logic level converter"""
SOFTWARE_GAMMA_CORRECTION = True
"""Set to True because Raspberry Pi doesn't use hardware dithering"""
LED_CONTROLLER = 'ws2801'
"""There are different types of led controllers. Default is ws281x.
Another option is WS2801.
"""

if DEVICE == 'blinkstick':
SOFTWARE_GAMMA_CORRECTION = True
Expand All @@ -49,13 +53,13 @@
DISPLAY_FPS = True
"""Whether to display the FPS when running (can reduce performance)"""

N_PIXELS = 144
N_PIXELS = 158
"""Number of pixels in the LED strip (must match ESP8266 firmware)"""

GAMMA_TABLE_PATH = os.path.join(os.path.dirname(__file__), 'gamma_table.npy')
"""Location of the gamma correction table"""

MIC_RATE = 48000
MIC_RATE = 44100
"""Sampling frequency of the microphone in Hz"""

FPS = 50
Expand Down
4 changes: 2 additions & 2 deletions python/install/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
download_url="https://github.com/naztronaut/dancyPi-audio-reactive-led",
description="Audio Reactive Raspberry Pi with WS2812b LEDs.",
license="MIT",
install_requires=['numpy', 'pyaudio', 'pyqtgraph', 'scipy==1.4.1', 'rpi_ws281x']
)
install_requires=['numpy', 'pyaudio', 'pyqtgraph', 'scipy==1.4.1', 'rpi_ws281x', 'adafruit-ws2801']
)
58 changes: 49 additions & 9 deletions python/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@
_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Raspberry Pi controls the LED strip directly
elif config.DEVICE == 'pi':
from rpi_ws281x import *
strip = Adafruit_NeoPixel(config.N_PIXELS, config.LED_PIN,
config.LED_FREQ_HZ, config.LED_DMA,
config.LED_INVERT, config.BRIGHTNESS)
strip.begin()
if config.LED_CONTROLLER == 'ws281x':
from rpi_ws281x import *
strip = Adafruit_NeoPixel(config.N_PIXELS, config.LED_PIN,
config.LED_FREQ_HZ, config.LED_DMA,
config.LED_INVERT, config.BRIGHTNESS)
strip.begin()
elif config.LED_CONTROLLER == 'ws2801':
# Make Raspberry pinout addresible
import RPi.GPIO as GPIO

# Import the WS2801 module.
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI

# Alternatively specify a hardware SPI connection on /dev/spidev0.0:
spi_port = 0
spi_device = 0
strip = Adafruit_WS2801.WS2801Pixels(config.N_PIXELS, spi=SPI.SpiDev(spi_port,spi_device), gpio=GPIO)

elif config.DEVICE == 'blinkstick':
from blinkstick import blinkstick
import signal
Expand Down Expand Up @@ -82,8 +96,7 @@ def _update_esp8266():
_sock.sendto(m, (config.UDP_IP, config.UDP_PORT))
_prev_pixels = np.copy(p)


def _update_pi():
def _update_ws281x():
"""Writes new LED values to the Raspberry Pi's LED strip

Raspberry Pi uses the rpi_ws281x to control the LED strip directly.
Expand All @@ -104,17 +117,44 @@ def _update_pi():
# Ignore pixels if they haven't changed (saves bandwidth)
if np.array_equal(p[:, i], _prev_pixels[:, i]):
continue

strip._led_data[i] = int(rgb[i])
_prev_pixels = np.copy(p)
strip.show()

def _update_ws2801():
"""Writes new LED values to the Raspberry Pi's LED strip
This function updates the LED strip with new values.
"""
global pixels, _prev_pixels
# Truncate values and cast to integer
pixels = np.clip(pixels, 0, 255).astype(int)
# Optional gamma correction
p = _gamma[pixels] if config.SOFTWARE_GAMMA_CORRECTION else np.copy(pixels)
r = [int(val) for val in p[0]]
g = [int(val) for val in p[1]]
b = [int(val) for val in p[2]]
# Update the pixels
for i in range(config.N_PIXELS):
# Ignore pixels if they haven't changed (saves bandwidth)
if np.array_equal(p[:, i], _prev_pixels[:, i]):
continue

strip.set_pixel(i, Adafruit_WS2801.RGB_to_color(r[i],g[i],b[i]))
_prev_pixels = np.copy(p)
strip.show()

def _update_pi():
if config.LED_CONTROLLER == 'ws281x':
_update_ws281x()
elif config.LED_CONTROLLER == 'ws2801':
_update_ws2801()

def _update_blinkstick():
"""Writes new LED values to the Blinkstick.
This function updates the LED strip with new values.
"""
global pixels

# Truncate values and cast to integer
pixels = np.clip(pixels, 0, 255).astype(int)
# Optional gamma correction
Expand Down
10 changes: 9 additions & 1 deletion python/microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ def start_stream(callback):
prev_ovf_time = time.time()
while True:
try:
y = np.fromstring(stream.read(frames_per_buffer, exception_on_overflow=False), dtype=np.int16)
try:
y = np.fromstring(stream.read(frames_per_buffer, exception_on_overflow=False), dtype=np.int16)
except OSError as ex:
if ex.errno == -9999:
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=config.MIC_RATE,
input=True,
frames_per_buffer=frames_per_buffer)
y = y.astype(np.float32)
stream.read(stream.get_read_available(), exception_on_overflow=False)
callback(y)
Expand Down
Loading