Updated many examples to work in CP 4 and 5

This commit is contained in:
Melissa LeBlanc-Williams 2019-11-27 11:37:29 -08:00
parent 1818d3f594
commit b2beea748f
18 changed files with 147 additions and 54 deletions

View file

@ -196,9 +196,13 @@ class Paint(object):
self._splash.append(self._palette)
self._display.show(self._splash)
self._display.refresh_soon()
gc.collect()
self._display.wait_for_frame()
try:
gc.collect()
self._display.refresh(target_frames_per_second=60)
except AttributeError:
self._display.refresh_soon()
gc.collect()
self._display.wait_for_frame()
self._brush = 0
self._cursor_bitmaps = [self._cursor_bitmap_1(), self._cursor_bitmap_3()]

View file

@ -37,4 +37,3 @@ tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
group = displayio.Group()
group.append(tile_grid)
display.show(group)
display.refresh_soon()

View file

@ -64,4 +64,3 @@ tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
group = displayio.Group()
group.append(tile_grid)
display.show(group)
display.refresh_soon()

View file

@ -62,4 +62,3 @@ tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
group = displayio.Group()
group.append(tile_grid)
display.show(group)
display.refresh_soon()

View file

@ -75,7 +75,10 @@ def show_image(filename):
face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0))
backlight.value = False
splash.append(face)
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.wait_for_frame()
backlight.value = True

View file

@ -99,7 +99,10 @@ def show_image(filename):
backlight.duty_cycle = 0
splash.append(face)
# Wait for the image to load.
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.wait_for_frame()
backlight.duty_cycle = max_brightness
beep(1) # startup beep

View file

@ -68,7 +68,10 @@ def show_image(filename):
face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0))
backlight.value = False
splash.append(face)
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.wait_for_frame()
backlight.value = True

View file

@ -6,10 +6,10 @@ support). WILL work with earlier versions, just no image shown!
"""
import time
import audioio
import busio
import board
import digitalio
import audioio
import touchio
import neopixel

View file

@ -10,12 +10,11 @@ support). WILL work with earlier versions, just no image shown!
import time
import math
import digitalio
import board
import busio
import audioio
import pulseio
import neopixel
import adafruit_lis3dh
def load_wav(name):
"""
@ -30,16 +29,36 @@ STOMP_WAV = load_wav('stomp') # WAV file to play with each step
ROAR_WAV = load_wav('roar') # WAV when jumping
IMAGEFILE = 'reptar.bmp' # BMP image to display
IS_HALLOWING_M4 = False
# Perform a couple extra steps for the HalloWing M4
try:
if getattr(board, "CAP_PIN"):
IS_HALLOWING_M4 = True
if getattr(board, "SPEAKER_ENABLE"):
# Enable the Speaker
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.direction = digitalio.Direction.OUTPUT
speaker_enable.value = True
except AttributeError:
pass
AUDIO = audioio.AudioOut(board.A0) # Speaker
BACKLIGHT = pulseio.PWMOut(board.TFT_BACKLIGHT) # Display backlight
board.DISPLAY.auto_brightness = False
# Set up accelerometer on I2C bus, 4G range:
I2C = busio.I2C(board.SCL, board.SDA)
try:
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18) # Production board
except ValueError:
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x19) # Beta hardware
ACCEL.range = adafruit_lis3dh.RANGE_4_G
if IS_HALLOWING_M4:
import adafruit_msa301
ACCEL = adafruit_msa301.MSA301(I2C)
else:
import adafruit_lis3dh
try:
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18) # Production board
except ValueError:
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x19) # Beta hardware
ACCEL.range = adafruit_lis3dh.RANGE_4_G
STEP_INTERVAL_MIN = 0.3 # Shortest interval to walk one step (seconds)
STEP_INTERVAL_MAX = 2.0 # Longest interval to walk one step (seconds)
@ -57,15 +76,15 @@ FILTER_INDEX = 0 # Current position in sample-averaging buffer
# older CircuitPython) and the code will continue with the step detection.
try:
import displayio
board.DISPLAY.brightness = 0
SCREEN = displayio.Group()
board.DISPLAY.show(SCREEN)
BITMAP = displayio.OnDiskBitmap(open(IMAGEFILE, 'rb'))
SCREEN.append(
displayio.Sprite(BITMAP,
pixel_shader=displayio.ColorConverter(),
position=(0, 0)))
board.DISPLAY.wait_for_frame() # Wait for the image to load.
BACKLIGHT.duty_cycle = 65535 # Turn on display backlight
displayio.TileGrid(BITMAP,
pixel_shader=displayio.ColorConverter(),
x=0, y=0))
board.DISPLAY.brightness = 1.0 # Turn on display backlight
except (ImportError, NameError, AttributeError) as err:
pass # Probably earlier CircuitPython; no displayio support

