Exemplo n.º 1
0
def initialize(width, height, amplitude):
    leds = ws.new_ws2811_t()
    for channum in range(2):
        channel = ws.ws2811_channel_get(leds, channum)
        ws.ws2811_channel_t_count_set(channel, 0)
        ws.ws2811_channel_t_gpionum_set(channel, 0)
        ws.ws2811_channel_t_invert_set(channel, 0)
        ws.ws2811_channel_t_brightness_set(channel, 0)

    channel = ws.ws2811_channel_get(leds, 0)

    ws.ws2811_channel_t_count_set(channel, width * height)
    ws.ws2811_channel_t_gpionum_set(channel, 18)
    ws.ws2811_channel_t_invert_set(channel, 0)
    ws.ws2811_channel_t_brightness_set(channel, amplitude)
    ws.ws2811_t_freq_set(leds, 800000)
    ws.ws2811_t_dmanum_set(leds, 5)

    resp = ws.ws2811_init(leds)
    if resp != ws.WS2811_SUCCESS:
        message = ws.ws2811_get_return_t_str(resp)
        raise RuntimeError('ws2811_init failed : {0} ({1})'.format(
            resp, message))

    return (channel, leds)
Exemplo n.º 2
0
    def __init__(self,
                 num,
                 pin,
                 freq_hz=800000,
                 dma=10,
                 invert=False,
                 brightness=255,
                 channel=0,
                 strip_type=None,
                 gamma=None):
        """Class to represent a SK6812/WS281x LED display.  Num should be the
        number of pixels in the display, and pin should be the GPIO pin connected
        to the display signal line (must be a PWM pin like 18!).  Optional
        parameters are freq, the frequency of the display signal in hertz (default
        800khz), dma, the DMA channel to use (default 10), invert, a boolean
        specifying if the signal line should be inverted (default False), and
        channel, the PWM channel to use (defaults to 0).
        """

        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))

        if strip_type is None:
            strip_type = ws.WS2811_STRIP_GRB

        # Create ws2811_t structure and fill in parameters.
        self._leds = ws.new_ws2811_t()

        # Initialize the channels to zero
        for channum in range(2):
            chan = ws.ws2811_channel_get(self._leds, channum)
            ws.ws2811_channel_t_count_set(chan, 0)
            ws.ws2811_channel_t_gpionum_set(chan, 0)
            ws.ws2811_channel_t_invert_set(chan, 0)
            ws.ws2811_channel_t_brightness_set(chan, 0)

        # Initialize the channel in use
        self._channel = ws.ws2811_channel_get(self._leds, channel)
        ws.ws2811_channel_t_gamma_set(self._channel, gamma)
        ws.ws2811_channel_t_count_set(self._channel, num)
        ws.ws2811_channel_t_gpionum_set(self._channel, pin)
        ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1)
        ws.ws2811_channel_t_brightness_set(self._channel, brightness)
        ws.ws2811_channel_t_strip_type_set(self._channel, strip_type)

        # Initialize the controller
        ws.ws2811_t_freq_set(self._leds, freq_hz)
        ws.ws2811_t_dmanum_set(self._leds, dma)

        # Grab the led data array.
        self._led_data = _LED_Data(self._channel, num)

        # Substitute for __del__, traps an exit condition and cleans up properly
        atexit.register(self._cleanup)
Exemplo n.º 3
0
    def __init__(self,
                 pin,
                 count,
                 brightness=255,
                 strip_type=ws.WS2811_STRIP_GRB,
                 frequency=800000,
                 dma=5,
                 invert=0,
                 channel=0):
        self.size = count
        self.channel = channel
        self._data = ws.new_ws2811_t()

        # reset
        for n in range(2):
            c = ws.ws2811_channel_get(self._data, n)
            ws.ws2811_channel_t_count_set(c, 0)
            ws.ws2811_channel_t_gpionum_set(c, 0)
            ws.ws2811_channel_t_invert_set(c, 0)
            ws.ws2811_channel_t_brightness_set(c, 0)

        # initialize
        self._channel = ws.ws2811_channel_get(self._data, self.channel)
        ws.ws2811_channel_t_count_set(self._channel, self.size)
        ws.ws2811_channel_t_gpionum_set(self._channel, pin)
        ws.ws2811_channel_t_invert_set(self._channel, invert)
        ws.ws2811_channel_t_brightness_set(self._channel, brightness)
        ws.ws2811_channel_t_strip_type_set(self._channel, strip_type)

        ws.ws2811_t_freq_set(self._data, frequency)
        ws.ws2811_t_dmanum_set(self._data, dma)

        ws.ws2811_init(self._data)
Exemplo n.º 4
0
	def __init__(self, num, pin, freq_hz=800000, dma=5, invert=False, channel=0):
		"""Class to represent a NeoPixel/WS281x LED display.  Num should be the
		number of pixels in the display, and pin should be the GPIO pin connected
		to the display signal line (must be a PWM pin like 18!).  Optional
		parameters are freq, the frequency of the display signal in hertz (default
		800khz), dma, the DMA channel to use (default 5), invert, a boolean
		specifying if the signal line should be inverted (default False), and
		channel, the PWM channel to use (defaults to 0).
		"""
		# Create ws2811_t structure and fill in parameters.
		self._leds = ws.new_ws2811_t()

		# Initialize the channels to zero
		for channum in range(2):
			chan = ws.ws2811_channel_get(self._leds, channum)
			ws.ws2811_channel_t_count_set(chan, 0)
			ws.ws2811_channel_t_gpionum_set(chan, 0)
			ws.ws2811_channel_t_invert_set(chan, 0)

		# Initialize the channel in use
		self._channel = ws.ws2811_channel_get(self._leds, channel)
		ws.ws2811_channel_t_count_set(self._channel, num)
		ws.ws2811_channel_t_gpionum_set(self._channel, pin)
		ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1)

		# Initialize the controller
		ws.ws2811_t_freq_set(self._leds, freq_hz)
		ws.ws2811_t_dmanum_set(self._leds, dma)

		# Grab the led data array.
		self._led_data = _LED_Data(self._channel, num)

		# Start at full brightness.
		self._brightness = 0
