Mercurial > code > home > repos > homeauto
annotate service/thermostat/rpi_buttons.py @ 870:e0f60d0e6e07
faster thermostat button polling. send updates to wallscreen (directly)
Ignore-this: 6259a882bbee19ef2dddd7f5b931890
darcs-hash:20130411035144-312f9-2d824c6addc75c06cfc3def8e2d9c9cac95ae615
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Wed, 10 Apr 2013 20:51:44 -0700 |
parents | 10b1e6a1b408 |
children |
rev | line source |
---|---|
861
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 #!/usr/bin/python3 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
2 """ |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
3 read button and knob on rpi, send back to thermostat program |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
4 """ |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
5 from RPi.GPIO import setmode, PUD_UP, setup, BCM, IN, input as pinInput |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
6 import time, json |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
7 # using a copy of urllib/request.py from py3.3 which supports 'method' |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 from request import urlopen, Request |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
10 requestedTemperature = 'http://bang.bigasterisk.com:10001/requestedTemperature' |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
11 requestOpts = dict(headers={'user-agent': 'rpi buttons'}) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 def getRequestedTemp(): |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 return (json.loads(urlopen( |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 Request(requestedTemperature, **requestOpts)).read().decode('utf-8')) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 )['tempF'] |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
18 def setRequestedTemp(f): |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 urlopen(Request(url=requestedTemperature, |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 method='PUT', |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 data=json.dumps({'tempF' : f}).encode('utf-8'), |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 **requestOpts)) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 PIN_KNOB_A = 1 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 PIN_KNOB_B = 4 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 PIN_BUTTON = 0 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 setmode(BCM) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 setup(PIN_KNOB_A, IN, PUD_UP) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 setup(PIN_KNOB_B, IN, PUD_UP) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 setup(PIN_BUTTON, IN, PUD_UP) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 print("reading knob and button, writing to %s" % requestedTemperature) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 prev = None |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 prevButton = 0 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 buttonHold = 0 |
870
e0f60d0e6e07
faster thermostat button polling. send updates to wallscreen (directly)
drewp <drewp@bigasterisk.com>
parents:
861
diff
changeset
|
37 step = .02 |
861
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 while True: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 a, b = pinInput(PIN_KNOB_A), pinInput(PIN_KNOB_B) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 button = not pinInput(PIN_BUTTON) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 pos = (b * 2) + (a ^ b) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
43 if prev is None: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 prev = pos |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 dpos = (pos - prev) % 4 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 if dpos == 1: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 print ("up") |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 setRequestedTemp(getRequestedTemp() + 1) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 elif dpos == -1 % 4: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 print ("down") |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 setRequestedTemp(getRequestedTemp() - 1) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 else: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 pass # 0 or unknown |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 prev = pos |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
56 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
57 if button: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
58 buttonHold += 1 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
59 if buttonHold == int(.1 / step): |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
60 print ("button to", button) |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
61 else: |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
62 buttonHold = 0 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
63 |
10b1e6a1b408
thermostat program and raspberry pi buttons reader
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
64 time.sleep(step) |