|
7 | 7 | from adafruit_hid.consumer_control import ConsumerControl |
8 | 8 | from adafruit_hid.consumer_control_code import ConsumerControlCode |
9 | 9 |
|
| 10 | +# how frequently we check the encoder value and apply brightness changes |
| 11 | +ACTION_INTERVAL = 3 # seconds |
10 | 12 |
|
| 13 | +# if encoder value increases at least this much |
| 14 | +# then brightness stays the same |
| 15 | +# if encoder value increases less than this |
| 16 | +# then brightness goes down |
11 | 17 | STAY_EVEN_CHANGE_THRESHOLD = 50 |
| 18 | + |
| 19 | +# if encoder value increases at least this much |
| 20 | +# then brightness goes up |
12 | 21 | INCREASE_CHANGE_THRESHOLD = 85 |
13 | | -ACTION_INTERVAL = 3 # seconds |
14 | 22 |
|
| 23 | +# timestamp of last time an action occured |
15 | 24 | LAST_ACTION_TIME = 0 |
16 | 25 |
|
| 26 | +# encoder value variable |
17 | 27 | CUR_VALUE = 0 |
18 | 28 |
|
| 29 | +# pause state |
| 30 | +PAUSED = True |
| 31 | + |
19 | 32 | cc = ConsumerControl(usb_hid.devices) |
20 | 33 |
|
21 | 34 | encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB) |
22 | 35 | switch = digitalio.DigitalInOut(board.SWITCH) |
23 | 36 | switch.switch_to_input(pull=digitalio.Pull.DOWN) |
24 | 37 |
|
25 | 38 | switch_state = None |
| 39 | + |
| 40 | +# previous encoder position variable |
26 | 41 | last_position = encoder.position |
27 | 42 |
|
| 43 | +prev_switch_value = False |
28 | 44 |
|
29 | 45 | while True: |
30 | 46 | now = time.monotonic() |
31 | | - if (now > LAST_ACTION_TIME + ACTION_INTERVAL): |
32 | | - print("Time for action") |
33 | | - print(CUR_VALUE) |
34 | 47 |
|
35 | | - LAST_ACTION_TIME = now |
36 | | - if CUR_VALUE < STAY_EVEN_CHANGE_THRESHOLD: |
37 | | - cc.send(ConsumerControlCode.BRIGHTNESS_DECREMENT) |
38 | | - print("brightness down") |
39 | | - elif CUR_VALUE < INCREASE_CHANGE_THRESHOLD: |
40 | | - print("stay even") |
41 | | - else: |
42 | | - print("brightness up") |
43 | | - cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT) |
| 48 | + if switch.value and not prev_switch_value: |
| 49 | + print("toggling pause") |
| 50 | + PAUSED = not PAUSED |
| 51 | + |
| 52 | + if not PAUSED: |
| 53 | + LAST_ACTION_TIME = now |
| 54 | + |
| 55 | + prev_switch_value = switch.value |
| 56 | + |
| 57 | + if not PAUSED: |
| 58 | + # is it time for an action? |
| 59 | + if (now > LAST_ACTION_TIME + ACTION_INTERVAL): |
| 60 | + # print(CUR_VALUE) |
| 61 | + |
| 62 | + # update previous time variable |
| 63 | + LAST_ACTION_TIME = now |
| 64 | + |
| 65 | + # less than stay even threshold |
| 66 | + if CUR_VALUE < STAY_EVEN_CHANGE_THRESHOLD: |
| 67 | + cc.send(ConsumerControlCode.BRIGHTNESS_DECREMENT) |
| 68 | + print("brightness down") |
44 | 69 |
|
45 | | - CUR_VALUE = 0 |
| 70 | + # more than stay even threshold |
| 71 | + elif CUR_VALUE < INCREASE_CHANGE_THRESHOLD: |
| 72 | + print("stay even") |
46 | 73 |
|
| 74 | + # more than increase threshold |
| 75 | + else: |
| 76 | + cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT) |
| 77 | + print("brightness up") |
47 | 78 |
|
48 | | - current_position = encoder.position |
49 | | - position_change = int(current_position - last_position) |
| 79 | + # reset encoder value |
| 80 | + CUR_VALUE = 0 |
50 | 81 |
|
51 | | - if position_change > 0: |
| 82 | + # read current encoder value |
| 83 | + current_position = encoder.position |
| 84 | + position_change = int(current_position - last_position) |
52 | 85 |
|
53 | | - for _ in range(position_change): |
54 | | - CUR_VALUE += position_change |
| 86 | + # positive change |
| 87 | + if position_change > 0: |
| 88 | + for _ in range(position_change): |
| 89 | + CUR_VALUE += position_change |
55 | 90 |
|
56 | | - elif position_change < 0: |
57 | | - for _ in range(-position_change): |
58 | | - CUR_VALUE += int(math.fabs(position_change)) |
| 91 | + # negative change |
| 92 | + elif position_change < 0: |
| 93 | + for _ in range(-position_change): |
| 94 | + # use absolute value to convert to positive |
| 95 | + CUR_VALUE += int(math.fabs(position_change)) |
59 | 96 |
|
60 | | - last_position = current_position |
61 | | - if not switch.value and switch_state is None: |
62 | | - switch_state = "pressed" |
63 | | - if switch.value and switch_state == "pressed": |
64 | | - print("switch pressed.") |
| 97 | + last_position = current_position |
0 commit comments