I am using a python library called Bleak. Here is my code:
import asyncio
import sys
from bleak import BleakClient
CSCMeasurementUUID = '00002a5b-0000-1000-8000-00805f9b34fb'
def CSC_Decoding(handle, data):
CumulativeWheelRevolution = int.from_bytes(data[1:5],"little")
LastWheelEventTime = int.from_bytes(data[5:7],"little")
async def main():
address = "EA:95:75:A8:80:01" #it's the address of the Tacx Bike trainer.
async with BleakClient(address) as client:
if (not client.is_connected):
raise "client not connected"
await client.start_notify(CSCMeasurementUUID,CSC_Decoding)
await asyncio.sleep(10)
await client.stop_notify(CSCMeasurementUUID)
if __name__ == "__main__":
asyncio.run(main())
I want to know, how I can use the two variable CumulativeWheelRevoluiton and LastWheelEventTime from outside the CSC_Decoding function during the loop.
The CSC_Decoding function is callback function and every time the data input of this function will be updated. I want to being able to use this data out of this CSC_Decoding function. something like below:
#somewhere return the CumulativeWheelRevolution and LastWheelEventTime.
if __name__ == "__main__":
preWheel = 0
preTime = 0
while true:
deltaWheel = CumulativeWheelRevolution - preWheel # the CumulativeWheelevolution will be updated
deltaTime = LastWheelEventTime - preTime # the LastWheelEventTime will be updated
preWheel = CumulativeWheelRevolution
preTime = LastWheelEventTime
Can you guide me a bit, I don't have experience with asyncio, await, etc. I used tried to use documentation and example to write this code.
I tried to add two extra arguments to the CSC_Decoding call back function to use them out side the function but this solution didn't work because the Bleak library says that the call back function should have two parameter (not more). Thanks.