forked from MiczFlor/RPi-Jukebox-RFID
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add shutdown_button.py rename python files
- Loading branch information
Showing
14 changed files
with
132 additions
and
154 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import math | ||
import time | ||
from RPi import GPIO | ||
import logging | ||
from components.gpio_control.simple_button import SimpleButton | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
class ShutdownButton(SimpleButton): | ||
|
||
def __init__(self, pin, action=lambda *args: None, name=None, bouncetime=500, edge=GPIO.FALLING, | ||
hold_time=.1, led_pin=None, time_pressed=2): | ||
self.led_pin = led_pin | ||
self.time_pressed = 2 | ||
self.iteration_time = .2 | ||
super(ShutdownButton, self).__init__(pin=pin, action=action, name=name, bouncetime=bouncetime, edge=edge, | ||
hold_time=hold_time, hold_repeat=False) | ||
|
||
|
||
# function to provide user feedback (= flashing led) while the shutdown button is pressed | ||
# do not directly call shutdown, in case it was hit accedently | ||
# shutdown is only issued when the button remains pressed for all interations of the for loop | ||
def set_led(self, status): | ||
if self.led_pin is not None: | ||
logger.debug('set LED on pin {} to {}'.format(self.led_pin, status)) | ||
GPIO.output(self.led_pin, status) | ||
else: | ||
logger.debug('cannot set LED to {}: no LED pin defined'.format(status)) | ||
|
||
def callbackFunctionHandler(self, *args): | ||
status = False | ||
n_checks = math.ceil(self.time_pressed/self.iteration_time) | ||
logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s: {}'.format( | ||
self.time_pressed, self.iteration_time, n_checks | ||
)) | ||
for x in range(n_checks): | ||
self.set_led(x & 1) | ||
time.sleep(.2) | ||
status = not self.is_pressed | ||
if status: | ||
break | ||
self.set_led(status) | ||
if not status: | ||
# trippel off period to indicate command accepted | ||
time.sleep(.6) | ||
self.set_led(GPIO.HIGH) | ||
# leave it on for the moment, it will be off when the system is down | ||
self.when_pressed(*args) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
|
||
from mock import Mock, patch | ||
import mock | ||
|
||
from components.gpio_control.shutdown_button import ShutdownButton, GPIO | ||
|
||
mock_time = Mock() | ||
|
||
mocked_function = Mock() | ||
|
||
|
||
@pytest.fixture | ||
def shutdown_button(): | ||
mocked_function.reset_mock() | ||
return ShutdownButton(pin=1, action=mocked_function) | ||
|
||
|
||
class TestShutdownButton(): | ||
def test_init(self): | ||
ShutdownButton(pin=1) | ||
|
||
@patch('time.sleep', mock_time) | ||
def test_action(self, shutdown_button): | ||
for i in range(9): | ||
GPIO.input.reset_mock() | ||
GPIO.input.side_effect = i*[0]+[1] | ||
shutdown_button.callbackFunctionHandler() | ||
assert GPIO.input.call_count == i+1 | ||
mocked_function.assert_not_called() | ||
|
||
|
||
@patch('time.sleep', mock_time) | ||
def test_action2(self, shutdown_button): | ||
GPIO.input.side_effect = lambda *args: 1 | ||
shutdown_button.callbackFunctionHandler() | ||
mocked_function.assert_not_called() | ||
|
||
@patch('time.sleep', mock_time) | ||
def test_action3(self, shutdown_button): | ||
GPIO.input.side_effect = lambda *args: 0 | ||
shutdown_button.callbackFunctionHandler() | ||
mocked_function.assert_called_once() |
Oops, something went wrong.