Exemplo n.º 5
0
	def __init__(self, num, pin, freq_hz=800000, dma=5, invert=False, brightness=255, channel=0):
		"""Class to represent a NeoPixel/WS281x LED display.  Num should be the
		number of pixels in the display, and pin should be the GPIO pin connected
		to the display signal line (must be a PWM pin like 18!).  Optional
		parameters are freq, the frequency of the display signal in hertz (default
		800khz), dma, the DMA channel to use (default 5), invert, a boolean
		specifying if the signal line should be inverted (default False), and
		channel, the PWM channel to use (defaults to 0).
		"""
		# Create ws2811_t structure and fill in parameters.
		self._leds = ws.new_ws2811_t()

		# Initialize the channels to zero
		for channum in range(2):
			chan = ws.ws2811_channel_get(self._leds, channum)
			ws.ws2811_channel_t_count_set(chan, 0)
			ws.ws2811_channel_t_gpionum_set(chan, 0)
			ws.ws2811_channel_t_invert_set(chan, 0)
			ws.ws2811_channel_t_brightness_set(chan, 0)

		# Initialize the channel in use
		self._channel = ws.ws2811_channel_get(self._leds, channel)
		ws.ws2811_channel_t_count_set(self._channel, num)
		ws.ws2811_channel_t_gpionum_set(self._channel, pin)
		ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1)
		ws.ws2811_channel_t_brightness_set(self._channel, brightness)

		# Initialize the controller
		ws.ws2811_t_freq_set(self._leds, freq_hz)
		ws.ws2811_t_dmanum_set(self._leds, dma)

		# Grab the led data array.
		self._led_data = _LED_Data(self._channel, num)
Exemplo n.º 6
0
def colorChange(color):
	print(color)
        # LED configuration.
        LED_CHANNEL    = 0
        LED_COUNT      = 16         # How many LEDs to light.
        LED_FREQ_HZ    = 800000     # Frequency of the LED signal.  Should be 800khz or 400khz.
        LED_DMA_NUM    = 5          # DMA channel to use, can be 0-14.
        LED_GPIO       = 18         # GPIO connected to the LED signal line.  Must support PWM!
        LED_BRIGHTNESS = 255        # Set to 0 for darkest and 255 for brightest
        LED_INVERT     = 0          # Set to 1 to invert the LED signal, good if using NPN
        							# transistor as a 3.3V->5V level converter.  Keep at 0
        							# for a normal/non-inverted signal.
        leds = ws.new_ws2811_t()

        # Initialize all channels to off
        for channum in range(2):
            channel = ws.ws2811_channel_get(leds, channum)
            ws.ws2811_channel_t_count_set(channel, 0)
            ws.ws2811_channel_t_gpionum_set(channel, 0)
            ws.ws2811_channel_t_invert_set(channel, 0)
            ws.ws2811_channel_t_brightness_set(channel, 0)

        channel = ws.ws2811_channel_get(leds, LED_CHANNEL)

        ws.ws2811_channel_t_count_set(channel, LED_COUNT)
        ws.ws2811_channel_t_gpionum_set(channel, LED_GPIO)
        ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
        ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)

        ws.ws2811_t_freq_set(leds, LED_FREQ_HZ)
        ws.ws2811_t_dmanum_set(leds, LED_DMA_NUM)

        # Initialize library with LED configuration.
        resp = ws.ws2811_init(leds)
        if resp != 0:
        	raise RuntimeError('ws2811_init failed with code {0}'.format(resp))
	try:
        	DOT_COLOR = int(color[1:7],16)

		# Update each LED color in the buffer.
		for i in range(LED_COUNT):
		# Pick a color based on LED position and an offset for animation.
		# Set the LED color buffer value.
			ws.ws2811_led_set(channel, i, DOT_COLOR)
			time.sleep(.15)
		# Send the LED color data to the hardware.
			resp = ws.ws2811_render(leds)
		if resp != 0:
			raise RuntimeError('ws2811_render failed with code {0}'.format(resp))

		# Delay for a small period of time.
		time.sleep(.25)
	finally:
		# Ensure ws2811_fini is called before the program quits.
		ws.ws2811_fini(leds)
		# Example of calling delete function to clean up structure memory.  Isn't
		# strictly necessary at the end of the program execution here, but is good practice.
		ws.delete_ws2811_t(leds)