View file

@ -37,7 +37,10 @@ while True:
splash.append(face)
# Wait for the image to load.
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.wait_for_frame()
# Fade up the backlight
for b in range(101):

View file

@ -219,7 +219,10 @@ def reset_board():
compute_counts()
def play_sound(file_name):
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.wait_for_frame()
wavfile = open(file_name, "rb")
wavedata = WaveFile(wavfile)
speaker_enable.value = True
@ -242,8 +245,11 @@ def lose():
for _ in range(10):
tilegrid.x = randint(-2, 2)
tilegrid.y = randint(-2, 2)
display.refresh_soon()
display.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
tilegrid.x = 0
tilegrid.y = 0
wait_for_sound_and_cleanup(wavfile)

View file

@ -282,8 +282,12 @@ class Time_State(State):
temperature_text = '%3d F' % round(((temperature * 9 / 5) + 32))
self.text_areas[2].text = temperature_text
self.weather_refresh = now
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
except RuntimeError as e:
self.weather_refresh = now - 540 # delay a minute before retrying
@ -295,8 +299,12 @@ class Time_State(State):
current_time = time.localtime()
time_string = '%02d:%02d' % (current_time.tm_hour,current_time.tm_min)
self.text_areas[0].text = time_string
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
# Check if alarm should sound
if current_time is not None and not snooze_time:
@ -343,8 +351,12 @@ class Time_State(State):
self.text_areas[1].text = '%2d:%02d' % (alarm_hour, alarm_minute)
else:
self.text_areas[1].text = ' '
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
def exit(self):
@ -374,8 +386,12 @@ class Mugsy_State(Time_State):
low_light = False
pyportal.set_backlight(1.00)
pyportal.set_background(mugsy_background)
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
@ -423,8 +439,12 @@ class Alarm_State(State):
pyportal.set_backlight(1.00)
pyportal.set_background(alarm_background)
low_light = False
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
def exit(self):
@ -493,8 +513,12 @@ class Setting_State(State):
logger.debug('Alarm minute now: %d', alarm_minute)
self.text_areas[0].text = '%02d:%02d' % (alarm_hour, alarm_minute)
self.previous_touch = t
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
else:
self.previous_touch = None
return bool(t)

View file

@ -495,7 +495,10 @@ if oven.sensor_status:
for b in buttons:
pyportal.splash.append(b.group)
board.DISPLAY.refresh_soon()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
print("display complete")
last_temp = 0
last_state = "ready"
@ -503,7 +506,10 @@ last_control = False
second_timer = time.monotonic()
timer = time.monotonic()
while True:
board.DISPLAY.refresh_soon()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
oven.beep.refresh() # this allows beeps less than one second in length
try:
oven_temp = int(oven.sensor.temperature)

View file

@ -109,7 +109,11 @@ def update_display(current_time, update_iss=False):
current_time.tm_min,
current_time.tm_sec)
board.DISPLAY.refresh_soon()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
# Initial refresh
update_display(time.localtime(), True)

View file

@ -167,8 +167,11 @@ class Clock(object):
self.text_areas[1].text = ampm_string
self.text_areas[2].text = (months[int(self.current_time.tm_mon - 1)] +
" " + str(self.current_time.tm_mday))
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name

View file

@ -142,7 +142,10 @@ def update_display(time_info, update_tides=False):
time_info.tm_min,
time_info.tm_sec)
board.DISPLAY.refresh_soon()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
# First run update
tide_data = get_tide_data()

View file

@ -114,6 +114,9 @@ class Azure_GFX(displayio.Group):
position=(0,0))
self._icon_group.append(self._icon_sprite)
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()

View file

@ -87,8 +87,12 @@ class WeatherStation_GFX(displayio.Group):
:param str status_text: Description of Adafruit IO status
"""
self.io_status_text.text = status_text
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
def display_data(self, uv_index, bme_data, sgp_data, wind_speed):
"""Displays the data from the sensors attached
@ -123,8 +127,12 @@ class WeatherStation_GFX(displayio.Group):
print("eCO2 = %d ppm \t TVOC = %d ppb"%(sgp_data[0], sgp_data[1]))
self.sgp_text.text = "eCO2: %d ppm, TVOC: %d ppb"%(sgp_data[0], sgp_data[1])
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
def set_icon(self, filename):
"""Sets the background image to a bitmap file.
@ -150,5 +158,9 @@ class WeatherStation_GFX(displayio.Group):
position=(0,0))
self._icon_group.append(self._icon_sprite)
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()
try:
board.DISPLAY.refresh(target_frames_per_second=60)
except AttributeError:
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()