Skip to content

Commit 29adbbc

Browse files
authored
Merge pull request #335 from jedgarpark/magic-nine-ball
first commit Magic Nine Ball code
2 parents 8746b5f + cf1c955 commit 29adbbc

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Magic_Nine_Ball/magic_nine_ball.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Magic 9 Ball
2+
# Turn HalloWing face down and then face up to change images at random
3+
# place 128 x 128 pixel 24-bit .bmp images at root level of HalloWing
4+
5+
import time
6+
import os
7+
import random
8+
import board
9+
import displayio
10+
import pulseio
11+
import busio
12+
import adafruit_lis3dh
13+
14+
backlight = pulseio.PWMOut(board.TFT_BACKLIGHT)
15+
splash = displayio.Group()
16+
board.DISPLAY.show(splash)
17+
18+
max_brightness = 2 ** 15
19+
20+
images = list(filter(lambda x: x.endswith("bmp"), os.listdir("/")))
21+
22+
i = random.randint(0, 19) # initial image is randomly selected
23+
24+
# Set up accelerometer on I2C bus, 4G range:
25+
I2C = busio.I2C(board.SCL, board.SDA)
26+
27+
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18)
28+
29+
ACCEL.range = adafruit_lis3dh.RANGE_4_G
30+
31+
while True:
32+
shaken = False
33+
with open(images[i], "rb") as f:
34+
print("Image load {}".format(images[i]))
35+
try:
36+
odb = displayio.OnDiskBitmap(f)
37+
except ValueError:
38+
print("Image unsupported {}".format(images[i]))
39+
del images[i]
40+
continue
41+
face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(),
42+
position=(0, 0))
43+
splash.append(face)
44+
# Wait for the image to load.
45+
board.DISPLAY.wait_for_frame()
46+
47+
# Fade up the backlight
48+
for b in range(100):
49+
backlight.duty_cycle = b * max_brightness // 100
50+
time.sleep(0.01) # default (0.01)
51+
52+
# Wait forever
53+
while not shaken:
54+
try:
55+
ACCEL_X, ACCEL_Y, ACCEL_Z = ACCEL.acceleration # Read the accelerometer
56+
except IOError:
57+
pass
58+
# print(ACCEL_Z) # uncomment to see the accelerometer z reading
59+
if ACCEL_Z > 5:
60+
shaken = True
61+
62+
# Fade down the backlight
63+
for b in range(50, -1, -1):
64+
backlight.duty_cycle = b * max_brightness // 100
65+
time.sleep(0.005) # default (0.005)
66+
67+
splash.pop()
68+
69+
if ACCEL_Z > 5:
70+
i = random.randint(0, 19) # initial image is randomly selected
71+
print("shaken")
72+
faceup = False
73+
i %= len(images) - 1

0 commit comments

Comments
 (0)