annotate service/thermostat/rpi_buttons.py @ 65:cbc557c35121

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