def larson_scanner(primary, secondary): global larson_index, larson_dir, larson_last_dir, leds_1 size = 6 leds_1 = bytearray(n * secondary) leds_1[larson_index * 4:larson_index * 4 + 4] = bytearray(primary) for i in range(size): b = larson_index + 1 + i a = larson_index - 1 - i t = float(i + 1) / size fading = interpolate_rgbw(secondary, primary, 1 - t) if larson_bounds[0] <= a and a < larson_bounds[1]: leds_1[a * 4:a * 4 + 4] = bytearray(fading) if larson_bounds[0] <= b and b < larson_bounds[1]: leds_1[b * 4:b * 4 + 4] = bytearray(fading) neopixel_write(pin, leds_1, True) larson_last_dir = larson_dir larson_index += larson_dir if larson_dir == 1 and larson_index == larson_bounds[1] - 1: larson_dir = -1 elif larson_dir == -1 and larson_index == larson_bounds[0]: larson_dir = 1
def solun_demo(): global leds_0, leds_1 paris('leds_1') fade_to() year, month, day, hour, minute, second, weekday, yearday = utime.localtime( ) for h in range(24): for m in range(0, 60): # clear paris('leds_0') # hour angle = (h % 12 + m / 60.) / 12. * 2. * math.pi distance = angle_to_unwind(northclockwise2math(angle))[0] set_area2(distance, 4, [158, 81, 188, 0], leds_0) # solar/lunar draw_solun_positions(coords, (year, month, day, h, m, 0, weekday, yearday), leds_0) # apply neopixel_write(pin, leds_0, True) paris('leds_1') fade_to()
def spin(color): global leds_0, last_millis, last_angle tail = 24 rps = 0.25 now_millis = utime.ticks_ms() dt = (now_millis - last_millis) / 1000. last_millis = now_millis angle = wrap_to_0_2pi(last_angle + dt * rps * 2. * math.pi) fraction_led, intensity = intersect_angle_frame(northclockwise2math(angle), True, True) last_angle = angle frac, frac_led_index = math.modf(fraction_led) frac_led_index = int(frac_led_index) background = [0, 0, 0, 0] beam_color = [int(intensity * c) for c in color] leds_0 = bytearray(n * background) # initialize color for i in range(tail): index = (frac_led_index - i) % n t = 1. - float(i) / tail fading = interpolate_rgbw([0, 0, 0, 0], beam_color, t) leds_0[index * 4:index * 4 + 4] = bytearray(fading) neopixel_write(pin, leds_0, True)
def tick(self): if self.config['powered_on'] == 0: return if self.forceRefreshColor and self.config['use_color']: self.forceRefreshColor = False bytes = ubinascii.unhexlify(self.config['color']) color = [bytes[1], bytes[0], bytes[2]] esp.neopixel_write(self.pin, bytearray(color * self.config['leds']), 1) return self.forceRefreshColor = False if self.forceRefreshLeds != 0: esp.neopixel_write(self.pin, bytearray([0] * 3 * self.forceRefreshLeds), 1) for animation in self.animations: animation.setup(self.config['leds']) self.forceRefreshLeds = 0 if self.config['use_color']: return if utime.ticks_ms() - self.lastChangedAnimation > self.config[ 'seconds_per_animation'] * 1000 and self.config[ 'animation'] == -1: self.config['current_animation'] = ( self.config['current_animation'] + 1) % len(self.animations) self.lastChangedAnimation = utime.ticks_ms() if self.tickCount % (_MAX_SPEED - self.config['speed'] + 1) == 0: self.animations[self.config['current_animation']].tick() self.tickCount = (self.tickCount + 1) % _MAX_SPEED
async def __fade_effect(self, pixels, length, delay, callback): incs = bytearray(self.colors * self.cnt) col = bytearray(4) # Calculate increments (decrements) for each color # range -> color, e.g.: # [(range(0, 5), b'\xff\xff\xff\xff')] for leds, color in pixels: # convert color col[0] = color[1] col[1] = color[0] col[2:4] = color[2:4] if not leds: leds = range(0, self.cnt) for l in leds: for c in range(self.colors): idx = l * self.colors + c incval = int((col[c] - self.buf[idx]) / length) incs[idx] = incval # Run effect for step in range(length): for leds, color in pixels: if not leds: leds = range(0, self.cnt) for l in leds: for c in range(self.colors): idx = l * self.colors + c self.buf[idx] += incs[idx] esp.neopixel_write(self.pin, self.buf, True) await asyncio.sleep_ms(delay) # Set final (desired) color self.__change_color(pixels) # Run finish callback, if any if callback: callback()
def write(self): if self._brightness > 0.99: neopixel_write(self.pin, self.buf, self.timing) else: neopixel_write( self.pin, bytearray([int(i * self._brightness) for i in self.buf]), self.timing)
def cycle_color(color, n_cycles=1, timeout_ms=1): global leds_0 for i in range(n * n_cycles): leds_0 = bytearray(n * 4) index = (i % n) * 4 leds_0[index:index + 4] = bytearray(color) neopixel_write(pin, leds_0, True) utime.sleep_ms(timeout_ms) off()
def cycle_channels(brightness=255, n_cycles=1, timeout_ms=1): global leds_0 for i in range(n * 4 * n_cycles): leds_0 = bytearray(n * 4) index = i % (n * 4) leds_0[index] = brightness neopixel_write(pin, leds_0, True) utime.sleep_ms(timeout_ms) off()
def render(): for x in range(WIDTH): for y in range(HEIGHT): idx = led_map[x][y] led_buffer[idx + 1], led_buffer[idx], led_buffer[idx + 2] = hsv_to_rainbow_rgb( *led_matrix[x][y]) esp.neopixel_write(LED_PIN, led_buffer, 1)
def fade_to(steps=10, sleep=1): if steps <= 1: neopixel_write(pin, leds_1, True) return for i in range(steps): t = (i + 1) / steps for j in range(n * 4): # iterate all leds_0[j] = int(interpolate(leds_0[j], leds_1[j], t)) neopixel_write(pin, leds_0, True) utime.sleep_ms(sleep)
def __change_color(self, pixels): col = bytearray(4) for leds, color in pixels: # Neopixel color bytes: G -> 0, R -> 1, B -> 2, W = 3 col[0] = color[1] col[1] = color[0] col[2:4] = color[2:4] # Check range. None - means all pixels if not leds: leds = range(self.cnt) for l in leds: idx = l * self.colors self.buf[idx:idx + self.colors] = col[:self.colors] esp.neopixel_write(self.pin, self.buf, True)
def ramp_up(): global leds_0 center = cardinals['south'][0] size = (cols + 2 * rows) d = (size + 1) % 2 ramp_color_1 = bytearray((0, 0, 0, 5)) ramp_color_2 = bytearray((0, 0, 0, 50)) ramp_color_3 = bytearray((0, 0, 0, 200)) leds_0 = bytearray(n * 4) set_area2(center / leds_per_cm, width / 3, ramp_color_2, leds_0) neopixel_write(pin, leds_0, True) utime.sleep_ms(200) for i in range(size // 2): i1 = (center - (i % n) - d) % n i2 = (center + (i % n)) % n leds_0[i1 * 4:i1 * 4 + 4] = ramp_color_1 leds_0[i2 * 4:i2 * 4 + 4] = ramp_color_1 neopixel_write(pin, leds_0, True) utime.sleep_ms(12) for i in range(16): color = interpolate_rgbw(ramp_color_1, ramp_color_3, (i + 1) / 16) set_area2(cardinals['north'][0] / leds_per_cm, width, color, leds_0) neopixel_write(pin, leds_0, True) utime.sleep_ms(1) utime.sleep(1) fade_to()
def write(self, frac=1.0): neopixel_write(self.pin, self.buf, True, frac)
def setLeds(self): esp.neopixel_write(self.pin, bytearray(self.colors), 1)
def write(self): neopixel_write(self.pin, self.buf, True)
import config import esp import machine import utils import ubinascii utils.printLog('ANIMATOR', 'fast set leds') _config = utils.readJson('animator.data') if _config and _config['powered_on'] and _config['use_color']: pin = machine.Pin(config.D4, machine.Pin.OUT) bytes = ubinascii.unhexlify(_config['color']) color = [bytes[1], bytes[0], bytes[2]] esp.neopixel_write(pin, bytearray(color * _config['leds']), 1) utils.printLog('ANIMATOR', 'finish')
def clear(self): esp.neopixel_write(self.f_Neo, bytearray(self.f_BufferSize), True)
def periodic(self, p_Now): esp.neopixel_write(self.f_Neo, next(self.f_Generator), True)
greenarray = [] for pos in range(pixelcount): redarray.extend(hue_to_rgb((0 + (pos/pixelcount)) % 1)) greenarray.extend(hue_to_rgb((0.33 + (pos/pixelcount)) % 1)) bluearray.extend(hue_to_rgb((0.66 + (pos/pixelcount)) % 1)) ''' if pos == 11: redarray.extend([0, 0, 255]) greenarray.extend([0, 255, 0]) bluearray.extend([255, 0, 0]) elif pos == 0: redarray.extend([255, 0, 0]) greenarray.extend([0, 0, 255]) bluearray.extend([0, 255, 0]) else: # handle RGB ''' redarray = array.array('B', redarray) bluearray = array.array('B', bluearray) greenarray = array.array('B', greenarray) while True: for arr in [redarray, bluearray, greenarray]: sec = time.time() while time.time() == sec: state = disable_irq() esp.neopixel_write(out, arr, True) enable_irq(state)
def write(self): neopixel_write(self.pin, self.buf, self.timing)
def _write(self): neopixel_write(self._pin, self._buf, True)
def set_color(count, color): global leds_0 count = clamp(count, 0, n) leds_0 = bytearray(count * color + (n - 1) * (0, 0, 0, 0)) neopixel_write(pin, leds_0, True)
def write(self): neopixel_write(self.pin, self._state.flatten(), True)
esp.flash_read(byte_offset, buffer) # The esp32 module: import esp32 esp32.hall_sensor() # read the internal hall sensor esp32.raw_temperature( ) # read the internal temperature of the MCU, in Fahrenheit esp32.ULP() # access to the Ultra-Low-Power Co-processor # RMT import esp32 from machine import Pin r = esp32.RMT(0, pin=Pin(18), clock_div=8) r # RMT(channel=0, pin=18, source_freq=80000000, clock_div=8) # The channel resolution is 100ns (1/(source_freq/clock_div)). r.write_pulses((1, 20, 2, 40), 0) # Send 0 for 100ns, 1 for 2000ns, 0 for 200ns, 1 for 4000ns # For low-level driving of a NeoPixel: # BUG: Why is esp.neopixel_write not stubbed ? pin = Pin(18) grb_buf = (1, 20, 2, 40) is800khz = False import esp esp.neopixel_write(pin, grb_buf, is800khz) # type: ignore # FIXME:
def setLeds(self): (r, g, b) = animation_utils.hsvToRgb((self.currentH, 1.0, 1.0)) esp.neopixel_write(self.pin, bytearray([r, g, b] * self.leds), 1)
def esprgb(np, grb_list): esp.neopixel_write(np.pin, bytearray(i for grb in grb_list for i in grb), True)
def redraw_cycle(np, config, globals, neopixel_write): enum = list(enumerate(globals.stripData)) for index, strip in enum: if "replicate" in strip["animation_data"]: if not "replicate_done" in strip["animation_data"]: strip["animation_data"]["replicate_done"] = True replicateCount = strip["animation_data"]["replicate"] for i in range(1, replicateCount): copy = strip.copy() copy["animation_data"] = strip["animation_data"].copy() copy["animation_data"]["animations"] = [] for animation in strip["animation_data"]["animations"]: copy["animation_data"]["animations"].append( animation.copy()) globals.stripData.insert(index + i, copy) enum = list(enumerate(globals.stripData)) # Align offsets of zones for index, strip in enum: if globals.compressedOutput: strip["animation_data"]["offset"] = index * 5 if index > 0: strip["animation_data"]["offset"] = globals.stripData[ index - 1]["animation_data"]["zoneEnd"] if "flickerTransition" in strip["animation_data"]: strip["animation_data"]["zoneEnd"] = strip["animation_data"][ "offset"] + 5 * strip["animation_data"]["zoneLength"] else: strip["animation_data"][ "zoneEnd"] = strip["animation_data"]["offset"] + 5 else: strip["animation_data"]["offset"] = sum_lengths( globals.stripData, index) # Pass previous animation data (for transitions) if globals.previousData and index in globals.previousData: strip["animation_data"]["previous"] = globals.previousData[index][ "animation_data"]["animations"][globals.previousData[index][ "animation_data"]["animation_index"]] msPerFrame = int(1000 / config["frameRate"]) frameCount = 0 # One frame cycle while globals.redraw_active: # Frame start time frameStart = time.ticks_ms() np.currentOffset = 0 # Process animations for index, strip in enum: if not "animation_index" in strip["animation_data"]: strip["animation_data"]["animation_index"] = 0 if "done" in strip["animation_data"] and strip["animation_data"][ "done"] == True: if "drawn" in strip["animation_data"] and strip[ "animation_data"]["drawn"] == True: strip["animation_data"]["drawn"] = False strip["animation_data"]["done"] = False if strip["animation_data"]["animation_index"] < len( strip["animation_data"]["animations"]) - 1: #strip["animation_data"]["previous"] = strip["animation_data"]["animations"][strip["animation_data"]["animation_index"]] strip["animation_data"]["animations"][ strip["animation_data"]["animation_index"]] = False strip["animation_data"]["animation_index"] += 1 animation = strip["animation_data"]["animations"][ strip["animation_data"]["animation_index"]] if animation["animation_name"] == "flicker": animations.flicker(np, config, index, strip["animation_data"], globals.compressedOutput) if animation["animation_name"] == "blink": animations.blink(np, config, index, strip["animation_data"], globals.compressedOutput) if animation["animation_name"] == "blinkrng": animations.blinkrng(np, config, index, globals.stripData[index]["animation_data"], globals.compressedOutput) if animation["animation_name"] == "blink_solid": animations.blink(np, config, index, strip["animation_data"], globals.compressedOutput, True) if animation["animation_name"] == "blinkrng_solid": animations.blinkrng(np, config, index, strip["animation_data"], globals.compressedOutput, True) if animation["animation_name"] == "onoff": animations.onoff(np, config, index, strip["animation_data"], globals.compressedOutput, True) if animation["animation_name"] == "solid": animations.solid_color(np, config, index, strip["animation_data"], globals.compressedOutput) del animation del index, strip # Send data to LEDs neopixel_write(np.pin, np.buf, np.timing) # Calculate time spent for frame frameTime = time.ticks_diff(time.ticks_ms(), frameStart) if frameTime <= msPerFrame: time.sleep_ms(msPerFrame - frameTime) del enum gc.collect()
def blit(self): neopixel_write(self.pin, self._get_buff(), True)
def write(self): neopixel_write(self.pin, self.timing, self.buf)
# _________________________________________________________ # NeoPixel driver # Use the neopixel module: from machine import Pin from neopixel import NeoPixel pin = Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels np = NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels np[0] = (255, 255, 255) # set the first pixel to white np.write() # write data to all pixels r, g, b = np[0] # get first pixel colour For low-level driving of a NeoPixel: import esp esp.neopixel_write(pin, grb_buf, is800khz) # _________________________________________________________ # APA102 driver # Use the apa102 module: from machine import Pin from apa102 import APA102 clock = Pin(14, Pin.OUT) # set GPIO14 to output to drive the clock data = Pin(13, Pin.OUT) # set GPIO13 to output to drive the data apa = APA102(clock, data, 8) # create APA102 driver on the clock and the data pin for 8 pixels apa[0] = (255, 255, 255, 31) # set the first pixel to white with a maximum brightness of 31 apa.write() # write data to all pixels
def update(self): neopixel_write(self.np.pin, self.buf, self.np.timing)
def powerOffIfNeeded(self): if self.config['powered_on'] == 0: esp.neopixel_write(self.pin, bytearray([0] * 3 * self.config['leds']), 1)