|
7 | 7 | import random |
8 | 8 | import board |
9 | 9 | import displayio |
10 | | -import pulseio |
11 | | -import busio |
12 | 10 | import adafruit_lis3dh |
13 | 11 |
|
14 | | -backlight = pulseio.PWMOut(board.TFT_BACKLIGHT) |
15 | 12 | splash = displayio.Group() |
16 | 13 | board.DISPLAY.show(splash) |
17 | 14 |
|
18 | | -max_brightness = 2 ** 15 |
19 | 15 | SENSITIVITY = 5 # reading in Z direction to trigger, adjustable |
20 | 16 |
|
21 | 17 | images = list(filter(lambda x: x.endswith("bmp"), os.listdir("/"))) |
22 | 18 |
|
23 | 19 | i = random.randint(0, (len(images)-1)) # initial image is randomly selected |
24 | 20 |
|
25 | 21 | # 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) |
29 | 23 |
|
30 | 24 | ACCEL.range = adafruit_lis3dh.RANGE_4_G |
31 | 25 |
|
|
39 | 33 | print("Image unsupported {}".format(images[i])) |
40 | 34 | del images[i] |
41 | 35 | continue |
42 | | - face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), |
43 | | - position=(0, 0)) |
| 36 | + face = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter()) |
| 37 | + |
44 | 38 | splash.append(face) |
45 | 39 | # Wait for the image to load. |
46 | 40 | board.DISPLAY.wait_for_frame() |
47 | 41 |
|
48 | 42 | # 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 |
51 | 45 | time.sleep(0.01) # default (0.01) |
52 | 46 |
|
53 | | - # Wait forever |
| 47 | + # Wait until the board gets shaken |
54 | 48 | while not shaken: |
55 | 49 | try: |
56 | 50 | ACCEL_Z = ACCEL.acceleration[2] # Read Z axis acceleration |
57 | | - except IOError: |
| 51 | + except OSError: |
58 | 52 | pass |
59 | 53 | # print(ACCEL_Z) # uncomment to see the accelerometer z reading |
60 | 54 | if ACCEL_Z > SENSITIVITY: |
61 | 55 | shaken = True |
62 | 56 |
|
63 | 57 | # 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 |
66 | 60 | time.sleep(0.005) # default (0.005) |
67 | 61 |
|
68 | 62 | splash.pop() |
|
0 commit comments