Skip to content

Commit

Permalink
Merge pull request #4 from Eelviny/master
Browse files Browse the repository at this point in the history
Merge from master nxt-python repository
  • Loading branch information
tcwan authored Apr 23, 2018
2 parents df85ea3 + 86ebdf2 commit 20c396e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 196 deletions.
118 changes: 0 additions & 118 deletions nxt/fantomsock.py

This file was deleted.

84 changes: 32 additions & 52 deletions nxt/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ class NoBackendError(Exception):
class Method():
"""Used to indicate which comm backends should be tried by find_bricks/
find_one_brick. Any or all can be selected."""
def __init__(self, usb=True, bluetooth=True, fantomusb=False, fantombt=False, device=False):
def __init__(self, usb=True, bluetooth=True, device=False):
#new method options MUST default to False!
self.usb = usb
self.bluetooth = bluetooth
self.fantom = fantomusb or fantombt
self.fantomusb = fantomusb
self.fantombt = fantombt
self.device = device

def find_bricks(host=None, name=None, silent=False, method=Method()):
Expand Down Expand Up @@ -72,22 +69,6 @@ def find_bricks(host=None, name=None, silent=False, method=Method()):
except IOError:
pass

if method.fantom:
try:
from . import fantomsock
methods_available += 1
if method.fantomusb:
usbsocks = fantomsock.find_bricks(host, name, False)
for s in usbsocks:
yield s
if method.fantombt:
btsocks = fantomsock.find_bricks(host, name, True)
for s in btsocks:
yield s
except ImportError:
import sys
if not silent: print("Fantom module unavailable, not searching there", file=sys.stderr)

if methods_available == 0:
raise NoBackendError("No selected backends are available! Did you install the comm modules?")

