-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypad.py
executable file
·79 lines (59 loc) · 1.85 KB
/
keypad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright (C) 2015 Mike Gould
#
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 2.1, as
# published by the Free Software Foundation. This program is
# distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
# Interface to the alarm keypad(s)
# requires a serial connection
# Allows you to:
# display message
# set lights
# subscribe to keypresses
import functools
import wiringpi2 as wp
class Keypad:
"""Interface to the alarm keypad
requires a serial connection
allows you to subscribe to keypresses, set leds, write to lcd"""
ADDR_KEY = "K"
ADDR_LED = "P"
ADDR_LCD = "L"
KEY_BELL = ":"
KEY_OMIT = ";"
KEY_X = "<"
KEY_P = "="
KEY_Y = ">"
KEY_UP = "?"
KEY_SOS = "\xaa"
LCD_CLEAR = "\f"
LCD_CMD = "\x04"
LCD_LINE1 = 0x80
LCD_LINE2 = 0xc0
LED_UNSET = 0x01
LED_TAMP = 0x00
LED_SOS = 0x00
LED_PWR = 0x0a
def __init__(self, connection):
self.connection = connection
def write(self, s):
m = s + chr(self.checksum(s))
print("Writing LCD: '" + m + "'")
self.connection.writeAddrMsg(m)
def checksum(self, s):
return functools.reduce(lambda a, b: a + ord(b), s, 0) & 0xff
def writeLcd(self, s):
self.write(self.ADDR_LCD + chr(len(s)) + s)
def setLeds(self, zones, leds):
self.write(self.ADDR_LED + chr(zones) + chr(leds))
def readKey(self):
msg = self.connection.readMsg(3)
return msg[1] if len(msg) >= 3 and msg[0] == self.ADDR_KEY and self.checksum(msg[:-1]) == ord(msg[-1]) else None