Exemplo n.º 7
0
def init(maxBrightness=10):
    global LED_COUNT
    global LED_BRIGHTNESS
    global LED_GPIO
    global CHANNEL
    global LEDS

    if (LEDS == False):
        LEDS = ws.new_ws2811_t()
    else:
        # Ensure ws2811_fini is called before the program quits.
        ws.ws2811_fini(LEDS)
        # Example of calling delete function to clean up structure memory.  Isn't
        # strictly necessary at the end of the program execution here, but is good practice.
        ws.delete_ws2811_t(LEDS)
        LEDS = ws.new_ws2811_t()

    SPLIT_LED_COUNT = [420, 402]
    LED_COUNT = 420 + 402
    LED_BRIGHTNESS = maxBrightness
    LED_GPIO = [18, 13]
    CHANNEL = []
    LED_CHANNEL = [0, 1]
    # Create a ws2811_t structure from the LED configuration.
    # Note that this structure will be created on the heap so you need to be careful
    # that you delete its memory by calling delete_ws2811_t when it's not needed.

    # Initialize all channels to off
    for channum in range(2):
        channel = ws.ws2811_channel_get(LEDS, channum)
        ws.ws2811_channel_t_count_set(channel, 0)
        ws.ws2811_channel_t_gpionum_set(channel, 0)
        ws.ws2811_channel_t_invert_set(channel, 0)
        ws.ws2811_channel_t_brightness_set(channel, 0)

        CHANNEL.append(ws.ws2811_channel_get(LEDS, channum))

        ws.ws2811_channel_t_count_set(CHANNEL[channum],
                                      SPLIT_LED_COUNT[channum])
        ws.ws2811_channel_t_gpionum_set(CHANNEL[channum], LED_GPIO[channum])
        ws.ws2811_channel_t_invert_set(CHANNEL[channum], LED_INVERT)
        ws.ws2811_channel_t_brightness_set(CHANNEL[channum], LED_BRIGHTNESS)
        ws.ws2811_channel_t_strip_type_set(CHANNEL[channum], LED_STRIP)

    ws.ws2811_t_freq_set(LEDS, LED_FREQ_HZ)
    ws.ws2811_t_dmanum_set(LEDS, LED_DMA_NUM)

    # Initialize library with LED configuration.
    resp = ws.ws2811_init(LEDS)
    if resp != ws.WS2811_SUCCESS:
        message = ws.ws2811_get_return_t_str(resp)
        raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(
            resp, message))

    fill({"R": 0, "G": 0, "B": 0})
    render()
    print('Initialized ✓')
Exemplo n.º 8
0
    def __init__(self,
                 num,
                 pin,
                 freq_hz=800000,
                 dma=5,
                 invert=False,
                 brightness=128,
                 channel=0,
                 gamma=None):
        """Class to represent a SK6812/WS281x LED display.  Num should be the
        number of pixels in the display, and pin should be the GPIO pin connected
        to the display signal line (must be a PWM pin like 18!).  Optional
        parameters are freq, the frequency of the display signal in hertz (default
        800khz), dma, the DMA channel to use (default 5), invert, a boolean
        specifying if the signal line should be inverted (default False), and
        channel, the PWM channel to use (defaults to 0).
        """

        if gamma is None:
            gamma = list(range(256))

        self._gamma = gamma

        # Keep track of successful init so we don't call ws2811_fini unecessarily
        self._init_successful = False

        # Create ws2811_t structure and fill in parameters.
        self._leds = ws.new_ws2811_t()

        # Initialize the channels to zero
        for channum in range(2):
            chan = ws.ws2811_channel_get(self._leds, channum)
            ws.ws2811_channel_t_count_set(chan, 0)
            ws.ws2811_channel_t_gpionum_set(chan, 0)
            ws.ws2811_channel_t_invert_set(chan, 0)
            ws.ws2811_channel_t_brightness_set(chan, 0)

        # Initialize the channel in use
        self._channel = ws.ws2811_channel_get(self._leds, channel)
        ws.ws2811_channel_t_gamma_set(self._channel, self._gamma)
        ws.ws2811_channel_t_count_set(self._channel, num)
        ws.ws2811_channel_t_gpionum_set(self._channel, pin)
        ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1)
        ws.ws2811_channel_t_brightness_set(self._channel, brightness)
        ws.ws2811_channel_t_strip_type_set(self._channel, ws.WS2811_STRIP_GRB)

        # Initialize the controller
        ws.ws2811_t_freq_set(self._leds, freq_hz)
        ws.ws2811_t_dmanum_set(self._leds, dma)

        # Grab the led data array.
        self._led_data = _LED_Data(self._channel, num)

        # Substitute for __del__, traps an exit condition and cleans up properly
        atexit.register(self._cleanup)
