125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
import atexit
|
|
from pyleds.lib.Color import Color
|
|
|
|
|
|
class _LedData(object):
|
|
|
|
def __init__(self, channel, size):
|
|
self.size = size
|
|
self.channel = channel
|
|
self._led = {}
|
|
for i in range(size):
|
|
self._led[i] = 0
|
|
|
|
def __getitem__(self, pos):
|
|
"""Return the 24-bit RGB color value at the provided position or slice
|
|
of positions.
|
|
"""
|
|
# Handle if a slice of positions are passed in by grabbing all the values
|
|
# and returning them in a list.
|
|
if isinstance(pos, slice):
|
|
return [self._led[n] for n in range(*pos.indices(self.size))]
|
|
else:
|
|
return self._led[pos]
|
|
|
|
def __setitem__(self, pos, value):
|
|
"""Set the 24-bit RGB color value at the provided position or slice of
|
|
positions.
|
|
"""
|
|
# Handle if a slice of positions are passed in by setting the appropriate
|
|
# LED data values to the provided values.
|
|
if isinstance(pos, slice):
|
|
index = 0
|
|
for n in range(*pos.indices(self.size)):
|
|
self._led[n] = value[index]
|
|
index += 1
|
|
# Else assume the passed in value is a number to the position.
|
|
else:
|
|
self._led[pos] = value
|
|
|
|
|
|
class FakeStrip(object):
|
|
_brightness: int
|
|
|
|
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
|
|
brightness=255, channel=0, strip_type=None, gamma=None):
|
|
|
|
if gamma is None:
|
|
# Support gamma in place of strip_type for back-compat with
|
|
# previous version of forked library
|
|
if type(strip_type) is list and len(strip_type) == 256:
|
|
gamma = strip_type
|
|
# strip_type = None
|
|
else:
|
|
gamma = list(range(256))
|
|
|
|
# Initialize the channel in use
|
|
self._channel = None
|
|
self._gamma = gamma
|
|
self._brightness = brightness
|
|
|
|
# Grab the led data array.
|
|
self._led_data = _LedData(self._channel, num)
|
|
|
|
# Substitute for __del__, traps an exit condition and cleans up properly
|
|
atexit.register(self._cleanup)
|
|
|
|
def _cleanup(self):
|
|
# Clean up memory used by the library when not needed anymore.
|
|
if self._channel is not None:
|
|
self._channel = None
|
|
|
|
def setGamma(self, gamma):
|
|
if type(gamma) is list and len(gamma) == 256:
|
|
self._gamma = gamma
|
|
|
|
def begin(self):
|
|
"""Initialize library, must be called once before other functions are called."""
|
|
return
|
|
|
|
def show(self):
|
|
"""Update the display with the data from the LED buffer."""
|
|
return
|
|
|
|
def getBrightness(self) -> int:
|
|
return self._brightness
|
|
|
|
def setBrightness(self, brightness: int):
|
|
"""Scale each LED in the buffer by the provided brightness.
|
|
A brightness of 0 is the darkest and 255 is the brightest.
|
|
"""
|
|
self._brightness = brightness
|
|
|
|
def numPixels(self):
|
|
"""Return the number of pixels in the display."""
|
|
return self._led_data.size
|
|
|
|
def setPixelColor(self, n, color):
|
|
"""Set LED at position n to the provided 24-bit color value (in RGB order).
|
|
"""
|
|
self._led_data[n] = color
|
|
|
|
def setPixelColorRGB(self, n, red, green, blue):
|
|
"""Set LED at position n to the provided red, green, and blue color.
|
|
Each color component should be a value from 0 to 255 (where 0 is the
|
|
lowest intensity and 255 is the highest intensity).
|
|
"""
|
|
self.setPixelColor(n, Color(red, green, blue))
|
|
|
|
def getPixels(self):
|
|
"""Return an object which allows access to the LED display data as if
|
|
it were a sequence of 24-bit RGB values.
|
|
"""
|
|
return self._led_data
|
|
|
|
def getPixelColor(self, n):
|
|
"""Get the 24-bit RGB color value for the LED at position n."""
|
|
return self._led_data[n]
|
|
|
|
def getPixelColorRGB(self, n):
|
|
c = lambda: None
|
|
setattr(c, 'r', self._led_data[n] >> 16 & 0xff)
|
|
setattr(c, 'g', self._led_data[n] >> 8 & 0xff)
|
|
setattr(c, 'b', self._led_data[n] & 0xff)
|
|
return c
|