forked from dhague/vpower
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpower.py
68 lines (58 loc) · 1.97 KB
/
vpower.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
#!/usr/bin/env python
import time
from ant.core import driver
from ant.core import node
from PowerMeterTx import PowerMeterTx
from SpeedCadenceSensorRx import SpeedCadenceSensorRx
from config import DEBUG, LOG, NETKEY, POWER_CALCULATOR, POWER_SENSOR_ID, SENSOR_TYPE, SPEED_SENSOR_ID
antnode = None
speed_sensor = None
power_meter = None
try:
print "Using " + POWER_CALCULATOR.__class__.__name__
stick = driver.USB2Driver(None, log=LOG, debug=DEBUG)
antnode = node.Node(stick)
print "Starting ANT node"
antnode.start()
key = node.NetworkKey('N:ANT+', NETKEY)
antnode.setNetworkKey(0, key)
print "Starting speed sensor"
try:
# Create the speed sensor object and open it
speed_sensor = SpeedCadenceSensorRx(antnode, SENSOR_TYPE, SPEED_SENSOR_ID)
speed_sensor.open()
# Notify the power calculator every time we get a speed event
speed_sensor.notify_change(POWER_CALCULATOR)
except Exception as e:
print"speed_sensor error: " + e.message
speed_sensor = None
print "Starting power meter with ANT+ ID " + repr(POWER_SENSOR_ID)
try:
# Create the power meter object and open it
power_meter = PowerMeterTx(antnode, POWER_SENSOR_ID)
power_meter.open()
except Exception as e:
print "power_meter error: " + e.message
power_meter = None
# Notify the power meter every time we get a calculated power value
POWER_CALCULATOR.notify_change(power_meter)
print "Main wait loop"
while True:
try:
time.sleep(1)
except (KeyboardInterrupt, SystemExit):
break
except Exception as e:
print "Exception: "+repr(e)
finally:
if speed_sensor:
print "Closing speed sensor"
speed_sensor.close()
speed_sensor.unassign()
if power_meter:
print "Closing power meter"
power_meter.close()
power_meter.unassign()
if antnode:
print "Stopping ANT node"
antnode.stop()