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 doorpi |
9
|
|
|
from doorpi.action.base import SingleAction |
10
|
|
|
|
11
|
|
|
HIGH_LEVEL = ['1', 'high', 'on', 'true'] |
12
|
|
|
LOW_LEVEL = ['0', 'low', 'off', 'false'] |
13
|
|
|
|
14
|
|
|
class KeyboardDestroyAction(SingleAction): pass |
15
|
|
|
|
16
|
|
|
class KeyboardAbstractBaseClass(object): |
17
|
|
|
|
18
|
|
|
def register_destroy_action(self, destroy_function = False): |
19
|
|
|
if destroy_function is False: destroy_function = self.destroy |
20
|
|
|
return doorpi.DoorPi().event_handler.register_action('OnShutdown', KeyboardDestroyAction(destroy_function)) |
21
|
|
|
|
22
|
|
|
# -------------methods to implement-------------- |
23
|
|
|
def __init__(self): raise NotImplementedError("Subclasses should implement this!") |
24
|
|
|
|
25
|
|
|
# def destroy(self): pass #logger.warning("Subclasses should implement this!") |
26
|
|
|
|
27
|
|
|
def self_test(self): pass # optional - raise NotImplementedError("Subclasses should implement this!") |
28
|
|
|
|
29
|
|
|
def status_input(self, pin): raise NotImplementedError("Subclasses should implement this!") |
30
|
|
|
|
31
|
|
|
def set_output(self, pin, value, log_output = True): raise NotImplementedError("Subclasses should implement this!") |
32
|
|
|
# ----------------------------------------------- |
33
|
|
|
|
34
|
|
|
keyboard_name = '' |
35
|
|
|
_pressed_on_key_down = True |
36
|
|
|
|
37
|
|
|
@property |
38
|
|
|
def keyboard_typ(self): return self.__class__.__name__ |
39
|
|
|
|
40
|
|
|
@property |
41
|
|
|
def name(self): |
42
|
|
|
if self.keyboard_name is '': return '%s Keyboard' % self.keyboard_typ |
43
|
|
|
else: return '%s (Typ: %s) Keyboard' % (self.keyboard_name, self.keyboard_typ) |
44
|
|
|
|
45
|
|
|
_InputPins = [] |
46
|
|
|
|
47
|
|
|
@property |
48
|
|
|
def input_pins(self): return self._InputPins |
49
|
|
|
_OutputPins = [] |
50
|
|
|
|
51
|
|
|
@property |
52
|
|
|
def output_pins(self): return self._OutputPins |
53
|
|
|
_OutputStatus = {} |
54
|
|
|
|
55
|
|
|
@property |
56
|
|
|
def output_status(self): return self._OutputStatus |
57
|
|
|
last_key = None |
58
|
|
|
#@property |
59
|
|
|
#def last_key(self): return self._last_key |
60
|
|
|
|
61
|
|
|
@property |
62
|
|
|
def additional_info(self): return { |
63
|
|
|
'keyboard_name' : self.keyboard_name, |
64
|
|
|
'keyboard_typ' : self.keyboard_typ, |
65
|
|
|
'name' : self.name, |
66
|
|
|
'pin' : self.last_key |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
@property |
70
|
|
|
def pressed_keys(self): |
71
|
|
|
pressed_keys = [] |
72
|
|
|
for input_pin in self._InputPins: |
73
|
|
|
if self.status_inputpin(input_pin): |
74
|
|
|
pressed_keys.append(input_pin) |
75
|
|
|
return pressed_keys |
76
|
|
|
|
77
|
|
|
@property |
78
|
|
|
def pressed_key(self): |
79
|
|
|
try: |
80
|
|
|
return self.pressed_keys[0] |
81
|
|
|
except KeyError or IndexError or TypeError: |
82
|
|
|
return None |
83
|
|
|
|
84
|
|
|
@property |
85
|
|
|
def is_destroyed(self): return self.__destroyed |
86
|
|
|
__destroyed = False |
87
|
|
|
|
88
|
|
|
def status_output(self, pin): |
89
|
|
|
return self._OutputStatus[pin] |
90
|
|
|
|
91
|
|
|
def _register_EVENTS_for_pin(self, pin, name): |
92
|
|
|
for event in ['OnKeyPressed', 'OnKeyUp', 'OnKeyDown']: |
93
|
|
|
doorpi.DoorPi().event_handler.register_event(event, name) |
94
|
|
|
doorpi.DoorPi().event_handler.register_event(event+'_'+str(pin), name) |
95
|
|
|
doorpi.DoorPi().event_handler.register_event(event+'_'+self.keyboard_name+'.'+str(pin), name) |
96
|
|
|
|
97
|
|
|
def _fire_EVENT(self, event_name, pin, name): |
98
|
|
|
if self.keyboard_name == '': |
99
|
|
|
doorpi.DoorPi().keyboard.last_key = self.last_key = pin |
100
|
|
|
else: |
101
|
|
|
doorpi.DoorPi().keyboard.last_key = self.last_key = self.keyboard_name+'.'+str(pin) |
102
|
|
|
doorpi.DoorPi().event_handler(event_name, name, self.additional_info) |
103
|
|
|
doorpi.DoorPi().event_handler(event_name+'_'+str(pin), name, self.additional_info) |
104
|
|
|
doorpi.DoorPi().event_handler(event_name+'_'+self.keyboard_name+'.'+str(pin), name, self.additional_info) |
105
|
|
|
|
106
|
|
|
def _fire_OnKeyUp(self, pin, name): self._fire_EVENT('OnKeyUp', pin, name) |
107
|
|
|
|
108
|
|
|
def _fire_OnKeyDown(self, pin, name): self._fire_EVENT('OnKeyDown', pin, name) |
109
|
|
|
|
110
|
|
|
def _fire_OnKeyPressed(self, pin, name): self._fire_EVENT('OnKeyPressed', pin, name) |
111
|
|
|
|
112
|
|
|
get_input = status_input |
113
|
|
|
status_inputpin = status_input |
114
|
|
|
get_output = status_output |
115
|
|
|
get_last_key = last_key |
116
|
|
|
which_keys_are_pressed = pressed_keys |
117
|
|
|
#__del__ = destroy |
118
|
|
|
|