-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poller.py
executable file
·70 lines (52 loc) · 1.65 KB
/
Poller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ubw
import logging
import time
import Detector
import ConfigParser
import pprint
class Poller():
def __init__(self, configfile):
config = ConfigParser.ConfigParser()
config.read(configfile)
self.port = config.get('Poller', 'port')
self.u = ubw.Ubw(self.port)
self.detector = Detector.Detector(configfile)
def start(self):
self.u.Reset()
self.detector.start()
# Add additional handler
self.u.responses.append((ubw.INPUT_STATE, self.HandleInputState))
self.u.Configure(255,255,255,0)
#Should for input thread to identify INPUT_STATE and call our handler
self.u.Timer(50,0)
#for i in range(10*1):
# self.u.InputState()
# time.sleep(1)
def stop(self):
self.u.Timer(0,0)
self.u.Stop()
self.detector.stop()
def HandleInputState(self,input_state):
"""Handle the current pin input state response from the UBW.
Print out our own response
Args:
input_state: dictionary of response values
"""
portc = int(input_state['portc'])
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(input_state)
open = ubw.ValueToMap(portc)[2]
buttonpressed = not open
self.detector.poll(buttonpressed, False)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(levelname)-8s %(message)s')
ump = Poller('pingpong.ini')
ump.start()
try:
#Timer is running, just bide our time
while True:
time.sleep(2)
finally:
ump.stop()