Exemplo n.º 9
0
def neopixel_write(gpio, buf):
    global _led_strip # we'll have one strip we init if its not at first

    if _led_strip is None:
        # Create a ws2811_t structure from the LED configuration.
        # Note that this structure will be created on the heap so you
        # need to be careful that you delete its memory by calling
        # delete_ws2811_t when it's not needed.
        _led_strip = ws.new_ws2811_t()

        # Initialize all channels to off
        for channum in range(2):
            channel = ws.ws2811_channel_get(_led_strip, channum)
            ws.ws2811_channel_t_count_set(channel, 0)
            ws.ws2811_channel_t_gpionum_set(channel, 0)
            ws.ws2811_channel_t_invert_set(channel, 0)
            ws.ws2811_channel_t_brightness_set(channel, 0)

        channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)

        # Initialize the channel in use
        ws.ws2811_channel_t_count_set(channel, math.ceil(len(buf)/3)) # we manage 4 vs 3 bytes in the library
        ws.ws2811_channel_t_gpionum_set(channel, gpio._pin.id)
        ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
        ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)
        ws.ws2811_channel_t_strip_type_set(channel, LED_STRIP)

        # Initialize the controller
        ws.ws2811_t_freq_set(_led_strip, LED_FREQ_HZ)
        ws.ws2811_t_dmanum_set(_led_strip, LED_DMA_NUM)
    
        resp = ws.ws2811_init(_led_strip)
        if resp != ws.WS2811_SUCCESS:
            message = ws.ws2811_get_return_t_str(resp)
            raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, message))
        atexit.register(neopixel_cleanup)

    channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
    if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
        raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")

    # assign all colors!
    for i in range(len(buf) // 3):
        r = buf[3*i]
        g = buf[3*i+1]
        b = buf[3*i+2]
        pixel = (r << 16) | (g << 8) | b
        ws.ws2811_led_set(channel, i, pixel)
    
    resp = ws.ws2811_render(_led_strip)
    if resp != ws.WS2811_SUCCESS:
        message = ws.ws2811_get_return_t_str(resp)
        raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, message))
    time.sleep(0.001 * ((len(buf)//100)+1))  # about 1ms per 100 bytes
Exemplo n.º 10
0
    def __init__(
            self,
            num,
            pin,
            freq_hz=800000,
            dma=10,
            invert=(False, False),
            brightness=(255, 255),
            available_watts=(5.0, 5.0),
            strip_type=(ws.WS2811_STRIP_RGB, ws.WS2811_STRIP_RGB),
    ):
        """Class to represent a NeoPixel/WS281x LED display.  Num should be the
        number of pixels in the display, and pin should be the GPIO pin connected
        to the display signal line (must be a PWM pin like 18!).  Optional
        parameters are freq, the frequency of the display signal in hertz (default
        800khz), dma, the DMA channel to use (default 10), invert, a boolean
        specifying if the signal line should be inverted (default False), and
        channel, the PWM channel to use (defaults to 0).
        """
        # Create ws2811_t structure and fill in parameters.
        self._leds = ws.new_ws2811_t()

        # Initialize the channels to zero
        for channum in range(2):
            chan = ws.ws2811_channel_get(self._leds, channum)
            ws.ws2811_channel_t_count_set(chan, num[channum])
            ws.ws2811_channel_t_gpionum_set(chan, pin[channum])
            ws.ws2811_channel_t_invert_set(chan,
                                           0 if not invert[channum] else 1)
            ws.ws2811_channel_t_brightness_set(chan, brightness[channum])
            ws.ws2811_channel_t_strip_type_set(chan, strip_type[channum])
            ws.ws2811_channel_t_available_watts_set(chan,
                                                    available_watts[channum])

        self._channel = [
            ws.ws2811_channel_get(self._leds, 0),
            ws.ws2811_channel_get(self._leds, 1),
        ]

        # Initialize the controller
        ws.ws2811_t_freq_set(self._leds, freq_hz)
        ws.ws2811_t_dmanum_set(self._leds, dma)

        # Grab the led data array.
        self._led_data = [
            _LED_Data(self._leds, 0, self._channel[0], num[0]),
            _LED_Data(self._leds, 1, self._channel[1], num[1]),
        ]

        # Substitute for __del__, traps an exit condition and cleans up properly
        atexit.register(self._cleanup)
Exemplo n.º 11
0
 def channel_off(self):
     # Initialize all channels to off
     for channum in range(2):
         channel = ws.ws2811_channel_get(self.leds, channum)
         ws.ws2811_channel_t_count_set(channel, 0)
         ws.ws2811_channel_t_gpionum_set(channel, 0)
         ws.ws2811_channel_t_invert_set(channel, 0)
         ws.ws2811_channel_t_brightness_set(channel, 0)
    def __init__(self, device):

        # Call the constructor of the base class.
        super(OutputRaspi, self).__init__(device)

        import _rpi_ws281x as ws  # pylint: disable=import-error

        output_id = "output_raspi"

        # LED strip configuration:
        self._led_count = int(
            self._device_config["LED_Count"])  # Number of LED pixels.
        self._led_pin = int(
            self._device_config["output"][output_id]
            ["LED_Pin"])  # GPIO pin connected to the pixels (18 uses PWM!).
        self._led_freq_hz = int(
            self._device_config["output"][output_id]
            ["LED_Freq_Hz"])  # LED signal frequency in hertz (usually 800khz)
        self._led_dma = int(
            self._device_config["output"][output_id]
            ["LED_Dma"])  # DMA channel to use for generating signal (try 10)
        self._led_brightness = int(
            self._device_config["output"][output_id]
            ["LED_Brightness"])  # Set to 0 for darkest and 100 for brightest
        self._led_invert = int(
            self._device_config["output"][output_id]["LED_Invert"]
        )  # True to invert the signal (when using NPN transistor level shift)
        self._led_channel = int(
            self._device_config["output"][output_id]
            ["LED_Channel"])  # set to '1' for GPIOs 13, 19, 41, 45 or 53

        self._led_brightness_translated = int(255 *
                                              (self._led_brightness / 100))

        print("LED Brightness: " + str(self._led_brightness))
        print("LED Brightness Translated: " +
              str(self._led_brightness_translated))

        self._leds = ws.new_ws2811_t()

        self.channel = ws.ws2811_channel_get(self._leds, 0)

        ws.ws2811_channel_t_count_set(self.channel, self._led_count)
        ws.ws2811_channel_t_gpionum_set(self.channel, self._led_pin)
        ws.ws2811_channel_t_invert_set(self.channel, self._led_invert)
        ws.ws2811_channel_t_brightness_set(self.channel,
                                           self._led_brightness_translated)

        ws.ws2811_t_freq_set(self._leds, self._led_freq_hz)
        ws.ws2811_t_dmanum_set(self._leds, self._led_dma)

        # Initialize library with LED configuration.
        resp = ws.ws2811_init(self._leds)
        if resp != ws.WS2811_SUCCESS:
            message = ws.ws2811_get_return_t_str(resp)
            raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(
                resp, message))
Exemplo n.º 13
0
    def start(self, delegate, style):
        print("Setting up LED environment...")
        try:
            self.LED_BRIGHTNESS = style['brightness'] or 64
            self.LED_COUNT = style['led_count'] or 630
            self.leds = ws.new_ws2811_t()
            clean_up = style['cleanup'] == 1

            # Initialize all channels to off
            for channum in range(2):
                self.channel = ws.ws2811_channel_get(self.leds, channum)
                ws.ws2811_channel_t_count_set(self.channel, 0)
                ws.ws2811_channel_t_gpionum_set(self.channel, 0)
                ws.ws2811_channel_t_invert_set(self.channel, 0)
                ws.ws2811_channel_t_brightness_set(self.channel, 0)

            self.channel = ws.ws2811_channel_get(self.leds, self.LED_CHANNEL)

            ws.ws2811_channel_t_count_set(self.channel, self.LED_COUNT)
            ws.ws2811_channel_t_gpionum_set(self.channel, self.LED_GPIO)
            ws.ws2811_channel_t_invert_set(self.channel, self.LED_INVERT)
            ws.ws2811_channel_t_brightness_set(
                self.channel, self.LED_BRIGHTNESS)

            ws.ws2811_t_freq_set(self.leds, self.LED_FREQ_HZ)
            ws.ws2811_t_dmanum_set(self.leds, self.LED_DMA_NUM)

            # Initialize library with LED configuration.
            resp = ws.ws2811_init(self.leds)
            if resp != 0:
                raise RuntimeError(
                    'ws2811_init failed with code {0}'.format(resp))

            print(
                "Starting {0} via {1}...".format(
                    style['style_name'], style['method_name'])
            )
            delegate(style)

        finally:
            if clean_up:
                self.cleanup()
            ws.ws2811_fini(self.leds)
            ws.delete_ws2811_t(self.leds)
Exemplo n.º 14
0
    def start(self, delegate, style):
        print("Setting up LED environment...")
        try:
            self.LED_BRIGHTNESS = style['brightness'] or 64
            self.LED_COUNT = style['led_count'] or 630
            self.leds = ws.new_ws2811_t()
            clean_up = style['cleanup'] == 1

            # Initialize all channels to off
            for channum in range(2):
                self.channel = ws.ws2811_channel_get(self.leds, channum)
                ws.ws2811_channel_t_count_set(self.channel, 0)
                ws.ws2811_channel_t_gpionum_set(self.channel, 0)
                ws.ws2811_channel_t_invert_set(self.channel, 0)
                ws.ws2811_channel_t_brightness_set(self.channel, 0)

            self.channel = ws.ws2811_channel_get(self.leds, self.LED_CHANNEL)

            ws.ws2811_channel_t_count_set(self.channel, self.LED_COUNT)
            ws.ws2811_channel_t_gpionum_set(self.channel, self.LED_GPIO)
            ws.ws2811_channel_t_invert_set(self.channel, self.LED_INVERT)
            ws.ws2811_channel_t_brightness_set(self.channel,
                                               self.LED_BRIGHTNESS)

            ws.ws2811_t_freq_set(self.leds, self.LED_FREQ_HZ)
            ws.ws2811_t_dmanum_set(self.leds, self.LED_DMA_NUM)

            # Initialize library with LED configuration.
            resp = ws.ws2811_init(self.leds)
            if resp != 0:
                raise RuntimeError(
                    'ws2811_init failed with code {0}'.format(resp))

            print("Starting {0} via {1}...".format(style['style_name'],
                                                   style['method_name']))
            delegate(style)

        finally:
            if clean_up:
                self.cleanup()
            ws.ws2811_fini(self.leds)
            ws.delete_ws2811_t(self.leds)
Exemplo n.º 15
0
def InitializeNeopixel():
    # Initialize all channels to off
    for channum in range(2):
        channel = ws.ws2811_channel_get(leds, channum)
        ws.ws2811_channel_t_count_set(channel, 0)
        ws.ws2811_channel_t_gpionum_set(channel, 0)
        ws.ws2811_channel_t_invert_set(channel, 0)
        ws.ws2811_channel_t_brightness_set(channel, 0)
	
    channel = ws.ws2811_channel_get(leds, LED_CHANNEL)
	
    ws.ws2811_channel_t_count_set(channel, LED_COUNT)
    ws.ws2811_channel_t_gpionum_set(channel, LED_PIN)
    ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
    ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)

    ws.ws2811_t_freq_set(leds, LED_FREQ_HZ)
    ws.ws2811_t_dmanum_set(leds, LED_DMA)
	
    # Initialize library with LED configuration.
    ws.ws2811_init(leds)
    return channel
Exemplo n.º 16
0
	def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False,
			brightness=255, channel=0, strip_type=ws.WS2811_STRIP_RGB):
		"""Class to represent a NeoPixel/WS281x LED display.  Num should be the
		number of pixels in the display, and pin should be the GPIO pin connected
		to the display signal line (must be a PWM pin like 18!).  Optional
		parameters are freq, the frequency of the display signal in hertz (default
		800khz), dma, the DMA channel to use (default 10), invert, a boolean
		specifying if the signal line should be inverted (default False), and
		channel, the PWM channel to use (defaults to 0).
		"""
		# Create ws2811_t structure and fill in parameters.
		self._leds = ws.new_ws2811_t()

		# Initialize the channels to zero
		for channum in range(2):
			chan = ws.ws2811_channel_get(self._leds, channum)
			ws.ws2811_channel_t_count_set(chan, 0)
			ws.ws2811_channel_t_gpionum_set(chan, 0)
			ws.ws2811_channel_t_invert_set(chan, 0)
			ws.ws2811_channel_t_brightness_set(chan, 0)

		# Initialize the channel in use
		self._channel = ws.ws2811_channel_get(self._leds, channel)
		ws.ws2811_channel_t_count_set(self._channel, num)
		ws.ws2811_channel_t_gpionum_set(self._channel, pin)
		ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1)
		ws.ws2811_channel_t_brightness_set(self._channel, brightness)
		ws.ws2811_channel_t_strip_type_set(self._channel, strip_type)

		# Initialize the controller
		ws.ws2811_t_freq_set(self._leds, freq_hz)
		ws.ws2811_t_dmanum_set(self._leds, dma)

		# Grab the led data array.
		self._led_data = _LED_Data(self._channel, num)

		# Substitute for __del__, traps an exit condition and cleans up properly
		atexit.register(self._cleanup)