Expand Down Expand Up @@ -119,40 +100,39 @@ def find_one_brick(host=None, name=None, silent=False, strict=None, debug=False,
if not method: method = Method()
if debug:
print("Host: %s Name: %s Strict: %s" % (host, name, str(strict)))
print("USB: %s BT: %s Fantom: %s FUSB: %s FBT: %s" % (method.usb, method.bluetooth, method.fantom, method.fantomusb, method.fantombt))
print("USB: {} BT: {}".format(method.usb, method.bluetooth))

for s in find_bricks(host, name, silent, method):
print('Point Reached')
try:
if host and 'host' in dir(s) and s.host != host:
if debug:
print("Warning: the brick found does not match the host provided (s.host).")
if strict: continue
b = s.connect()
info = b.get_device_info()
print(info)
strict = False
if host and info[1] != host:
if debug:
print("Warning: the brick found does not match the host provided (get_device_info).")
if strict:
s.close()
continue
info = list(info)
info[0] = str(info[0])
info[0] = info[0][2:(len(info[0])-1)]
info[0] = info[0].strip('\\x00')
if info[0] != name:
try:
if host and 'host' in dir(s) and s.host != host:
if debug:
print("Warning: the brick found does not match the host provided (s.host).")
if strict: continue
b = s.connect()
info = b.get_device_info()
print(info)
strict = False
if host and info[1] != host:
if debug:
print("Warning: the brick found does not match the host provided (get_device_info).")
if strict:
s.close()
continue
info = list(info)
info[0] = str(info[0])
info[0] = info[0][2:(len(info[0])-1)]
info[0] = info[0].strip('\\x00')
if info[0] != name:
if debug:
print("Warning; the brick found does not match the name provided.")
if strict:
s.close()
continue
return b
except:
if debug:
print("Warning; the brick found does not match the name provided.")
if strict:
s.close()
continue
return b
except:
if debug:
traceback.print_exc()
print("Failed to connect to possible brick")
traceback.print_exc()
print("Failed to connect to possible brick")

print("""No brick was found.
Is the brick turned on?
Expand Down Expand Up @@ -195,7 +175,7 @@ def make_config(confpath=None):
conf.set('Brick', 'name', 'MyNXT')
conf.set('Brick', 'host', '54:32:59:92:F9:39')
conf.set('Brick', 'strict', 0)
conf.set('Brick', 'method', 'usb=True, bluetooth=False, fantomusb=True')
conf.set('Brick', 'method', 'usb=True, bluetooth=False')
conf.write(open(confpath, 'w'))
print("The file has been written at %s" % confpath)
print("The file contains less-than-sane default values to get you started.")
Expand Down
22 changes: 11 additions & 11 deletions nxt/sensor/digital.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def __init__(self, version, product_id, sensor_type):
self.version = version
self.product_id = product_id
self.sensor_type = sensor_type

def clarifybinary(self, instr, label):
outstr = ''
outstr += (label + ': `' + instr + '`\n')
for char in instr:
outstr += (hex(ord(char))+', ')
outstr += ('\n')
return outstr

def __str__(self):
outstr = ''
outstr += (self.clarifybinary(str(self.version), 'Version'))
Expand All @@ -55,7 +55,7 @@ class BaseDigitalSensor(Sensor):
'factory_scale_factor': (0x12, 'B'),
'factory_scale_divisor': (0x13, 'B'),
}

def __init__(self, brick, port, check_compatible=True):
"""Creates a BaseDigitalSensor. If check_compatible is True, queries
the sensor for its name, and if a wrong sensor class was used, prints
Expand All @@ -72,7 +72,7 @@ def __init__(self, brick, port, check_compatible=True):
if check_compatible:
sensor = self.get_sensor_info()
if not sensor in self.compatible_sensors:
print(('WARNING: Wrong sensor class chosen for sensor ' +
print(('WARNING: Wrong sensor class chosen for sensor ' +
str(sensor.product_id) + ' on port ' + str(port) + '. ' + """
You may be using the wrong type of sensor or may have connected the cable
incorrectly. If you are sure you're using the correct sensor class for the
Expand Down Expand Up @@ -125,7 +125,7 @@ def _i2c_query(self, address, format):
raise I2CError('Read failure: Not enough bytes')
data = struct.unpack(format, data[-n_bytes:])
return data

def read_value(self, name):
"""Reads a value from the sensor. Name must be a string found in
self.I2C_ADDRESS dictionary. Entries in self.I2C_ADDRESS are in the
Expand All @@ -150,13 +150,13 @@ def write_value(self, name, value):
"""
address, fmt = self.I2C_ADDRESS[name]
self._i2c_command(address, value, fmt)

def get_sensor_info(self):
version = self.read_value('version')[0].decode('windows-1252').split('\0')[0]
product_id = self.read_value('product_id')[0].decode('windows-1252').split('\0')[0]
sensor_type = self.read_value('sensor_type')[0].decode('windows-1252').split('\0')[0]
return SensorInfo(version, product_id, sensor_type)

@classmethod
def add_compatible_sensor(cls, version, product_id, sensor_type):
"""Adds an entry in the compatibility table for the sensor. If version
Expand All @@ -171,8 +171,8 @@ def add_compatible_sensor(cls, version, product_id, sensor_type):
cls.compatible_sensors.append(SCompatibility(version, product_id,
sensor_type))
add_mapping(cls, version, product_id, sensor_type)


class SCompatibility(SensorInfo):
"""An object that helps manage the sensor mappings"""
def __eq__(self, other):
Expand All @@ -194,7 +194,7 @@ def add_mapping(cls, version, product_id, sensor_type):
if product_id not in sensor_mappings:
sensor_mappings[product_id] = {}
models = sensor_mappings[product_id]

if sensor_type is None:
if sensor_type in models:
raise ValueError('Already registered!')
Expand All @@ -204,7 +204,7 @@ def add_mapping(cls, version, product_id, sensor_type):
if sensor_type not in models:
models[sensor_type] = {}
versions = models[sensor_type]

if version in versions:
raise ValueError('Already registered!')
else:
Expand Down
Loading

0 comments on commit 20c396e

Please sign in to comment.