def __init__(self, name, onchange): self.name = name self.last = False self.onchange = onchange pin = getattr(board, name) self.touch = TouchIn(pin) self.touch.threshold += self.THRESHOLD_ADJUSTMENT
def on(*buttons, fn=None, action=DOWN): # type: (Any, Callable, int) -> Callable global GAMEPAD for button in buttons: if button not in BUTTONS: if button in DIGITALIO: dio = DigitalInOut(button) dio.direction = Direction.INPUT dio.pull = Pull.DOWN elif button in TOUCHIO: dio = TouchIn(button) else: print("unknown button {}".format(button)) BUTTONS.append(button) DIOS.append(dio) value = tuple(buttons) def wrapper(fn): PRESSES.append((fn, buttons, action)) return fn if fn: return wrapper(fn) return wrapper
def wrapper(fn): # type: (Callable) -> Callable btn_id = id(button) if btn_id not in self.inputs: if touch: dio = TouchIn(button) else: dio = DigitalInOut(button) dio.direction = Direction.INPUT dio.pull = Pull.DOWN self.inputs[btn_id] = (dio, False, 0) if btn_id not in self.input_hooks: self.input_hooks[btn_id] = [fn] else: self.input_hooks[btn_id].append(fn) return fn
def main(): touch2 = TouchIn(board.A2) # stretch goal of switching by touch delay = 0.02 # let's have fun with this while True: if touch2.value: print("A2 touched!") delay = delay / 2 dot[0] = (0xFF, 0x14, 0x93) fade(LOW, HIGH, 1, delay) # up fade(HIGH, LOW, -1, delay) # down
def main(): led_on = dot_on = True touch0 = TouchIn(board.A0) touch2 = TouchIn(board.A2) while True: if touch0.value: if led_on: print("Turn Off") led_on = False else: print("Turn On") led_on = True led.value = led_on if touch2.value: if dot_on: print("Turn Off") dot_on = False dot[0] = (0,0,0) else: print("Turn On") dot_on = True dot[0] = (0xFF,0x14,0x93) sleep(0.2)
from digitalio import DigitalInOut, Direction from touchio import TouchIn import adafruit_dotstar as dotstar import board import time # One pixel connected internally! dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1) dot.brightness = 0 # Built in red LED led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT # Capacitive touch touch0 = TouchIn(board.A0) touch1 = TouchIn(board.A1) touch2 = TouchIn(board.A2) # based on experimentation. anything less than 500 loops is a 'dit', anything more is a 'dah' dash_length = 500 morse_code_dict = { '.-': 'a', '-...': 'b', '-.-.': 'c', '-..': 'd', '.': 'e', '..-.': 'f', '--.': 'g', '....': 'h',
brightNess = { # Brightness on LEDs isn't linear, nor is the human eye. 0: 0.017, 1: 0.078, 2: 0.36, 3: 1.00 } dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=1) # Built-in red LED led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT # Capacitive touch changePulse = TouchIn(board.A0) changeBrightness = TouchIn(board.A2) def updateDelay(): # Get the chip temperature temp = microcontroller.cpu.temperature # Convert to F temp = temp * 9.0 / 5.0 + 32.0 print("Temp: %0.1f" % temp) # Calculate our delay function newDelay = (80 - temp) + 15 if newDelay < 5: return 5 if newDelay > 25:
import adafruit_dotstar as dotstar import microcontroller import board import time import busio import array # One pixel connected internally! dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Built in red LED led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT # Capacitive touch on A2 touch_in = TouchIn(board.A0) uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=10) arr_on = bytes("Y", ascii) arr_off = bytes("N", ascii) time.sleep(1) # Sleep for a bit to avoid a race condition on some systems ######################### HELPERS ############################## # Helper to convert analog input to voltage # Helper to give us a nice color swirl 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.
import microcontroller import board import time import random import math from perlin import gen_perlin # One pixel connected internally! dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.3) # Built in red LED led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT # Capacitive touch on A2 touch2 = TouchIn(board.A2) # Used if we do HID output, see below kbd = Keyboard() REG_BRIGHTNESS = 0.05 FLASH_BRIGHTNESS = 0.2 ######################### HELPERS ############################## def hue_to_rgb(hue): """ Convert float hue value to a rgb color value. Probably doesn't match actual hsv conversion, but good for rainbows. """ return wheel(hue * 255)
from touchio import TouchIn import adafruit_dotstar as dotstar import board import time dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.5) colors = [0,0,0] a0 = TouchIn(board.A0) a1 = TouchIn(board.A1) a2 = TouchIn(board.A2) while True: if a0.value: colors[0] = 255 else: colors[0] = 0 if a1.value: colors[1] = 255 else: colors[1] = 0 if a2.value: colors[2] = 255 else: colors[2] = 0 dot[0] = colors dot.show()
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False) # One pixel connected internally! dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Built in red LED led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT # Analog input on A1 analog1in = AnalogIn(board.A1) # Capacitive touch on A2 touch = TouchIn(board.A2) # Helper to convert analog input to voltage def getVal(): Valpot = analog1in.value / 256 if Valpot > 255: Valpot = 255 if Valpot < 0: Valpot = 0 Valpot = int(Valpot) return (Valpot) ValR = 0 ValG = 0
from touchio import TouchIn import adafruit_dotstar as dotstar import time import board import neopixel jewel = neopixel.NeoPixel(board.A1, 7, brightness=0.1, auto_write=True) dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) touch = TouchIn(board.A0) # touch.threshold = touch.raw_value - 50 touch.threshold = touch.raw_value + 100 count = 0 def handleColor(count, jewel): red = (255, 0, 0) magenta = (255, 0, 255) purple = (149, 66, 244) color4 = (0, 255, 255) color5 = (0, 0, 255) color6 = (0, 255, 0) color7 = (0, 255, 50) colorPositions = [red, magenta, purple, color4, color5, color6, color7] count += 1 if count > 6: count = 0
from adafruit_circuitplayground.express import cpx from time import sleep # the easy way to do touch with a CPX is just to call cpx.touch_A1, cpx.touch_A2, etc. # you can also use the board and touchio modules from touchio import TouchIn import board pinArray = [ TouchIn(board.A1), TouchIn(board.A2), TouchIn(board.A3), TouchIn(board.A4), TouchIn(board.A5), TouchIn(board.A6), TouchIn(board.A7) ] # these are the NeoPixels closest to teach touch pin pixArray = [6, 8, 9, 1, 2, 3, 4] while True: noTouch = True for num, pin in enumerate(pinArray): if pin.value: # if a pin is touched, num will be the index in the pinArray print(num) cpx.start_tone(220 * num) noTouch = False # a pin was touched cpx.pixels[pixArray[num]] = (100, 0, 0) if noTouch: # if no pin is touched, no sound and no light
def __init__(self, pin, name): self.pin = TouchIn(pin) self.name = name
pixel_pin = board.D1 # The pin the NeoPixels are connected to pixel_count = 4 # Number of pixels in your strip color_change = 16 # Number to increase color change (how fast you want it) pixels = neopixel.NeoPixel(pixel_pin, pixel_count, brightness=.4, auto_write=False) # One pixel connected internally! dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=1.0) # Capacitive touch on A1 touch1 = TouchIn(board.A1) # Analog input on A2 analog1in = AnalogIn(board.A2) def wheel(pos): """ pos: place on color wheel of 0-255 values Helper to give us a nice color swirl Thank you Adafruit for the nice wheel routine! 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):
from analogio import AnalogIn, AnalogOut from touchio import TouchIn import adafruit_dotstar as dotstar import microcontroller import board import time # One pixel connected internally! dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.3) # Built in red LED led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT # Analog output on A0 aout = TouchIn(board.A0) # Analog input on A1 analog1in = AnalogIn(board.A1) # Capacitive touch on A2 touch2 = TouchIn(board.A2) # Used if we do HID output, see below kbd = Keyboard() ######################### HELPERS ############################## # Helper to convert analog input to voltage def getVoltage(pin):
# Gemma IO demo # Welcome to CircuitPython 2.2.4 :) from touchio import TouchIn import microcontroller import neopixel import board import time num_pixels = 16 pixels = neopixel.NeoPixel(board.A1, num_pixels, brightness=0.02, auto_write=False) touch2 = TouchIn(board.A2) touch0 = TouchIn(board.A0) ######################### HELPERS ############################## # Helper to give us a nice color swirl 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): return [0, 0, 0] if (pos > 255): return [0, 0, 0] if (pos < 85): return [int(pos * 3), int(255 - (pos * 3)), 0]
from touchio import TouchIn import adafruit_dotstar as dotstar import board import neopixel from hoodie import Hoodie import time jewel = neopixel.NeoPixel(board.A1, 7, brightness=0.1, auto_write=True) dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) touchL = TouchIn(board.A0) touchR = TouchIn(board.A2) touchL.threshold = touchL.raw_value + 200 touchR.threshold = touchR.raw_value + 200 hoodie = Hoodie(jewel) while True: # print(touchL.raw_value) if touchL.value and touchR.value: print("touched both") hoodie.handleColor(hoodie.HOP) elif touchL.value: print("touched left") dot[0] = [0, 255, 0] hoodie.handleColor(hoodie.WHEEL) elif touchR.value: print("touched right")