0

replacing colorwheel
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

replacing colorwheel

by Rcayot on Fri Jan 08, 2021 1:27 pm

In Arduino FastLED library they have two colorspaces called rainbow and spectrum see:
https://github.com/FastLED/FastLED/wiki/FastLED-HSV-Colors

Anyway, the colorwheel function in adafruit_led_animations library uses as far as I can tell something that reesults in the colorspace that would be similar if not identical to the spetrum colorspace in FastLED.

I have successfully reproduced the FastLED rainbow colorspace in CP. I am using a FancyLED library pallet function.

Ok. If I replace the colorwheel function in the color.py library for adafruit_led_animations library with this modified colorwheel function, the first thing I discovered was that colorwheel is loaded from _pixelbuf. I comment out that portion of color.py and then it tries to load my colorwheel function but results in the following error:

Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "code.py", line 93, in <module>
  File "adafruit_led_animation/animation/rainbowsparkle.py", line 83, in __init__
  File "adafruit_led_animation/animation/rainbow.py", line 77, in __init__
  File "adafruit_led_animation/animation/rainbowsparkle.py", line 93, in generate_rainbow
  File "adafruit_led_animation/animation/rainbow.py", line 84, in generate_rainbow
MemoryError: memory allocation failed, allocating 1024 bytes


What I think is happening is that the color that fancy.CRGB returns is not in the proper format, I think it is in a float tuple. The adafruit_led_animation libraries I believe returns an integer tuple.

Is there a way to convert the float tuple to an integer tuple that the adafruit_led_animations libraries will be able top use?

My color.py code is below, and my dpprogram to use it below that.

