Skip to content

Commit 1f9a298

Browse files
authored
Merge pull request #74 from pimoroni/support-disable-led-button
Support disabling LED/button for #70
2 parents a9ec6b0 + 473531a commit 1f9a298

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

examples/automatic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def set_automatic(status):
8989
last_change = 0
9090

9191

92-
fanshim = FanShim()
92+
fanshim = FanShim(disable_button=args.nobutton, disable_led=args.noled)
9393
fanshim.set_hold_time(1.0)
9494
fanshim.set_fan(False)
9595
armed = True

library/fanshim/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class FanShim():
11-
def __init__(self, pin_fancontrol=18, pin_button=17, button_poll_delay=0.05):
11+
def __init__(self, pin_fancontrol=18, pin_button=17, button_poll_delay=0.05, disable_button=False, disable_led=False):
1212
"""FAN Shim.
1313
1414
:param pin_fancontrol: BCM pin for fan on/off
@@ -24,24 +24,36 @@ def __init__(self, pin_fancontrol=18, pin_button=17, button_poll_delay=0.05):
2424
self._button_hold_time = 2.0
2525
self._t_poll = None
2626

27+
self._disable_button = disable_button
28+
self._disable_led = disable_led
29+
2730
GPIO.setwarnings(False)
2831
GPIO.setmode(GPIO.BCM)
2932
GPIO.setup(self._pin_fancontrol, GPIO.OUT)
30-
GPIO.setup(self._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
3133

32-
self._led = apa102.APA102(1, 15, 14, None, brightness=0.05)
34+
if not self._disable_button:
35+
GPIO.setup(self._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
36+
37+
if not self._disable_led:
38+
self._led = apa102.APA102(1, 15, 14, None, brightness=0.05)
3339

3440
atexit.register(self._cleanup)
3541

3642
def start_polling(self):
3743
"""Start button polling."""
44+
if self._disable_button:
45+
return
46+
3847
if self._t_poll is None:
3948
self._t_poll = Thread(target=self._run)
4049
self._t_poll.daemon = True
4150
self._t_poll.start()
4251

4352
def stop_polling(self):
4453
"""Stop button polling."""
54+
if self._disable_button:
55+
return
56+
4557
if self._t_poll is not None:
4658
self._running = False
4759
self._t_poll.join()
@@ -112,6 +124,9 @@ def set_light(self, r, g, b, brightness=None):
112124
:param b: Blue (0-255)
113125
114126
"""
127+
if self._disable_led:
128+
return
129+
115130
self._led.set_pixel(0, r, g, b)
116131
if brightness is not None:
117132
self._led.set_brightness(0, brightness)

library/tests/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Test configuration.
2+
These allow the mocking of various Python modules
3+
that might otherwise have runtime side-effects.
4+
"""
5+
import sys
6+
import mock
7+
import pytest
8+
9+
10+
@pytest.fixture(scope='function', autouse=False)
11+
def FanShim():
12+
import fanshim
13+
yield fanshim.FanShim
14+
del sys.modules['fanshim']
15+
16+
17+
@pytest.fixture(scope='function', autouse=False)
18+
def GPIO():
19+
"""Mock RPi.GPIO module."""
20+
GPIO = mock.MagicMock()
21+
# Fudge for Python < 37 (possibly earlier)
22+
sys.modules['RPi'] = mock.Mock()
23+
sys.modules['RPi'].GPIO = GPIO
24+
sys.modules['RPi.GPIO'] = GPIO
25+
yield GPIO
26+
del sys.modules['RPi']
27+
del sys.modules['RPi.GPIO']
28+
29+
30+
@pytest.fixture(scope='function', autouse=False)
31+
def apa102():
32+
"""Mock APA102 module."""
33+
apa102 = mock.MagicMock()
34+
sys.modules['apa102'] = apa102
35+
yield apa102
36+
del sys.modules['apa102']
37+
38+
39+
@pytest.fixture(scope='function', autouse=False)
40+
def spidev():
41+
"""Mock spidev module."""
42+
spidev = mock.MagicMock()
43+
sys.modules['spidev'] = spidev
44+
yield spidev
45+
del sys.modules['spidev']
46+
47+
48+
@pytest.fixture(scope='function', autouse=False)
49+
def atexit():
50+
"""Mock atexit module."""
51+
atexit = mock.MagicMock()
52+
sys.modules['atexit'] = atexit
53+
yield atexit
54+
del sys.modules['atexit']
55+

library/tests/test_setup.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@
22
import sys
33

44

5-
def test_setup():
6-
sys.modules['RPi'] = mock.Mock()
7-
sys.modules['RPi.GPIO'] = mock.Mock()
8-
sys.modules['plasma'] = mock.Mock()
9-
10-
from fanshim import FanShim
5+
def test_setup(GPIO, apa102, FanShim):
116
fanshim = FanShim()
12-
del fanshim
7+
8+
GPIO.setwarnings.assert_called_once_with(False)
9+
GPIO.setmode.assert_called_once_with(GPIO.BCM)
10+
GPIO.setup.assert_has_calls([
11+
mock.call(fanshim._pin_fancontrol, GPIO.OUT),
12+
mock.call(fanshim._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
13+
])
14+
15+
apa102.APA102.assert_called_once_with(1, 15, 14, None, brightness=0.05)
16+
17+
18+
def test_button_disable(GPIO, apa102, FanShim):
19+
fanshim = FanShim(disable_button=True)
20+
21+
GPIO.setwarnings.assert_called_once_with(False)
22+
GPIO.setmode.assert_called_once_with(GPIO.BCM)
23+
GPIO.setup.assert_called_once_with(fanshim._pin_fancontrol, GPIO.OUT)
24+
25+
26+
def test_led_disable(GPIO, apa102, FanShim):
27+
fanshim = FanShim(disable_led=True)
28+
29+
GPIO.setwarnings.assert_called_once_with(False)
30+
GPIO.setmode.assert_called_once_with(GPIO.BCM)
31+
GPIO.setup.assert_has_calls([
32+
mock.call(fanshim._pin_fancontrol, GPIO.OUT),
33+
mock.call(fanshim._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
34+
])
35+
36+
assert not apa102.APA102.called
37+
38+
fanshim.set_light(0, 0, 0)

library/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,35},qa
2+
envlist = py{27,35,37},qa
33
skip_missing_interpreters = True
44

55
[testenv]

0 commit comments

Comments
 (0)