From 689a324214f56c1c5efff14041a95744750a3cd9 Mon Sep 17 00:00:00 2001 From: James Carr Date: Sat, 7 Aug 2021 23:13:02 +0100 Subject: [PATCH] Update OnDiskBitmap code to take a filename string with CP7 Replace board.A0 with board.SPEAKER Replace board.A... with board.TOUCH... Replace busio.I2C(...) with board.I2C() --- Hallowing_Jump_Sound/jump-sound.py | 39 +++++++++++++++----------- Hallowing_Jump_Sound/stomp-and-roar.py | 32 ++++++++++++--------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/Hallowing_Jump_Sound/jump-sound.py b/Hallowing_Jump_Sound/jump-sound.py index 1c274ae3c..9781f68d0 100755 --- a/Hallowing_Jump_Sound/jump-sound.py +++ b/Hallowing_Jump_Sound/jump-sound.py @@ -1,14 +1,12 @@ """ Jump & touch sound example for Adafruit Hallowing. Plays different sounds in response to jumping and capacitive touch pads. -Image display requires CircuitPython 4.0.0-alpha1 or later (with displayio -support). WILL work with earlier versions, just no image shown! """ import time -import busio import board import digitalio +import displayio import audioio import audiocore import touchio @@ -57,16 +55,16 @@ def play_wav(wav): except AttributeError: pass -AUDIO = audioio.AudioOut(board.A0) # Speaker +AUDIO = audioio.AudioOut(board.SPEAKER) # Speaker board.DISPLAY.auto_brightness = False -TOUCH1 = touchio.TouchIn(board.A2) # Capacitive touch pads -TOUCH2 = touchio.TouchIn(board.A3) -TOUCH3 = touchio.TouchIn(board.A4) -TOUCH4 = touchio.TouchIn(board.A5) +TOUCH1 = touchio.TouchIn(board.TOUCH1) # Capacitive touch pads +TOUCH2 = touchio.TouchIn(board.TOUCH2) +TOUCH3 = touchio.TouchIn(board.TOUCH3) +TOUCH4 = touchio.TouchIn(board.TOUCH4) # Set up accelerometer on I2C bus, 4G range: -I2C = busio.I2C(board.SCL, board.SDA) +I2C = board.I2C() if IS_HALLOWING_M4: import adafruit_msa301 ACCEL = adafruit_msa301.MSA301(I2C) @@ -79,18 +77,25 @@ def play_wav(wav): ACCEL.range = adafruit_lis3dh.RANGE_4_G try: - import displayio board.DISPLAY.brightness = 0 SCREEN = displayio.Group() board.DISPLAY.show(SCREEN) + + # CircuitPython 6 & 7 compatible BITMAP = displayio.OnDiskBitmap(open(IMAGEFILE, 'rb')) - SCREEN.append( - displayio.TileGrid(BITMAP, - pixel_shader=getattr(BITMAP, 'pixel_shader', displayio.ColorConverter()), - x=0, y=0)) - board.DISPLAY.brightness = 1.0 -except (ImportError, NameError, AttributeError) as err: - pass # Probably earlier CircuitPython; no displayio support + TILEGRID = displayio.TileGrid( + BITMAP, + pixel_shader=getattr(BITMAP, 'pixel_shader', displayio.ColorConverter()) + ) + + # # CircuitPython 7+ compatible + # BITMAP = displayio.OnDiskBitmap(IMAGEFILE) + # TILEGRID = displayio.TileGrid(BITMAP, pixel_shader=BITMAP.pixel_shader) + + SCREEN.append(TILEGRID) + board.DISPLAY.brightness = 1.0 # Turn on display backlight +except (OSError, ValueError): + pass # If everything has initialized correctly, turn off the onboard NeoPixel: PIXEL = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0) diff --git a/Hallowing_Jump_Sound/stomp-and-roar.py b/Hallowing_Jump_Sound/stomp-and-roar.py index 1c6720862..7f5a3c79b 100755 --- a/Hallowing_Jump_Sound/stomp-and-roar.py +++ b/Hallowing_Jump_Sound/stomp-and-roar.py @@ -4,15 +4,13 @@ detection based on "Full-Featured Pedometer Design Realized with 3-Axis Digital Accelerometer" by Neil Zhao, Analog Dialogue Technical Journal, June 2010. -Image display requires CircuitPython 4.0.0-alpha1 or later (with displayio -support). WILL work with earlier versions, just no image shown! """ import time import math import digitalio +import displayio import board -import busio import audioio import audiocore import neopixel @@ -44,12 +42,12 @@ def load_wav(name): except AttributeError: pass -AUDIO = audioio.AudioOut(board.A0) # Speaker +AUDIO = audioio.AudioOut(board.SPEAKER) # Speaker board.DISPLAY.auto_brightness = False # Set up accelerometer on I2C bus, 4G range: -I2C = busio.I2C(board.SCL, board.SDA) +I2C = board.I2C() if IS_HALLOWING_M4: import adafruit_msa301 ACCEL = adafruit_msa301.MSA301(I2C) @@ -73,21 +71,27 @@ def load_wav(name): FILTER_SUM = 0 # Initial average value FILTER_INDEX = 0 # Current position in sample-averaging buffer -# Display BMP image. If this fails, it's not catastrophic (probably just -# older CircuitPython) and the code will continue with the step detection. +# Display BMP image. try: - import displayio board.DISPLAY.brightness = 0 SCREEN = displayio.Group() board.DISPLAY.show(SCREEN) + + # CircuitPython 6 & 7 compatible BITMAP = displayio.OnDiskBitmap(open(IMAGEFILE, 'rb')) - SCREEN.append( - displayio.TileGrid(BITMAP, - pixel_shader=getattr(BITMAP, 'pixel_shader', displayio.ColorConverter()), - x=0, y=0)) + TILEGRID = displayio.TileGrid( + BITMAP, + pixel_shader=getattr(BITMAP, 'pixel_shader', displayio.ColorConverter()) + ) + + # # CircuitPython 7+ compatible + # BITMAP = displayio.OnDiskBitmap(IMAGEFILE) + # TILEGRID = displayio.TileGrid(BITMAP, pixel_shader=BITMAP.pixel_shader) + + SCREEN.append(TILEGRID) board.DISPLAY.brightness = 1.0 # Turn on display backlight -except (ImportError, NameError, AttributeError) as err: - pass # Probably earlier CircuitPython; no displayio support +except (OSError, ValueError): + pass # If everything has initialized correctly, turn off the onboard NeoPixel: PIXEL = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0)