Exemplo n.º 17
0
    def init(self):
        self.channel_off()
        self.channel = ws.ws2811_channel_get(self.leds, self.led_channel)
        ws.ws2811_channel_t_count_set(self.channel, self.height * self.width)
        ws.ws2811_channel_t_gpionum_set(self.channel, self.gpio)
        ws.ws2811_channel_t_invert_set(self.channel, self.invert)
        ws.ws2811_channel_t_brightness_set(self.channel, self.brightness)

        ws.ws2811_t_freq_set(self.leds, self.freq_hz)
        ws.ws2811_t_dmanum_set(self.leds, self.dma_num)

        # Initialize library with LED configuration.
        resp = ws.ws2811_init(self.leds)
        if resp != ws.WS2811_SUCCESS:
            message = ws.ws2811_get_return_t_str(resp)
            raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(
                resp, message))
        print("init success")
Exemplo n.º 18
0
 def set_leds(self, status):
     """ Set the color according to the humidity threshold
     In the optimal state when no threshold is reached the
     led is turned off.
     @param status: An integer array with 4 integers between 0 and 4
     @type status: int[4]
     @return: None
     """
     leds = ws.new_ws2811_t()
     channel = ws.ws2811_channel_get(leds, self.get_channel())
     ws.ws2811_channel_t_count_set(channel, self.get_count())
     ws.ws2811_channel_t_gpionum_set(channel, self.get_gpio())
     ws.ws2811_channel_t_invert_set(channel, self.get_invert())
     ws.ws2811_channel_t_brightness_set(channel, self.get_brightness())
     ws.ws2811_t_freq_set(leds, self.get_freq())
     ws.ws2811_t_dmanum_set(leds, self.get_dma())
     try:
         resp = ws.ws2811_init(leds)
     except Exception as err:
         raise RuntimeError('ws2811_init failed with code {0} ({1}) {}', resp, err)
     finally:
         if resp != ws.WS2811_SUCCESS:
             ws.ws2811_get_return_t_str(resp)
     try:
         for i in range(self.get_count()):
             ws.ws2811_led_set(channel, i, self.get_color(status[i]))
         resp = ws.ws2811_render(leds)
         if resp != ws.WS2811_SUCCESS:
             ws.ws2811_get_return_t_str(resp)
         time.sleep(0.015)
     except Exception as err:
         raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, err))
     finally:
         logging.getLogger().info("LED status:\t\t\t{}".format(status))
         ws.ws2811_fini(leds)
         ws.delete_ws2811_t(leds)
