mirror of
https://github.com/kawasaki/pyscrlink.git
synced 2025-09-07 18:20:07 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2482bba1d8 | ||
|
06df4af4a0 | ||
|
adb746995e | ||
|
157e3458b0 | ||
|
fe3cd35ac0 | ||
|
94a9555c4c | ||
|
39b157e839 | ||
|
f79e7ac889 |
@@ -102,7 +102,7 @@ Installation
|
|||||||
|
|
||||||
5. For micro:bit, install Scratch-link hex on your device.
|
5. For micro:bit, install Scratch-link hex on your device.
|
||||||
|
|
||||||
* Download and unzip the [micro:bit Scratch Hex file](https://downloads.scratch.mit.edu/microbit/scratch-microbit.hex.zip).
|
* Download and unzip the [micro:bit Scratch Hex file](https://downloads.scratch.mit.edu/microbit/scratch-microbit-1.1.0.hex.zip).
|
||||||
* Flash the micro:bit over USB with the Scratch Hex File, you will see the
|
* Flash the micro:bit over USB with the Scratch Hex File, you will see the
|
||||||
five character name of the micro:bit scroll across the screen such as
|
five character name of the micro:bit scroll across the screen such as
|
||||||
'zo9ev'.
|
'zo9ev'.
|
||||||
|
@@ -34,8 +34,8 @@ class BLEDBusSession(pyscrlink.scratch_link.Session):
|
|||||||
connected_devices = {}
|
connected_devices = {}
|
||||||
|
|
||||||
class Device():
|
class Device():
|
||||||
def __init__(self, iface, path, node_name, name, address):
|
def __init__(self, interface, path, node_name, name, address):
|
||||||
self.iface = iface
|
self.interface = interface
|
||||||
self.path = path
|
self.path = path
|
||||||
self.node_name = node_name
|
self.node_name = node_name
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -95,7 +95,7 @@ class BLEDBusSession(pyscrlink.scratch_link.Session):
|
|||||||
logger.debug(f"service to check: {s}")
|
logger.debug(f"service to check: {s}")
|
||||||
given_uuid = BTUUID(s)
|
given_uuid = BTUUID(s)
|
||||||
logger.debug(f"given UUID: {given_uuid} hash={given_uuid.__hash__()}")
|
logger.debug(f"given UUID: {given_uuid} hash={given_uuid.__hash__()}")
|
||||||
dev_uuids = await dev.iface.uuids
|
dev_uuids = await dev.interface.uuids
|
||||||
if not dev_uuids:
|
if not dev_uuids:
|
||||||
logger.debug(f"dev UUID not available")
|
logger.debug(f"dev UUID not available")
|
||||||
continue
|
continue
|
||||||
@@ -122,7 +122,7 @@ class BLEDBusSession(pyscrlink.scratch_link.Session):
|
|||||||
async def _notify_device(self, device) -> None:
|
async def _notify_device(self, device) -> None:
|
||||||
params = { 'rssi': -80, 'name': 'Unknown' }
|
params = { 'rssi': -80, 'name': 'Unknown' }
|
||||||
try:
|
try:
|
||||||
params['rssi'] = await device.iface.rssi
|
params['rssi'] = await device.interface.rssi
|
||||||
except Exception:
|
except Exception:
|
||||||
None
|
None
|
||||||
if device.name:
|
if device.name:
|
||||||
@@ -149,17 +149,17 @@ class BLEDBusSession(pyscrlink.scratch_link.Session):
|
|||||||
devpath = self.iface + "/" + node_name
|
devpath = self.iface + "/" + node_name
|
||||||
if BLEDBusSession.connected_devices.get(devpath):
|
if BLEDBusSession.connected_devices.get(devpath):
|
||||||
continue
|
continue
|
||||||
iface = DeviceInterfaceAsync()
|
interface = DeviceInterfaceAsync()
|
||||||
iface._connect('org.bluez', devpath, bus=self.dbus)
|
interface._connect('org.bluez', devpath, bus=self.dbus)
|
||||||
try:
|
try:
|
||||||
devname = await iface.name
|
devname = await interface.name
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(f"device {node_name} does not have name: {e}")
|
logger.debug(f"device {node_name} does not have name: {e}")
|
||||||
devaddr = await iface.address
|
devaddr = await interface.address
|
||||||
device = self.Device(iface, devpath, node_name, devname,
|
device = self.Device(interface, devpath, node_name, devname,
|
||||||
devaddr)
|
devaddr)
|
||||||
if not await self._matches(device, self.discover_filters):
|
if not await self._matches(device, self.discover_filters):
|
||||||
await iface.disconnect()
|
await interface.disconnect()
|
||||||
continue
|
continue
|
||||||
self.found_devices[node_name] = device
|
self.found_devices[node_name] = device
|
||||||
await self._notify_device(device)
|
await self._notify_device(device)
|
||||||
@@ -309,7 +309,7 @@ class BLEDBusSession(pyscrlink.scratch_link.Session):
|
|||||||
dev = self.found_devices[params['peripheralId']]
|
dev = self.found_devices[params['peripheralId']]
|
||||||
try:
|
try:
|
||||||
logger.debug(f" {dev}")
|
logger.debug(f" {dev}")
|
||||||
await dev.iface.connect()
|
await dev.interface.connect()
|
||||||
res["result"] = None
|
res["result"] = None
|
||||||
self.device = dev
|
self.device = dev
|
||||||
self.status = self.CONNECTED
|
self.status = self.CONNECTED
|
||||||
@@ -362,7 +362,7 @@ class BLEDBusSession(pyscrlink.scratch_link.Session):
|
|||||||
dev = self.device
|
dev = self.device
|
||||||
logger.info(f"Disconnecting from '{dev.name}'@{dev.address}")
|
logger.info(f"Disconnecting from '{dev.name}'@{dev.address}")
|
||||||
self._stop_notifications()
|
self._stop_notifications()
|
||||||
await dev.iface.disconnect()
|
await dev.interface.disconnect()
|
||||||
BLEDBusSession.connected_devices.pop(dev.path)
|
BLEDBusSession.connected_devices.pop(dev.path)
|
||||||
logger.info(f"Disconnected from '{dev.name}'@{dev.address}")
|
logger.info(f"Disconnected from '{dev.name}'@{dev.address}")
|
||||||
self.device = None
|
self.device = None
|
||||||
|
@@ -33,8 +33,17 @@ from pyscrlink import gencert
|
|||||||
|
|
||||||
from pyscrlink import ble
|
from pyscrlink import ble
|
||||||
|
|
||||||
|
logLevel = logging.INFO
|
||||||
|
|
||||||
# for logging
|
# for logging
|
||||||
logger = logging.getLogger('pyscrlink.scratch_link')
|
logger = logging.getLogger('pyscrlink.scratch_link')
|
||||||
|
formatter = logging.Formatter(fmt='%(asctime)s %(message)s')
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
handler.setLevel(logLevel)
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
logger.setLevel(logLevel)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
logger.propagate = False
|
||||||
|
|
||||||
HOSTNAME="device-manager.scratch.mit.edu"
|
HOSTNAME="device-manager.scratch.mit.edu"
|
||||||
scan_seconds=10.0
|
scan_seconds=10.0
|
||||||
@@ -588,24 +597,14 @@ def main():
|
|||||||
parser.add_argument('-b', '--dbus', action='store_true',
|
parser.add_argument('-b', '--dbus', action='store_true',
|
||||||
help='use DBus backend for BLE devices')
|
help='use DBus backend for BLE devices')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
logLevel = logging.INFO
|
|
||||||
if args.debug:
|
if args.debug:
|
||||||
|
print("Print debug messages")
|
||||||
logLevel = logging.DEBUG
|
logLevel = logging.DEBUG
|
||||||
|
handler.setLevel(logLevel)
|
||||||
formatter = logging.Formatter(fmt='%(asctime)s %(message)s')
|
logger.setLevel(logLevel)
|
||||||
handler = logging.StreamHandler()
|
|
||||||
handler.setLevel(logLevel)
|
|
||||||
handler.setFormatter(formatter)
|
|
||||||
logger.setLevel(logLevel)
|
|
||||||
logger.addHandler(handler)
|
|
||||||
logger.propagate = False
|
|
||||||
|
|
||||||
scan_seconds = args.scan_seconds
|
scan_seconds = args.scan_seconds
|
||||||
scan_retry = args.scan_retry
|
scan_retry = args.scan_retry
|
||||||
dbus = args.dbus
|
dbus = args.dbus
|
||||||
if args.debug:
|
|
||||||
logger.debug("Print debug messages")
|
|
||||||
logger.debug(f"set scan_seconds: {scan_seconds}")
|
logger.debug(f"set scan_seconds: {scan_seconds}")
|
||||||
logger.debug(f"set scan_retry: {scan_retry}")
|
logger.debug(f"set scan_retry: {scan_retry}")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user