/
csc.py
167 lines (143 loc) · 6.17 KB
/
csc.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import struct
import time
from micropython import const
from machine import Pin, Timer
import bluetooth
from ble_advertising import advertising_payload
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_INDICATE_DONE = const(20)
# CSC service
_CSC_SERVICE_UUID=bluetooth.UUID(0x1816)
# CSC Feature Characteristic
_CSC_FEATURE_CHAR = (
bluetooth.UUID(0x2A5C),
bluetooth.FLAG_READ,
)
# CSC Measurement Characteristic
_CSC_MEASUREMENT_CHAR = (
bluetooth.UUID(0x2A5B),
bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
# We only plan to have these two features
_CSC_FEATURE_WHEEL_REV_DATA=0x01
_CSC_FEATURE_CRANK_REV_DATA=0x02
_CSC_SERVICE = (
_CSC_SERVICE_UUID,
(_CSC_FEATURE_CHAR,_CSC_MEASUREMENT_CHAR),
)
# org.bluetooth.characteristic.gap.appearance.xml for cadence sensor
_ADV_APPEARANCE_CYCLING_SPEED_CADENCE_SENSOR = const(1157)
class BLECycling:
def __init__(self, ble, speed_sensor_pin, cadence_sensor_pin=None, name="cheapensor", debug=False):
self._ble = ble
self._ble.active(True)
self._ble.irq(handler=self._irq)
self._CSC_FEATURES = _CSC_FEATURE_WHEEL_REV_DATA
if cadence_sensor_pin:
self._CSC_FEATURES = _CSC_FEATURE_WHEEL_REV_DATA | _CSC_FEATURE_CRANK_REV_DATA
((self._handle_feature,self._handle_measurement,),) = self._ble.gatts_register_services((_CSC_SERVICE,))
self._connections = set()
self._payload = advertising_payload(
name=name, services=[_CSC_SERVICE_UUID], appearance=_ADV_APPEARANCE_CYCLING_SPEED_CADENCE_SENSOR
)
# Write the feature
self._ble.gatts_write(self._handle_feature, struct.pack("<h", self._CSC_FEATURES))
self._advertise()
self._cumulative_wheel_revolutions=0
self._last_wheel_event_time=0
self._cumulative_crank_revolutions=0
self._last_crank_event_time=0
self._debug=debug
# IR Speed Sensor
self.speed_sensor = Pin(speed_sensor_pin, Pin.IN)
self.speed_sensor.irq(trigger=Pin.IRQ_RISING, handler=self.speed_sensor_irq)
if cadence_sensor_pin:
self.cadence_sensor = Pin(cadence_sensor_pin, Pin.IN)
self.cadence_sensor.irq(trigger=Pin.IRQ_RISING, handler=self.cadence_sensor_irq)
# Use a timer to periodically send out data.
self.enable_transmit=False
timer3 = Timer(3)
timer3.init(period=1000, mode=Timer.PERIODIC, callback=self.arm_measurement)
if debug:
print("Initialised CSC sensor")
def arm_measurement(self, timer):
self.enable_transmit=True
def send_measurement(self):
# If measurement is not enabled - Do nothing
if self.enable_transmit == False:
return
# Measurement
# Byte 0 is CSC Feature
# Byte 1 to 4 are cumulative_wheel_revolutions
# Byte 5,6 are last_wheel_event_time
# Byte 7,8 are cumulative_crank_revolutions
# Bytw 9,10 are last_crank_event_time
_measurement=bytearray(7)
_measurement[0]=self._CSC_FEATURES
# Pack into unsigned int - little-endian
_measurement[1:5]=struct.pack("<I",self._cumulative_wheel_revolutions)
# 1 second = 1024 for the event time as per CSC spec
_measurement[5:7]=struct.pack("<H",int(self._last_wheel_event_time*1.024))
if self._CSC_FEATURES==(_CSC_FEATURE_WHEEL_REV_DATA | _CSC_FEATURE_CRANK_REV_DATA):
_measurement.extend(struct.pack("<H",self._cumulative_crank_revolutions))
# 1 second = 1024 for the event time as per CSC spec
_measurement.extend(struct.pack("<H",int(self._last_crank_event_time*1.024)))
for conn_handle in self._connections:
# Notify connected centrals.
self._ble.gatts_notify(conn_handle, self._handle_measurement, _measurement)
# After transmitting disable measurement so that the timer can enable it again in a second.
# This prevents transmitting at the same rate as wheel / cadence events
self.enable_transmit=False
if self._debug:
print(self._cumulative_wheel_revolutions)
print(self._last_wheel_event_time)
print(self._cumulative_crank_revolutions)
print(self._last_crank_event_time)
print(_measurement)
def wheel_event(self):
ticks_ms=time.ticks_ms()
# Limiting to 60KMPH on a 700x35c ( 2.17m circumference cycle)
if time.ticks_diff(ticks_ms,self._last_wheel_event_time) > 130:
self._last_wheel_event_time=ticks_ms
self._cumulative_wheel_revolutions+=1
# Send the measurement out
self.send_measurement()
def crank_event(self):
ticks_ms=time.ticks_ms()
# Limiting to ~420 RPM
if time.ticks_diff(ticks_ms,self._last_crank_event_time) > 130:
self._last_crank_event_time=ticks_ms
self._cumulative_crank_revolutions+=1
# Send the measurement out
self.send_measurement()
def speed_sensor_irq(self, pin):
self.wheel_event()
if self._debug:
print("Speed sensor")
def cadence_sensor_irq(self, pin):
self.crank_event()
if self._debug:
print("Candence sensor")
def _irq(self, event, data):
# Track connections so we can send notifications.
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _, = data
self._connections.add(conn_handle)
print("Someone connected")
# Start advertising again to allow a new connection.
self._advertise()
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _, = data
self._connections.remove(conn_handle)
print("Someone disconnected")
# Start advertising again to allow a new connection.
self._advertise()
elif event == _IRQ_GATTS_INDICATE_DONE:
conn_handle, value_handle, status, = data
def _advertise(self, interval_us=500000):
self._ble.gap_advertise(interval_us, adv_data=self._payload)
def activate(debug=False):
ble = bluetooth.BLE()
csc = BLECycling(ble, speed_sensor_pin=15, debug=debug)
print("CSC Activated")