Exemplo n.º 19
0
 def set_available_watts(self, watts, channel=0):
     chan = ws.ws2811_channel_get(self._leds, channel)
     ws.ws2811_channel_t_available_watts_set(chan, watts)
     self.show()
Exemplo n.º 20
0
    0x202000,  # yellow
    0x002000,  # green
    0x002020,  # lightblue
    0x000020,  # blue
    0x100010,  # purple
    0x200010
]  # pink

# Create a ws2811_t structure from the LED configuration.
# Note that this structure will be created on the heap so you need to be careful
# that you delete its memory by calling delete_ws2811_t when it's not needed.
leds = ws.new_ws2811_t()

# Initialize all channels to off
for channum in range(2):
    channel = ws.ws2811_channel_get(leds, channum)
    ws.ws2811_channel_t_count_set(channel, 0)
    ws.ws2811_channel_t_gpionum_set(channel, 0)
    ws.ws2811_channel_t_invert_set(channel, 0)

channel = ws.ws2811_channel_get(leds, LED_CHANNEL)

ws.ws2811_channel_t_count_set(channel, LED_COUNT)
ws.ws2811_channel_t_gpionum_set(channel, LED_GPIO)
ws.ws2811_channel_t_invert_set(channel, LED_INVERT)

ws.ws2811_t_freq_set(leds, LED_FREQ_HZ)
ws.ws2811_t_dmanum_set(leds, LED_DMA_NUM)

