diff --git a/docs/api/sensors/index.rst b/docs/api/sensors/index.rst index 75e8dfe..959cb17 100644 --- a/docs/api/sensors/index.rst +++ b/docs/api/sensors/index.rst @@ -6,10 +6,6 @@ Work in progress... .. automodule:: nxt.sensor :members: -.. automodule:: nxt.sensor.common - :members: - :undoc-members: - .. automodule:: nxt.sensor.analog :members: :undoc-members: diff --git a/docs/migration.rst b/docs/migration.rst index e9658fe..ab70e82 100644 --- a/docs/migration.rst +++ b/docs/migration.rst @@ -42,8 +42,9 @@ use this code before calling any NXT-Python function:: import logging logging.basicConfig(level=logging.DEBUG) -The :mod:`!nxt` module no longer exports name from sub-modules. In general, -NXT-Python now avoids to have two names for the same object. +The :mod:`!nxt` and :mod:`nxt.sensor` modules no longer exports name from +sub-modules. In general, NXT-Python now avoids to have two names for the same +object. Output port constants are replaced by enumerations, using the :mod:`enum` module: @@ -72,6 +73,50 @@ NXT-Python 2 NXT-Python 3 You can now create :class:`nxt.motor.Motor` objects using :meth:`nxt.brick.Brick.get_motor`, however direct creation still works. +Input port constants are replaced by enumerations, using the :mod:`enum` +module. The :mod:`!nxt.sensor.common` module has been removed, its content is +directly available in :mod:`nxt.sensor`: + +.. py:currentmodule:: nxt.sensor + +=============================== ============================ +NXT-Python 2 NXT-Python 3 +=============================== ============================ +:data:`!PORT_1` :attr:`Port.S1` +:data:`!PORT_2` :attr:`Port.S2` +:data:`!PORT_3` :attr:`Port.S3` +:data:`!PORT_4` :attr:`Port.S4` +:attr:`!Type.NO_SENSOR` :attr:`Type.NO_SENSOR` +:attr:`!Type.SWITCH` :attr:`Type.SWITCH` +:attr:`!Type.TEMPERATURE` :attr:`Type.TEMPERATURE` +:attr:`!Type.REFLECTION` :attr:`Type.REFLECTION` +:attr:`!Type.ANGLE` :attr:`Type.ANGLE` +:attr:`!Type.LIGHT_ACTIVE` :attr:`Type.LIGHT_ACTIVE` +:attr:`!Type.LIGHT_INACTIVE` :attr:`Type.LIGHT_INACTIVE` +:attr:`!Type.SOUND_DB` :attr:`Type.SOUND_DB` +:attr:`!Type.SOUND_DBA` :attr:`Type.SOUND_DBA` +:attr:`!Type.CUSTOM` :attr:`Type.CUSTOM` +:attr:`!Type.LOW_SPEED` :attr:`Type.LOW_SPEED` +:attr:`!Type.LOW_SPEED_9V` :attr:`Type.LOW_SPEED_9V` +:attr:`!Type.HIGH_SPEED` :attr:`Type.HIGH_SPEED` +:attr:`!Type.COLORFULL` :attr:`Type.COLOR_FULL` +:attr:`!Type.COLORRED` :attr:`Type.COLOR_RED` +:attr:`!Type.COLORGREEN` :attr:`Type.COLOR_GREEN` +:attr:`!Type.COLORBLUE` :attr:`Type.COLOR_BLUE` +:attr:`!Type.COLORNONE` :attr:`Type.COLOR_NONE` +:attr:`!Type.COLOREXIT` :attr:`Type.COLOR_EXIT` +:attr:`!Mode.RAW` :attr:`Mode.RAW` +:attr:`!Mode.BOOLEAN` :attr:`Mode.BOOL` +:attr:`!Mode.TRANSITION_CNT` :attr:`Mode.EDGE` +:attr:`!Mode.PERIOD_COUNTER` :attr:`Mode.PULSE` +:attr:`!Mode.PCT_FULL_SCALE` :attr:`Mode.PERCENT` +:attr:`!Mode.CELSIUS` :attr:`Mode.CELSIUS` +:attr:`!Mode.FAHRENHEIT` :attr:`Mode.FAHRENHEIT` +:attr:`!Mode.ANGLE_STEPS` :attr:`Mode.ROTATION` +:attr:`!Mode.MASK` Removed +:attr:`!Mode.MASK_SLOPE` Removed +=============================== ============================ + Text String or Binary String ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -131,6 +176,11 @@ From :mod:`nxt.error`: - :exc:`!FileNotFound` has been renamed to :exc:`FileNotFoundError`. - :exc:`!ModuleNotFound` has been renamed to :exc:`ModuleNotFoundError`. +Sensors: + +- :class:`!nxt.sensor.generic.Color20` has been renamed to + :class:`nxt.sensor.generic.Color`. + Removed ^^^^^^^ @@ -186,11 +236,6 @@ From :class:`nxt.brick.Brick`: - :meth:`~Brick.boot` now takes a argument to avoid accidental firmware erasure. -Sensors: - -- :class:`!nxt.sensor.generic.Color20` has been renamed to - :class:`nxt.sensor.generic.Color`. - Other: - :class:`nxt.motcont.MotCont` methods accept tuple as argument to control diff --git a/nxt/brick.py b/nxt/brick.py index ec17c9d..593c236 100644 --- a/nxt/brick.py +++ b/nxt/brick.py @@ -21,6 +21,7 @@ import nxt.error import nxt.motor import nxt.sensor +import nxt.sensor.digital from nxt.telegram import Opcode, Telegram __all__ = ["Brick"] @@ -271,7 +272,19 @@ def find_modules(self, pattern="*.*"): finally: self.module_close(handle) - get_sensor = nxt.sensor.get_sensor + def get_sensor(self, port): + """Tries to detect the sensor type and return the correct sensor object. + + :param nxt.sensor.Port port: Input port identifier. + :return: A sensor object. + :rtype: nxt.sensor.Sensor + :raises nxt.sensor.digital.SearchError: When sensor can not be identified. + + Only work for digital sensors with identification information. + """ + base_sensor = nxt.sensor.digital.BaseDigitalSensor(self, port, False) + info = base_sensor.get_sensor_info() + return nxt.sensor.digital.find_class(info)(self, port, check_compatible=False) def get_motor(self, port): """Return a motor object connected to one of the brick output port. @@ -372,14 +385,17 @@ def set_output_state( def set_input_mode(self, port, sensor_type, sensor_mode): """Set input port mode on the brick. - :param int port: Input port constant. - :param int sensor_type: Sensor type. - :param int sensor_mode: Sensor mode. + :param nxt.sensor.Port port: Input port identifier. + :param nxt.sensor.Type sensor_type: Sensor type. + :param nxt.sensor.Mode sensor_mode: Sensor mode. + + .. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor` + class. """ tgram = Telegram(Opcode.DIRECT_SET_IN_MODE, reply_req=False) - tgram.add_u8(port) - tgram.add_u8(sensor_type) - tgram.add_u8(sensor_mode) + tgram.add_u8(port.value) + tgram.add_u8(sensor_type.value) + tgram.add_u8(sensor_mode.value) self._cmd(tgram) def get_output_state(self, port): @@ -439,15 +455,16 @@ def get_output_state(self, port): def get_input_values(self, port): """Get input port values from the brick. - :param int port: Input port constant. + :param nxt.sensor.Port port: Input port identifier. :return: A tuple with `port`, `valid`, `calibrated`, `sensor_type`, `sensor_mode`, `raw_value`, `normalized_value`, `scaled_value`, and `calibrated_value`. `rotation_count`. - :rtype: (int, int, int, int, int, int, int, int, int) + :rtype: (nxt.sensor.Port, bool, bool, nxt.sensor.Type, nxt.sensor.Mode, int, + int, int, int) Return value details: - - **port** Input port constant. + - **port** Input port identifier. - **valid** ``True`` if the value is valid, else ``False``. - **calibrated** Always ``False``, there is no calibration in NXT firmware. - **sensor_type** Sensor type. @@ -457,15 +474,18 @@ def get_input_values(self, port): - **scaled_value** Scaled value. - **calibrated_value** Always normalized value, there is no calibration in NXT firmware. + + .. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor` + class. """ tgram = Telegram(Opcode.DIRECT_GET_IN_VALS) - tgram.add_u8(port) + tgram.add_u8(port.value) tgram = self._cmd(tgram) - port = tgram.parse_u8() - valid = tgram.parse_u8() - calibrated = tgram.parse_u8() - sensor_type = tgram.parse_u8() - sensor_mode = tgram.parse_u8() + port = nxt.sensor.Port(tgram.parse_u8()) + valid = tgram.parse_bool() + calibrated = tgram.parse_bool() + sensor_type = nxt.sensor.Type(tgram.parse_u8()) + sensor_mode = nxt.sensor.Mode(tgram.parse_u8()) raw_value = tgram.parse_u16() normalized_value = tgram.parse_u16() scaled_value = tgram.parse_s16() @@ -485,12 +505,15 @@ def get_input_values(self, port): def reset_input_scaled_value(self, port): """Reset scaled value for an input port on the brick. - :param int port: Input port constant. + :param nxt.sensor.Port port: Input port identifier. This can be used to reset accumulated value for some sensor modes. + + .. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor` + class. """ tgram = Telegram(Opcode.DIRECT_RESET_IN_VAL) - tgram.add_u8(port) + tgram.add_u8(port.value) self._cmd(tgram) def message_write(self, inbox, message): @@ -553,14 +576,17 @@ def keep_alive(self): def ls_get_status(self, port): """Get status of last low-speed transaction to a brick input port. - :param int port: Input port constant. + :param nxt.sensor.Port port: Input port identifier. :return: Number of bytes to read as a result of the transaction. :rtype: int :raises nxt.error.I2CPendingError: When transaction is still in progress. :raises nxt.error.DirectProtocolError: When there is an error on the bus. + + .. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor` + class. """ tgram = Telegram(Opcode.DIRECT_LS_GET_STATUS) - tgram.add_u8(port) + tgram.add_u8(port.value) tgram = self._cmd(tgram) size = tgram.parse_u8() return size @@ -568,15 +594,18 @@ def ls_get_status(self, port): def ls_write(self, port, tx_data, rx_bytes): """Write data to a brick input port using low speed transaction. - :param int port: Input port constant. + :param nxt.sensor.Port port: Input port identifier. :param bytes tx_data: Data to send. :param int rx_bytes: Number of bytes to receive. Function returns immediately. Transaction status can be retrieved using :meth:`ls_get_status` and result must be read using :meth:`ls_read`. + + .. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor` + class. """ tgram = Telegram(Opcode.DIRECT_LS_WRITE) - tgram.add_u8(port) + tgram.add_u8(port.value) tgram.add_u8(len(tx_data)) tgram.add_u8(rx_bytes) tgram.add_bytes(tx_data) @@ -585,16 +614,19 @@ def ls_write(self, port, tx_data, rx_bytes): def ls_read(self, port): """Read result of low speed transaction. - :param int port: Input port constant. + :param nxt.sensor.Port port: Input port identifier. :return: Data received. :rtype: bytes :raises nxt.error.I2CPendingError: When transaction is still in progress. :raises nxt.error.DirectProtocolError: When there is an error on the bus. The :meth:`ls_write` function must be called to initiate the transaction. + + .. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor` + class. """ tgram = Telegram(Opcode.DIRECT_LS_READ) - tgram.add_u8(port) + tgram.add_u8(port.value) tgram = self._cmd(tgram) size = tgram.parse_u8() rx_data = tgram.parse_bytes(size) diff --git a/nxt/sensor/__init__.py b/nxt/sensor/__init__.py index 1baa1c9..994357a 100644 --- a/nxt/sensor/__init__.py +++ b/nxt/sensor/__init__.py @@ -2,6 +2,7 @@ # Copyright (C) 2006,2007 Douglas P Lau # Copyright (C) 2009 Marcus Wanner, Paulo Vieira, rhn # Copyright (C) 2010 Marcus Wanner +# Copyright (C) 2021 Nicolas Schodet # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -13,39 +14,130 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -from .common import * -from .analog import BaseAnalogSensor -from .digital import BaseDigitalSensor, find_class -from .generic import Touch, Light, Sound, Ultrasonic, Color, Temperature -from . import mindsensors -MSSumoEyes = mindsensors.SumoEyes -MSCompassv2 = mindsensors.Compassv2 -MSDIST = mindsensors.DIST -MSRTC = mindsensors.RTC -MSACCL = mindsensors.ACCL -MSServo = mindsensors.Servo -MSMTRMUX = mindsensors.MTRMUX -MSLineLeader = mindsensors.LineLeader -MSMMX = mindsensors.MMX -MSPS2 = mindsensors.PS2 -MSHID = mindsensors.HID -from . import hitechnic -HTCompass = hitechnic.Compass -HTAccelerometer = hitechnic.Accelerometer -HTGyro = hitechnic.Gyro -HTColorv2 = hitechnic.Colorv2 -HTEOPD = hitechnic.EOPD -HTIRReceiver = hitechnic.IRReceiver -HTIRSeekerv2 = hitechnic.IRSeekerv2 -HTPrototype = hitechnic.Prototype -HTAngle = hitechnic.Angle - - -def get_sensor(brick, port): - """Tries to detect the sensor type and return the correct sensor -object. Does not work for sensors with no identification information (such as -all analog sensors or the MindSensors RTC. +import enum + +__all__ = ["Port", "Type", "Mode", "Sensor"] + + +class Port(enum.Enum): + """Input port identifier. + + The prefix is needed because a Python identifier can not start with a digit. """ - base_sensor = BaseDigitalSensor(brick, port, False) - info = base_sensor.get_sensor_info() - return find_class(info)(brick, port, check_compatible=False) + + S1 = 0 + """Sensor port 1.""" + + S2 = 1 + """Sensor port 2.""" + + S3 = 2 + """Sensor port 3.""" + + S4 = 3 + """Sensor port 4.""" + + +class Type(enum.Enum): + """Sensor type.""" + + NO_SENSOR = 0 + """No sensor is connected.""" + + SWITCH = 1 + """Touch sensor.""" + + TEMPERATURE = 2 + """RCX temperature sensor.""" + + REFLECTION = 3 + """RCX light sensor.""" + + ANGLE = 4 + """RCX rotation sensor.""" + + LIGHT_ACTIVE = 5 + """Light sensor with light active.""" + + LIGHT_INACTIVE = 6 + """Light sensor with light off.""" + + SOUND_DB = 7 + """Sound sensor (unadjusted).""" + + SOUND_DBA = 8 + """Sound sensor (adjusted).""" + + CUSTOM = 9 + """Custom sensor (unused).""" + + LOW_SPEED = 10 + """Low speed digital sensor.""" + + LOW_SPEED_9V = 11 + """Low speed digital sensor with 9V supply voltage.""" + + HIGH_SPEED = 12 + """High speed sensor.""" + + COLOR_FULL = 13 + """NXT color sensor in full color mode (color sensor mode).""" + + COLOR_RED = 14 + """NXT color sensor with red light on (light sensor mode).""" + + COLOR_GREEN = 15 + """NXT color sensor with green light on (light sensor mode).""" + + COLOR_BLUE = 16 + """NXT color sensor in with blue light on (light sensor mode).""" + + COLOR_NONE = 17 + """NXT color sensor in with light off (light sensor mode).""" + + COLOR_EXIT = 18 + """NXT color sensor internal state.""" + + +class Mode(enum.Enum): + """Sensor mode.""" + + RAW = 0x00 + """Raw value, from 0 to 1023.""" + + BOOL = 0x20 + """Boolean value, 0 or 1.""" + + EDGE = 0x40 + """Count number of transitions.""" + + PULSE = 0x60 + """Count number of pulse.""" + + PERCENT = 0x80 + """Value from 0 to 100.""" + + CELSIUS = 0xA0 + """Temperature in degree Celsius.""" + + FAHRENHEIT = 0xC0 + """Temperature in degree Fahrenheit.""" + + ROTATION = 0xE0 + """RCX rotation sensor mode.""" + + +class Sensor: + """Sensor base class.""" + + def __init__(self, brick, port): + self._brick = brick + self._port = port + + def set_input_mode(self, sensor_type, sensor_mode): + """Set sensor input mode. + + :param Type sensor_type: Sensor type. + :param Mode sensor_mode: Sensor mode. + """ + self._brick.set_input_mode(self._port, sensor_type, sensor_mode) diff --git a/nxt/sensor/analog.py b/nxt/sensor/analog.py index 14fac4d..54bb23f 100644 --- a/nxt/sensor/analog.py +++ b/nxt/sensor/analog.py @@ -1,6 +1,7 @@ # nxt.sensor.analog module -- submodule for use with analog sensors # Copyright (C) 2006,2007 Douglas P Lau # Copyright (C) 2009 Marcus Wanner, Paulo Vieira, rhn +# Copyright (C) 2021 Nicolas Schodet # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -12,30 +13,55 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -from .common import * +import nxt.sensor -class RawReading: # can be converted to the old version - """A pseudo-structure holding the raw sensor values as returned by the NXT - brick. - """ +class RawReading: + """A object holding the raw sensor values for a sensor.""" + def __init__(self, values): - (self.port, self.valid, self.calibrated, self.sensor_type, self.mode, - self.raw_value, self.normalized_value, self.scaled_value, - self.calibrated_value) = values - + ( + self.port, + self.valid, + self.calibrated, + self.sensor_type, + self.mode, + self.raw_value, + self.normalized_value, + self.scaled_value, + self.calibrated_value, + ) = values + def __repr__(self): - return str((self.port, self.valid, self.calibrated, self.sensor_type, self.mode, - self.raw_value, self.normalized_value, self.scaled_value, - self.calibrated_value)) - + return str( + ( + self.port, + self.valid, + self.calibrated, + self.sensor_type, + self.mode, + self.raw_value, + self.normalized_value, + self.scaled_value, + self.calibrated_value, + ) + ) + -class BaseAnalogSensor(Sensor): +class BaseAnalogSensor(nxt.sensor.Sensor): """Object for analog sensors.""" + def get_input_values(self): - """Returns the raw sensor values as returned by the NXT brick.""" - return RawReading(self.brick.get_input_values(self.port)) + """Get raw sensor readings. + + :return: An object with the read values. + :rtype: RawReading + """ + return RawReading(self._brick.get_input_values(self._port)) def reset_input_scaled_value(self): - self.brick.reset_input_scaled_value(self.port) + """Reset sensor scaled value. + This can be used to reset accumulated value for some sensor modes. + """ + self._brick.reset_input_scaled_value(self._port) diff --git a/nxt/sensor/common.py b/nxt/sensor/common.py deleted file mode 100644 index 5afd6c8..0000000 --- a/nxt/sensor/common.py +++ /dev/null @@ -1,67 +0,0 @@ -# nxt.sensor.common module -- submodule with stuff useful in all sensors -# Copyright (C) 2006,2007 Douglas P Lau -# Copyright (C) 2009 Marcus Wanner, Paulo Vieira, rhn -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -PORT_1 = 0x00 -PORT_2 = 0x01 -PORT_3 = 0x02 -PORT_4 = 0x03 - -class Type(object): - 'Namespace for enumeration of the type of sensor' - # NOTE: just a namespace (enumeration) - NO_SENSOR = 0x00 - SWITCH = 0x01 # Touch sensor - TEMPERATURE = 0x02 - REFLECTION = 0x03 - ANGLE = 0x04 - LIGHT_ACTIVE = 0x05 # Light sensor (illuminated) - LIGHT_INACTIVE = 0x06 # Light sensor (ambient) - SOUND_DB = 0x07 # Sound sensor (unadjusted) - SOUND_DBA = 0x08 # Sound sensor (adjusted) - CUSTOM = 0x09 - LOW_SPEED = 0x0A - LOW_SPEED_9V = 0x0B # Low-speed I2C (Ultrasonic sensor) - HIGH_SPEED = 0x0C #Possibly other mode for I2C; may be used by future sensors. - COLORFULL = 0x0D #NXT 2.0 color sensor in full color mode (color sensor mode) - COLORRED = 0x0E #NXT 2.0 color sensor with red light on (light sensor mode) - COLORGREEN = 0x0F #NXT 2.0 color sensor with green light on (light sensor mode) - COLORBLUE = 0x10 #NXT 2.0 color sensor in with blue light on (light sensor mode) - COLORNONE = 0x11 #NXT 2.0 color sensor in with light off (light sensor mode) - COLOREXIT = 0x12 #NXT 2.0 color sensor internal state (not sure what this is for yet) - - -class Mode(object): - 'Namespace for enumeration of the mode of sensor' - # NOTE: just a namespace (enumeration) - RAW = 0x00 - BOOLEAN = 0x20 - TRANSITION_CNT = 0x40 - PERIOD_COUNTER = 0x60 - PCT_FULL_SCALE = 0x80 - CELSIUS = 0xA0 - FAHRENHEIT = 0xC0 - ANGLE_STEPS = 0xE0 - MASK = 0xE0 - MASK_SLOPE = 0x1F # Why isn't this slope thing documented? - - -class Sensor(object): - 'Main sensor object' - - def __init__(self, brick, port): - self.brick = brick - self.port = port - - def set_input_mode(self, type_, mode): - self.brick.set_input_mode(self.port, type_, mode) diff --git a/nxt/sensor/digital.py b/nxt/sensor/digital.py index f70bbe3..6b98635 100644 --- a/nxt/sensor/digital.py +++ b/nxt/sensor/digital.py @@ -18,8 +18,7 @@ import time from nxt.error import I2CError, I2CPendingError, DirectProtocolError - -from .common import * +import nxt.sensor logger = logging.getLogger(__name__) @@ -45,7 +44,7 @@ def __str__(self): outstr += (self.clarifybinary(str(self.sensor_type), 'Type')) return outstr -class BaseDigitalSensor(Sensor): +class BaseDigitalSensor(nxt.sensor.Sensor): """Object for digital sensors. I2C_ADDRESS is the dictionary storing name to i2c address mappings. It should be updated in every subclass. When subclassing this class, make sure to call add_compatible_sensor to add @@ -66,7 +65,7 @@ def __init__(self, brick, port, check_compatible=True): a warning. """ super(BaseDigitalSensor, self).__init__(brick, port) - self.set_input_mode(Type.LOW_SPEED_9V, Mode.RAW) + self.set_input_mode(nxt.sensor.Type.LOW_SPEED_9V, nxt.sensor.Mode.RAW) self.last_poll = time.time() self.poll_delay = 0.01 time.sleep(0.1) # Give I2C time to initialize @@ -77,9 +76,9 @@ def __init__(self, brick, port, check_compatible=True): sensor = self.get_sensor_info() if not sensor in self.compatible_sensors: logger.warning( - "wrong sensor class chosen for sensor %s on port %d", + "wrong sensor class chosen for sensor %s on port %s", sensor.product_id, - port + 1, + port, ) logger.warning( " You may be using the wrong type of sensor or may have " @@ -94,7 +93,7 @@ def __init__(self, brick, port, check_compatible=True): def _ls_get_status(self, size): for n in range(30): #https://code.google.com/p/nxt-python/issues/detail?id=35 try: - b = self.brick.ls_get_status(self.port) + b = self._brick.ls_get_status(self._port) if b >= size: return b except I2CPendingError: @@ -112,7 +111,7 @@ def _i2c_command(self, address, value, format): diff = now - self.last_poll time.sleep(self.poll_delay - diff) self.last_poll = time.time() - self.brick.ls_write(self.port, msg, 0) + self._brick.ls_write(self._port, msg, 0) def _i2c_query(self, address, format): """Reads an i2c value from given address, and returns a value unpacked @@ -126,12 +125,12 @@ def _i2c_query(self, address, format): diff = now - self.last_poll time.sleep(self.poll_delay - diff) self.last_poll = time.time() - self.brick.ls_write(self.port, msg, size) + self._brick.ls_write(self._port, msg, size) try: self._ls_get_status(size) finally: #we should clear the buffer no matter what happens - data = self.brick.ls_read(self.port) + data = self._brick.ls_read(self._port) if len(data) < size: raise I2CError('Read failure: Not enough bytes') data = struct.unpack(format, data[-size:]) diff --git a/nxt/sensor/generic.py b/nxt/sensor/generic.py index 5f125a8..08ad0e4 100644 --- a/nxt/sensor/generic.py +++ b/nxt/sensor/generic.py @@ -13,7 +13,7 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -from .common import * +from nxt.sensor import Type, Mode from .digital import BaseDigitalSensor from .analog import BaseAnalogSensor @@ -23,7 +23,7 @@ class Touch(BaseAnalogSensor): def __init__(self, brick, port): super(Touch, self).__init__(brick, port) - self.set_input_mode(Type.SWITCH, Mode.BOOLEAN) + self.set_input_mode(Type.SWITCH, Mode.BOOL) def is_pressed(self): return bool(self.get_input_values().scaled_value) @@ -136,14 +136,14 @@ def set_interval(self, interval): class Color(BaseAnalogSensor): def __init__(self, brick, port): super(Color, self).__init__(brick, port) - self.set_light_color(Type.COLORFULL) + self.set_light_color(Type.COLOR_FULL) def set_light_color(self, color): - """color should be one of the COLOR* Type namespace values, e.g. Type.COLORBLUE""" + """color should be one of the COLOR_* Type namespace values, e.g. Type.COLOR_BLUE""" self.set_input_mode(color, Mode.RAW) def get_light_color(self): - """Returns one of the COLOR* Type namespace values, e.g. Type.COLORRED""" + """Returns one of the COLOR_* Type namespace values, e.g. Type.COLOR_RED""" return self.get_input_values().sensor_type def get_reflected_light(self, color): @@ -151,7 +151,7 @@ def get_reflected_light(self, color): return self.get_input_values().scaled_value def get_color(self): - self.get_reflected_light(Type.COLORFULL) + self.get_reflected_light(Type.COLOR_FULL) return self.get_input_values().scaled_value get_sample = get_color diff --git a/nxt/sensor/hitechnic.py b/nxt/sensor/hitechnic.py index 4cb16f2..3e22ab1 100644 --- a/nxt/sensor/hitechnic.py +++ b/nxt/sensor/hitechnic.py @@ -14,7 +14,7 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -from .common import * +from nxt.sensor import Type, Mode from .digital import BaseDigitalSensor from .analog import BaseAnalogSensor diff --git a/nxt/sensor/mindsensors.py b/nxt/sensor/mindsensors.py index 08cd5ba..d06cac2 100644 --- a/nxt/sensor/mindsensors.py +++ b/nxt/sensor/mindsensors.py @@ -15,7 +15,7 @@ import logging -from .common import * +from nxt.sensor import Type, Mode from .digital import BaseDigitalSensor, SensorInfo from .analog import BaseAnalogSensor diff --git a/nxt/telegram.py b/nxt/telegram.py index 22e5127..f8020ba 100644 --- a/nxt/telegram.py +++ b/nxt/telegram.py @@ -204,6 +204,9 @@ def parse_string(self, size=-1): def parse_filename(self): return self.parse_string(20) + def parse_bool(self): + return unpack("