bledev.cyclingspeed

Cycling Speed and Cadence Service (0x1816): a wheel and crank sensor for cycling apps.

Serve it and report the cumulative counts as the sensor sees them:

from bledev.cyclingspeed import CyclingSpeedCadence

sensor = CyclingSpeedCadence(wheel=True, crank=True)
ble.register_services(sensor.service)
connection = await ble.advertise(name="CSC", services=[sensor.SERVICE])
sensor.update(wheel_revolutions=120, wheel_time_s=62.3,
              crank_revolutions=48, crank_time_s=62.1)

An app works out speed and cadence from two measurements: the revolutions between them divided by the event time between them. So send each count with the time of its last event, in seconds on any clock that keeps running. The service sends the time in 1/1024 s and lets it wrap at 64 s and the wheel count at 2**32, as the specification says; the crank count wraps at 2**16.

The SC Control Point lets a central set the cumulative wheel count and ask for the supported sensor locations. It answers with indications, so run asyncio.create_task(sensor.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 a 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.cyclingspeed.encode_measurement(wheel_revolutions=None, wheel_time_s=None, crank_revolutions=None, crank_time_s=None)[source]

The CSC Measurement bytes for the data you pass (each count with its time).

class bledev.cyclingspeed.CyclingSpeedCadence(wheel=True, crank=True, location=None, locations=(), on_set_cumulative=None)[source]

Bases: object

A Cycling Speed and Cadence Service.

Parameters:
  • wheel – True if the sensor counts wheel revolutions.

  • crank – True if the sensor counts crank revolutions.

  • location – A Sensor Location number (see bledev._sig.LOCATIONS), or None to leave out the Sensor Location characteristic.

  • 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 CSC Measurement bledev.Characteristic

update(wheel_revolutions=None, wheel_time_s=None, crank_revolutions=None, crank_time_s=None)[source]

Send a measurement to every central subscribed to it.

Parameters:
  • 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.

async run()[source]

Answer the SC Control Point, forever. Run it as a task.