Code: Select all | TOGGLE FULL SIZE
# The MIT License (MIT)
#
# Copyright (c) 2019 Kattni Rembor for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_led_animation.color`
================================================================================
Color variables assigned to RGB values made available for import.
* Author(s): Kattni Rembor
Implementation Notes
--------------------
**Hardware:**
* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
* `Adafruit DotStars <https://www.adafruit.com/category/885>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
  https://circuitpython.org/downloads
"""
import adafruit_fancyled.adafruit_fancyled as fancy
pos = 0
RED = (255, 0, 0)
"""Red."""
YELLOW = (170, 170, 0)
"""Yellow."""
ORANGE = (170, 85, 0)
"""Orange."""
GREEN = (0, 255, 0)
"""Green."""
TEAL = (0, 255, 120)
"""Teal."""
CYAN = (0, 170, 85)
"""Cyan."""
BLUE = (0, 0, 255)
"""Blue."""
PURPLE = (85, 0, 170)
"""Purple."""
MAGENTA = (255, 0, 20)
"""Magenta."""
WHITE = (255, 255, 255)
"""White."""
BLACK = (0, 0, 0)
"""Black or off."""

GOLD = (255, 222, 30)
"""Gold."""
PINK = (170, 0, 85)
"""Pink."""
AQUA = (50, 255, 255)
"""Aqua."""
JADE = (0, 255, 40)
"""Jade."""
AMBER = (255, 100, 0)
"""Amber."""
OLD_LACE = (253, 245, 230)  # Warm white.
"""Old lace or warm white."""

RGBW_WHITE_RGB = (255, 255, 255, 0)
"""RGBW_WHITE_RGB is for RGBW strips to illuminate only the RGB diodes."""
RGBW_WHITE_W = (0, 0, 0, 255)
"""RGBW_WHITE_W is for RGBW strips to illuminate only White diode."""
RGBW_WHITE_RGBW = (255, 255, 255, 255)
"""RGBW_WHITE_RGBW is for RGBW strips to illuminate the RGB and White diodes."""

RAINBOW = (RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, PINK)
"""RAINBOW is a list of colors to use for cycling through.
Includes, in order: red, orange, yellow, green, blue, and purple."""

# try:
    #  try:
        # Backwards compatibility for 5.3.0 and prior
    #    from _pixelbuf import colorwheel  # pylint: disable=unused-import
    # except ImportError:
    #    from _pixelbuf import wheel as colorwheel  # pylint: disable=unused-import
    #  except ImportError:

def colorwheel(pos):
    palette = [fancy.CRGB(255, 0, 0),   # red
        fancy.CRGB(170, 85, 0),   # orange
        fancy.CRGB(170, 170, 0),   # yellow
        fancy.CRGB(0, 255, 0),   # green
        fancy.CRGB(0, 170, 85),   # cyan
        fancy.CRGB(0, 0, 255),   # blue
        fancy.CRGB(85, 0, 170), # purple
        fancy.CRGB(170, 0, 85)]   # pink
    color = fancy.palette_lookup(palette, pos)
    return (color)

def calculate_intensity(color, intensity=1.0):
    """
    Takes a RGB[W] color tuple and adjusts the intensity.
    :param float intensity:
    :param color: color value (tuple, list or int)
    :return (color
    """
    # Note: This code intentionally avoids list comprehensions and intermediate variables
    # for an approximately 2x performance gain.
    if isinstance(color, int):
        return (
            (int((color & 0xFF0000) * intensity) & 0xFF0000)
            | (int((color & 0xFF00) * intensity) & 0xFF00)
            | (int((color & 0xFF) * intensity) & 0xFF)
        )

    if len(color) == 3:
        return (
            int(color[0] * intensity),
            int(color[1] * intensity),
            int(color[2] * intensity),
        )
    if len(color) == 4 and isinstance(color[3], float):
        return (
            int(color[0] * intensity),
            int(color[1] * intensity),
            int(color[2] * intensity),
            color[3],
        )
    return (
        int(color[0] * intensity),
        int(color[1] * intensity),
        int(color[2] * intensity),
        int(color[3] * intensity),
    )



here is what I would like to be able to use my rainbow spectrum:

Code: Select all | TOGGLE FULL SIZE
import board
import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy
import time
import random


from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket

from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.group import AnimationGroup
from adafruit_led_animation.helper import PixelSubset

ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)

buttonpressed = 0
num_leds = 16
i = 0.0
hue = 0.0
offset = 0
pixels = neopixel.NeoPixel(board.A0, num_leds, brightness=0.50,
                           auto_write=False)
pixels1 = PixelSubset(pixels, 0, 8)
pixels2 = PixelSubset(pixels, 8, 16)

rainbow_comet1 = RainbowComet(pixels1, speed=0.051, tail_length=8, bounce=True)
rainbow_comet2 = RainbowComet(pixels2, speed=0.051, tail_length=8, bounce=True)
rainbow_comet3 = RainbowComet(pixels1, speed=0.051, tail_length=8, ring=True, reverse=True)
rainbow_comet4 = RainbowComet(pixels2, speed=0.051, tail_length=8, ring=True)
animation2 = AnimationSequence(AnimationGroup(rainbow_comet1, rainbow_comet2), AnimationGroup(rainbow_comet3, rainbow_comet4), auto_clear=True,
advance_interval=10)
def rainbowcomet():
    while True:
        animation2.animate()
        if uart.in_waiting:
            packet = Packet.from_stream(uart)
            if packet.pressed:
                return packet.button


rainbow_chaseF = RainbowChase(pixels, speed=0.1, size=3, spacing=2, step=16)
rainbow_chaseR = RainbowChase(pixels, speed=0.1, size=3, spacing=2, step=16, reverse=True)
chase_animations = AnimationSequence(rainbow_chaseF, rainbow_chaseR, auto_clear=True, advance_interval=5)
def rainbow_chaser():
    while True:
        chase_animations.animate()
        if uart.in_waiting:
            packet = Packet.from_stream(uart)
            if packet.pressed:
                return packet.button

def rainbow():
    palette = [fancy.CRGB(255, 0, 0),   # red
            fancy.CRGB(170, 85, 0),   # orange
            fancy.CRGB(170, 170, 0),   # yellow
            fancy.CRGB(0, 255, 0),   # green
            fancy.CRGB(0, 170, 85),   # cyan
            fancy.CRGB(0, 0, 255),   # blue
            fancy.CRGB(85, 0, 170), # purple
            fancy.CRGB(170, 0, 85)]   # pink
    offset = 0.0  # Position offset into palette to make it "spin"
    i = 0
    initial = time.monotonic()
    while buttonpressed == 3:
        now = time.monotonic()
        if now - initial > .02:
            for i in range(len(pixels)):
                color = fancy.palette_lookup(palette, (offset + i / (num_leds)))
                pixels[i] = color.pack()
                offset += 0.001953125  # Bigger number = faster spin
            pixels.show()
        initial = now
        if uart.in_waiting:
            packet = Packet.from_stream(uart)
            if packet.pressed:
                return packet.button

rainbowsparkle = RainbowSparkle(pixels, speed=0.05, period=3, num_sparkles=3, background_brightness=0.1)
sparkle_animations = AnimationSequence(rainbowsparkle, auto_clear=True,)

def sparkles():
    while True:
        sparkle_animations.animate()
        if uart.in_waiting:
            packet = Packet.from_stream(uart)
            if packet.pressed:
                return packet.button

while True:
    ble.start_advertising(advertisement)
    while not ble.connected:
        pass
# Now we're connected
    while ble.connected:
        try:
            packet = Packet.from_stream(uart)
        except ValueError:
            continue

        if isinstance(packet, ButtonPacket):
            if packet.button == ButtonPacket.BUTTON_1:
                buttonpressed = 1
                print(buttonpressed)
                rainbowcomet()
            if packet.button == ButtonPacket.BUTTON_2:
                buttonpressed = 2
                print(buttonpressed)
                rainbow_chaser()
            if packet.button == ButtonPacket.BUTTON_3:
                buttonpressed = 3
                print(buttonpressed)
                rainbow()
            if packet.button == ButtonPacket.BUTTON_4:
                buttonpressed = 4
                print(buttonpressed)
                sparkles()

Rcayot
 
Posts: 99
Joined: Sat Feb 08, 2020 6:48 pm

Re: replacing colorwheel

by Rcayot on Fri Jan 08, 2021 1:28 pm

by the way, my post was rejected due to the use of a banned spam word, which was included din the Adafruit MIT license text!

Roger

Rcayot
 
Posts: 99
Joined: Sat Feb 08, 2020 6:48 pm

Please be positive and constructive with your questions and comments.