class Led(object): def __init__(self, serial_object): self._logger = logging.getLogger(__name__) self.pixels = NeoPixel(crickit.seesaw, 20, 12) def on(self, red, green, blue): self._logger.debug("Setting LED to (%d, %d, %d)", red, green, blue) self.pixels.fill((red, green, blue)) def off(self): self._logger.debug("Turning LED off") self.pixels.fill((0, 0, 0))
class Lights(): def __init__(self, num_pixels): self.pixels = NeoPixel(crickit.seesaw, 20, num_pixels) self.pixels.fill((0, 0, 0)) self.num_pixels = num_pixels self.pixels.fill((0, 0, 0)) def updateLEDs(self, newColor, delay): r = 0 g = 0 b = 0 while (r < newColor[0] or g < newColor[1] or b < newColor[2]): if (r < newColor[0]): r += 1 if (g < newColor[1]): g += 1 if (b < newColor[2]): b += 1 self.pixels.fill((r, g, b)) time.sleep(delay) def fillEach(self, color, delay): for x in range(self.num_pixels): self.pixels[x] = color time.sleep(delay)
def start_capture(category, interval, num_pics): camera = picamera.PiCamera() capture_home_directory = "teachable_machine/" #NeoPixel num_pixels = 16 # 16 for old robot - change for the number your NeoPixel ring has pixels = NeoPixel(crickit.seesaw, 20, num_pixels, brightness=0.4, pixel_order=(1, 0, 2, 3), bpp=4) neo_black = (0,0,0,0) #rgbw blink_color = (256,256,256,256) #rgbw cap_directory = capture_home_directory + category CHECK_FOLDER = os.path.isdir(cap_directory) # If folder doesn't exist, then create it. if not CHECK_FOLDER: os.makedirs(cap_directory) print("created folder : ", cap_directory) else: print(cap_directory, "folder already exists.") intro = "about to start capturing " + str(num_pics) + " images for " + category print(intro) tts_pico.speak(intro, "GB") for i in range(1,num_pics + 1, 1): image_name = cap_directory + "/" + category + "-" + str(i).zfill(2) + ".jpg" time.sleep(interval) pixels.fill(blink_color) tts_pico.speak(category + ", " + str(i) + "of " + str(num_pics) + ", in 3, 2, 1", "GB") # pixels.fill(neo_black) # time.sleep(0.2) print("image name: ", image_name) camera.capture(image_name) time.sleep(0.5) pixels.fill(neo_black) time.sleep(1) tts_pico.speak("all finished with " + category, "GB") camera.close()
num_pixels = 64 # Number of pixels driven from Crickit NeoPixel terminal # The following line sets up a NeoPixel strip on Seesaw pin 20 for Feather pixelss = NeoPixel(crickit.seesaw, 20, num_pixels, brightness=0.05) RED = (255, 0, 0) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) ORANGE = (255, 125, 0) #for i in range(1, 3): pixelss.fill(PURPLE) pixelss.show() #print(i) #time.sleep(1) a = 0 # Create one continuous servo on crickit servo port #1 left_wheel = crickit.dc_motor_1 # Create one continuous servo on crickit servo port #2 right_wheel = crickit.dc_motor_2 holder = amg.pixels #print(holder) while True:
# Drive NeoPixels on the NeoPixels Block on Crickit FeatherWing import logging import time from adafruit_crickit import crickit from adafruit_seesaw.neopixel import NeoPixel num_pixels = 64 # Number of pixels driven from Crickit NeoPixel terminal #pixels = NeoPixel(crickit.seesaw, 20, 50) #BLACK = (0, 0, 0) #pixels.fill(BLACK) # The following line sets up a NeoPixel strip on Seesaw pin 20 for Feather pixels = NeoPixel(crickit.seesaw, 20, num_pixels) BLACK = (0, 0, 0) pixels.fill(BLACK) time.sleep(0.2) def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if pos < 0 or pos > 255: return (0, 0, 0) if pos < 85: return (255 - pos * 3, pos * 3, 0) if pos < 170: pos -= 85 return (0, 255 - pos * 3, pos * 3) pos -= 170 return (pos * 3, 0, 255 - pos * 3)
if (num % 2) == 0: polyX.append(int(polyNums[num])) else: polyY.append(int(polyNums[num])) print(polyX, polyY) polygon = [(polyX[0], polyY[0]), (polyX[1], polyY[1]), (polyX[2], polyY[2]), (polyX[3], polyY[3]), (polyX[4], polyY[4]), (polyX[5], polyY[5])] cam = PiCamera() #cam.shutter_speed = 1000000 cam.iso = 400 cam.resolution = (800, 600) pixels.fill((0, 0, 0, 255)) #sleep(1) cam.start_preview() sleep(2) cam.exposure_mode = 'off' cam.capture('cube.jpg') cam.stop_preview() pixels.fill((0, 0, 0, 0)) image = Image.open('cube.jpg') mask = Image.new("L", image.size, 0) draw = ImageDraw.Draw(mask) draw.polygon(polygon, fill=255, outline=None) black = Image.new("RGBA", image.size, 0) result = Image.composite(image, black, mask) resultcrop = result.crop((min(polyX), min(polyY), max(polyX), max(polyY)))
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT # Using Crickit's onboard NeoPixel # See http://www.color-hex.com/ for more colors and find your fav! from adafruit_crickit import crickit from adafruit_seesaw.neopixel import NeoPixel crickit_status_pixel = NeoPixel(crickit.seesaw, 27, 1) # one NeoPixel pin 27 # Fill them with our favorite color "#0099FF light blue" -> 0x0099FF crickit_status_pixel.fill(0x0099FF) while True: pass
rc_index = (i * 256 // num_pixels) + j pixels[i] = wheel(rc_index & 255) pixels.show() time.sleep(wait) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) while True: print("fill") pixels.fill(RED) pixels.show() # Increase or decrease to change the speed of the solid color change. time.sleep(1) pixels.fill(GREEN) pixels.show() time.sleep(1) pixels.fill(BLUE) pixels.show() time.sleep(1) print("chase") color_chase(RED, 0.1) # Increase the number to slow down the color chase color_chase(YELLOW, 0.1) color_chase(GREEN, 0.1) color_chase(CYAN, 0.1)
class Crickit: """Represents a Crickit board. Provides a number of devices available via properties, such as ``servo_1``. Devices are created on demand the first time they are referenced. It's fine to refer a device multiple times via its property, but it's faster and results in more compact code to assign a device to a variable. .. code-block:: python import time from adafruit_crickit import crickit # This is fine: crickit.servo_1.angle = 0 time.sleep(1) crickit.servo_1.angle = 90 time.sleep(1) # This is slightly faster and more compact: servo_1 = crickit.servo_1 servo_1.angle = 0 time.sleep(1) servo_1.angle = 90 time.sleep(1) """ SIGNAL1 = 2 """Signal 1 terminal""" SIGNAL2 = 3 """Signal 2 terminal""" SIGNAL3 = 40 """Signal 3 terminal""" SIGNAL4 = 41 """Signal 4 terminal""" SIGNAL5 = 11 """Signal 5 terminal""" SIGNAL6 = 10 """Signal 6 terminal""" SIGNAL7 = 9 """Signal 7 terminal""" SIGNAL8 = 8 """Signal 8 terminal""" def __init__(self, seesaw): self._seesaw = seesaw self._seesaw.pin_mapping = Crickit_Pinmap # Associate terminal(s) with certain devices. # Used to find existing devices. self._devices = dict() self._neopixel = None self._onboard_pixel = None @property def seesaw(self): """The Seesaw object that talks to the Crickit. Use this object to manipulate the signal pins that correspond to Crickit terminals. .. code-block:: python from adafruit_crickit import crickit ss = crickit.seesaw ss.pin_mode(crickit.SIGNAL4, ss.OUTPUT) ss.digital_write(crickit.SIGNAL4], True) """ return self._seesaw @property def servo_1(self): """``adafruit_motor.servo.Servo`` object on Servo 1 terminal""" return self._servo(_SERVO1, Servo) @property def servo_2(self): """``adafruit_motor.servo.Servo`` object on Servo 2 terminal""" return self._servo(_SERVO2, Servo) @property def servo_3(self): """``adafruit_motor.servo.Servo`` object on Servo 3 terminal""" return self._servo(_SERVO3, Servo) @property def servo_4(self): """``adafruit_motor.servo.Servo`` object on Servo 4 terminal""" return self._servo(_SERVO4, Servo) @property def continuous_servo_1(self): """``adafruit_motor.servo.ContinuousServo`` object on Servo 1 terminal""" return self._servo(_SERVO1, ContinuousServo) @property def continuous_servo_2(self): """``adafruit_motor.servo.ContinuousServo`` object on Servo 2 terminal""" return self._servo(_SERVO2, ContinuousServo) @property def continuous_servo_3(self): """``adafruit_motor.servo.ContinuousServo`` object on Servo 3 terminal""" return self._servo(_SERVO3, ContinuousServo) @property def continuous_servo_4(self): """``adafruit_motor.servo.ContinuousServo`` object on Servo 4 terminal""" return self._servo(_SERVO4, ContinuousServo) def _servo(self, terminal, servo_class): device = self._devices.get(terminal, None) if not isinstance(device, servo_class): pwm = PWMOut(self._seesaw, terminal) pwm.frequency = 50 device = servo_class(pwm) self._devices[terminal] = device return device @property def dc_motor_1(self): """``adafruit_motor.motor.DCMotor`` object on Motor 1 terminals""" return self._motor(_MOTOR1, DCMotor) @property def dc_motor_2(self): """``adafruit_motor.motor.DCMotor`` object on Motor 2 terminals""" return self._motor(_MOTOR2, DCMotor) @property def stepper_motor(self): """``adafruit_motor.motor.StepperMotor`` object on Motor 1 and Motor 2 terminals""" return self._motor(_MOTOR_STEPPER, StepperMotor) @property def drive_stepper_motor(self): """``adafruit_motor.motor.StepperMotor`` object on Drive terminals""" return self._motor(_DRIVE_STEPPER, StepperMotor) @property def feather_drive_stepper_motor(self): """``adafruit_motor.motor.StepperMotor`` object on Drive terminals on Crickit FeatherWing""" return self._motor(tuple(reversed(_DRIVE_STEPPER)), StepperMotor) def _motor(self, terminals, motor_class): device = self._devices.get(terminals, None) if not isinstance(device, motor_class): device = motor_class( *(PWMOut(self._seesaw, terminal) for terminal in terminals) ) self._devices[terminals] = device return device @property def drive_1(self): """``adafruit_seesaw.pwmout.PWMOut`` object on Drive 1 terminal, with ``frequency=1000``""" return self._drive(_DRIVE1) @property def drive_2(self): """``adafruit_seesaw.pwmout.PWMOut`` object on Drive 2 terminal, with ``frequency=1000``""" return self._drive(_DRIVE2) @property def drive_3(self): """``adafruit_seesaw.pwmout.PWMOut`` object on Drive 3 terminal, with ``frequency=1000``""" return self._drive(_DRIVE3) @property def drive_4(self): """``adafruit_seesaw.pwmout.PWMOut`` object on Drive 4 terminal, with ``frequency=1000``""" return self._drive(_DRIVE4) feather_drive_1 = drive_4 """``adafruit_seesaw.pwmout.PWMOut`` object on Crickit Featherwing Drive 1 terminal, with ``frequency=1000`` """ feather_drive_2 = drive_3 """``adafruit_seesaw.pwmout.PWMOut`` object on Crickit Featherwing Drive 2 terminal, with ``frequency=1000`` """ feather_drive_3 = drive_2 """``adafruit_seesaw.pwmout.PWMOut`` object on Crickit Featherwing Drive 3 terminal, with ``frequency=1000`` """ feather_drive_4 = drive_1 """``adafruit_seesaw.pwmout.PWMOut`` object on Crickit Featherwing Drive 4 terminal, with ``frequency=1000`` """ def _drive(self, terminal): device = self._devices.get(terminal, None) if not isinstance(device, PWMOut): device = PWMOut(self._seesaw, terminal) device.frequency = 1000 self._devices[terminal] = device return device @property def touch_1(self): """``adafruit_crickit.CrickitTouchIn`` object on Touch 1 terminal""" return self._touch(_TOUCH1) @property def touch_2(self): """``adafruit_crickit.CrickitTouchIn`` object on Touch 2 terminal""" return self._touch(_TOUCH2) @property def touch_3(self): """``adafruit_crickit.CrickitTouchIn`` object on Touch 3 terminal""" return self._touch(_TOUCH3) @property def touch_4(self): """``adafruit_crickit.CrickitTouchIn`` object on Touch 4 terminal""" return self._touch(_TOUCH4) def _touch(self, terminal): touch_in = self._devices.get(terminal, None) if not touch_in: touch_in = CrickitTouchIn(self._seesaw, terminal) self._devices[terminal] = touch_in return touch_in @property def neopixel(self): """```adafruit_seesaw.neopixel`` object on NeoPixel terminal. Raises ValueError if ``init_neopixel`` has not been called. """ if not self._neopixel: raise ValueError("Call init_neopixel first") return self._neopixel def init_neopixel( self, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None ): """Set up a seesaw.NeoPixel object .. note:: On the CPX Crickit board, the NeoPixel terminal is by default controlled by CPX pin A1, and is not controlled by seesaw. So this object will not be usable. Instead, use the regular NeoPixel library and specify ``board.A1`` as the pin. You can change the jumper connection on the bottom of the CPX Crickit board to move control of the NeoPixel terminal to seesaw pin #20 (terminal.NEOPIXEL). In addition, the Crickit FeatherWing always uses seesaw pin #20. In either of those cases, this object will work. .. code-block:: python from adafruit_crickit.crickit import crickit crickit.init_neopixel(24) crickit.neopixel.fill((100, 0, 0)) """ from adafruit_seesaw.neopixel import ( # pylint: disable=import-outside-toplevel NeoPixel, ) self._neopixel = NeoPixel( self._seesaw, _NEOPIXEL, n, bpp=bpp, brightness=brightness, auto_write=auto_write, pixel_order=pixel_order, ) @property def onboard_pixel(self): """```adafruit_seesaw.neopixel`` object on the Seesaw on-board NeoPixel. Initialize on-board NeoPixel and clear upon first use. """ if not self._onboard_pixel: from adafruit_seesaw.neopixel import ( # pylint: disable=import-outside-toplevel NeoPixel, ) self._onboard_pixel = NeoPixel( self._seesaw, _SS_PIXEL, 1, bpp=3, brightness=1.0, auto_write=True, pixel_order=None, ) self._onboard_pixel.fill((0, 0, 0)) return self._onboard_pixel def reset(self): """Reset the whole Crickit board.""" self._seesaw.sw_reset()
blink = False blink_state = False # blink_color = (127,0,0) #rgb blink_color = (127,0,0,0) #rgbw # neo_black = (0,0,0) #rgb neo_black = (0,0,0,0) #rgbw blink_delay = 0.1 blink_times = 2 blink_next_time = time.time() + blink_delay # bpp=4 is required for RGBW NeoPixels pixels = NeoPixel(crickit.seesaw, 20, num_pixels, brightness=0.02, pixel_order=(1, 0, 2, 3), bpp=4) # bpp=3 is required for RGB NeoPixels # pixels = NeoPixel(crickit.seesaw, 20, num_pixels, brightness=0.04, pixel_order=neopixel.RGB, bpp=3) # black out the LEDs on start pixels.fill(neo_black) # there was a bug in the neopixel lib that ignores zeros in rgbw # https://github.com/adafruit/Adafruit_CircuitPython_seesaw/issues/32 # DEFINE sensors # For signal control, we'll chat directly with seesaw, use 'ss' to shorted typing! ss = crickit.seesaw # analogin = False analog_interval = .5 analog_next_time = time.time() + analog_interval # ports to be scanned each interval analog_ports = [False,False,False,False,False,False,False,False,False] touch_interval = .5 touch_next_time = time.time() + touch_interval
pixels.show() time.sleep(wait) BRIGHTWHITE = (255, 255, 255, 255) WHITE = (0, 0, 0, 255) RED = (255, 0, 0, 0) YELLOW = (255, 150, 0, 0) GREEN = (0, 255, 0, 0) CYAN = (0, 255, 255, 0) BLUE = (0, 0, 255, 0) PURPLE = (180, 0, 255, 0) while True: print("fill") pixels.fill(WHITE) pixels.show() time.sleep(1) # Increase or decrease to change the speed of the solid color change. time.sleep(1) pixels.fill(BRIGHTWHITE) pixels.show() time.sleep(1) pixels.fill(BLUE) pixels.show() time.sleep(1) print("chase") color_chase(RED, 0.1) # Increase the number to slow down the color chase color_chase(YELLOW, 0.1) color_chase(GREEN, 0.1)
# tweet and flap once for every hour for x in range(i): beakOpen() tweet() time.sleep(1) beakClose() time.sleep(1) birdIn() # Turn off all pixels BLACK = (0, 0, 0) if len(ring_colors) == 4: BLACK = BLACK + (0, ) pixels.fill((BLACK)) pixels.show() ### FUNCTIONS # convert minutes to pixel number def minutePixel(i): # make sure i is an int i = int(i) # default px px = 0 # 24 pixel ring if ring_pixels == 24:
from gtts import gTTS import os import time from adafruit_crickit import crickit from adafruit_seesaw.neopixel import NeoPixel import datetime r = 0 g = 0 b = 150 pulse_dir = 10 num_pixels = 74 # Number of pixels driven from Crickit NeoPixel terminal # The following line sets up a NeoPixel strip on Seesaw pin 20 for Feather pixels = NeoPixel(crickit.seesaw, 20, num_pixels) pixels.fill(0) # Audio recording parameters, set for our USB mic. RATE = 48000 #if you change mics - be sure to change this :) CHUNK = int(RATE / 10) # 100ms credential_path = "/home/pi/DET-2019.json" #replace with your file name! os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credential_path client = speech.SpeechClient() pygame.init() pygame.mixer.init() user_report = " " #Some boolean values to control state DEMO = True
def translate(value, leftMin, leftMax, rightMin, rightMax): #Adam Luchjenbroers, 12/8/2009 # Figure out how 'wide' each range is leftSpan = leftMax - leftMin rightSpan = rightMax - rightMin # Convert the left range into a 0-1 range (float) valueScaled = float(value - leftMin) / float(leftSpan) # Convert the 0-1 range into a value in the right range. return rightMin + (valueScaled * rightSpan) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) BLANK = (0, 0, 0) while True: print("blank") pixels.fill(BLANK) pixels.show() time.sleep(3) pot_chase(0)
blink = False blink_state = False blink_color = (127, 0, 0, 0) blink_delay = 0.1 blink_times = 2 blink_next_time = time.time() + blink_delay # bpp=4 is required for RGBW pixels = NeoPixel(crickit.seesaw, 20, num_pixels, brightness=0.02, pixel_order=neopixel.RGBW, bpp=4) # black out the LEDs pixels.fill( (1, 2, 3, 0)) # there's a bug in the neopixel lib that ignores zeros in rgbw # https://github.com/adafruit/Adafruit_CircuitPython_seesaw/issues/32 # DEFINE sensors # For signal control, we'll chat directly with seesaw, use 'ss' to shorted typing! ss = crickit.seesaw # analogin = False analog_interval = .5 analog_next_time = time.time() + analog_interval # ports to be scanned each interval analog_ports = [False, False, False, False, False, False, False, False, False] touch_interval = .5 touch_next_time = time.time() + touch_interval # ports to be scanned each interval
class MyNeoPixels: def __init__(self, n, seesaw=crickit.seesaw, pin=20, bpp=3, brightness=1.0, auto_write=True, pixel_order=None): self.numPixels = n self.pixels = NeoPixel( seesaw, pin, n, auto_write=False) #, bpp, brightness, auto_write, pixel_order) self.pixels.brightness = 0.5 def setPixelColor(self, idx, color): self.pixels[idx] = color def show(self): self.pixels.show() return def fill(self, color): self.pixels.fill(color) # Define functions which animate LEDs in various ways. def colorWipe(self, color, wait_ms=50): """Wipe color across display a pixel at a time.""" for i in range(self.numPixels): self.setPixelColor(i, color) self.show() time.sleep(wait_ms / 1000.0) def theaterChase(self, color, wait_ms=50, iterations=10): """Movie theater light style chaser animation.""" for j in range(iterations): for q in range(3): for i in range(0, self.numPixels, 3): self.setPixelColor(i + q, color) self.show() time.sleep(wait_ms / 1000.0) for i in range(0, self.numPixels, 3): self.setPixelColor(i + q, 0) def rainbow(self, wait_ms=20, iterations=1): """Draw rainbow that fades across all pixels at once.""" for j in range(256 * iterations): for i in range(self.numPixels): self.setPixelColor(i, wheel((i + j) & 255)) self.show() time.sleep(wait_ms / 1000.0) def rainbowCycle(self, wait_ms=20, iterations=5): """Draw rainbow that uniformly distributes itself across all pixels.""" for j in range(256 * iterations): for i in range(self.numPixels): self.setPixelColor( i, wheel((int(i * 256 / self.numPixels) + j) & 255)) self.show() time.sleep(wait_ms / 1000.0) def theaterChaseRainbow(self, wait_ms=50): """Rainbow movie theater light style chaser animation.""" for j in range(256): for q in range(3): for i in range(0, self.numPixels, 3): self.setPixelColor(i + q, wheel((i + j) % 255)) self.show() time.sleep(wait_ms / 1000.0) for i in range(0, self.numPixels, 3): self.setPixelColor(i + q, 0) def set_neopixels(self, color, count): if count > self.numPixels: count = self.numPixels logging.info("setting %d pixesl to %s" % (count, color)) for i in range(0, self.numPixels): if i < count: self.setPixelColor(i, color) else: self.setPixelColor(i, colors['off']) self.show() # health will be a setting of 10 pixels, and the number will be out of 100 def health(self, color, health, tip='off', wait_ms=50): self.set_neopixels(color, health) time.sleep(0.002) self.setPixelColor(self.numPixels - 1, colors[tip]) self.show() ############## operations = { # custom = has A, B, C, D #'magic_item': magic_item, # needs colour and count 'set': set_neopixels, 'health': health, #needs colour 'colourwipe': colorWipe, 'theatrechase': theaterChase, #no colour option 'rainbow': rainbow, 'rainbow_cycle': rainbowCycle, } ############### def play(self, payload={}): operationname = get(payload, 'operation', 'colourwipe') operation = get(MyNeoPixels.operations, operationname, MyNeoPixels.operations['colourwipe']) logging.info("playing %s" % operationname) if operationname == 'magic_item': operation(self, payload) return if operationname == 'rainbow' or operationname == 'rainbow_cycle': operation(self) return colourname = get(payload, 'colour', 'off') colour = get(colors, colourname, colors['off']) # TODO: maybe change to using HTML colors #000000 style? if operationname == 'colourwipe' or operationname == 'theatrechase': operation(self, colour) return count = get(payload, 'count', 10) operation(self, colour, count)