-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the possibility of building and running the project on Mac (wit…
…hout bluetooth) (#32)
- Loading branch information
1 parent
b7c35d3
commit a540a28
Showing
7 changed files
with
566 additions
and
534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,59 @@ | ||
import struct | ||
|
||
from bluepy.btle import BTLEDisconnectError, DefaultDelegate, Peripheral | ||
|
||
from radiacode.bytes_buffer import BytesBuffer | ||
import platform | ||
|
||
|
||
class DeviceNotFound(Exception): | ||
pass | ||
|
||
|
||
class Bluetooth(DefaultDelegate): | ||
def __init__(self, mac): | ||
self._resp_buffer = b'' | ||
self._resp_size = 0 | ||
self._response = None | ||
|
||
try: | ||
self.p = Peripheral(mac) | ||
except BTLEDisconnectError as ex: | ||
raise DeviceNotFound('Device not found or bluetooth adapter is not powered on') from ex | ||
|
||
self.p.withDelegate(self) | ||
|
||
service = self.p.getServiceByUUID('e63215e5-7003-49d8-96b0-b024798fb901') | ||
self.write_fd = service.getCharacteristics('e63215e6-7003-49d8-96b0-b024798fb901')[0].getHandle() | ||
notify_fd = service.getCharacteristics('e63215e7-7003-49d8-96b0-b024798fb901')[0].getHandle() | ||
self.p.writeCharacteristic(notify_fd + 1, b'\x01\x00') | ||
|
||
def handleNotification(self, chandle, data): | ||
if self._resp_size == 0: | ||
self._resp_size = 4 + struct.unpack('<i', data[:4])[0] | ||
self._resp_buffer = data[4:] | ||
else: | ||
self._resp_buffer += data | ||
self._resp_size -= len(data) | ||
assert self._resp_size >= 0 | ||
if self._resp_size == 0: | ||
self._response = self._resp_buffer | ||
self._resp_buffer = b'' | ||
if platform.system() == 'Darwin': | ||
|
||
def execute(self, req) -> BytesBuffer: | ||
for pos in range(0, len(req), 18): | ||
rp = req[pos : min(pos + 18, len(req))] | ||
self.p.writeCharacteristic(self.write_fd, rp) | ||
class Bluetooth: | ||
def __init__(self): | ||
# Create an empty class if we are on MacOS | ||
pass | ||
else: | ||
from bluepy.btle import BTLEDisconnectError, DefaultDelegate, Peripheral | ||
from radiacode.bytes_buffer import BytesBuffer | ||
|
||
while self._response is None: | ||
self.p.waitForNotifications(2.0) | ||
|
||
br = BytesBuffer(self._response) | ||
self._response = None | ||
return br | ||
class Bluetooth(DefaultDelegate): | ||
def __init__(self, mac): | ||
self._resp_buffer = b'' | ||
self._resp_size = 0 | ||
self._response = None | ||
|
||
try: | ||
self.p = Peripheral(mac) | ||
except BTLEDisconnectError as ex: | ||
raise DeviceNotFound('Device not found or bluetooth adapter is not powered on') from ex | ||
|
||
self.p.withDelegate(self) | ||
|
||
service = self.p.getServiceByUUID('e63215e5-7003-49d8-96b0-b024798fb901') | ||
self.write_fd = service.getCharacteristics('e63215e6-7003-49d8-96b0-b024798fb901')[0].getHandle() | ||
notify_fd = service.getCharacteristics('e63215e7-7003-49d8-96b0-b024798fb901')[0].getHandle() | ||
self.p.writeCharacteristic(notify_fd + 1, b'\x01\x00') | ||
|
||
def handleNotification(self, chandle, data): | ||
if self._resp_size == 0: | ||
self._resp_size = 4 + struct.unpack('<i', data[:4])[0] | ||
self._resp_buffer = data[4:] | ||
else: | ||
self._resp_buffer += data | ||
self._resp_size -= len(data) | ||
assert self._resp_size >= 0 | ||
if self._resp_size == 0: | ||
self._response = self._resp_buffer | ||
self._resp_buffer = b'' | ||
|
||
def execute(self, req) -> BytesBuffer: | ||
for pos in range(0, len(req), 18): | ||
rp = req[pos : min(pos + 18, len(req))] | ||
self.p.writeCharacteristic(self.write_fd, rp) | ||
|
||
while self._response is None: | ||
self.p.waitForNotifications(2.0) | ||
|
||
br = BytesBuffer(self._response) | ||
self._response = None | ||
return br |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import struct | ||
|
||
import usb.core | ||
|
||
from radiacode.bytes_buffer import BytesBuffer | ||
|