Skip to content

Commit 496fd86

Browse files
committed
Adding Slider demos.
1 parent 7e1b71e commit 496fd86

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Slider Trinkey Hue Dimmer Python Example
3+
(Requires Hue Dimmer CircuitPython example to be running on the Slider Trinkey)
4+
"""
5+
from phue import Bridge
6+
import serial
7+
from serial.tools import list_ports
8+
9+
# Update this to the room, zone or individual lamp you want to control.
10+
LAMP_OR_GROUP_NAME = "Office Desk Stand Lamp"
11+
12+
# Update this to the IP address of your Hue Bridge.
13+
b = Bridge("10.19.69.50")
14+
15+
slider_trinkey_port = None
16+
ports = list_ports.comports(include_links=False)
17+
for p in ports:
18+
if p.pid is not None:
19+
print("Port:", p.device, "-", hex(p.pid), end="\t")
20+
if p.pid == 0x8102:
21+
slider_trinkey_port = p
22+
print("Found Slider Trinkey!")
23+
break
24+
else:
25+
print()
26+
else:
27+
print("Did not find Slider Trinkey port :(")
28+
exit(-1)
29+
30+
trinkey = serial.Serial(p.device)
31+
32+
# If the app is not registered and the button on the Hue Bridge is not pressed, press the button
33+
# and call connect() (this only needs to be run a single time)
34+
b.connect()
35+
b.get_api()
36+
37+
is_group = False
38+
light = None
39+
40+
# First, check if it's a group name.
41+
for group_data in b.get_group().values():
42+
if group_data["name"] == LAMP_OR_GROUP_NAME:
43+
print("Found group with name", LAMP_OR_GROUP_NAME)
44+
is_group = True
45+
46+
# If it's not a group, find the lamp by name.
47+
if not is_group:
48+
light_names = b.get_light_objects("name")
49+
light = light_names[LAMP_OR_GROUP_NAME]
50+
print("Found light with name", LAMP_OR_GROUP_NAME)
51+
52+
current_brightness = None
53+
while True:
54+
x = trinkey.readline().decode("utf-8")
55+
if not x.startswith("Slider: "):
56+
continue
57+
58+
# Convert the Slider Trinkey output value of 0-100 to 0-254.
59+
brightness_value = int((float(x.split(": ")[1]) / 100) * 254)
60+
61+
if current_brightness is None or brightness_value != current_brightness:
62+
print("Setting brightness to:", brightness_value)
63+
if is_group:
64+
b.set_group(LAMP_OR_GROUP_NAME, {"bri": brightness_value})
65+
else:
66+
light.brightness = brightness_value
67+
current_brightness = brightness_value
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
import board
3+
from analogio import AnalogIn
4+
import adafruit_simplemath
5+
6+
analog_in = AnalogIn(board.POTENTIOMETER)
7+
8+
def read_pot(samples, min_val, max_val):
9+
sum = 0
10+
for _ in range(samples):
11+
sum += analog_in.value
12+
sum /= samples # ok take the average
13+
14+
return adafruit_simplemath.map_range(sum, 100, 65535, min_val, max_val)
15+
16+
while True:
17+
print("Slider:", round(read_pot(10, 0, 100)))
18+
time.sleep(0.1)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import screen_brightness_control as sbc
2+
import serial
3+
from serial.tools import list_ports
4+
5+
slider_trinkey_port = None
6+
ports = list_ports.comports(include_links=False)
7+
for p in ports:
8+
if p.pid is not None:
9+
print("Port:", p.device, "-", hex(p.pid), end="\t")
10+
if p.pid == 0x8102:
11+
slider_trinkey_port = p
12+
print("Found Slider Trinkey!")
13+
break
14+
else:
15+
print()
16+
else:
17+
print("Did not find Slider Trinkey port :(")
18+
exit(-1)
19+
20+
trinkey = serial.Serial(p.device)
21+
curr_brightness = sbc.get_brightness()
22+
23+
while True:
24+
x = trinkey.readline().decode('utf-8')
25+
if not x.startswith("Slider: "):
26+
continue
27+
28+
val = int(float(x.split(": ")[1]))
29+
30+
if val != curr_brightness:
31+
print("Setting brightness to:", val)
32+
sbc.set_brightness(val)
33+
curr_brightness = sbc.get_brightness()

0 commit comments

Comments
 (0)