forked from jorticus/pymate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
79 lines (64 loc) · 1.7 KB
/
scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
#
# Scans the Mate bus for any attached devices,
# and displays the result.
#
from pymate.matenet import MateNET, MateNETPJON, MateDevice
import settings
import serial
import logging
import time
#log = logging.getLogger('mate')
#log.setLevel(logging.DEBUG)
#log.addHandler(logging.StreamHandler())
print("MATE Bus Scan")
# Create a MateNET bus connection
if settings.SERIAL_PROTO == 'PJON':
port = MateNETPJON(settings.SERIAL_PORT)
bus = MateNET(port)
# PJON is more reliable, so we don't need to retry packets
bus.RETRY_PACKET = 0
# Time for the Arduino to boot (connecting serial may reset it)
time.sleep(1.0)
else:
bus = MateNET(settings.SERIAL_PORT)
def print_device(d):
dtype = d.scan()
# No response from the scan command.
# there is nothing at this port.
if dtype is None:
print('Port%d: -' % (
d.port
))
else:
try:
rev = d.revision
except Exception as e:
rev = str(e)
if dtype not in MateNET.DEVICE_TYPES:
print("Port%d: Unknown device type: %d" % (
d.port,
dtype
))
else:
print("Port%d: %s (Rev: %s)" % (
d.port,
MateNET.DEVICE_TYPES[dtype],
rev
))
return dtype
# The root device
d0 = MateDevice(bus, port=0)
dtype = d0.scan()
if not dtype:
print('No device connected!')
exit()
print_device(d0)
# Child devices attached to a hub
# (Only valid if the root device is a hub)
if dtype == MateNET.DEVICE_HUB:
for i in range(1,10):
subdev = MateDevice(bus, port=i)
print_device(subdev)
print
print('Finished!')