Skip to content

Commit ca1ef64

Browse files
committed
comments and adding pause functionality
1 parent 3c12b36 commit ca1ef64

File tree

1 file changed

+60
-27
lines changed
  • Rotary_Trinkey_Brightness_Crank

1 file changed

+60
-27
lines changed

Rotary_Trinkey_Brightness_Crank/code.py

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,91 @@
77
from adafruit_hid.consumer_control import ConsumerControl
88
from adafruit_hid.consumer_control_code import ConsumerControlCode
99

10+
# how frequently we check the encoder value and apply brightness changes
11+
ACTION_INTERVAL = 3 # seconds
1012

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
1117
STAY_EVEN_CHANGE_THRESHOLD = 50
18+
19+
# if encoder value increases at least this much
20+
# then brightness goes up
1221
INCREASE_CHANGE_THRESHOLD = 85
13-
ACTION_INTERVAL = 3 # seconds
1422

23+
# timestamp of last time an action occured
1524
LAST_ACTION_TIME = 0
1625

26+
# encoder value variable
1727
CUR_VALUE = 0
1828

29+
# pause state
30+
PAUSED = True
31+
1932
cc = ConsumerControl(usb_hid.devices)
2033

2134
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
2235
switch = digitalio.DigitalInOut(board.SWITCH)
2336
switch.switch_to_input(pull=digitalio.Pull.DOWN)
2437

2538
switch_state = None
39+
40+
# previous encoder position variable
2641
last_position = encoder.position
2742

43+
prev_switch_value = False
2844

2945
while True:
3046
now = time.monotonic()
31-
if (now > LAST_ACTION_TIME + ACTION_INTERVAL):
32-
print("Time for action")
33-
print(CUR_VALUE)
3447

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")
4469

45-
CUR_VALUE = 0
70+
# more than stay even threshold
71+
elif CUR_VALUE < INCREASE_CHANGE_THRESHOLD:
72+
print("stay even")
4673

74+
# more than increase threshold
75+
else:
76+
cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT)
77+
print("brightness up")
4778

48-
current_position = encoder.position
49-
position_change = int(current_position - last_position)
79+
# reset encoder value
80+
CUR_VALUE = 0
5081

51-
if position_change > 0:
82+
# read current encoder value
83+
current_position = encoder.position
84+
position_change = int(current_position - last_position)
5285

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
5590

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))
5996

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

Comments
 (0)