Code for Storage and Cap Touch templates.

This commit is contained in:
Kattni Rembor 2021-05-06 13:46:13 -04:00
parent 3817cc65b6
commit 46a3ccbf6b
8 changed files with 223 additions and 0 deletions

View file

@ -0,0 +1,19 @@
"""
CircuitPython Capacitive Touch Pad Example - Print to the serial console when one pad is touched.
Update TOUCH_PAD_PIN to the touch pad pin name for the board you're using.
For example:
If you are using the BLM Badge, update TOUCH_PAD_PIN to CAP1.
If using a CPX, update TOUCH_PAD_PIN to A1.
"""
import time
import board
import touchio
touch = touchio.TouchIn(board.TOUCH_PAD_PIN)
while True:
if touch.value:
print("Pad touched!")
time.sleep(0.1)

View file

@ -0,0 +1,18 @@
"""
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
Update TOUCH_PIN to the touch-capable pin name for the board you're using.
For example:
If you are using A0 on the Feather RP2040, update TOUCH_PIN to A0.
"""
import time
import board
import touchio
touch = touchio.TouchIn(board.TOUCH_PIN)
while True:
if touch.value:
print("Pin touched!")
time.sleep(0.1)

View file

@ -0,0 +1,39 @@
"""CircuitPython Touch-Compatible Pin Identification Script"""
import board
import touchio
from microcontroller import Pin
def is_touch_capable(pin_name):
"""Attempts to create touchio.TouchIn() object on all available pins. Returns True if valid."""
try:
_ = touchio.TouchIn(pin_name)
# Print the touch-capable pins that do not need, or already have, an external pulldown.
return True
except ValueError as e: # A ValueError is raised when a pin is invalid or needs a pulldown.
x = getattr(e, "message", str(e)) # Obtain the message associated with the ValueError.
if "pulldown" in x: # If the ValueError is regarding needing a pulldown...
return True # ...the pin is valid.
else:
return False # Otherwise, the pins are invalid.
except TypeError: # Error returned when checking a non-pin object in dir(board).
pass # Passes over non-pin objects in dir(board).
def get_pin_names():
"""Gets all unique pin names available in the board module, excluding a defined list."""
exclude = ["NEOPIXEL", "APA102_MOSI", "APA102_SCK", "LED", "NEOPIXEL_POWER", "BUTTON",
"BUTTON_UP", "BUTTON_DOWN", "BUTTON_SELECT", "DOTSTAR_CLOCK", "DOTSTAR_DATA",
"IR_PROXIMITY"]
pins = [pin for pin in [getattr(board, p) for p in dir(board) if p not in exclude]
if isinstance(pin, Pin)]
pin_names = []
for p in pins:
if p not in pin_names:
pin_names.append(p)
return pin_names
for possible_touch_pin in get_pin_names(): # Get the pin name.
if is_touch_capable(possible_touch_pin): # Check if the pin is touch-capable.
print("Touch on:", str(possible_touch_pin).replace("board.", "")) # Print the valid list.

View file

@ -0,0 +1,23 @@
"""
CircuitPython Capacitive Two Touch Pad Example - Print to the serial console when a pad is touched.
Update TOUCH_PAD_PIN_ONE to the first touch pad pin name for the board you're using.
Update TOUCH_PAD_PIN_TWO to the pin name for the second touch pad.
For example:
If you are using the BLM Badge, update TOUCH_PAD_PIN_ONE to CAP1, and TOUCH_PAD_PIN_TWO to CAP2.
If using a CPX, update TOUCH_PAD_PIN to A1, and TOUCH_PAD_PIN_TWO to A2.
"""
import time
import board
import touchio
touch_one = touchio.TouchIn(board.TOUCH_PAD_PIN_ONE)
touch_two = touchio.TouchIn(board.TOUCH_PAD_PIN_TWO)
while True:
if touch_one.value:
print("Pad one touched!")
if touch_two.value:
print("Pad two touched!")
time.sleep(0.1)

View file

