Skip to content

Commit 60b5582

Browse files
authored
Merge pull request #625 from dhalbert/update-magic-9-ball
Update display and brightness code in Magic_Nine_Ball
2 parents e48993f + c37d240 commit 60b5582

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

Magic_Nine_Ball/magic_nine_ball.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@
77
import random
88
import board
99
import displayio
10-
import pulseio
11-
import busio
1210
import adafruit_lis3dh
1311

14-
backlight = pulseio.PWMOut(board.TFT_BACKLIGHT)
1512
splash = displayio.Group()
1613
board.DISPLAY.show(splash)
1714

18-
max_brightness = 2 ** 15
1915
SENSITIVITY = 5 # reading in Z direction to trigger, adjustable
2016

2117
images = list(filter(lambda x: x.endswith("bmp"), os.listdir("/")))
2218

2319
i = random.randint(0, (len(images)-1)) # initial image is randomly selected
2420

2521
# Set up accelerometer on I2C bus, 4G range:
26-
I2C = busio.I2C(board.SCL, board.SDA)
27-
28-
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18)
22+
ACCEL = adafruit_lis3dh.LIS3DH_I2C(board.I2C(), address=0x18)
2923

3024
ACCEL.range = adafruit_lis3dh.RANGE_4_G
3125

@@ -39,30 +33,30 @@
3933
print("Image unsupported {}".format(images[i]))
4034
del images[i]
4135
continue
42-
face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(),
43-
position=(0, 0))
36+
face = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter())
37+
4438
splash.append(face)
4539
# Wait for the image to load.
4640
board.DISPLAY.wait_for_frame()
4741

4842
# Fade up the backlight
49-
for b in range(100):
50-
backlight.duty_cycle = b * max_brightness // 100
43+
for b in range(101):
44+
board.DISPLAY.brightness = b / 100
5145
time.sleep(0.01) # default (0.01)
5246

53-
# Wait forever
47+
# Wait until the board gets shaken
5448
while not shaken:
5549
try:
5650
ACCEL_Z = ACCEL.acceleration[2] # Read Z axis acceleration
57-
except IOError:
51+
except OSError:
5852
pass
5953
# print(ACCEL_Z) # uncomment to see the accelerometer z reading
6054
if ACCEL_Z > SENSITIVITY:
6155
shaken = True
6256

6357
# Fade down the backlight
64-
for b in range(50, -1, -1):
65-
backlight.duty_cycle = b * max_brightness // 100
58+
for b in range(100, 0, -1):
59+
board.DISPLAY.brightness = b
6660
time.sleep(0.005) # default (0.005)
6761

6862
splash.pop()

0 commit comments

Comments
 (0)