Skip to content

Commit

Permalink
Merge pull request schodet#134 from feinstaub/b1
Browse files Browse the repository at this point in the history
Remove trailing whitespace
  • Loading branch information
GoldSloth authored Apr 22, 2018
2 parents e9cfe56 + a3920f8 commit 86ebdf2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
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
29 changes: 14 additions & 15 deletions nxt/sensor/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class Touch(BaseAnalogSensor):
def __init__(self, brick, port):
super(Touch, self).__init__(brick, port)
self.set_input_mode(Type.SWITCH, Mode.BOOLEAN)

def is_pressed(self):
return bool(self.get_input_values().scaled_value)

get_sample = is_pressed


Expand All @@ -46,8 +46,8 @@ def set_illuminated(self, active):
self.set_input_mode(type_, Mode.RAW)

def get_lightness(self):
return self.get_input_values().scaled_value
return self.get_input_values().scaled_value

get_sample = get_lightness


Expand All @@ -64,10 +64,10 @@ def set_adjusted(self, active):
else:
type_ = Type.SOUND_DB
self.set_input_mode(type_, Mode.RAW)

def get_loudness(self):
return self.get_input_values().scaled_value

get_sample = get_loudness


Expand All @@ -82,32 +82,31 @@ class Ultrasonic(BaseDigitalSensor):
'actual_scale_factor': (0x51, 'B'),
'actual_scale_divisor': (0x52, 'B'),
})

class Commands:
'These are for passing to command()'
OFF = 0x00
SINGLE_SHOT = 0x01
CONTINUOUS_MEASUREMENT = 0x02
EVENT_CAPTURE = 0x03 #Optimize results when other Ultrasonic sensors running
REQUEST_WARM_RESET = 0x04

def __init__(self, brick, port, check_compatible=True):
super(Ultrasonic, self).__init__(brick, port, check_compatible)
self.set_input_mode(Type.LOW_SPEED_9V, Mode.RAW)

def get_distance(self):
'Function to get data from the ultrasonic sensor'
return self.read_value('measurement_byte_0')[0]

get_sample = get_distance

def get_measurement_units(self):
return self.read_value('measurement_units')[0].decode('windows-1252').split('\0')[0]

def get_all_measurements(self):
"Returns all the past readings in measurement_byte_0 through 7"
return self.read_value('measurements')

def get_measurement_no(self, number):
"Returns measurement_byte_number"
if not 0 <= number < 8:
Expand All @@ -117,11 +116,11 @@ def get_measurement_no(self, number):

def command(self, command):
self.write_value('command', (command, ))

def get_interval(self):
'Get the sample interval for continuous measurement mode -- Unknown units'
return self.read_value('continuous_measurement_interval')

def set_interval(self, interval):
"""Set the sample interval for continuous measurement mode.
Unknown units; default is 1"""
Expand All @@ -146,9 +145,9 @@ def get_light_color(self):
def get_reflected_light(self, color):
self.set_light_color(color)
return self.get_input_values().scaled_value

def get_color(self):
self.get_reflected_light(Type.COLORFULL)
return self.get_input_values().scaled_value

get_sample = get_color

0 comments on commit 86ebdf2

Please sign in to comment.