def __init__(self, count, data, clock=None, apa102c=False): """Initializes the LED driver Parameters ---------- count : int Required Number of LEDs in the LED strip/matrix data : int Required Pin of the Data Line clock : int Optional Pin for the clock Line. Used only by the APA102 apa102c : bool Optional Used only by the APA102C to reorder the color sequence """ data_pin = Pin(data, Pin.OUT) self.neopixel = True if clock is None: self.leds = NeoPixel(data_pin, count) else: self.neopixel = False clock_pin = Pin(clock, Pin.OUT) self.leds = APA102(data_pin, clock_pin, count) if apa102c: self.leds.ORDER = (0, 2, 1, 3) self.count = count
class Flashing_lights: 'Controls two neopixels that flash continuously' def __init__(self, neopixel_pin): self.n = NeoPixel(Pin(neopixel_pin, Pin.OUT), 2) self.n[0] = (0, 0, 0) self.n[1] = (0, 0, 0) self.n.write() self.brightness_max = 1 self.brightness_min = .03 self.color = (100, 0, 0) self.progress = 0 self.epoch = time.ticks_ms() self.breath = 27 def set_color(self, color, brightness_min=0.03): self.color = color self.epoch = localtime() self.brightness_min = brightness_min def loop(self): k = ((math.cos((time.ticks_ms() - self.epoch) / 450) + 1) / 2.8) * ( self.brightness_max - self.brightness_min) + self.brightness_min self.n[1] = self.n[0] = dim(self.color, k) self.n.write()
class neo16x16_img: def __init__(self,pin): self.np=NeoPixel(pin,256) def clear(self): self.np.clear() def show(self,dat,pos=0): for x in range(16): for y in range(8): if ((x+pos)*8)>=len(dat): self.np[x*16+y*2]=(0,0,0) self.np[x*16+y*2+1]=(0,0,0) else: t=dat[(x+pos)*8+y] r=t%16 g=(t>>4)%16 b=(t>>8)%16 if pos%2: self.np[x*16+y*2]=(r,g,b) else: self.np[x*16+15-y*2]=(r,g,b) r=(t>>12)%16 g=(t>>16)%16 b=(t>>20)%16 if pos%2: self.np[x*16+y*2+1]=(r,g,b) else: self.np[x*16+14-y*2]=(r,g,b) self.np.show()
def __init__(self, pixel_count=125, pixel_pin=D18): """ :param pixel_count: amount of pixels :param pixel_pin: The neopixel library makes use of the BCM pin numbering scheme. """ self.pixels = NeoPixel(pixel_pin, pixel_count, auto_write=False, pixel_order=GRB) \ if pixel_count > 0 else None
def __init__(self, number_of_leds=300): self.stop = False self.leds = number_of_leds self.pixels = NeoPixel( board.D18, 300, auto_write=False ) # Hard coded 300 for timing issues TODO (should re-look at this) # Define Neo States self.neo_state_off = 0 self.neo_state_solid = 1 self.neo_state_flashing = 2 self.neo_state_alternate = 3 self.neo_state_pulse = 4 self.neo_state_chase = 5 self.neo_state_bounce = 6 self.neo_state_no = 7 # Initial Colour self.colour = (29, 60, 125) self.period_delay = 0.5 self.brightness = 0.5 # Current State self.neo_state = self.neo_state_off # Chaser Variables self.chaser_leds_on = 3 self.chaser_leds_off = 7 self.chaser_reverse = False Thread.__init__(self)
def __init__( self, pin_num=None, n=30, start_led=0, test=False, overwrite_line=True, debug=False, target='micropython' # or 'adafruit' ): self.debug = debug self.target = target self.strip_length = n self.addressable_strip_length = n self.start_led = start_led self.test = test self.pin_num = pin_num if pin_num else 10 if self.target == 'micropython' else 18 if self.target == 'adafruit' else None self.overwrite_line = overwrite_line self.sections = self.get_sections() if self.test: self.leds = [[0, 0, 0] for x in range(self.strip_length)] else: from neopixel import NeoPixel as NeoPixelOriginal if self.target == 'micropython': self.leds = NeoPixelOriginal(self.get_pin(), self.strip_length) elif self.target == 'adafruit': self.leds = NeoPixelOriginal(self.get_pin(), self.strip_length, auto_write=False)
class _Display: def __init__(self): self._np = NeoPixel(Pin(5, Pin.OUT), 9) def __setitem__(self, subscript, value): self._np[_get_index(subscript)] = value def __getitem__(self, subscript): return self._np[_get_index(subscript)] def anim_pixel(self, subscript, r, g, b, steps=20): index = _get_index(subscript) np = self._np start = np[index] end = r, g, b for i in range(0, steps): j = steps - i np[index] = [ int((s * j + e * i) / steps) for s, e in zip(start, end) ] yield 1 / 60 np[index] = end def write_now(self): self._np.write()
def init(): from neopixel import NeoPixel global neopixelstrip stop() neopixelstrip = NeoPixel(microbit.pin8, 9); pixels_off() eyestrip = NeoPixel(microbit.pin8, 2) set_eye_color_on_start()
def init(): global neopixelstrip stop() neopixelstrip = const(NeoPixel(microbit.pin8, 9)) pixels_off() eyestrip = const(NeoPixel(microbit.pin8, 2)) set_eye_color_on_start()
class NeoPixelAdaptor(BaseDisplayAdaptor): def __init__(self): """ Ascii Adaptor class takes the display object and convert it into an ascii display. """ super().__init__() def setup(self, display_data): """Overides the base class """ strip_size = display_data.height * display_data.width self._grid = NeoPixel(Pin(23), strip_size) def show(self, delta, display_data): """ Show function will show the ascii display using the given display object. Is used every loop in Controller. :param display: Instance that will be converted :type display: Display :param delta: The amount of time that has passed since the start of the loop in the controller :type delta: Number """ pixels = display_data.pixels for y_index in range(display_data.height): for x_index in range(display_data.width): led_index = convert_to_index(x_index, y_index, display_data.height) self._grid[led_index] = pixels[x_index][y_index] self._grid.write()
def __init__(self, color="0000ff"): self.leds_strip = NeoPixel(Pin(Gpio.DATA), LEDS) self.clear_all() self.time = NtpTime() self.set_color(color, False)
def test_led(): px = NeoPixel(Pin(D5), 4) px[0] = (255, 0, 0) px[1] = (0, 255, 0) px[2] = (0, 0, 255) px[3] = (255, 255, 255) px.write()
class NeoPixelWriter: def __init__(self, num_pixels=10, bytes_per_pixel=4, pinno=15): pin = Pin(pinno, Pin.OUT) self.np = NeoPixel(pin, num_pixels, bpp=bytes_per_pixel) self.bytes_per_pixel = bytes_per_pixel self.tuple_len = bytes_per_pixel+1 def on_next(self, x): """The event should be a tuple/list where the first element is the pixel number and the rest are the settings for that pixel OR it can be a standard (sensor_id, ts, event) tuple, where the control message is in the third element. """ if len(x)==3 and (isinstance(x[2], tuple) or isinstance(x[2], list)) and \ len(x[2])==self.tuple_len: x = x[2] # the control message is embedded in a standard triple elif len(x)!=self.tuple_len: raise Exception("expecting a tuple of length %d" % self.tuple_len) pixel = x[0] self.np[pixel] = x[1:] self.np.write() def on_error(self, e): pass def on_completed(self): pass
def __init__(self, pin_l, pin_r, num_leds): self.pin_l = Pin(pin_l, Pin.OUT) self.pin_r = Pin(pin_r, Pin.OUT) self.num_leds = num_leds self.np_l = NeoPixel(self.pin_l, self.num_leds) self.np_r = NeoPixel(self.pin_r, self.num_leds) self.prev_num_leds = 0
class ColorWheel: NEO_PIXEL_PIN = 26 # on my board the neopixel is connected to GPIO 26 NO_OF_LEDS = 7 def __init__(self): self.red = 0 self.green = 0 self.blue = 0 if sys.platform == "esp32": from machine import Pin from neopixel import NeoPixel # init data pin to control LEDs self.brightness=0.1 #brightness: 0-1.0 pin = Pin(self.NEO_PIXEL_PIN, Pin.OUT) self.np = NeoPixel(pin, self.NO_OF_LEDS) def colors(self,pos): if pos<60: self.red=255 self.green=int(255*pos/60) self.blue=0 elif pos >=60 and pos < 120: self.red=255-int(255*(pos-60)/60) self.green = 255 self.blue = 0 elif pos >=120 and pos < 180: self.red = 0 self.blue = int(255*(pos-120)/60) self.green = 255 elif pos >= 180 and pos < 240: self.red = 0 self.green = 255-int(255*(pos-180)/60) self.blue = 255 elif pos >= 240 and pos < 300: self.red = int(255*(pos-240)/60) self.green = 0 self.blue = 255 else: self.red = 255 self.green = 0 self.blue = 255 - int(255*(pos-300)/60) print("red: {:03d}, green: {:03d}, blue: {:03d}".format(self.red, self.green, self.blue)) return (self.red,self.green,self.blue) def show(self): if sys.platform == "esp32": for i in range(0,self.NO_OF_LEDS): self.np[i] = (int(self.red*self.brightness), int(self.green*self.brightness), int(self.blue*self.brightness)) self.np.write() sleep_ms(200)
def __init__(self, number=8): self.np = NeoPixel(Pin(13), number) self.sat = 1 self.val = 0.2 self.number = number print( "espws - diody ws2812: .alloff .random(ile,co ile) .allcolors .linijka(kolor) .dioda(nr, color)" )
def __init__(self, auto_write=True, brightness=1.0, part=_TRELLISM4_LEFT_PART, left_part=None): if part == _TRELLISM4_LEFT_PART: self.pix = NeoPixel(board.NEOPIXEL, 32, auto_write=False, brightness=brightness) elif part == _TRELLISM4_RIGHT_PART: self.pix = left_part.pix self.auto_write = auto_write self._offset = part
def __init__(self, pin, width, height, snake=True): self.pin_number = pin self.width = width self.height = height self.snake = snake self._pin = Pin(self.pin_number, Pin.OUT) self._device = NeoPixel(self._pin, self.total_pixels)
def ledrgb(n, r, g, b): n = min(max(n, 0), 3) r = min(max(r, 0), 255) g = min(max(g, 0), 255) b = min(max(b, 0), 255) np = NeoPixel(pin15, 4) np[floor(n)] = (floor(r), floor(g), floor(b)) np.show()
class MoveMini: def __init__(self): self.np = NeoPixel(pin0, 5) self.pos = 0 self.motor_right = pin1 self.motor_left = pin2 self.stop() def forward(self): display.show(Image.ARROW_N) self.np[1] = self.np[3] = (0, LIGHT_LEVEL, LIGHT_LEVEL) self.np[2] = (LIGHT_LEVEL, LIGHT_LEVEL, LIGHT_LEVEL) self.np[0] = self.np[4] = (0, 0, 0) self.np.show() self.motor_right.set_analog_period(20) self.motor_left.set_analog_period(20) self.motor_right.write_analog(50) self.motor_left.write_analog(100) def backward(self): display.show(Image.ARROW_S) self.np[1] = self.np[2] = self.np[3] = (LIGHT_LEVEL, 0, 0) self.np[0] = self.np[4] = (0, 0, 0) self.np.show() self.motor_right.set_analog_period(20) self.motor_left.set_analog_period(20) self.motor_right.write_analog(100) self.motor_left.write_analog(50) def left(self): display.show(Image.ARROW_W) self.np[1] = self.np[2] = (0, LIGHT_LEVEL, 0) self.np[0] = (LIGHT_LEVEL, LIGHT_LEVEL, 0) self.np[3] = self.np[4] = (0, 0, 0) self.np.show() self.motor_right.set_analog_period(20) self.motor_left.set_analog_period(20) self.motor_right.write_analog(100) self.motor_left.write_analog(100) def right(self): display.show(Image.ARROW_E) self.np[2] = self.np[3] = (0, LIGHT_LEVEL, 0) self.np[4] = (LIGHT_LEVEL, LIGHT_LEVEL, 0) self.np[0] = self.np[1] = (0, 0, 0) self.np.show() self.motor_right.set_analog_period(20) self.motor_left.set_analog_period(20) self.motor_right.write_analog(50) self.motor_left.write_analog(50) def stop(self): display.show(Image.HAPPY) for i in range(5): self.np[i] = (0, 0, 0) self.np.show() self.motor_right.read_digital() self.motor_left.read_digital()
def __init__(self, pin_number, time_manager): # Init the rgb strip pin = Pin(pin_number, Pin.OUT) self._neo_pixel = NeoPixel(pin, 48) self._current_mode = 0 # 0=static, 1=timed self._current_color = (0, 0, 0) self._time_manager = time_manager self._clear_color()
def neo_write(t_color, sleep, nb=NB_LEDS): # write a table of colors to neopixel import machine from neopixel import NeoPixel leds = NeoPixel(machine.Pin(2), nb) for i, color in enumerate(t_color): leds[i] = color sleep(100) leds.write()
def set_random(): pin = Pin(4, Pin.OUT) # set GPIO0 to output to drive NeoPixels np = NeoPixel(pin, 30) # create NeoPixel driver on GPIO0 for 8 pixels for x in range(30): r = random.randint(0, 100) g = random.randint(0, 100) b = random.randint(0, 100) np[x] = (r, g, b) np.write() # write data to all pixels
def set_range(cls, pixels: NeoPixel, range_slice: slice, col: RGBBytesColor, clear_others: bool = False): if clear_others: pixels.fill(COL_BLACK) pix_range = NeoPixelRange(pixels, range_slice) pix_range.set_colors(col)
class NeoPixelPlayer: def __init__(self, pin, nlength): self.nlength = nlength self.np = NeoPixel(Pin(pin), nlength) #input length self.ilength = nlength def out(self, output): self.np.buf = output self.np.write()
def __init__(self): sta_if = WLAN(STA_IF) self.ip_address = sta_if.ifconfig()[0] self.port = 80 # set your port here pin_id = 5 nparray = 60 self.conn = None self.s = None self.np = NeoPixel(Pin(pin_id), nparray) self.run_socket()
def led_game(): np = NeoPixel(pin, LED) for column, row in game.get_block_coord(): number_of_led = row + 2 + (11 - column) * 25 np[number_of_led] = led_color(game.block_color, 15) for (led_column, led_row), color in game.waste_dict.items(): if led_row >= 0 and led_row < 12 and led_column >= 0 and led_column < 12: number_of_led = led_row + 2 + (11 - led_column) * 25 np[number_of_led] = led_color(color, 20) np.write()
def __init__(self, ledsCount, color, brightness): self.pixels = NeoPixel(board.D18, ledsCount, auto_write=False, brightness=brightness) self.color = color self.brightness = brightness self.pixels.fill(self.color) self.show_pixels()
def main(): enable_speakers() speaker = AudioOut(board.SPEAKER) pixels = NeoPixel(board.NEOPIXEL, PIXEL_COUNT) pixels.brightness = 0.05 handler = Handler(speaker, pixels) events = [TouchEvent(i, handler.handle) for i in TOUCH_PADS] while True: for event in events: event.process()
def __init__(self, room_num, mfrc_reader, remote_addr, mqtt_obj, aes_key, aes_iv): self.room = room_num self.rdr = mfrc_reader self.server_addr = remote_addr self.mqtt = mqtt_obj self.__key = aes_key self.__iv = aes_iv # Only using the neopixel instance for showing visually if the RFID tag was valid self.np = NeoPixel(Pin(17,Pin.OUT),32) print("RFID Door Unlock Client created") self.__running_thread = _thread.start_new_thread(self.run, tuple())
class DHT11(DHTBase): def humidity(self): return self.buf[0] def temperature(self): return self.buf[2] class DHT22(DHTBase): def humidity(self): return(self.buf[0]<<8|self.buf[1])*0.1 def temperature(self): t=((self.buf[2]&0x7f)<<8|self.buf[3])*0.1 if self.buf[2]&0x80: t=-t return t buzz=Buzz() oled=OLED() display=oled accelerometer=Accelerometer() rgb=NeoPixel(Pin(17,Pin.OUT),3,3,1) rgb.write() light=ADC(Pin(39)) sound=ADC(Pin(36)) ext=ADC(Pin(34)) button_a=Pin(0,Pin.IN,Pin.PULL_UP) button_b=Pin(2,Pin.IN,Pin.PULL_UP) touchPad_P=TouchPad(Pin(27)) touchPad_Y=TouchPad(Pin(14)) touchPad_T=TouchPad(Pin(12)) touchPad_H=TouchPad(Pin(13)) touchPad_O=TouchPad(Pin(15)) touchPad_N=TouchPad(Pin(4))
from microbit import * from neopixel import NeoPixel np = NeoPixel(pin0,5) np.clear() pin1.set_analog_period_microseconds(3000) pin2.set_analog_period_microseconds(3000) fwd = 255 bck = 767 off = 511 state = 4 numactions = 4 action = 0 def forward(t): pin1.write_analog(fwd) pin2.write_analog(bck) sleep(t) pin1.write_digital(0) pin2.write_digital(0) def backward(t): pin1.write_analog(bck) pin2.write_analog(fwd) sleep(t) pin1.write_digital(0) pin2.write_digital(0)