# Initialize library with LED configuration.
resp = ws.ws2811_init(leds)
Exemplo n.º 21
0
def ws2811_channel_get(*args):
    return _rpi_ws281x.ws2811_channel_get(*args)
Exemplo n.º 22
0
    def __init__(self, device):
        # Call the constructor of the base class.
        super(OutputRaspi, self).__init__(device)
        self.logger = logging.getLogger(__name__)

        import _rpi_ws281x as ws  # pylint: disable=import-error

        output_id = "output_raspi"

        # LED strip configuration:
        self._led_count = int(
            self._device_config["led_count"])  # Number of LED pixels.
        self._led_pin = int(
            self._device_config["output"][output_id]
            ["led_pin"])  # GPIO pin connected to the pixels (18 uses PWM!).
        self._led_freq_hz = int(
            self._device_config["output"][output_id]
            ["led_freq_hz"])  # LED signal frequency in hertz (usually 800khz).
        self._led_dma = int(
            self._device_config["output"][output_id]
            ["led_dma"])  # DMA channel to use for generating signal (try 10).
        self._led_brightness = int(
            self._device_config["led_brightness"]
        )  # Set to '0' for darkest and 100 for brightest.
        self._led_invert = int(
            self._device_config["output"][output_id]["led_invert"]
        )  # Set to 'True' to invert the signal (when using NPN transistor level shift).
        self._led_channel = int(
            self._device_config["output"][output_id]
            ["led_channel"])  # set to '1' for GPIOs 13, 19, 41, 45 or 53.
        self._led_strip = self._device_config["led_strip"]

        # Set Fallback Strip
        self._led_strip_translated = ws.WS2811_STRIP_RGB

        self._led_strip_mapper = {
            "sk6812_strip_rgbw": ws.SK6812_STRIP_RGBW,
            "sk6812_strip_rbgw": ws.SK6812_STRIP_RBGW,
            "sk6812_strip_grbw": ws.SK6812_STRIP_GRBW,
            "sk6812_strip_gbrw": ws.SK6812_STRIP_GBRW,
            "sk6812_strip_brgw": ws.SK6812_STRIP_BRGW,
            "sk6812_strip_bgrw": ws.SK6812_STRIP_BGRW,
            "sk6812_shift_wmask": ws.SK6812_SHIFT_WMASK,
            "ws2811_strip_rgb": ws.WS2811_STRIP_RGB,
            "ws2811_strip_rbg": ws.WS2811_STRIP_RBG,
            "ws2811_strip_grb": ws.WS2811_STRIP_GRB,
            "ws2811_strip_gbr": ws.WS2811_STRIP_GBR,
            "ws2811_strip_brg": ws.WS2811_STRIP_BRG,
            "ws2811_strip_bgr": ws.WS2811_STRIP_BGR,
            "ws2812_strip": ws.WS2812_STRIP,
            "sk6812_strip": ws.SK6812_STRIP,
            "sk6812w_strip": ws.SK6812W_STRIP
        }

        try:
            led_strip = self._led_strip_mapper[self._led_strip]
            if led_strip is not None:
                self._led_strip_translated = led_strip
                self.logger.debug(f"Found Led Strip {self._led_strip}")
        except Exception as e:
            self.logger.exception(
                f"Could not find LED Strip Type. Exception: {str(e)}")
            pass

        self._led_brightness_translated = int(255 *
                                              (self._led_brightness / 100))

        self.logger.debug(f"LED Brightness: {self._led_brightness}")
        self.logger.debug(
            f"LED Brightness converted: {self._led_brightness_translated}")

        self._leds = ws.new_ws2811_t()

        self.channel = ws.ws2811_channel_get(self._leds, self._led_channel)

        ws.ws2811_channel_t_strip_type_set(self.channel,
                                           self._led_strip_translated)
        ws.ws2811_channel_t_count_set(self.channel, self._led_count)
        ws.ws2811_channel_t_gpionum_set(self.channel, self._led_pin)
        ws.ws2811_channel_t_invert_set(self.channel, self._led_invert)
        ws.ws2811_channel_t_brightness_set(self.channel,
                                           self._led_brightness_translated)

        ws.ws2811_t_freq_set(self._leds, self._led_freq_hz)
        ws.ws2811_t_dmanum_set(self._leds, self._led_dma)

        # Initialize library with LED configuration.
        resp = ws.ws2811_init(self._leds)
        if resp != ws.WS2811_SUCCESS:
            message = ws.ws2811_get_return_t_str(resp)
            raise RuntimeError(
                f'ws2811_init failed with code {resp} ({message})')
Exemplo n.º 23
0
def ws2811_channel_get(ws, channelnum):
    return _rpi_ws281x.ws2811_channel_get(ws, channelnum)
Exemplo n.º 24
0
				0x202000,   # yellow
				0x002000,   # green
				0x002020,   # lightblue
				0x000020,   # blue
				0x100010,   # purple
				0x200010 ]  # pink


# Create a ws2811_t structure from the LED configuration.
# Note that this structure will be created on the heap so you need to be careful
# that you delete its memory by calling delete_ws2811_t when it's not needed.
leds = ws.new_ws2811_t()

