author | Luke Hoersten <luke@hoersten.org> |
Mon, 30 Jul 2018 21:19:20 -0500 | |
changeset 21 | 6ad3cf1d57c3 |
parent 13 | 684ac329e7da |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/env python3 |
2 |
||
3 |
import time |
|
4 |
import automationhat |
|
5 |
import sys |
|
6 |
||
7 |
import queue |
|
8 |
import threading |
|
9 |
||
10 |
||
11 |
def main(): |
|
12 |
command_queue = queue.LifoQueue() |
|
13 |
read_thread = threading.Thread(target=read_loop, args=[command_queue]) |
|
14 |
read_thread.start() |
|
15 |
run_loop(command_queue) |
|
16 |
||
17 |
||
18 |
def read_loop(command_queue): |
|
19 |
while True: |
|
20 |
command_queue.put_nowait(sys.stdin.readline().rstrip('\n')) |
|
21 |
||
22 |
||
23 |
def run_loop(command_queue): |
|
24 |
thread_local = threading.local() |
|
25 |
thread_local.doorbell_on_state = False |
|
26 |
||
27 |
while True: |
|
28 |
run_command(command_queue) |
|
29 |
read_doorbell(thread_local) |
|
30 |
||
31 |
||
32 |
def run_command(command_queue): |
|
33 |
try: |
|
34 |
command = command_queue.get(timeout=0.5) |
|
35 |
except queue.Empty: |
|
36 |
pass |
|
37 |
else: |
|
38 |
automationhat.relay.on() if command == "unlock" else automationhat.relay.off() |
|
39 |
||
40 |
||
41 |
def read_doorbell(thread_local): |
|
42 |
analog_value = automationhat.analog.one.read() |
|
12
6f6c8df37897
More doorbell value tweaks.
Luke Hoersten <luke@hoersten.org>
parents:
11
diff
changeset
|
43 |
doorbell_on_state = 6.0 < analog_value and analog_value <= 6.22 |
0 | 44 |
|
13
684ac329e7da
Stable doorbell-off range fell for some reason. Cold weather?
Luke Hoersten <luke@hoersten.org>
parents:
12
diff
changeset
|
45 |
if analog_value < 7.55: |
12
6f6c8df37897
More doorbell value tweaks.
Luke Hoersten <luke@hoersten.org>
parents:
11
diff
changeset
|
46 |
print("doorbell analog value: {}; ringing: {}; ring range: (6.0, 6.22]".format(analog_value, doorbell_on_state)) |
10
38c4094d0d57
Added doorbell value debug logging.
Luke Hoersten <luke@hoersten.org>
parents:
1
diff
changeset
|
47 |
|
0 | 48 |
if doorbell_on_state != thread_local.doorbell_on_state: |
49 |
thread_local.doorbell_on_state = doorbell_on_state |
|
11
a70d948427d3
More doorbell debug range tweaks.
Luke Hoersten <luke@hoersten.org>
parents:
10
diff
changeset
|
50 |
print("doorbell on") if doorbell_on_state else print("doorbell off") |
0 | 51 |
|
52 |
||
53 |
if __name__ == "__main__": |
|
54 |
main() |