Skip to content

Commit 46a3ccb

Browse files
committed
Code for Storage and Cap Touch templates.
1 parent 3817cc6 commit 46a3ccb

8 files changed

Lines changed: 223 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
CircuitPython Capacitive Touch Pad Example - Print to the serial console when one pad is touched.
3+
4+
Update TOUCH_PAD_PIN to the touch pad pin name for the board you're using.
5+
6+
For example:
7+
If you are using the BLM Badge, update TOUCH_PAD_PIN to CAP1.
8+
If using a CPX, update TOUCH_PAD_PIN to A1.
9+
"""
10+
import time
11+
import board
12+
import touchio
13+
14+
touch = touchio.TouchIn(board.TOUCH_PAD_PIN)
15+
16+
while True:
17+
if touch.value:
18+
print("Pad touched!")
19+
time.sleep(0.1)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
3+
4+
Update TOUCH_PIN to the touch-capable pin name for the board you're using.
5+
6+
For example:
7+
If you are using A0 on the Feather RP2040, update TOUCH_PIN to A0.
8+
"""
9+
import time
10+
import board
11+
import touchio
12+
13+
touch = touchio.TouchIn(board.TOUCH_PIN)
14+
15+
while True:
16+
if touch.value:
17+
print("Pin touched!")
18+
time.sleep(0.1)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""CircuitPython Touch-Compatible Pin Identification Script"""
2+
import board
3+
import touchio
4+
from microcontroller import Pin
5+
6+
7+
def is_touch_capable(pin_name):
8+
"""Attempts to create touchio.TouchIn() object on all available pins. Returns True if valid."""
9+
try:
10+
_ = touchio.TouchIn(pin_name)
11+
# Print the touch-capable pins that do not need, or already have, an external pulldown.
12+
return True
13+
except ValueError as e: # A ValueError is raised when a pin is invalid or needs a pulldown.
14+
x = getattr(e, "message", str(e)) # Obtain the message associated with the ValueError.
15+
if "pulldown" in x: # If the ValueError is regarding needing a pulldown...
16+
return True # ...the pin is valid.
17+
else:
18+
return False # Otherwise, the pins are invalid.
19+
except TypeError: # Error returned when checking a non-pin object in dir(board).
20+
pass # Passes over non-pin objects in dir(board).
21+
22+
23+
def get_pin_names():
24+
"""Gets all unique pin names available in the board module, excluding a defined list."""
25+
exclude = ["NEOPIXEL", "APA102_MOSI", "APA102_SCK", "LED", "NEOPIXEL_POWER", "BUTTON",
26+
"BUTTON_UP", "BUTTON_DOWN", "BUTTON_SELECT", "DOTSTAR_CLOCK", "DOTSTAR_DATA",
27+
"IR_PROXIMITY"]
28+
pins = [pin for pin in [getattr(board, p) for p in dir(board) if p not in exclude]
29+
if isinstance(pin, Pin)]
30+
pin_names = []
31+
for p in pins:
32+
if p not in pin_names:
33+
pin_names.append(p)
34+
return pin_names
35+
36+
37+
for possible_touch_pin in get_pin_names(): # Get the pin name.
38+
if is_touch_capable(possible_touch_pin): # Check if the pin is touch-capable.
39+
print("Touch on:", str(possible_touch_pin).replace("board.", "")) # Print the valid list.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
CircuitPython Capacitive Two Touch Pad Example - Print to the serial console when a pad is touched.
3+
4+
Update TOUCH_PAD_PIN_ONE to the first touch pad pin name for the board you're using.
5+
Update TOUCH_PAD_PIN_TWO to the pin name for the second touch pad.
6+
7+
For example:
8+
If you are using the BLM Badge, update TOUCH_PAD_PIN_ONE to CAP1, and TOUCH_PAD_PIN_TWO to CAP2.
9+
If using a CPX, update TOUCH_PAD_PIN to A1, and TOUCH_PAD_PIN_TWO to A2.
10+
"""
11+
import time
12+
import board
13+
import touchio
14+
15+
touch_one = touchio.TouchIn(board.TOUCH_PAD_PIN_ONE)
16+
touch_two = touchio.TouchIn(board.TOUCH_PAD_PIN_TWO)
17+
18+
while True:
19+
if touch_one.value:
20+
print("Pad one touched!")
21+
if touch_two.value:
22+
print("Pad two touched!")
23+
time.sleep(0.1)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
3+
4+
Update TOUCH_PIN_ONE to the first touch-capable pin name for the board you're using.
5+
Update TOUCH_PIN_TWO to the pin name for the second touch-capable pin.
6+
7+
For example:
8+
If you are using A0 and A1 on a Feather RP2040, update TOUCH_PIN_ONE to A0 and TOUCH_PIN_TWO to A2.
9+
"""
10+
import time
11+
import board
12+
import touchio
13+
14+
touch_one = touchio.TouchIn(board.TOUCH_PIN_ONE)
15+
touch_two = touchio.TouchIn(board.TOUCH_PIN_TWO)
16+
17+
while True:
18+
if touch_one.value:
19+
print("Pin one touched!")
20+
if touch_two.value:
21+
print("Pin two touched!")
22+
time.sleep(0.1)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
CircuitPython Essentials Storage CP Filesystem boot.py file
3+
4+
There are three things to be updated in this file to match your board:
5+
* Update OBJECT_NAME to match the physical thing you are using, e.g. button or pin.
6+
* Update OBJECT_PIN to match the pin name to which the button or pin is attached.
7+
* Update UP_OR_DOWN to match the Pull necessary for the chosen pin.
8+
9+
For example:
10+
If using the up button on a FunHouse, update OBJECT_NAME to button, and OBJECT_PIN to BUTTON_UP.
11+
If using pin A0 on a Feather RP2040, update OBJECT_NAME to pin, and OBJECT_PIN to A0.
12+
13+
For example:
14+
If using the up button on a FunHouse, update UP_OR_DOWN to DOWN.
15+
IF using pin A0 on a Feather RP2040, update UP_OR_DOWN to UP.
16+
"""
17+
import board
18+
import digitalio
19+
import storage
20+
21+
OBJECT_NAME = digitalio.DigitalInOut(board.OBJECT_PIN)
22+
OBJECT_NAME.switch_to_input(pull=digitalio.Pull.UP_OR_DOWN)
23+
24+
# If the OBJECT_NAME is connected to ground, the filesystem is writable by CircuitPython
25+
storage.remount("/", readonly=OBJECT_NAME.value)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
CircuitPython Essentials Storage CP Filesystem code.py file
3+
4+
For use with boards with a built-in red LED.
5+
"""
6+
import time
7+
import board
8+
import digitalio
9+
import microcontroller
10+
11+
led = digitalio.DigitalInOut(board.LED)
12+
led.switch_to_output()
13+
14+
try:
15+
with open("/temperature.txt", "a") as temp_log:
16+
while True:
17+
# The microcontroller temperature in Celsius. Include the
18+
# math to do the C to F conversion here, if desired.
19+
temperature = microcontroller.cpu.temperature
20+
21+
# Write the temperature to the temperature.txt file every 10 seconds.
22+
temp_log.write('{0:.2f}\n'.format(temperature))
23+
temp_log.flush()
24+
25+
# Blink the LED on every write...
26+
led.value = True
27+
time.sleep(1) # ...for one second.
28+
led.value = False # Then turn it off...
29+
time.sleep(9) # ...for the other 9 seconds.
30+
31+
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
32+
delay = 0.5 # ...blink the LED every half second.
33+
if e.args[0] == 28: # If the file system is full...
34+
delay = 0.15 # ...blink the LED every 0.15 seconds!
35+
while True:
36+
led.value = not led.value
37+
time.sleep(delay)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
CircuitPython Essentials Storage CP Filesystem code.py file
3+
4+
For use with boards that have a built-in NeoPixel or NeoPixels, but no little red LED.
5+
6+
It will use only one pixel as an indicator, even if there is more than one NeoPixel.
7+
"""
8+
import time
9+
import board
10+
import microcontroller
11+
import neopixel
12+
13+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
14+
15+
try:
16+
with open("/temperature.txt", "a") as temp_log:
17+
while True:
18+
# The microcontroller temperature in Celsius. Include the
19+
# math to do the C to F conversion here, if desired.
20+
temperature = microcontroller.cpu.temperature
21+
22+
# Write the temperature to the temperature.txt file every 10 seconds.
23+
temp_log.write('{0:.2f}\n'.format(temperature))
24+
temp_log.flush()
25+
26+
# Blink the NeoPixel on every write...
27+
pixel.fill((255, 0, 0))
28+
time.sleep(1) # ...for one second.
29+
pixel.fill((0, 0, 0)) # Then turn it off...
30+
time.sleep(9) # ...for the other 9 seconds.
31+
32+
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
33+
delay = 0.5 # ...blink the NeoPixel every half second.
34+
if e.args[0] == 28: # If the file system is full...
35+
delay = 0.15 # ...blink the NeoPixel every 0.15 seconds!
36+
while True:
37+
pixel.fill((255, 0, 0))
38+
time.sleep(delay)
39+
pixel.fill((0, 0, 0))
40+
time.sleep(delay)

0 commit comments

Comments
 (0)