S3 TFT code, and template updates.

This commit is contained in:
Kattni Rembor 2022-08-17 16:38:35 -04:00
parent 53e7a4c87f
commit 4588d3329b
18 changed files with 404 additions and 32 deletions

View file

@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython analog voltage value example
"""
import time
import board
import analogio
analog_pin = analogio.AnalogIn(board.A0)
def get_voltage(pin):
return (pin.value * 3.1) / 61000
while True:
print(get_voltage(analog_pin))
time.sleep(0.1)

View file

@ -0,0 +1,80 @@
# SPDX-FileCopyrightText: Copyright (c) 2022 Dan Halbert for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2022 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
CircuitPython asyncio example for two NeoPixel rings and one button.
"""
import asyncio
import board
import neopixel
import keypad
from rainbowio import colorwheel
button_pin = board.BUTTON # The pin the button is connected to.
num_pixels = 16 # The number of NeoPixels on a single ring.
brightness = 0.2 # The LED brightness.
# Set up NeoPixel rings.
ring_one = neopixel.NeoPixel(board.A1, num_pixels, brightness=brightness, auto_write=False)
ring_two = neopixel.NeoPixel(board.A2, num_pixels, brightness=brightness, auto_write=False)
class AnimationControls:
"""The controls to allow you to vary the rainbow and blink animations."""
def __init__(self):
self.reverse = False
self.wait = 0.0
self.delay = 0.5
async def rainbow_cycle(controls):
"""Rainbow cycle animation on ring one."""
while True:
for j in range(255, -1, -1) if controls.reverse else range(0, 256, 1):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
ring_one[i] = colorwheel(rc_index & 255)
ring_one.show()
await asyncio.sleep(controls.wait)
async def blink(controls):
"""Blink animation on ring two."""
while True:
ring_two.fill((0, 0, 255))
ring_two.show()
await asyncio.sleep(controls.delay)
ring_two.fill((0, 0, 0))
ring_two.show()
await asyncio.sleep(controls.delay)
await asyncio.sleep(controls.wait)
async def monitor_button(button, controls):
"""Monitor button that reverses rainbow direction and changes blink speed.
Assume button is active low.
"""
with keypad.Keys((button,), value_when_pressed=False, pull=True) as key:
while True:
key_event = key.events.get()
if key_event:
if key_event.pressed:
controls.reverse = True
controls.delay = 0.1
elif key_event.released:
controls.reverse = False
controls.delay = 0.5
await asyncio.sleep(0)
async def main():
animation_controls = AnimationControls()
button_task = asyncio.create_task(monitor_button(button_pin, animation_controls))
animation_task = asyncio.create_task(rainbow_cycle(animation_controls))
blink_task = asyncio.create_task(blink(animation_controls))
# This will run forever, because no tasks ever finish.
await asyncio.gather(button_task, animation_task, blink_task)
asyncio.run(main())

View file

@ -0,0 +1,15 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
"""
import time
import board
import touchio
touch = touchio.TouchIn(board.A4)
while True:
if touch.value:
print("Pin touched!")
time.sleep(0.1)

View file

@ -0,0 +1,18 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
"""
import time
import board
import touchio
touch_one = touchio.TouchIn(board.A4)
touch_two = touchio.TouchIn(board.D12)
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,29 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython I2S Tone playback example.
Plays a tone for one second on, one
second off, in a loop.
"""
import time
import array
import math
import audiocore
import board
import audiobusio
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
tone_volume = 0.1 # Increase this to increase the volume of the tone.
frequency = 440 # Set this to the Hz of the tone you want to generate.
length = 8000 // frequency
sine_wave = array.array("h", [0] * length)
for i in range(length):
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
sine_wave_sample = audiocore.RawSample(sine_wave)
while True:
audio.play(sine_wave_sample, loop=True)
time.sleep(1)
audio.stop()
time.sleep(1)

Binary file not shown.

View file

@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython I2S WAV file playback.
Plays a WAV file once.
"""
import audiocore
import board
import audiobusio
audio = audiobusio.I2SOut(board.A0, board.A1, board.A2)
with open("StreetChicken.wav", "rb") as wave_file:
wav = audiocore.WaveFile(wave_file)
print("Playing wav file!")
audio.play(wav)
while audio.playing:
pass
print("Done!")

View file

