Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: introduce asyncio.Lock #24

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions myo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,7 @@ async def command(self, client: BleakClient, cmd: Command):
"""
Command Characteristic
"""
try:
await client.write_gatt_char(Handle.COMMAND.value, cmd.data, True)
except AttributeError:
# wait a bit and try once again
await asyncio.sleep(0.1)
await client.write_gatt_char(Handle.COMMAND.value, cmd.data, True)
await client.write_gatt_char(Handle.COMMAND.value, cmd.data, True)

async def deep_sleep(self, client: BleakClient):
"""
Expand Down Expand Up @@ -241,6 +236,7 @@ def __init__(self, aggregate_all=False, aggregate_emg=False):
self._client = None
self.fv_aggregated = None # for aggregate_all
self.imu_aggregated = None # for aggregate_all
self._lock = asyncio.Lock() # for aggregate_all

@classmethod
async def with_device(cls, mac=None, aggregate_all=False, aggregate_emg=False):
Expand Down Expand Up @@ -337,15 +333,16 @@ async def on_data(self, data):
"""
<> for on_aggregated_data: data is either FVData or IMUData
"""
if isinstance(data, FVData):
self.fv_aggregated = data
elif isinstance(data, IMUData):
self.imu_aggregated = data
# trigger on_aggregated_data when both FVData and IMUData are ready
if all((self.fv_aggregated, self.imu_aggregated)):
await self.on_aggregated_data(AggregatedData(self.fv_aggregated, self.imu_aggregated))
self.fv_aggregated = None
self.imu_aggregated = None
async with self._lock:
if isinstance(data, FVData):
self.fv_aggregated = data
elif isinstance(data, IMUData):
self.imu_aggregated = data
# trigger on_aggregated_data when both FVData and IMUData are ready
if all(d is not None for d in (self.fv_aggregated, self.imu_aggregated)):
await self.on_aggregated_data(AggregatedData(self.fv_aggregated, self.imu_aggregated))
self.fv_aggregated = None
self.imu_aggregated = None

async def on_aggregated_data(self, ad: AggregatedData):
"""
Expand Down