@ -0,0 +1,22 @@
"""
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
Update TOUCH_PIN_ONE to the first touch-capable pin name for the board you're using.
Update TOUCH_PIN_TWO to the pin name for the second touch-capable pin.
For example:
If you are using A0 and A1 on a Feather RP2040, update TOUCH_PIN_ONE to A0 and TOUCH_PIN_TWO to A2.
"""
import time
import board
import touchio
touch_one = touchio.TouchIn(board.TOUCH_PIN_ONE)
touch_two = touchio.TouchIn(board.TOUCH_PIN_TWO)
while True:
if touch_one.value:
print("Pin one touched!")
if touch_two.value:
print("Pin two touched!")
time.sleep(0.1)

View file

@ -0,0 +1,25 @@
"""
CircuitPython Essentials Storage CP Filesystem boot.py file
There are three things to be updated in this file to match your board:
* Update OBJECT_NAME to match the physical thing you are using, e.g. button or pin.
* Update OBJECT_PIN to match the pin name to which the button or pin is attached.
* Update UP_OR_DOWN to match the Pull necessary for the chosen pin.
For example:
If using the up button on a FunHouse, update OBJECT_NAME to button, and OBJECT_PIN to BUTTON_UP.
If using pin A0 on a Feather RP2040, update OBJECT_NAME to pin, and OBJECT_PIN to A0.
For example:
If using the up button on a FunHouse, update UP_OR_DOWN to DOWN.
IF using pin A0 on a Feather RP2040, update UP_OR_DOWN to UP.
"""
import board
import digitalio
import storage
OBJECT_NAME = digitalio.DigitalInOut(board.OBJECT_PIN)
OBJECT_NAME.switch_to_input(pull=digitalio.Pull.UP_OR_DOWN)
# If the OBJECT_NAME is connected to ground, the filesystem is writable by CircuitPython
storage.remount("/", readonly=OBJECT_NAME.value)

View file

@ -0,0 +1,37 @@
"""
CircuitPython Essentials Storage CP Filesystem code.py file
For use with boards with a built-in red LED.
"""
import time
import board
import digitalio
import microcontroller
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()
try:
with open("/temperature.txt", "a") as temp_log:
while True:
# The microcontroller temperature in Celsius. Include the
# math to do the C to F conversion here, if desired.
temperature = microcontroller.cpu.temperature
# Write the temperature to the temperature.txt file every 10 seconds.
temp_log.write('{0:.2f}\n'.format(temperature))
temp_log.flush()
# Blink the LED on every write...
led.value = True
time.sleep(1) # ...for one second.
led.value = False # Then turn it off...
time.sleep(9) # ...for the other 9 seconds.
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
delay = 0.5 # ...blink the LED every half second.
if e.args[0] == 28: # If the file system is full...
delay = 0.15 # ...blink the LED every 0.15 seconds!
while True:
led.value = not led.value
time.sleep(delay)

View file

@ -0,0 +1,40 @@
"""
CircuitPython Essentials Storage CP Filesystem code.py file
For use with boards that have a built-in NeoPixel or NeoPixels, but no little red LED.
It will use only one pixel as an indicator, even if there is more than one NeoPixel.
"""
import time
import board
import microcontroller
import neopixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
try:
with open("/temperature.txt", "a") as temp_log:
while True:
# The microcontroller temperature in Celsius. Include the
# math to do the C to F conversion here, if desired.
temperature = microcontroller.cpu.temperature
# Write the temperature to the temperature.txt file every 10 seconds.
temp_log.write('{0:.2f}\n'.format(temperature))
temp_log.flush()
# Blink the NeoPixel on every write...
pixel.fill((255, 0, 0))
time.sleep(1) # ...for one second.
pixel.fill((0, 0, 0)) # Then turn it off...
time.sleep(9) # ...for the other 9 seconds.
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
delay = 0.5 # ...blink the NeoPixel every half second.
if e.args[0] == 28: # If the file system is full...
delay = 0.15 # ...blink the NeoPixel every 0.15 seconds!
while True:
pixel.fill((255, 0, 0))
time.sleep(delay)
pixel.fill((0, 0, 0))
time.sleep(delay)