bledev.cyclingpower
Cycling Power Service (0x1818): a power meter for cycling apps.
Serve it and send the power you measure, with whichever extras your meter has:
from bledev.cyclingpower import CyclingPower
meter = CyclingPower(pedal_balance=True, crank=True, location=6)
ble.register_services(meter.service)
connection = await ble.advertise(name="Power", services=[meter.SERVICE])
meter.update(220, pedal_balance=51.5, crank_revolutions=48, crank_time_s=62.1)
update() sends only the fields your meter was created to report. The
service puts the measurement’s flags together for you: pedal power balance in
percent, accumulated torque in newton metres, wheel and crank revolutions with
their event times in seconds, and accumulated energy in kilojoules. Event times
go out in the units the specification gives, 1/2048 s for the wheel and 1/1024 s
for the crank, and wrap at 32 s and 64 s.
The Cycling Power Control Point lets a central set the cumulative wheel count,
update the sensor location and ask for the supported locations. It answers
with indications, so run asyncio.create_task(meter.run()) beside your loop
and have the central subscribe to them. Give on_set_cumulative a function
that takes the new wheel count to accept Set Cumulative Value; without one the
service answers “op code not supported”. On CircuitPython an indication goes out
but its acknowledgement isn’t reported (see bledev.cpble).
ble.register_services() replaces whatever was registered before, so pass
this service together with the others. Any peripheral backend serves it:
MicroPython (bledev.mpble), CircuitPython (bledev.cpble) and the fake one
for tests.
- bledev.cyclingpower.encode_measurement(power, pedal_balance=None, accumulated_torque=None, crank_torque=False, wheel_revolutions=None, wheel_time_s=None, crank_revolutions=None, crank_time_s=None, energy_kj=None)[source]
The Cycling Power Measurement bytes for the data you pass.
- class bledev.cyclingpower.CyclingPower(pedal_balance=False, torque=False, crank_torque=False, wheel=False, crank=False, energy=False, location=0, locations=(), on_set_cumulative=None)[source]
Bases:
objectA Cycling Power Service.
- Parameters:
pedal_balance – True if the meter reports pedal power balance.
torque – True if it reports accumulated torque.
crank_torque – True if that torque is measured at the crank, false at the wheel.
wheel – True if it counts wheel revolutions.
crank – True if it counts crank revolutions.
energy – True if it reports accumulated energy.
location – A Sensor Location number (see
bledev._sig.LOCATIONS). The characteristic is required by the profile, so the default is 0, “other”.locations – The locations the sensor can be set to, when it supports more than one.
on_set_cumulative –
f(wheel_revolutions)called when a central sets the cumulative value; return False to refuse.
- characteristic
notify only.
- Type:
The Cycling Power Measurement
bledev.Characteristic
- update(power, pedal_balance=None, accumulated_torque=None, wheel_revolutions=None, wheel_time_s=None, crank_revolutions=None, crank_time_s=None, energy_kj=None)[source]
Send a measurement to every central subscribed to it.
- Parameters:
power – Instantaneous power in watts.
pedal_balance – Percent of the power from the reference pedal.
accumulated_torque – Accumulated torque in newton metres.
wheel_revolutions – Cumulative wheel revolutions.
wheel_time_s – Time of the last wheel event, in seconds.
crank_revolutions – Cumulative crank revolutions.
crank_time_s – Time of the last crank event, in seconds.
energy_kj – Accumulated energy in kilojoules.