Skip to content

Commit 42c196f

Browse files
committed
Documentation and code improvements.
1 parent 211808d commit 42c196f

6 files changed

Lines changed: 125 additions & 15 deletions

File tree

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,92 @@ Latest/development library from GitHub:
1717
* `cd fanshim-python`
1818
* `sudo ./install.sh`
1919

20+
# Reference
21+
22+
You should first set up an instance of the `FANShim` class, eg:
23+
24+
```
25+
from fanshim import FanShim
26+
fanshim = FanShim()
27+
```
28+
29+
## Fan
30+
31+
Turn the fan on with: `fanshim.set_fan(True)`
32+
33+
Turn it off with: `fanshim.set_fan(False)`
34+
35+
You can also toggle the fan with: `fanshim.toggle_fan()`
36+
37+
## LED
38+
39+
Fan Shim includes one RGB APA-102 LED.
40+
41+
Set it to any colour with: `fanshim.set_light(r, g, b)`
42+
43+
Arguments r, g and b should be numbers between 0 and 255 that describe the colour you want.
44+
45+
For example, full red: `fanshim.set_light(255, 0, 0)`
46+
47+
## Button
48+
49+
Fan Shim includes a button, you can bind actions to press, release and hold events.
50+
51+
Do something when the button is pressed:
52+
53+
```
54+
@fanshim.on_press()
55+
def button_pressed():
56+
print("The button has been pressed!")
57+
```
58+
59+
Or when it has been released:
60+
61+
```
62+
@fanshim.on_release()
63+
def button_released(was_held):
64+
print("The button has been pressed!")
65+
```
66+
67+
Or when it's been pressed long enough to trigger a hold:
68+
69+
```
70+
fanshim.set_hold_time(2.0)
71+
72+
@fanshim.on_hold()
73+
def button_held():
74+
print("The button was held for 2 seconds")
75+
```
76+
77+
The function you bind to `on_release()` is passed a `was_held` parameter,
78+
this lets you know if the button was held down for longer than the configured
79+
hold time. If you want to bind an action to "press" and another to "hold" you
80+
should check this flag and perform your action in the `on_release()` handler:
81+
82+
```
83+
@fanshim.on_release()
84+
def button_released(was_held):
85+
if was_held:
86+
print("Long press!")
87+
else:
88+
print("Short press!")
89+
```
90+
91+
To configure the amount of time the button should be held (in seconds), use:
92+
93+
```
94+
fanshim.set_hold_time(number_of_seconds)
95+
```
96+
97+
If you need to stop Fan Shim from polling the button, use:
98+
99+
```
100+
fanshim.stop_polling()
101+
```
102+
103+
You can start it again with:
104+
105+
```
106+
fanshim.start_polling()
107+
```
108+

examples/automatic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fanshim import FANShim
1+
from fanshim import FanShim
22
import psutil
33
import argparse
44
import time
@@ -38,7 +38,7 @@ def set_automatic(status):
3838

3939
args = parser.parse_args()
4040

41-
fanshim = FANShim()
41+
fanshim = FanShim()
4242
fanshim.set_hold_time(1.0)
4343
fanshim.set_fan(False)
4444
armed = True

examples/button.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import signal
2-
from fanshim import FANShim
2+
from fanshim import FanShim
33

44
"""
55
This example attaches basic pressed, released and held handlers for Fan SHIM's button.
@@ -11,7 +11,7 @@
1111
the "held" handler at all if you're just doing a standard short/long press action.
1212
"""
1313

14-
fanshim = FANShim()
14+
fanshim = FanShim()
1515

1616
# Set the button hold time, in seconds
1717
fanshim.set_hold_time(1.0)

examples/led.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from fanshim import FANShim
1+
from fanshim import FanShim
22
import time
33
import colorsys
44

5-
fanshim = FANShim()
5+
fanshim = FanShim()
66
fanshim.set_fan(False)
77

88
try:

examples/toggle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import signal
2-
from fanshim import FANShim
2+
from fanshim import FanShim
33

44
"""
55
This example attaches basic pressed, released and held handlers for Fan SHIM's button.
@@ -11,7 +11,7 @@
1111
the "held" handler at all if you're just doing a standard short/long press action.
1212
"""
1313

14-
fanshim = FANShim()
14+
fanshim = FanShim()
1515

1616

1717
def update_led(state):

library/fanshim/__init__.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
__version__ = '0.0.1'
88

99

10-
class FANShim():
11-
def __init__(self):
12-
self._pin_fancontrol = 18
13-
self._pin_button = 17
10+
class FanShim():
11+
def __init__(self, pin_fancontrol=18, pin_button=17):
12+
"""FAN Shim.
13+
14+
:param pin_fancontrol: BCM pin for fan on/off
15+
:param pin_button: BCM pin for button
16+
17+
"""
18+
self._pin_fancontrol = pin_fancontrol
19+
self._pin_button = pin_button
1420
self._button_press_handler = None
1521
self._button_release_handler = None
1622
self._button_hold_handler = None
23+
self._button_hold_time = 2.0
1724
self._t_poll = None
1825

1926
atexit.register(self._cleanup)
@@ -27,20 +34,23 @@ def __init__(self):
2734
plasma.set_light_count(1)
2835
plasma.set_light(0, 0, 0, 0)
2936

30-
self.start()
37+
self.start_polling()
3138

32-
def start(self):
39+
def start_polling(self):
40+
"""Start button polling."""
3341
if self._t_poll is None:
3442
self._t_poll = Thread(target=self._run)
3543
self._t_poll.daemon = True
3644
self._t_poll.start()
3745

38-
def stop(self):
46+
def stop_polling(self):
47+
"""Stop button polling."""
3948
if self._t_poll is not None:
4049
self._running = False
4150
self._t_poll.join()
4251

4352
def on_press(self, handler=None):
53+
"""Attach function to button press event."""
4454
def attach_handler(handler):
4555
self._button_press_handler = handler
4656

@@ -50,6 +60,7 @@ def attach_handler(handler):
5060
return attach_handler
5161

5262
def on_release(self, handler=None):
63+
"""Attach function to button release event."""
5364
def attach_handler(handler):
5465
self._button_release_handler = handler
5566

@@ -59,6 +70,7 @@ def attach_handler(handler):
5970
return attach_handler
6071

6172
def on_hold(self, handler=None):
73+
"""Attach function to button hold event."""
6274
def attach_handler(handler):
6375
self._button_hold_handler = handler
6476

@@ -76,9 +88,11 @@ def set_hold_time(self, hold_time):
7688
self._button_hold_time = hold_time
7789

7890
def get_fan(self):
91+
"""Get current fan state."""
7992
return GPIO.input(self._pin_fancontrol)
8093

8194
def toggle_fan(self):
95+
"""Toggle fan state."""
8296
return self.set_fan(False if self.get_fan() else True)
8397

8498
def set_fan(self, fan_state):
@@ -91,6 +105,13 @@ def set_fan(self, fan_state):
91105
return True if fan_state else False
92106

93107
def set_light(self, r, g, b):
108+
"""Set LED.
109+
110+
:param r: Red (0-255)
111+
:param g: Green (0-255)
112+
:param b: Blue (0-255)
113+
114+
"""
94115
plasma.set_light(0, r, g, b)
95116
plasma.show()
96117

0 commit comments

Comments
 (0)