|
10 | 10 | import time
|
11 | 11 |
|
12 | 12 | r = redis.StrictRedis(host="localhost", port=6379, db=0)
|
13 |
| -s = serial.Serial('/dev/ttyProMicro') |
14 |
| -p = gpiozero.DigitalOutputDevice(17) # the locking output pin |
| 13 | +s = serial.Serial("/dev/ttyProMicro") |
| 14 | +p = gpiozero.DigitalOutputDevice(17) # the locking output pin |
15 | 15 | h = hid.device()
|
16 |
| -h.open(0x1b4f, 0x9206) |
17 |
| -unlock_max_time = 3600 # max unlock is 1 hour |
| 16 | +h.open(0x1B4F, 0x9206) |
| 17 | +unlock_max_time = 3600 # max unlock is 1 hour |
| 18 | + |
18 | 19 |
|
19 | 20 | def button():
|
20 |
| - keepopen = False |
21 |
| - lastopen = 0 |
22 |
| - while True: |
23 |
| - # read low level HID report |
24 |
| - d = h.read(30, 200) |
25 |
| - if d and d[0] == 2 and d[1] == 3 and d[2] == 0: |
26 |
| - # SHIFT + CTRL |
27 |
| - if d[3] == 0x33: # KEY_SEMICOLON |
28 |
| - # open |
29 |
| - r.publish('door_action', 'OPEN') |
30 |
| - print("door open pressed", flush=True) |
31 |
| - elif d[3] == 0x31: # KEY_BACKSLASH |
32 |
| - # toggle keep open (i.e. unlock) |
33 |
| - keepopen = not keepopen |
34 |
| - if keepopen: |
35 |
| - print("unlock pressed, will open for %d seconds" % unlock_max_time, flush=True) |
36 |
| - r.publish('door_action', 'OPEN') |
| 21 | + keepopen = False |
| 22 | + lastopen = 0 |
| 23 | + while True: |
| 24 | + # read low level HID report |
| 25 | + d = h.read(30, 200) |
| 26 | + if d and d[0] == 2 and d[1] == 3 and d[2] == 0: |
| 27 | + # SHIFT + CTRL |
| 28 | + if d[3] == 0x33: # KEY_SEMICOLON |
| 29 | + # open |
| 30 | + r.publish("door_action", "OPEN") |
| 31 | + print("door open pressed", flush=True) |
| 32 | + elif d[3] == 0x31: # KEY_BACKSLASH |
| 33 | + # toggle keep open (i.e. unlock) |
| 34 | + keepopen = not keepopen |
| 35 | + if keepopen: |
| 36 | + print( |
| 37 | + "unlock pressed, will open for %d seconds" % unlock_max_time, |
| 38 | + flush=True, |
| 39 | + ) |
| 40 | + r.publish("door_action", "OPEN") |
| 41 | + else: |
| 42 | + print("unlock pressed while unlocked, locking", flush=True) |
| 43 | + r.publish("door_action", "CLOSE") |
| 44 | + elif d[3] == 0x36: # KEY_COMMA |
| 45 | + # bell, TODO |
| 46 | + print("bell", flush=True) |
37 | 47 | else:
|
38 |
| - print("unlock pressed while unlocked, locking", flush=True) |
39 |
| - r.publish('door_action', 'CLOSE') |
40 |
| - elif d[3] == 0x36: # KEY_COMMA |
41 |
| - # bell, TODO |
42 |
| - print("bell", flush=True) |
43 |
| - else: |
44 |
| - if keepopen: |
45 |
| - sincelastopen = (time.time()-lastopen) |
46 |
| - if sincelastopen > unlock_max_time: |
47 |
| - print("unlock exceed max time, locking", flush=True) |
48 |
| - keepopen = False |
49 |
| - r.publish('door_action', 'CLOSE') |
50 |
| - elif sincelastopen > 2.0: |
51 |
| - r.publish('door_action', 'OPEN') |
| 48 | + if keepopen: |
| 49 | + sincelastopen = time.time() - lastopen |
| 50 | + if sincelastopen > unlock_max_time: |
| 51 | + print("unlock exceed max time, locking", flush=True) |
| 52 | + keepopen = False |
| 53 | + r.publish("door_action", "CLOSE") |
| 54 | + elif sincelastopen > 2.0: |
| 55 | + r.publish("door_action", "OPEN") |
| 56 | + |
52 | 57 |
|
53 | 58 | async def doorlight():
|
54 |
| - lighton = (p.value == 1) |
55 |
| - try: |
56 |
| - while True: |
57 |
| - if p.value == 0 and lighton == True: |
58 |
| - s.write(b'X') # turn off all lights |
59 |
| - lighton = False |
60 |
| - print("door closed", flush=True) |
61 |
| - if p.value == 1 and lighton == False: |
62 |
| - s.write(b'G') # set lights to green |
63 |
| - lighton = True |
64 |
| - await asyncio.sleep(0.3) |
65 |
| - except asyncio.CancelledError: |
66 |
| - print('doorlight done', flush=True) |
| 59 | + lighton = p.value == 1 |
| 60 | + try: |
| 61 | + while True: |
| 62 | + if p.value == 0 and lighton == True: |
| 63 | + s.write(b"X") # turn off all lights |
| 64 | + lighton = False |
| 65 | + print("door closed", flush=True) |
| 66 | + if p.value == 1 and lighton == False: |
| 67 | + s.write(b"G") # set lights to green |
| 68 | + lighton = True |
| 69 | + await asyncio.sleep(0.3) |
| 70 | + except asyncio.CancelledError: |
| 71 | + print("doorlight done", flush=True) |
| 72 | + |
67 | 73 |
|
68 | 74 | async def runall():
|
69 |
| - t_button = asyncio.create_task(asyncio.to_thread(button)) |
70 |
| - t_doorlight = asyncio.create_task(doorlight()) |
71 |
| - await t_button |
72 |
| - await t_doorlight |
| 75 | + t_button = asyncio.create_task(asyncio.to_thread(button)) |
| 76 | + t_doorlight = asyncio.create_task(doorlight()) |
| 77 | + await t_button |
| 78 | + await t_doorlight |
| 79 | + |
73 | 80 |
|
74 | 81 | def main():
|
75 |
| - print('dsl_buttons running', flush=True) |
76 |
| - asyncio.run(runall()) |
77 |
| - |
78 |
| -if __name__ == '__main__': |
79 |
| - sys.exit(main()) |
| 82 | + print("dsl_buttons running", flush=True) |
| 83 | + asyncio.run(runall()) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + sys.exit(main()) |
0 commit comments