0

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.

Meysam
  • 11
  • 1
  • Basically I just want to have access to the data argument, from out side the CSC_Deconding callback function. – Meysam Mar 29 '23 at 09:24
  • So you all you need are global variables? https://stackoverflow.com/questions/423379/using-global-variables-in-a-function/423596#423596 – Ovski Mar 29 '23 at 11:06

0 Answers0