Skip to content

Commit 9215678

Browse files
authored
Merge pull request #2244 from kattni/s3-tft-feather-guide-code
S3 TFT code, and template updates.
2 parents 22de954 + 4588d33 commit 9215678

File tree

18 files changed

+404
-32
lines changed

18 files changed

+404
-32
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython analog voltage value example
5+
"""
6+
import time
7+
import board
8+
import analogio
9+
10+
analog_pin = analogio.AnalogIn(board.A0)
11+
12+
13+
def get_voltage(pin):
14+
return (pin.value * 3.1) / 61000
15+
16+
17+
while True:
18+
print(get_voltage(analog_pin))
19+
time.sleep(0.1)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2022 Kattni Rembor for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
"""
6+
CircuitPython asyncio example for two NeoPixel rings and one button.
7+
"""
8+
import asyncio
9+
import board
10+
import neopixel
11+
import keypad
12+
from rainbowio import colorwheel
13+
14+
button_pin = board.BUTTON # The pin the button is connected to.
15+
num_pixels = 16 # The number of NeoPixels on a single ring.
16+
brightness = 0.2 # The LED brightness.
17+
18+
# Set up NeoPixel rings.
19+
ring_one = neopixel.NeoPixel(board.A1, num_pixels, brightness=brightness, auto_write=False)
20+
ring_two = neopixel.NeoPixel(board.A2, num_pixels, brightness=brightness, auto_write=False)
21+
22+
23+
class AnimationControls:
24+
"""The controls to allow you to vary the rainbow and blink animations."""
25+
def __init__(self):
26+
self.reverse = False
27+
self.wait = 0.0
28+
self.delay = 0.5
29+
30+
31+
async def rainbow_cycle(controls):
32+
"""Rainbow cycle animation on ring one."""
33+
while True:
34+
for j in range(255, -1, -1) if controls.reverse else range(0, 256, 1):
35+
for i in range(num_pixels):
36+
rc_index = (i * 256 // num_pixels) + j
37+
ring_one[i] = colorwheel(rc_index & 255)
38+
ring_one.show()
39+
await asyncio.sleep(controls.wait)
40+
41+
42+
async def blink(controls):
43+
"""Blink animation on ring two."""
44+
while True:
45+
ring_two.fill((0, 0, 255))
46+
ring_two.show()
47+
await asyncio.sleep(controls.delay)
48+
ring_two.fill((0, 0, 0))
49+
ring_two.show()
50+
await asyncio.sleep(controls.delay)
51+
await asyncio.sleep(controls.wait)
52+
53+
54+
async def monitor_button(button, controls):
55+
"""Monitor button that reverses rainbow direction and changes blink speed.
56+
Assume button is active low.
57+
"""
58+
with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
59+
while True:
60+
key_event = key.events.get()
61+
if key_event:
62+
if key_event.pressed:
63+
controls.reverse = True
64+
controls.delay = 0.1
65+
elif key_event.released:
66+
controls.reverse = False
67+
controls.delay = 0.5
68+
await asyncio.sleep(0)
69+
70+
71+
async def main():
72+
animation_controls = AnimationControls()
73+
button_task = asyncio.create_task(monitor_button(button_pin, animation_controls))
74+
animation_task = asyncio.create_task(rainbow_cycle(animation_controls))
75+
blink_task = asyncio.create_task(blink(animation_controls))
76+
77+
# This will run forever, because no tasks ever finish.
78+
await asyncio.gather(button_task, animation_task, blink_task)
79+
80+
asyncio.run(main())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
5+
"""
6+
import time
7+
import board
8+
import touchio
9+
10+
touch = touchio.TouchIn(board.A4)
11+
12+
while True:
13+
if touch.value:
14+
print("Pin touched!")
15+
time.sleep(0.1)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
5+
"""
6+
import time
7+
import board
8+
import touchio
9+
10+
touch_one = touchio.TouchIn(board.A4)
11+
touch_two = touchio.TouchIn(board.D12)
12+
13+
while True:
14+
if touch_one.value:
15+
print("Pin one touched!")
16+
if touch_two.value:
17+
print("Pin two touched!")
18+
time.sleep(0.1)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S Tone playback example.
5+
Plays a tone for one second on, one
6+
second off, in a loop.
7+
"""
8+
import time
9+
import array
10+
import math
11+
import audiocore
12+
import board
13+
import audiobusio
14+
15+
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
16+
17+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
18+
frequency = 440 # Set this to the Hz of the tone you want to generate.
19+
length = 8000 // frequency
20+
sine_wave = array.array("h", [0] * length)
21+
for i in range(length):
22+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
23+
sine_wave_sample = audiocore.RawSample(sine_wave)
24+
25+
while True:
26+
audio.play(sine_wave_sample, loop=True)
27+
time.sleep(1)
28+
audio.stop()
29+
time.sleep(1)
413 KB
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S WAV file playback.
5+
Plays a WAV file once.
6+
"""
7+
import audiocore
8+
import board
9+
import audiobusio
10+
11+
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
12+
13+
with open("StreetChicken.wav", "rb") as wave_file:
14+
wav = audiocore.WaveFile(wave_file)
15+
16+
print("Playing wav file!")
17+
audio.play(wav)
18+
while audio.playing:
19+
pass
20+
21+
print("Done!")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Essentials Storage CP Filesystem boot.py file
5+
"""
6+
import time
7+
import board
8+
import digitalio
9+
import storage
10+
import neopixel
11+
12+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
13+
14+
button = digitalio.DigitalInOut(board.BUTTON)
15+
button.switch_to_input(pull=digitalio.Pull.UP)
16+
17+
# Turn the NeoPixel white for one second to indicate when to press the boot button.
18+
pixel.fill((255, 255, 255))
19+
time.sleep(1)
20+
21+
# If the button is connected to ground, the filesystem is writable by CircuitPython
22+
storage.remount("/", readonly=button.value)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Essentials Storage CP Filesystem code.py file
5+
6+
For use with boards with a built-in red LED.
7+
8+
Logs temperature using MCP9808 temperature sensor.
9+
"""
10+
import time
11+
import board
12+
import digitalio
13+
import adafruit_mcp9808
14+
15+
led = digitalio.DigitalInOut(board.LED)
16+
led.switch_to_output()
17+
18+
# For connecting MCP9808 via STEMMA QT
19+
mcp9808 = adafruit_mcp9808.MCP9808(board.STEMMA_I2C())
20+
21+
# For connecting MCP9808 via pins and breadboard
22+
# mcp9808 = adafruit_mcp9808.MCP9808(board.I2C())
23+
24+
try:
25+
with open("/temperature.txt", "a") as temp_log:
26+
while True:
27+
# The temperature in Celsius. Include the
28+
# math to do the C to F conversion here, if desired.
29+
temperature = mcp9808.temperature
30+
31+
# Write the temperature to the temperature.txt file every 10 seconds.
32+
temp_log.write('{0:.2f}\n'.format(temperature))
33+
temp_log.flush()
34+
35+
# Blink the LED on every write...
36+
led.value = True
37+
time.sleep(1) # ...for one second.
38+
led.value = False # Then turn it off...
39+
time.sleep(9) # ...for the other 9 seconds.
40+
41+
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
42+
delay = 0.5 # ...blink the LED every half second.
43+
if e.args[0] == 28: # If the file system is full...
44+
delay = 0.15 # ...blink the LED every 0.15 seconds!
45+
while True:
46+
led.value = not led.value
47+
time.sleep(delay)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython analog voltage value example
5+
"""
6+
import time
7+
import board
8+
import analogio
9+
10+
analog_pin = analogio.AnalogIn(board.A0)
11+
12+
13+
def get_voltage(pin):
14+
return (pin.value * 3.1) / 61000
15+
16+
17+
while True:
18+
print(get_voltage(analog_pin))
19+
time.sleep(0.1)

0 commit comments

Comments
 (0)