Skip to content

Commit 37936e0

Browse files
committed
Update display and brightness code in Magic_Nine_Ball
1 parent f3a157a commit 37936e0

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

Magic_Nine_Ball/magic_nine_ball.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@
1111
import busio
1212
import adafruit_lis3dh
1313

14-
backlight = pulseio.PWMOut(board.TFT_BACKLIGHT)
1514
splash = displayio.Group()
1615
board.DISPLAY.show(splash)
1716

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

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

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

2523
# 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)
24+
ACCEL = adafruit_lis3dh.LIS3DH_I2C(board.I2C(), address=0x18)
2925

3026
ACCEL.range = adafruit_lis3dh.RANGE_4_G
3127

@@ -39,30 +35,30 @@
3935
print("Image unsupported {}".format(images[i]))
4036
del images[i]
4137
continue
42-
face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(),
43-
position=(0, 0))
38+
face = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter())
39+
4440
splash.append(face)
4541
# Wait for the image to load.
4642
board.DISPLAY.wait_for_frame()
4743

4844
# Fade up the backlight
49-
for b in range(100):
50-
backlight.duty_cycle = b * max_brightness // 100
45+
for b in range(101):
46+
board.DISPLAY.brightness = b / 100
5147
time.sleep(0.01) # default (0.01)
5248

53-
# Wait forever
49+
# Wait until the board gets shaken
5450
while not shaken:
5551
try:
5652
ACCEL_Z = ACCEL.acceleration[2] # Read Z axis acceleration
57-
except IOError:
53+
except OSError:
5854
pass
5955
# print(ACCEL_Z) # uncomment to see the accelerometer z reading
6056
if ACCEL_Z > SENSITIVITY:
6157
shaken = True
6258

6359
# Fade down the backlight
64-
for b in range(50, -1, -1):
65-
backlight.duty_cycle = b * max_brightness // 100
60+
for b in range(100, 0, -1):
61+
board.DISPLAY.brightness = b
6662
time.sleep(0.005) # default (0.005)
6763

6864
splash.pop()

0 commit comments

Comments
 (0)