@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Essentials Storage CP Filesystem boot.py file
"""
import time
import board
import digitalio
import storage
import neopixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
# Turn the NeoPixel white for one second to indicate when to press the boot button.
pixel.fill((255, 255, 255))
time.sleep(1)
# If the button is connected to ground, the filesystem is writable by CircuitPython
storage.remount("/", readonly=button.value)

View file

@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Essentials Storage CP Filesystem code.py file
For use with boards with a built-in red LED.
Logs temperature using MCP9808 temperature sensor.
"""
import time
import board
import digitalio
import adafruit_mcp9808
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()
# For connecting MCP9808 via STEMMA QT
mcp9808 = adafruit_mcp9808.MCP9808(board.STEMMA_I2C())
# For connecting MCP9808 via pins and breadboard
# mcp9808 = adafruit_mcp9808.MCP9808(board.I2C())
try:
with open("/temperature.txt", "a") as temp_log:
while True:
# The temperature in Celsius. Include the
# math to do the C to F conversion here, if desired.
temperature = mcp9808.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,19 @@
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython analog voltage value example
"""
import time
import board
import analogio
analog_pin = analogio.AnalogIn(board.A0)
def get_voltage(pin):
return (pin.value * 3.1) / 61000
while True:
print(get_voltage(analog_pin))
time.sleep(0.1)

View file

@ -7,9 +7,11 @@ import board
# To use default I2C bus (most boards)
i2c = board.I2C()
# To use the STEMMA QT connector (most boards)
# i2c = board.STEMMA_I2C()
# To create I2C bus on specific pins
# import busio
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
while not i2c.try_lock():

View file

@ -21,10 +21,11 @@ import audiobusio
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
mp3 = audiomp3.MP3Decoder(open("slow.mp3", "rb"))
with open("slow.mp3", "rb") as mp3_file:
mp3 = audiomp3.MP3Decoder(mp3_file)
audio.play(mp3)
while audio.playing:
audio.play(mp3)
while audio.playing:
pass
print("Done playing!")

View file

@ -21,11 +21,12 @@ import audiobusio
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
wave_file = open("StreetChicken.wav", "rb")
wav = audiocore.WaveFile(wave_file)
with open("StreetChicken.wav", "rb") as wave_file:
wav = audiocore.WaveFile(wave_file)
print("Playing wav file!")
audio.play(wav)
while audio.playing:
print("Playing wav file!")
audio.play(wav)
while audio.playing:
pass
print("Done!")

View file

@ -24,11 +24,11 @@ of imports and the hardware setup. For example, a generic SAMD51 example should
mp3files = ["slow.mp3", "happy.mp3"]
mp3 = open(mp3files[0], "rb")
with open(mp3files[0], "rb") as mp3:
decoder = audiomp3.MP3Decoder(mp3)
for filename in mp3files:
decoder.file = open(filename, "rb")
with open(filename, "rb") as decoder.file:
audio.play(decoder)
print("Playing:", filename)
while audio.playing:
@ -79,11 +79,11 @@ audio = audioio.AudioOut(board.A0)
mp3files = ["slow.mp3", "happy.mp3"]
mp3 = open(mp3files[0], "rb")
decoder = audiomp3.MP3Decoder(mp3)
with open(mp3files[0], "rb") as mp3:
decoder = audiomp3.MP3Decoder(mp3)
for filename in mp3files:
decoder.file = open(filename, "rb")
for filename in mp3files:
with open(filename, "rb") as decoder.file:
audio.play(decoder)
print("Playing:", filename)
while audio.playing:

View file

@ -71,10 +71,11 @@ audio = audioio.AudioOut(board.A0)
#
# audio = audioio.AudioOut(board.SPEAKER)
decoder = audiomp3.MP3Decoder(open("slow.mp3", "rb"))
with open("slow.mp3", "rb") as mp3_file:
decoder = audiomp3.MP3Decoder(mp3_file)
audio.play(decoder)
while audio.playing:
audio.play(decoder)
while audio.playing:
pass
print("Done playing!")

View file

@ -4,7 +4,7 @@
CircuitPython Essentials Storage CP Filesystem boot.py file
REMOVE THIS LINE AND ALL TEXT BELOW BEFORE SUBMITTING TO GITHUB.
This file is specific to boards like ESP32-S2 where the boot button is used for bootloader and
This file is specific to boards like ESP32-S2/S3 where the boot button is used for bootloader and
safe mode, and therefore the button must be pressed at the right time to get into readonly mode.
There are two things to be updated in this file to match your board:

View file

@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
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.
Logs temperature using MCP9808 temperature sensor.
"""
import time
import board
import neopixel
import adafruit_mcp9808
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
# For connecting MCP9808 via STEMMA QT
mcp9808 = adafruit_mcp9808.MCP9808(board.STEMMA_I2C())
# For connecting MCP9808 via pins and breadboard
# mcp9808 = adafruit_mcp9808.MCP9808(board.I2C())
try:
with open("/temperature.txt", "a") as temp_log:
while True:
# The temperature in Celsius. Include the
# math to do the C to F conversion here, if desired.
temperature = mcp9808.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)

View file

@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Essentials Storage CP Filesystem code.py file
For use with boards with a built-in red LED.
Logs temperature using MCP9808 temperature sensor.
"""
import time
import board
import digitalio
import adafruit_mcp9808
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()
# For connecting MCP9808 via STEMMA QT
mcp9808 = adafruit_mcp9808.MCP9808(board.STEMMA_I2C())
# For connecting MCP9808 via pins and breadboard
# mcp9808 = adafruit_mcp9808.MCP9808(board.I2C())
try:
with open("/temperature.txt", "a") as temp_log:
while True:
# The temperature in Celsius. Include the
# math to do the C to F conversion here, if desired.
temperature = mcp9808.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)