Simple test
Ensure your device works with this simple test.
examples/m5stack_unit8_angle_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2023 Neradoc https://neradoc.me
2# SPDX-License-Identifier: Unlicense
3
4import board
5import time
6from rainbowio import colorwheel
7from m5stack_unit8.angle import Unit8Angle
8
9i2c = board.STEMMA_I2C()
10angles = Unit8Angle(i2c, brightness=0.2)
11angles.pixels.fill(0)
12
13state = None
14button_status = [True] * 8
15led_status = [True] * 8
16
17while True:
18 # this is the 12 bits positions by default, adjusted to 16 bits
19 positions = angles.angles
20 # read 8 bit angles, hopefully more stable
21 positions_8b = angles.angles_8bit
22 # switch
23 switch = angles.switch
24 # if anything changed
25 if (positions_8b, switch) != state:
26 state = (positions_8b, switch)
27 # print the values as percentages
28 print("-" * (9 + 8 * 5))
29 print(
30 "Angle:",
31 "".join(f"{100 - round(p * 100 / 0xFFFF):>4d}%" for p in positions),
32 "|",
33 "on " if switch else "off",
34 )
35 print("-" * (9 + 8 * 5))
36 # use the switch to set the brightness
37 if switch:
38 angles.pixels.brightness = 1
39 angles.pixels[8] = 0x00FF00
40 else:
41 angles.pixels.brightness = 0.2
42 angles.pixels[8] = 0xFF0000
43 # use the 8-bit values to set the color, they are already in the range
44 colors = [colorwheel(x) for x in positions_8b]
45 # got some errors during tests, but it seems stable now
46 try:
47 angles.pixels[:8] = colors
48 except OSError as er:
49 print(er)
50 time.sleep(0.01)
examples/m5stack_unit8_encoder_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2023 Neradoc https://neradoc.me
2# SPDX-License-Identifier: Unlicense
3
4import board
5import time
6from rainbowio import colorwheel
7from m5stack_unit8.encoder import Unit8Encoder
8
9i2c = board.STEMMA_I2C()
10encoder = Unit8Encoder(i2c, brightness=0.2)
11
12state = None
13pressed = set()
14
15while True:
16 # get all the status from the device
17 positions = encoder.positions
18 increments = encoder.increments
19 buttons = encoder.buttons
20 switch = encoder.switch
21 # if anything changed
22 if (positions, increments, buttons, switch) != state:
23 state = (positions, increments, buttons, switch)
24 # print current state
25 print("-" * (13 + 8 * 5))
26 print(" Position:", "".join(f"{p:>5}" for p in positions), "|")
27 print("Increment:", "".join(f"{p:>5}" for p in increments), "|")
28 print(
29 " Button:",
30 "".join([" on" if b else " off" for b in buttons]),
31 "|",
32 "on " if switch else "off",
33 )
34 print("-" * (13 + 8 * 5))
35 # use the switch to set the brightness
36 if switch:
37 encoder.pixels.brightness = 1
38 encoder.pixels[8] = 0x00FF00
39 else:
40 encoder.pixels.brightness = 0.2
41 encoder.pixels[8] = 0xFF0000
42 # press the first and last buttons to reset all positions to 0
43 if buttons[0] and buttons[-1]:
44 encoder.reset()
45 # press the second button from both sides to set all to cyan
46 if buttons[1] and buttons[-2]:
47 encoder.positions = [64] * 8
48 # use the pressed set to detect presses and releases
49 # set an encoder's position to 0 when pressed
50 for i in range(8):
51 if buttons[i] and i not in pressed:
52 # button i pressed
53 encoder.set_position(i, 0)
54 pressed.add(i)
55 if not buttons[i] and i in pressed:
56 # button i released
57 pressed.remove(i)
58 # compute the colors on a double scale to make it change faster
59 # note: some math to avoid feeding negative values to colorwheel
60 colors = [colorwheel((2 * x) % 256 + 256) for i, x in enumerate(positions)]
61 # set the pixels colors
62 encoder.pixels[:8] = colors
63 time.sleep(0.01)