Skip to content

Commit 6b81901

Browse files
authored
Merge pull request #61 from pimoroni/manual-control
Add manual control example
2 parents fe249e6 + 8c61398 commit 6b81901

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

examples/manual.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
import sys
3+
import os
4+
from fanshim import FanShim
5+
6+
FIFO_NAME = "/tmp/fanshim"
7+
8+
fanshim = FanShim()
9+
fanshim.set_hold_time(1.0)
10+
fanshim.set_fan(False)
11+
12+
try:
13+
os.unlink(FIFO_NAME)
14+
except IOError:
15+
pass
16+
17+
os.mkfifo(FIFO_NAME)
18+
19+
20+
def handle_command(data):
21+
if data == "on":
22+
fanshim.set_fan(True)
23+
print("Fan SHIM: Fan enabled")
24+
elif data == "off":
25+
fanshim.set_fan(False)
26+
print("Fan SHIM: Fan disabled")
27+
elif len(data) == 6:
28+
r, g, b = data[0:2], data[2:4], data[4:6]
29+
try:
30+
r, g, b = [int(c, 16) for c in (r, g, b)]
31+
except ValueError:
32+
print("Fan SHIM: Invalid colour {c}".format(c=data))
33+
return
34+
print("Fan SHIM: Setting LED to RGB: {r}, {g}, {b}".format(r=r, g=g, b=b))
35+
fanshim.set_light(r, g, b)
36+
37+
38+
print("""manual.py - Fan SHIM manual control
39+
40+
Example commands:
41+
42+
echo "on" > /tmp/fanshim - Turn fan on
43+
echo "off" > /tmp/fanshim - Turn fan off
44+
echo "FF0000" > /tmp/fanshim - Make LED red
45+
echo "00FF00" > /tmp/fanshim - Make LED green
46+
47+
""")
48+
49+
while True:
50+
with open(FIFO_NAME, "r") as fifo:
51+
while True:
52+
data = fifo.read().strip()
53+
if len(data) == 0:
54+
break
55+
handle_command(data)

0 commit comments

Comments
 (0)