# Initialize all channels to off
for channum in range(2):
    channel = ws.ws2811_channel_get(leds, channum)
    ws.ws2811_channel_t_count_set(channel, 0)
    ws.ws2811_channel_t_gpionum_set(channel, 0)
    ws.ws2811_channel_t_invert_set(channel, 0)
    ws.ws2811_channel_t_brightness_set(channel, 0)

channel = ws.ws2811_channel_get(leds, LED_CHANNEL)

ws.ws2811_channel_t_count_set(channel, LED_COUNT)
ws.ws2811_channel_t_gpionum_set(channel, LED_GPIO)
ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)

ws.ws2811_t_freq_set(leds, LED_FREQ_HZ)
ws.ws2811_t_dmanum_set(leds, LED_DMA_NUM)
Exemplo n.º 25
0
def neopixel_write(gpio, buf):
    """NeoPixel Writing Function"""
    global _led_strip  # we'll have one strip we init if its not at first

    if _led_strip is None:
        # Create a ws2811_t structure from the LED configuration.
        # Note that this structure will be created on the heap so you
        # need to be careful that you delete its memory by calling
        # delete_ws2811_t when it's not needed.
        _led_strip = ws.new_ws2811_t()

        # Initialize all channels to off
        for channum in range(2):
            channel = ws.ws2811_channel_get(_led_strip, channum)
            ws.ws2811_channel_t_count_set(channel, 0)
            ws.ws2811_channel_t_gpionum_set(channel, 0)
            ws.ws2811_channel_t_invert_set(channel, 0)
            ws.ws2811_channel_t_brightness_set(channel, 0)

        channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)

        # Initialize the channel in use
        count = 0
        if len(buf) % 3 == 0:
            # most common, divisible by 3 is likely RGB
            LED_STRIP = ws.WS2811_STRIP_RGB
            count = len(buf) // 3
        elif len(buf) % 4 == 0:
            LED_STRIP = ws.SK6812_STRIP_RGBW
            count = len(buf) // 4
        else:
            raise RuntimeError("We only support 3 or 4 bytes-per-pixel")

        ws.ws2811_channel_t_count_set(
            channel, count
        )  # we manage 4 vs 3 bytes in the library
        ws.ws2811_channel_t_gpionum_set(channel, gpio._pin.id)
        ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
        ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)
        ws.ws2811_channel_t_strip_type_set(channel, LED_STRIP)

        # Initialize the controller
        ws.ws2811_t_freq_set(_led_strip, LED_FREQ_HZ)
        ws.ws2811_t_dmanum_set(_led_strip, LED_DMA_NUM)

        resp = ws.ws2811_init(_led_strip)
        if resp != ws.WS2811_SUCCESS:
            if resp == -5:
                raise RuntimeError(
                    "NeoPixel support requires running with sudo, please try again!"
                )
            message = ws.ws2811_get_return_t_str(resp)
            raise RuntimeError(
                "ws2811_init failed with code {0} ({1})".format(resp, message)
            )
        atexit.register(neopixel_cleanup)

    channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
    if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
        raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")

    if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB:
        bpp = 3
    else:
        bpp = 4
    # assign all colors!
    for i in range(len(buf) // bpp):
        r = buf[bpp * i]
        g = buf[bpp * i + 1]
        b = buf[bpp * i + 2]
        if bpp == 3:
            pixel = (r << 16) | (g << 8) | b
        else:
            w = buf[bpp * i + 3]
            pixel = (w << 24) | (r << 16) | (g << 8) | b
        ws.ws2811_led_set(channel, i, pixel)

    resp = ws.ws2811_render(_led_strip)
    if resp != ws.WS2811_SUCCESS:
        message = ws.ws2811_get_return_t_str(resp)
        raise RuntimeError(
            "ws2811_render failed with code {0} ({1})".format(resp, message)
        )
    time.sleep(0.001 * ((len(buf) // 100) + 1))  # about 1ms per 100 bytes
Exemplo n.º 26
0
def ws2811_channel_get(*args):
  return _rpi_ws281x.ws2811_channel_get(*args)
Exemplo n.º 27
0
#!/usr/bin/env python3

import _rpi_ws281x as ws
leds = ws.new_ws2811_t()

# Initialize all channels to off
for channum in range(2):
    channel = ws.ws2811_channel_get(leds, channum)
    ws.ws2811_channel_t_count_set(channel, 0)
    ws.ws2811_channel_t_gpionum_set(channel, 0)
    ws.ws2811_channel_t_invert_set(channel, 0)
    ws.ws2811_channel_t_brightness_set(channel, 0)

channel = ws.ws2811_channel_get(leds, 0)

ws.ws2811_channel_t_count_set(channel, 2)
ws.ws2811_channel_t_gpionum_set(channel, 18)
ws.ws2811_channel_t_invert_set(channel, 0)
ws.ws2811_channel_t_brightness_set(channel, 128)

ws.ws2811_t_freq_set(leds, 800000)
ws.ws2811_t_dmanum_set(leds, 10)

# Initialize library with LED configuration.
resp = ws.ws2811_init(leds)
if resp != 0:
    raise RuntimeError('ws2811_init failed with code {0}'.format(resp))

while True:
    ws.ws2811_led_set(channel, 0, 0x202020)
    ws.ws2811_led_set(channel, 1, 0x202020)