1 | #!/usr/bin/env python |
||
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | # |
||
5 | # Configuration |
||
6 | # ------------- |
||
7 | # |
||
8 | # 1. Define a new keyboard of type 'rdm6300' |
||
9 | # 2. Define inputPins section for that keyboard |
||
10 | # 3. Each RFID tag has a decimal number printed |
||
11 | # on the surface. This is the Input PIN number. |
||
12 | # Define this number and an appropriate action. |
||
13 | # |
||
14 | # Sample: |
||
15 | # |
||
16 | # [keyboards] |
||
17 | # rfidreader = rdm6300 |
||
18 | # ... |
||
19 | # [rfidreader_InputPins] |
||
20 | # 1234567 = out:Tueroeffner,1,0,3 |
||
21 | # 2345678 = out:Tueroeffner,1,0,3 |
||
22 | # |
||
23 | # That's all... |
||
24 | # |
||
25 | # |
||
26 | # |
||
27 | # Hardware Connections |
||
28 | # -------------------- |
||
29 | # |
||
30 | # RDM6300 Pin Layout |
||
31 | # +-------------------------+ |
||
32 | # | | |
||
33 | # | (1) ANT1 | |
||
34 | # | (2) ANT2 | |
||
35 | # | P2 | |
||
36 | # | | |
||
37 | # | | |
||
38 | # | | |
||
39 | # | P1 | |
||
40 | # | +5V(DC) (5) | |
||
41 | # | P3 GND (4) | |
||
42 | # | (3) GND (3) | |
||
43 | # | (2) +5V(DC) RX (2) | |
||
44 | # | (1) LED TX (1) | |
||
45 | # | | |
||
46 | # +-------------------------+ |
||
47 | # |
||
48 | # Connect one of the two +5V(DC) and one of the two GND to |
||
49 | # 5V (Pin 2 on the RaspberryPi Board) and to GND (Pin 6 on |
||
50 | # the RaspberryPi Board). As I used a ribbon cable, the |
||
51 | # simplest way was to connect to (4) and (5) of P1 from the RDM6300. |
||
52 | # |
||
53 | # Then, connect TX (pin (1) of P1) to RXD from the UART (Pin 10 |
||
54 | # on the RaspberryPi Board) - BUT NOT DIRECTLY, OTHERWISE YOU |
||
55 | # MIGHT DAMAGE YOUR RASPBERRY PI!!! |
||
56 | # The RaspberryPi expects 3,3V level on the UART Pins, but the |
||
57 | # RDM6300 delivers 5V. |
||
58 | # |
||
59 | # Simplest solution for this is a voltage divider via resistors: |
||
60 | # RDM6300 P1(1) <--- Resistor R1 ---> RasPi Board(Pin 10) |
||
61 | # GND <--- Resistor R2 ---> RasPi Board(Pin 10) |
||
62 | # Ideal solution: R1=5k, R2=10k, this will deliver exactly 3,3V |
||
63 | # to RasPi Board(Pin 10) |
||
64 | # Alternative solution: As most RaspberryPi bundles only contain |
||
65 | # 10k resistors, you might either use 2 |
||
66 | # 10k resistors in parallel to get a 5k |
||
67 | # resistor, or simply use 10k for R1 instead. |
||
68 | # R1=R2=10k will deliver 2,5V to RasPi |
||
69 | # Board(Pin 10), but that works also. |
||
70 | # |
||
71 | # Reference: I used this resource to learn how to work with RDM6300, |
||
72 | # how to connect it to the RaspberryPi and how to handle |
||
73 | # RFID data: http://kampis-elektroecke.de/?page_id=3248 |
||
74 | |||
75 | import logging |
||
76 | logger = logging.getLogger(__name__) |
||
77 | logger.debug("%s loaded", __name__) |
||
78 | |||
79 | import threading |
||
80 | import serial |
||
81 | import time |
||
82 | |||
83 | from doorpi.keyboard.AbstractBaseClass import KeyboardAbstractBaseClass, HIGH_LEVEL, LOW_LEVEL |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
84 | import doorpi |
||
85 | |||
86 | START_FLAG = '\x02' |
||
87 | STOP_FLAG = '\x03' |
||
88 | MAX_LENGTH = 14 |
||
89 | |||
90 | def get(**kwargs): return RDM6300(**kwargs) |
||
91 | class RDM6300(KeyboardAbstractBaseClass): |
||
92 | name = 'RFID Reader RDM6300' |
||
93 | |||
94 | @staticmethod |
||
95 | def calculate_checksum(string): |
||
96 | checkSum = 0 |
||
97 | for I in range(1, 10, 2): |
||
98 | checkSum = checkSum ^ ((int(string[I], 16)) << 4) + int(string[I+1], 16) |
||
99 | return checkSum |
||
100 | |||
101 | @staticmethod |
||
102 | def check_checksum(string): |
||
103 | given_checksum = (int(string[11], 16) << 4) + int(string[12], 16) |
||
104 | return given_checksum == RDM6300.calculate_checksum(string) |
||
105 | |||
106 | def readUART(self): |
||
107 | while not self._shutdown: |
||
108 | logger.debug("readUART() started") |
||
109 | # initialize UART |
||
110 | # make sure that terminal via UART is disabled |
||
111 | # see http://kampis-elektroecke.de/?page_id=3248 for details |
||
112 | self._UART = serial.Serial(self.__port, self.__baudrate) |
||
113 | self._UART.timeout = 1 |
||
114 | self._UART.close() |
||
115 | self._UART.open() |
||
116 | try: |
||
117 | chars = "" |
||
118 | |||
119 | while not self._shutdown: |
||
120 | # char aus buffer holen |
||
121 | newChar = self._UART.read() |
||
122 | if newChar != "": |
||
123 | logger.debug("new char %s read", newChar) |
||
124 | chars += str(newChar) |
||
125 | |||
126 | # aktuelles Ergebnis kontrollieren |
||
127 | if newChar == STOP_FLAG and chars[0] == START_FLAG and len(chars) == MAX_LENGTH and RDM6300.check_checksum(chars): |
||
128 | logger.debug("found tag, checking dismisstime") |
||
129 | # alles okay... nur noch schauen, ob das nicht eine Erkennungs-Wiederholung ist |
||
130 | now = time.time() |
||
131 | if now - self.last_key_time > self.__dismisstime: |
||
132 | doorpi.DoorPi().event_handler('OnFoundTag', __name__) |
||
133 | self.last_key = int(chars[5:-3], 16) |
||
134 | self.last_key_time = now |
||
135 | logger.debug("key is %s", self.last_key) |
||
136 | if self.last_key in self._InputPins: |
||
137 | self._fire_OnKeyDown(self.last_key, __name__) |
||
138 | self._fire_OnKeyPressed(self.last_key, __name__) |
||
139 | self._fire_OnKeyUp(self.last_key, __name__) |
||
140 | doorpi.DoorPi().event_handler('OnFoundKnownTag', __name__) |
||
141 | else: |
||
142 | doorpi.DoorPi().event_handler('OnFoundUnknownTag', __name__) |
||
143 | |||
144 | # ggf. löschen |
||
145 | if newChar == STOP_FLAG or len(chars) > MAX_LENGTH: |
||
146 | chars = "" |
||
147 | |||
148 | except Exception as ex: |
||
149 | logger.exception(ex) |
||
150 | finally: |
||
151 | # shutdown the UART |
||
152 | self._UART.close() |
||
153 | self._UART = None |
||
154 | logger.debug("readUART thread ended") |
||
155 | |||
156 | |||
157 | def __init__(self, input_pins, keyboard_name, conf_pre, conf_post, *args, **kwargs): |
||
158 | logger.debug("__init__ (input_pins = %s)", input_pins) |
||
159 | self.keyboard_name = keyboard_name |
||
160 | self._InputPins = map(int, input_pins) |
||
161 | |||
162 | doorpi.DoorPi().event_handler.register_event('OnFoundTag', __name__) |
||
163 | doorpi.DoorPi().event_handler.register_event('OnFoundUnknownTag', __name__) |
||
164 | doorpi.DoorPi().event_handler.register_event('OnFoundKnownTag', __name__) |
||
165 | |||
166 | self.last_key = "" |
||
167 | self.last_key_time = 0 |
||
168 | |||
169 | # somit wirds aus der Config-Datei geladen, falls dort vorhanden. |
||
170 | section_name = conf_pre+'keyboard'+conf_post |
||
171 | self.__port = doorpi.DoorPi().config.get(section_name, 'port', "/dev/ttyAMA0") |
||
172 | self.__baudrate = doorpi.DoorPi().config.get_int(section_name, 'baudrate', 9600) |
||
173 | self.__dismisstime = doorpi.DoorPi().config.get_int(section_name, 'dismisstime', 5) |
||
174 | |||
175 | for input_pin in self._InputPins: |
||
176 | self._register_EVENTS_for_pin(input_pin, __name__) |
||
177 | |||
178 | self._shutdown = False |
||
179 | self._thread = threading.Thread(target = self.readUART) |
||
180 | self._thread.daemon = True |
||
181 | self._thread.start() |
||
182 | |||
183 | self.register_destroy_action() |
||
184 | |||
185 | def destroy(self): |
||
186 | if self.is_destroyed: return |
||
187 | logger.debug("destroy") |
||
188 | self._shutdown = True |
||
189 | doorpi.DoorPi().event_handler.unregister_source(__name__, True) |
||
190 | self.__destroyed = True |
||
191 | |||
192 | def status_input(self, tag): |
||
193 | logger.debug("status_input for tag %s", tag) |
||
194 | if tag == self.last_key: |
||
195 | return True |
||
196 | else: |
||
197 | return False |
||
198 | |||
199 | def set_output(self, pin, value, log_output = True): |
||
200 | # RDM6300 does not support output |
||
201 | return False |
||
202 |