def simple_walk(delay=0.05): global walk_index, offset trail_length = 4 color = fancy.palette_lookup(palette, offset) oppcol = fancy.palette_lookup(palette, offset+0.5) for i in range(trail_length): index = (walk_index + i) % num_leds index2 = (index + 10) % num_leds opp_index = opposite(index) opp_index2 = opposite(index2) bright = 1.0/(trail_length - i) pixels[index] = fancy.gamma_adjust(color, brightness = bright).pack() pixels[opp_index] = pixels[index] pixels[index2] = fancy.gamma_adjust(oppcol, brightness = bright).pack() pixels[opp_index2] = pixels[index2] pixels.show() time.sleep(delay) pixels[walk_index] = (0,0,0) pixels[opposite(walk_index)] = (0,0,0) index2 = (walk_index + 10) % num_leds pixels[index2] = (0,0,0) pixels[opposite(index2)] = (0,0,0) walk_index = (walk_index + 1) % num_leds offset = offset + .02
def mode_0(self): """ all on, one red, one yellow, one blue, one green""" self.neopixels[3] = fancy.gamma_adjust(TrainLantern.RED).pack() self.neopixels[0] = fancy.gamma_adjust(TrainLantern.YELLOW).pack() self.neopixels[1] = fancy.gamma_adjust(TrainLantern.BLUE).pack() self.neopixels[2] = fancy.gamma_adjust(TrainLantern.GREEN).pack() self.neopixels.show()
def rain(): wait = 0.08 nSteps = 100 p_new = 0.3 global hue drops_old = [] drops_new = [] for i in range(nSteps): for drop in drops_old: # update drop positions i = XYZ(drop[0], drop[1], drop[2]) pixels[i] = (0, 0, 0) # remove old dot if drop[2] > 0: drop[2] -= 1 drops_new.append([drop[0], drop[1], drop[2]]) # update list i = XYZ(drop[0], drop[1], drop[2]) color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels[i] = color.pack() if random.random() > p_new: # generate new drop x = random.randint(0, 3) y = random.randint(0, 3) drops_new.append([x, y, 3]) i = XYZ(x, y, 3) color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels[i] = color.pack() drops_old = drops_new.copy() drops_new.clear() pixels.show() time.sleep(wait) hue += 1
def jazzy(): for i in range(TREE_LEDS): color = fancy.palette_lookup(fairy_palette, (offset - i) / 4.8) color = fancy.gamma_adjust(color, brightness=0.3) tree[i] = color.pack() tree.show() for i in range(CPX_LEDS): color = fancy.palette_lookup(fairy_palette, (offset + i) / 4) color = fancy.gamma_adjust(color, brightness=0.3) cpx[i] = color.pack() cpx.show()
def latkes(): for i in range(TREE_LEDS): color = fancy.palette_lookup(hanukkah_palette, (offset - 24) / TREE_LEDS) color = fancy.gamma_adjust(color, brightness=0.3) tree[i] = color.pack() tree.show() for i in range(CPX_LEDS): color = fancy.palette_lookup(hanukkah_palette, (offset - 20) / CPX_LEDS) color = fancy.gamma_adjust(color, brightness=0.3) cpx[i] = color.pack() cpx.show()
def fancy_swirl(): for i in range(TREE_LEDS): color = fancy.palette_lookup(winter_palette, (offset + i) / TREE_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) tree[i] = color.pack() tree.show() for i in range(CPX_LEDS): color = fancy.palette_lookup(star_palette, (offset - i) / CPX_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) cpx[i] = color.pack() cpx.show()
def merry(): for i in range(TREE_LEDS): color = fancy.palette_lookup(merry_palette, (offset + i) / (TREE_LEDS / 2)) color = fancy.gamma_adjust(color, brightness=0.25) tree[i] = color.pack() tree.show() for i in range(60): color = fancy.palette_lookup(star_palette, (offset + i) / CPX_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) p = random.randint(0, (CPX_LEDS - 1)) cpx[p] = color.pack() cpx.show()
def twinkle(): for i in range(60): color = fancy.palette_lookup(fairy_palette, offset + i / CPX_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) p = random.randint(0, (CPX_LEDS - 1)) cpx[p] = color.pack() cpx.show() for i in range(60): color = fancy.palette_lookup(fairy_palette, offset + i / TREE_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) p = random.randint(0, (TREE_LEDS - 1)) tree[p] = color.pack() tree.show()
def routine_2(): """Adafruit Example 2.""" # Declare a NeoPixel object on pin D6 with num_leds pixels, no auto-write. # Set brightness to max because we'll be using FancyLED's brightness control. with neopixel.NeoPixel( BOARD, NUM_LEDS, brightness=1.0, auto_write=False ) as pixels: offset = 0 # Positional offset into color palette to get it to 'spin' start = time.monotonic() while True: now = time.monotonic() if abs(now - start) > RUN_SECONDS and USE_TIMER: break for i in range(NUM_LEDS): # Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(PALETE, offset + i / NUM_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) pixels[i] = color.pack() pixels.show() offset += 0.01 # Bigger number = faster spin
def breathe_lights(pixels, palette, eul, inveul, brightness): #Set color and brightness pattern frequency based on current values intensity = round(STATUS_VARS['SEVERITY_VALUE'] * 63) color = fancy.palette_lookup(palette, intensity) seconds = time.time() frequency = STATUS_VARS['RECENCY_VALUE'] floor = 0.1 #Breathing pattern to control brightness testblevel = (math.exp(math.sin( (seconds % 60) / frequency)) - inveul) * (brightness / (eul - inveul)) if testblevel > floor: led_level = testblevel else: led_level = floor #TODO: Use seconds % SOMEVALUE to control frequency #Simple sine wave #testblevel = (MAXIMUMBRIGHT / 2.0 * (1.0 + math.sin(SPEED * seconds)))/MAXIMUMBRIGHT #print("SINE Value=", led_level) levels = (led_level, led_level, led_level) color = fancy.gamma_adjust(color, brightness=levels) pixels.fill(color.pack()) pixels.show()
def getColor(self): # how far along are we in this cycle (in seconds): 0 to duration elapsed = (time.monotonic() - self.startTime) % self.duration # map elapsed to a float between 0 and the size of the palette position = elapsed * (len(self.palette)) / self.duration indexA = math.floor(position) indexB = math.ceil(position) if indexA >= len(self.palette): indexA = 0 indexB = 1 elif indexB >= len(self.palette): indexB = 0 # grab the colors before and after that position colorA = self.palette[indexA] colorB = self.palette[indexB] # calculate the color at this position inbetween those two color = fancy.gamma_adjust(fancy.mix(colorA, colorB, position - indexA)).pack() #print("elapsed: {}, position: {}, colorA: {}, colorB: {}, color: {}".format(elapsed, position, colorA, colorB, color)) return color
def set_palette(palette): for i in range(NUM_LEDS): # Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS) color = fancy.gamma_adjust(color, brightness=1.0) ring[i] = color.pack() ring.show() for i in range(NUM_LEDS): # Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS) color = fancy.gamma_adjust(color, brightness=1.0) cpx[i] = color.pack() cpx.show()
def applyGamma_video(n, g_r=GFACTOR, g_g=None, g_b=None, inplace=False): """Approximates various invocations of FastLED's many-ways-overloaded applyGamma_video() function. ACCEPTS: One of three ways: 1. A single brightness level (0-255) and optional gamma-correction factor (float usu. > 1.0, default if unspecified is 2.5). 2. A single CRGB, CHSV or packed integer type and optional gamma factor or separate R, G, B gamma values. 3. A list of CRGB, CHSV or packed integer types (and optional gamma(s)). In the tuple/list cases, the 'inplace' flag determines whether a new tuple/list is calculated and returned, or the existing value is modified in-place. By default this is 'False'. Can also use the napplyGamma_video() function to more directly approximate FastLED syntax/behavior. RETURNS: Corresponding to above cases: 1. Single gamma-corrected brightness level (0-255). 2. A gamma-corrected CRGB value (even if input is CHSV or packed). 3. A list of gamma-corrected CRGB values. In the tuple/list cases, there is NO return value if 'inplace' is true -- the original values are modified. """ # If single gamma value is passed, keep that, otherwise convert # gamma values to tuple for gamma_adjust function. if g_g is not None and g_b is not None: g_r = (g_r, g_g, g_b) return fancy.gamma_adjust(n, g_r, inplace=inplace)
def render(self, t): if (self.value == self.rendered_value and (self.palette_shift_speed == None or self.value == 0) and not self.dirty and self.highlight == None): return if self.last_value_t == None or self.speed is None: time_delta = self.max_val else: time_diff = t - self.last_value_t time_delta = int(time_diff / self.speed) if self.value > self.last_value: self.rendered_value = min(self.value, self.last_value + time_delta) else: self.rendered_value = max(self.value, self.last_value - time_delta) highlight_mix = abs(0.5 - ((t - self.highlight_t) % self.highlight_speed) / (self.highlight_speed) * 1.5) if self.highlight_speed != None else 0 if self.palette_shift_speed == None: palette_animation_offset = 0 else: palette_t = t if self.last_value_t == None else t - self.last_value_t palette_animation_offset = (palette_t % self.palette_shift_speed ) / self.palette_shift_speed i = 1 if self.color_from_end: palette_align = self.max_val - self.rendered_value else: palette_align = 0 for (x, y) in self.positions: if i <= self.min_value: c = None elif i <= self.rendered_value: c_idx = (i - 1 + palette_align ) * self.palette_step + palette_animation_offset c = fancy.palette_lookup(self.colors, c_idx) else: c = self.background_color if self.highlight != None and (x, y) in self.highlight: if c == None: c = fancy.CHSV(0, 0, 0.0) c = fancy.mix(self.highlight_color, c, highlight_mix) if c != None: c_packed = pack( fancy.gamma_adjust(c, brightness=self.brightness)) self.pixels[x, y] = c_packed i = i + 1 self.dirty = False
def colorcycle(): global hue for j in range(255): color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels.fill(color.pack()) pixels.show() # time.sleep(wait) hue += 2
def effect_plasma_update(self): """Plasma.""" for col_index in range(Matrix_col_count): for row_index in range(Matrix_row_count): # calculate plasma # mostly inspired by # https://www.bidouille.org/prog/plasma col = map_range( col_index, 0, Matrix_col_count-1, # 0, 1.0 -0.5, 0.5 ) row = map_range( row_index, 0, Matrix_row_count-1, # 0, 1.0 -0.5, 0.5 ) # moving rings cx = col + 0.5 * math.sin(self._offset / 5) cy = row + 0.5 * math.cos(self._offset / 3) value = math.sin( math.sqrt(100 * (cx*cx + cy*cy) + 1) + self._offset ) # mapping contrast = map_range( value, -1, 1, self._contrast_min, self._contrast_max ) hue = map_range( value, -1, 1, self._hue_min, self._hue_max ) # map to color color = fancyled.CHSV( hue, v=contrast ) # handle gamma and global brightness color_r, color_g, color_b = fancyled.gamma_adjust( color, brightness=self.brightness) pixels.set_pixel_float_value( pmap.map_raw[row_index][col_index], color_r, color_g, color_b ) # update animation offset self._offset += self.stepsize if self._offset > (math.pi * 30): self._offset = 0 # write data to chips pixels.show()
def rainbow_cycle(): global hue wait = 0.05 for i in range(num_pixels): color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels[i] = color.pack() pixels.show() time.sleep(wait) hue += 4
def rainbow_flush(delay=.02): global offset for i in range(num_leds): # Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(palette, offset + i / num_leds) color = fancy.gamma_adjust(color, brightness=0.25) pixels[i] = color.pack() pixels.show() time.sleep(delay) offset += 0.02 # Bigger number = faster spin
def rotatePalette(): global offset while True: for i in range(pixelCount): color = fancy.palette_lookup(palette, offset + i / pixelCount) color = fancy.gamma_adjust(color, brightness=0.25) #color = fancy.gamma_adjust(color, brightness=levels) pixels[i] = color.pack() pixels.show() offset += 0.005 # Bigger number = faster spin
def mode_3(self): """ front full white, others alternate """ self.tick_delay = 0.25 self.unit_speed = 0.5 for i in range(self.NUM_LED): if i == 3: color = TrainLantern.WHITE else: color = fancy.gamma_adjust(fancy.CHSV(self.unit_counter)) self.neopixels[i] = color.pack() self.neopixels.show() self.update_tick()
def mode_2(self): """ alternate red and yellow """ self.tick_delay = 1 for i in range(self.NUM_LED): i_mod = i % 2 if self.tick % 2: color = TrainLantern.BLACK if i_mod else TrainLantern.YELLOW else: color = TrainLantern.RED if i_mod else TrainLantern.BLACK self.neopixels[i] = fancy.gamma_adjust(color).pack() self.neopixels.show() self.update_tick()
def getColor(self): # how far along are we in this cycle (in seconds): 0 to duration elapsed = (time.monotonic() - self.startTime) % self.duration # map elapsed to a float between 0 and the duration position = elapsed / self.duration # calculate the color at this position inbetween those two color = fancy.gamma_adjust(fancy.mix(self.peakColor, BLACK, position)).pack() return color
def fire_2018(strip, offset): # heat colors palette = [ 0x330000, 0x660000, 0x990000, 0xCC0000, 0xFF0000, 0xFF3300, 0xFF6600, 0xFF9900, 0xFFCC00, 0xFFFF00, 0xFFFF33, 0xFFFF66, 0xFFFF99, 0xFFFFCC ] for i in range(num_leds): # FancyLED can handle the gamma adjustment, brightness and RGB settings color = fancy.palette_lookup(palette, offset + i / num_leds) color = fancy.gamma_adjust(color, brightness=brightness) strip[i] = color.pack()
def rainbow_update(self): """Rainbow.""" for row_index in range(Matrix_row_count): # Load each pixel's color from the palette using an offset # color = fancyled.palette_lookup( # palette, # self.offset + row_index / Matrix_row_count # # ) # results in 84,47ms # but has not as nice colors... # color_r, color_g, color_b = fancyled.CRGB(fancyled.CHSV( # self.offset + # # (row_index / Matrix_row_count), # map_range( # row_index, # 0, Matrix_row_count, # 0, 1.0 # ), # v=0.1 # )) # results in 99.41ms color = fancyled.CHSV( self.offset + # (row_index / Matrix_row_count), map_range(row_index, 0, Matrix_row_count, 0, 1.0), # v=0.05 ) color_r, color_g, color_b = fancyled.gamma_adjust( color, brightness=self.brightness) for col_index in range(Matrix_col_count): # pixels[pmap.map(col=col_index, row=row_index)] = color pixels.set_pixel_float_value( # pmap.map(col=col_index, row=row_index), pmap.map_raw[row_index][col_index], color_r, color_g, color_b) # pixels.set_pixel_float_value( # pmap.map_raw[row_index][col_index], # 0.1, # 0.5, # 0.5, # ) pixels.show() self.offset += 0.01 # Bigger number = faster spin if self.offset >= 10: self.offset -= 10
def frontback(): global hue wait = 0.1 for y in range(4): for x in range(4): for z in range(4): i = XYZ(x, y, z) color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels[i] = color.pack() hue += 1 pixels.show() time.sleep(wait)
def mode_4(self): """ front red, back green, sides flashing yellow """ self.tick_delay = 1 dim_yellow = fancy.gamma_adjust(TrainLantern.YELLOW, brightness=(0.25, 0.25, 0.25)) tick_mod = self.tick % 2 self.neopixels[3] = TrainLantern.RED.pack() self.neopixels[0] = dim_yellow.pack( ) if tick_mod else TrainLantern.BLACK.pack() self.neopixels[1] = TrainLantern.GREEN.pack() self.neopixels[2] = TrainLantern.BLACK.pack( ) if tick_mod else dim_yellow.pack() self.neopixels.show() self.update_tick()
def test_paper1_update(self): """Test.""" my_row_count = Matrix_row_count * 2 my_col_count = int(Matrix_col_count / 2) for row_index in range(my_row_count): color_raw = multi_map_tuple(row_index, self.paper_colors_current) # print("color_raw", color_raw) color = fancyled.CHSV( h=color_raw[0], s=color_raw[1], v=color_raw[2], ) # print("color", color) # results in 99.41ms # color = fancyled.CHSV( # self.offset + # # (row_index / my_row_count), # map_range( # row_index, # 0, my_row_count, # 0, 1.0 # ), # # v=0.05 # ) color_r, color_g, color_b = fancyled.gamma_adjust( color, brightness=self.brightness ) # row_i = row_index row_i = int(row_index / 2) for col_index in range(my_col_count): # print( # "row:{:2}, " # "col:{:2}, " # "".format(row_index, col_index), # end="" # ) col_i = col_index if row_index % 2: col_i = my_col_count + col_index # print( # "-> " # "row:{:2}, " # "col:{:2}, " # "".format(row_i, col_i) # ) pixels.set_pixel_float_value( pmap.map_raw[row_i][col_i], color_r, color_g, color_b ) pixels.show()
def simple_rainbow(delay = .03): global offset num_steps = 4 for i in range(num_steps): color = fancy.palette_lookup(palette, offset + i / num_steps) color = fancy.gamma_adjust(color, brightness=0.25) col = color.pack() nleds = num_leds//num_steps start = i*nleds for j in range(start,start+nleds,1): pixels[j] = col pixels.show() offset += .02 time.sleep(delay)
def test_minimal_update(self): """Minimal Full Loop with HSV.""" for row_index in range(Matrix_row_count): color = fancyled.CHSV( 0.5, # v=0.05 ) color_r, color_g, color_b = fancyled.gamma_adjust( color, brightness=self.brightness ) for col_index in range(Matrix_col_count): pixels.set_pixel_float_value( pmap.map_raw[row_index][col_index], color_r, color_g, color_b ) pixels.show()
def diagonal(): global hue wait = 0.02 for i in range(4): for y in range(i + 1): for x in range(i, -1, -1): for z in range(4): pos = XYZ(x, y, z) color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels[pos] = color.pack() hue += 1 pixels.show() # time.sleep(wait) for i in range(3): for y in range(i + 1, 4): for x in range(3, i, -1): for z in range(4): pos = XYZ(x, y, z) color = fancy.CHSV(hue % 255) color = fancy.gamma_adjust(color, brightness=brightness) pixels[pos] = color.pack() hue += 1 pixels.show()
import neopixel import adafruit_fancyled.adafruit_fancyled as fancy num_leds = 20 # Declare a 6-element RGB rainbow palette palette = [fancy.CRGB(1.0, 0.0, 0.0), # Red fancy.CRGB(0.5, 0.5, 0.0), # Yellow fancy.CRGB(0.0, 1.0, 0.0), # Green fancy.CRGB(0.0, 0.5, 0.5), # Cyan fancy.CRGB(0.0, 0.0, 1.0), # Blue fancy.CRGB(0.5, 0.0, 0.5)] # Magenta # Declare a NeoPixel object on pin D6 with num_leds pixels, no auto-write. # Set brightness to max because we'll be using FancyLED's brightness control. pixels = neopixel.NeoPixel(board.D6, num_leds, brightness=1.0, auto_write=False) offset = 0 # Positional offset into color palette to get it to 'spin' while True: for i in range(num_leds): # Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(palette, offset + i / num_leds) color = fancy.gamma_adjust(color, brightness=0.25) pixels[i] = color.pack() pixels.show() offset += 0.02 # Bigger number = faster spin
""" Simple FancyLED example for Circuit Playground Express """ from adafruit_circuitplayground.express import cpx import adafruit_fancyled.adafruit_fancyled as fancy cpx.pixels.auto_write = False # Refresh pixels only when we say cpx.pixels.brightness = 1.0 # We'll use FancyLED's brightness controls # Declare a 4-element color palette, this one happens to be a # 'blackbody' palette -- good for heat maps and firey effects. palette = [fancy.CRGB(1.0, 1.0, 1.0), # White fancy.CRGB(1.0, 1.0, 0.0), # Yellow fancy.CRGB(1.0, 0.0, 0.0), # Red fancy.CRGB(0.0, 0.0, 0.0)] # Black offset = 0 # Positional offset into color palette to get it to 'spin' levels = (0.25, 0.3, 0.15) # Color balance / brightness for gamma function while True: for i in range(10): # Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(palette, offset + i / 10) color = fancy.gamma_adjust(color, brightness=levels) cpx.pixels[i] = color.pack() cpx.pixels.show() offset += 0.033 # Bigger number = faster spin