1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import logging |
5
|
|
|
logger = logging.getLogger(__name__) |
6
|
|
|
logger.debug("%s loaded", __name__) |
7
|
|
|
|
8
|
|
|
import pifacedigitalio as p # basic for PiFce control |
9
|
|
|
from doorpi.keyboard.AbstractBaseClass import KeyboardAbstractBaseClass, HIGH_LEVEL, LOW_LEVEL |
10
|
|
|
import doorpi |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def get(**kwargs): return PiFace(**kwargs) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class PiFace(KeyboardAbstractBaseClass): |
17
|
|
|
|
18
|
|
|
def __init__(self, input_pins, output_pins, keyboard_name, bouncetime, |
|
|
|
|
19
|
|
|
polarity=0, pressed_on_key_down=True, *args, **kwargs): |
20
|
|
|
logger.debug("__init__(input_pins = %s, output_pins = %s, polarity = %s)", |
21
|
|
|
input_pins, output_pins, polarity) |
22
|
|
|
self.keyboard_name = keyboard_name |
23
|
|
|
self._polarity = polarity |
24
|
|
|
self._InputPins = map(int, input_pins) |
25
|
|
|
self._OutputPins = map(int, output_pins) |
26
|
|
|
self._pressed_on_key_down = pressed_on_key_down |
27
|
|
|
|
28
|
|
|
p.init() |
29
|
|
|
self.__listener = p.InputEventListener() |
30
|
|
|
for input_pin in self._InputPins: |
31
|
|
|
self.__listener.register( |
32
|
|
|
pin_num=input_pin, |
33
|
|
|
direction=p.IODIR_BOTH, |
34
|
|
|
callback=self.event_detect, |
35
|
|
|
settle_time=bouncetime / 1000 # from milliseconds to seconds |
36
|
|
|
) |
37
|
|
|
self._register_EVENTS_for_pin(input_pin, __name__) |
38
|
|
|
self.__listener.activate() |
39
|
|
|
|
40
|
|
|
# use set_output to register status @ dict self.__OutputStatus |
41
|
|
|
for output_pin in self._OutputPins: |
42
|
|
|
self.set_output(output_pin, 0, False) |
43
|
|
|
|
44
|
|
|
self.register_destroy_action() |
45
|
|
|
|
46
|
|
|
def destroy(self): |
47
|
|
|
if self.is_destroyed: |
48
|
|
|
return |
49
|
|
|
logger.debug("destroy") |
50
|
|
|
|
51
|
|
|
# shutdown listener |
52
|
|
|
self.__listener.deactivate() |
53
|
|
|
|
54
|
|
|
# shutdown all output-pins |
55
|
|
|
for output_pin in self._OutputPins: |
56
|
|
|
self.set_output(output_pin, 0, False) |
57
|
|
|
p.deinit() |
58
|
|
|
doorpi.DoorPi().event_handler.unregister_source(__name__, True) |
59
|
|
|
self.__destroyed = True |
60
|
|
|
|
61
|
|
|
def event_detect(self, event): |
62
|
|
|
if self.status_input(event.pin_num): |
63
|
|
|
self._fire_OnKeyDown(event.pin_num, __name__) |
64
|
|
|
if self._pressed_on_key_down: # issue 134 |
65
|
|
|
self._fire_OnKeyPressed(event.pin_num, __name__) |
66
|
|
|
else: |
67
|
|
|
self._fire_OnKeyUp(event.pin_num, __name__) |
68
|
|
|
if not self._pressed_on_key_down: # issue 134 |
69
|
|
|
self._fire_OnKeyPressed(event.pin_num, __name__) |
70
|
|
|
|
71
|
|
|
def status_input(self, pin): |
72
|
|
|
if self._polarity is 0: |
73
|
|
|
return str(p.digital_read(int(pin))).lower() in HIGH_LEVEL |
74
|
|
|
else: |
75
|
|
|
return str(p.digital_read(int(pin))).lower() in LOW_LEVEL |
76
|
|
|
|
77
|
|
View Code Duplication |
def set_output(self, pin, value, log_output = True): |
|
|
|
|
78
|
|
|
parsed_pin = doorpi.DoorPi().parse_string("!"+str(pin)+"!") |
79
|
|
|
if parsed_pin != "!"+str(pin)+"!": |
80
|
|
|
pin = parsed_pin |
81
|
|
|
|
82
|
|
|
pin = int(pin) |
83
|
|
|
value = str(value).lower() in HIGH_LEVEL |
84
|
|
|
if self._polarity is 1: value = not value |
85
|
|
|
log_output = str(log_output).lower() in HIGH_LEVEL |
86
|
|
|
|
87
|
|
|
if not pin in self._OutputPins: return False |
88
|
|
|
if log_output: logger.debug("out(pin = %s, value = %s, log_output = %s)", pin, value, log_output) |
89
|
|
|
|
90
|
|
|
p.digital_write(pin, value) |
91
|
|
|
self._OutputStatus[pin] = value |
92
|
|
|
return True |
93
|
|
|
|