Skip to content

Commit caf8101

Browse files
authored
Merge pull request #1587 from FoamyGuy/brightness_crank
Brightness rotary crank
2 parents 3a5a4ac + 174ef61 commit caf8101

7 files changed

+199
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
shaft_cutout = 6.2 + 1.7;
2+
3+
Z_HEIGHT = 30;
4+
X_HEIGHT = 36;
5+
Y_HEIGHT = 38;
6+
THICKNESS = 1.5 * 2;
7+
8+
USB_CUTOUT_Z = 20;
9+
USB_CUTOUT_X = 17;
10+
USB_CUTOUT_Y = 40;
11+
12+
TAB_SIZE = 6;
13+
TAB_HEIGHT = 2;
14+
TAB_TOLERANCE = 0.1;
15+
16+
USB_WALL_OFFSET = 1;
17+
18+
/*
19+
difference(){
20+
cube([X_HEIGHT, Y_HEIGHT, Z_HEIGHT], center=true);
21+
translate([0,0,THICKNESS/2])
22+
cube([X_HEIGHT-THICKNESS, Y_HEIGHT-THICKNESS, Z_HEIGHT], center=true);
23+
24+
translate([10,-3,0])
25+
rotate([0,90,0])
26+
cylinder(r=shaft_cutout/2, h=40, center=true, $fn=30);
27+
28+
translate([X_HEIGHT/2 - USB_CUTOUT_X/2 - THICKNESS/2 - USB_WALL_OFFSET,20,0])
29+
cube([USB_CUTOUT_X, USB_CUTOUT_Y, USB_CUTOUT_Z], center=true);
30+
}*/
31+
32+
33+
/*
34+
translate([7.5,20.5 - 3,0])
35+
rotate([-90,0,-90])
36+
import("4964 Rotary Trinkey.stl");
37+
*/
38+
39+
module close_tab(){
40+
difference(){
41+
cube([TAB_SIZE,TAB_SIZE,TAB_HEIGHT], center=true);
42+
translate([1.5, 1.5, 0])
43+
cube([TAB_SIZE,TAB_SIZE,TAB_HEIGHT+1], center=true);
44+
}
45+
}
46+
47+
48+
module lid(){
49+
cube([X_HEIGHT, Y_HEIGHT, THICKNESS/2], center=true);
50+
51+
translate([
52+
-X_HEIGHT/2 + 6/2 + THICKNESS/2 + TAB_TOLERANCE,
53+
-Y_HEIGHT/2 + 6/2 + THICKNESS/2 + TAB_TOLERANCE,
54+
THICKNESS/4 + 2/2
55+
])
56+
close_tab();
57+
58+
translate([
59+
X_HEIGHT/2 - 6/2 - THICKNESS/2 - TAB_TOLERANCE,
60+
Y_HEIGHT/2 - 6/2 - THICKNESS/2 - TAB_TOLERANCE,
61+
THICKNESS/4 + 2/2
62+
])
63+
rotate([0,0,180])
64+
close_tab();
65+
66+
translate([
67+
X_HEIGHT/2 - 6/2 - THICKNESS/2 - TAB_TOLERANCE,
68+
-Y_HEIGHT/2 + 6/2 + THICKNESS/2 + TAB_TOLERANCE,
69+
THICKNESS/4 + 2/2
70+
])
71+
rotate([0,0,90])
72+
close_tab();
73+
74+
translate([
75+
-X_HEIGHT/2 + 6/2 + THICKNESS/2 + TAB_TOLERANCE,
76+
Y_HEIGHT/2 - 6/2 - THICKNESS/2 - TAB_TOLERANCE,
77+
THICKNESS/4 + 2/2
78+
])
79+
rotate([0,0,270])
80+
close_tab();
81+
}
82+
83+
translate([0,0,16])
84+
//rotate([0,180,0])
85+
lid();
86+
87+
//translate([0, Y_HEIGHT/2-1, 10])
88+
//cube([1,1,1], center=true);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2021 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2021 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2021 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Rotary Trinkey gadget that forces you to crank the handle in order
3+
to keep the brightness up on your phone or tablet.
4+
"""
5+
6+
import time
7+
import math
8+
import board
9+
import digitalio
10+
import rotaryio
11+
import usb_hid
12+
from adafruit_hid.consumer_control import ConsumerControl
13+
from adafruit_hid.consumer_control_code import ConsumerControlCode
14+
15+
# how frequently we check the encoder value and apply brightness changes
16+
ACTION_INTERVAL = 3 # seconds
17+
18+
# if encoder value increases at least this much
19+
# then brightness stays the same
20+
# if encoder value increases less than this
21+
# then brightness goes down
22+
STAY_EVEN_CHANGE_THRESHOLD = 60
23+
24+
# if encoder value increases at least this much
25+
# then brightness goes up
26+
INCREASE_CHANGE_THRESHOLD = 95
27+
28+
# timestamp of last time an action occurred
29+
LAST_ACTION_TIME = 0
30+
31+
# encoder value variable
32+
CUR_VALUE = 0
33+
34+
# pause state
35+
PAUSED = False
36+
37+
cc = ConsumerControl(usb_hid.devices)
38+
39+
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
40+
switch = digitalio.DigitalInOut(board.SWITCH)
41+
switch.switch_to_input(pull=digitalio.Pull.DOWN)
42+
43+
switch_state = None
44+
45+
# previous encoder position variable
46+
last_position = encoder.position
47+
48+
# previous switch variable
49+
prev_switch_value = False
50+
51+
while True:
52+
now = time.monotonic()
53+
54+
if switch.value and not prev_switch_value:
55+
print("toggling pause")
56+
PAUSED = not PAUSED
57+
58+
if not PAUSED:
59+
LAST_ACTION_TIME = now
60+
61+
prev_switch_value = switch.value
62+
63+
# read current encoder value
64+
current_position = encoder.position
65+
position_change = int(current_position - last_position)
66+
67+
# positive change
68+
if position_change > 0:
69+
for _ in range(position_change):
70+
CUR_VALUE += position_change
71+
72+
# negative change
73+
elif position_change < 0:
74+
for _ in range(-position_change):
75+
# use absolute value to convert to positive
76+
CUR_VALUE += int(math.fabs(position_change))
77+
78+
last_position = current_position
79+
80+
if not PAUSED:
81+
# is it time for an action?
82+
if now > LAST_ACTION_TIME + ACTION_INTERVAL:
83+
print(CUR_VALUE)
84+
85+
# update previous time variable
86+
LAST_ACTION_TIME = now
87+
88+
# less than stay even threshold
89+
if CUR_VALUE < STAY_EVEN_CHANGE_THRESHOLD:
90+
cc.send(ConsumerControlCode.BRIGHTNESS_DECREMENT)
91+
print("brightness down")
92+
93+
# more than stay even threshold
94+
elif CUR_VALUE < INCREASE_CHANGE_THRESHOLD:
95+
print("stay even")
96+
97+
# more than increase threshold
98+
else:
99+
cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT)
100+
print("brightness up")
101+
102+
# reset encoder value
103+
CUR_VALUE = 0
104+
105+

0 commit comments

Comments
 (0)