Skip to content

Commit

Permalink
Merge pull request #27 from tensorturtle/master
Browse files Browse the repository at this point in the history
Add legacy support (Python 3.7 & 3.8) in Sterzo
  • Loading branch information
zacharyedwardbull authored Nov 19, 2023
2 parents ec828e8 + 7368e7a commit cc230d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Empty file added pycycling/data/__init__.py
Empty file.
17 changes: 13 additions & 4 deletions pycycling/sterzo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
import asyncio
import struct
import importlib.resources
import pycycling.data

sterzo_measurement_id = '347b0030-7635-408b-8918-8ff3949ce592'
sterzo_control_point_id = '347b0031-7635-408b-8918-8ff3949ce592'
Expand All @@ -22,10 +24,17 @@ async def enable_steering_measurement_notifications(self):
await self._activate_steering_measurements()

async def _activate_steering_measurements(self):
with importlib.resources.files(__package__).joinpath('data/sterzo-challenge-codes.dat').open('rb') as fp:
fp.seek(self._latest_challenge * 2, 1)
code_1 = int.from_bytes(fp.read(1), 'little')
code_2 = int.from_bytes(fp.read(1), 'little')
# moved .dat file out of 'data' because accessing directories not possible for importlib.resources.path
# importlib.resources.path is deprecated since 3.11
if sys.version_info >= (3,11):
challenge_file = importlib.resources.files(pycycling.data).joinpath('sterzo-challenge-codes.dat').open('rb')
else: # legacy support < 3.9
challenge_file = importlib.resources.open_binary(pycycling.data, 'sterzo-challenge-codes.dat')

with challenge_file:
challenge_file.seek(self._latest_challenge * 2, 1)
code_1 = int.from_bytes(challenge_file.read(1), 'little')
code_2 = int.from_bytes(challenge_file.read(1), 'little')

byte_array = bytearray([0x03, 0x11, code_1, code_2])
await self._client.write_gatt_char(sterzo_control_point_id, byte_array)
Expand Down

0 comments on commit cc230d7

Please sign in to comment.