Skip to content

Commit 9118179

Browse files
committed
Use enumerations for input constants
Also stop importing everything in nxt.sensor. This clobbers the name space with duplicate names for the same object.
1 parent 69cb422 commit 9118179

13 files changed

Lines changed: 478 additions & 339 deletions

File tree

docs/api/sensors/index.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ Work in progress...
66
.. automodule:: nxt.sensor
77
:members:
88

9-
.. automodule:: nxt.sensor.common
10-
:members:
11-
:undoc-members:
12-
139
.. automodule:: nxt.sensor.analog
1410
:members:
1511
:undoc-members:

docs/migration.rst

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ use this code before calling any NXT-Python function::
4242
import logging
4343
logging.basicConfig(level=logging.DEBUG)
4444

45-
The :mod:`!nxt` module no longer exports name from sub-modules. In general,
46-
NXT-Python now avoids to have two names for the same object.
45+
The :mod:`!nxt` and :mod:`nxt.sensor` modules no longer exports name from
46+
sub-modules. In general, NXT-Python now avoids to have two names for the same
47+
object.
4748

4849
Output port constants are replaced by enumerations, using the :mod:`enum`
4950
module:
@@ -72,6 +73,50 @@ NXT-Python 2 NXT-Python 3
7273
You can now create :class:`nxt.motor.Motor` objects using
7374
:meth:`nxt.brick.Brick.get_motor`, however direct creation still works.
7475

76+
Input port constants are replaced by enumerations, using the :mod:`enum`
77+
module. The :mod:`!nxt.sensor.common` module has been removed, its content is
78+
directly available in :mod:`nxt.sensor`:
79+
80+
.. py:currentmodule:: nxt.sensor
81+
82+
=============================== ============================
83+
NXT-Python 2 NXT-Python 3
84+
=============================== ============================
85+
:data:`!PORT_1` :attr:`Port.S1`
86+
:data:`!PORT_2` :attr:`Port.S2`
87+
:data:`!PORT_3` :attr:`Port.S3`
88+
:data:`!PORT_4` :attr:`Port.S4`
89+
:attr:`!Type.NO_SENSOR` :attr:`Type.NO_SENSOR`
90+
:attr:`!Type.SWITCH` :attr:`Type.SWITCH`
91+
:attr:`!Type.TEMPERATURE` :attr:`Type.TEMPERATURE`
92+
:attr:`!Type.REFLECTION` :attr:`Type.REFLECTION`
93+
:attr:`!Type.ANGLE` :attr:`Type.ANGLE`
94+
:attr:`!Type.LIGHT_ACTIVE` :attr:`Type.LIGHT_ACTIVE`
95+
:attr:`!Type.LIGHT_INACTIVE` :attr:`Type.LIGHT_INACTIVE`
96+
:attr:`!Type.SOUND_DB` :attr:`Type.SOUND_DB`
97+
:attr:`!Type.SOUND_DBA` :attr:`Type.SOUND_DBA`
98+
:attr:`!Type.CUSTOM` :attr:`Type.CUSTOM`
99+
:attr:`!Type.LOW_SPEED` :attr:`Type.LOW_SPEED`
100+
:attr:`!Type.LOW_SPEED_9V` :attr:`Type.LOW_SPEED_9V`
101+
:attr:`!Type.HIGH_SPEED` :attr:`Type.HIGH_SPEED`
102+
:attr:`!Type.COLORFULL` :attr:`Type.COLOR_FULL`
103+
:attr:`!Type.COLORRED` :attr:`Type.COLOR_RED`
104+
:attr:`!Type.COLORGREEN` :attr:`Type.COLOR_GREEN`
105+
:attr:`!Type.COLORBLUE` :attr:`Type.COLOR_BLUE`
106+
:attr:`!Type.COLORNONE` :attr:`Type.COLOR_NONE`
107+
:attr:`!Type.COLOREXIT` :attr:`Type.COLOR_EXIT`
108+
:attr:`!Mode.RAW` :attr:`Mode.RAW`
109+
:attr:`!Mode.BOOLEAN` :attr:`Mode.BOOL`
110+
:attr:`!Mode.TRANSITION_CNT` :attr:`Mode.EDGE`
111+
:attr:`!Mode.PERIOD_COUNTER` :attr:`Mode.PULSE`
112+
:attr:`!Mode.PCT_FULL_SCALE` :attr:`Mode.PERCENT`
113+
:attr:`!Mode.CELSIUS` :attr:`Mode.CELSIUS`
114+
:attr:`!Mode.FAHRENHEIT` :attr:`Mode.FAHRENHEIT`
115+
:attr:`!Mode.ANGLE_STEPS` :attr:`Mode.ROTATION`
116+
:attr:`!Mode.MASK` Removed
117+
:attr:`!Mode.MASK_SLOPE` Removed
118+
=============================== ============================
119+
75120

76121
Text String or Binary String
77122
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -131,6 +176,11 @@ From :mod:`nxt.error`:
131176
- :exc:`!FileNotFound` has been renamed to :exc:`FileNotFoundError`.
132177
- :exc:`!ModuleNotFound` has been renamed to :exc:`ModuleNotFoundError`.
133178

179+
Sensors:
180+
181+
- :class:`!nxt.sensor.generic.Color20` has been renamed to
182+
:class:`nxt.sensor.generic.Color`.
183+
134184

135185
Removed
136186
^^^^^^^
@@ -186,11 +236,6 @@ From :class:`nxt.brick.Brick`:
186236
- :meth:`~Brick.boot` now takes a argument to avoid accidental firmware
187237
erasure.
188238

189-
Sensors:
190-
191-
- :class:`!nxt.sensor.generic.Color20` has been renamed to
192-
:class:`nxt.sensor.generic.Color`.
193-
194239
Other:
195240

196241
- :class:`nxt.motcont.MotCont` methods accept tuple as argument to control

nxt/brick.py

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import nxt.error
2222
import nxt.motor
2323
import nxt.sensor
24+
import nxt.sensor.digital
2425
from nxt.telegram import Opcode, Telegram
2526

2627
__all__ = ["Brick"]
@@ -271,7 +272,19 @@ def find_modules(self, pattern="*.*"):
271272
finally:
272273
self.module_close(handle)
273274

274-
get_sensor = nxt.sensor.get_sensor
275+
def get_sensor(self, port):
276+
"""Tries to detect the sensor type and return the correct sensor object.
277+
278+
:param nxt.sensor.Port port: Input port identifier.
279+
:return: A sensor object.
280+
:rtype: nxt.sensor.Sensor
281+
:raises nxt.sensor.digital.SearchError: When sensor can not be identified.
282+
283+
Only work for digital sensors with identification information.
284+
"""
285+
base_sensor = nxt.sensor.digital.BaseDigitalSensor(self, port, False)
286+
info = base_sensor.get_sensor_info()
287+
return nxt.sensor.digital.find_class(info)(self, port, check_compatible=False)
275288

276289
def get_motor(self, port):
277290
"""Return a motor object connected to one of the brick output port.
@@ -372,14 +385,17 @@ def set_output_state(
372385
def set_input_mode(self, port, sensor_type, sensor_mode):
373386
"""Set input port mode on the brick.
374387
375-
:param int port: Input port constant.
376-
:param int sensor_type: Sensor type.
377-
:param int sensor_mode: Sensor mode.
388+
:param nxt.sensor.Port port: Input port identifier.
389+
:param nxt.sensor.Type sensor_type: Sensor type.
390+
:param nxt.sensor.Mode sensor_mode: Sensor mode.
391+
392+
.. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor`
393+
class.
378394
"""
379395
tgram = Telegram(Opcode.DIRECT_SET_IN_MODE, reply_req=False)
380-
tgram.add_u8(port)
381-
tgram.add_u8(sensor_type)
382-
tgram.add_u8(sensor_mode)
396+
tgram.add_u8(port.value)
397+
tgram.add_u8(sensor_type.value)
398+
tgram.add_u8(sensor_mode.value)
383399
self._cmd(tgram)
384400

385401
def get_output_state(self, port):
@@ -439,15 +455,16 @@ def get_output_state(self, port):
439455
def get_input_values(self, port):
440456
"""Get input port values from the brick.
441457
442-
:param int port: Input port constant.
458+
:param nxt.sensor.Port port: Input port identifier.
443459
:return: A tuple with `port`, `valid`, `calibrated`, `sensor_type`,
444460
`sensor_mode`, `raw_value`, `normalized_value`, `scaled_value`, and
445461
`calibrated_value`. `rotation_count`.
446-
:rtype: (int, int, int, int, int, int, int, int, int)
462+
:rtype: (nxt.sensor.Port, bool, bool, nxt.sensor.Type, nxt.sensor.Mode, int,
463+
int, int, int)
447464
448465
Return value details:
449466
450-
- **port** Input port constant.
467+
- **port** Input port identifier.
451468
- **valid** ``True`` if the value is valid, else ``False``.
452469
- **calibrated** Always ``False``, there is no calibration in NXT firmware.
453470
- **sensor_type** Sensor type.
@@ -457,15 +474,18 @@ def get_input_values(self, port):
457474
- **scaled_value** Scaled value.
458475
- **calibrated_value** Always normalized value, there is no calibration in NXT
459476
firmware.
477+
478+
.. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor`
479+
class.
460480
"""
461481
tgram = Telegram(Opcode.DIRECT_GET_IN_VALS)
462-
tgram.add_u8(port)
482+
tgram.add_u8(port.value)
463483
tgram = self._cmd(tgram)
464-
port = tgram.parse_u8()
465-
valid = tgram.parse_u8()
466-
calibrated = tgram.parse_u8()
467-
sensor_type = tgram.parse_u8()
468-
sensor_mode = tgram.parse_u8()
484+
port = nxt.sensor.Port(tgram.parse_u8())
485+
valid = tgram.parse_bool()
486+
calibrated = tgram.parse_bool()
487+
sensor_type = nxt.sensor.Type(tgram.parse_u8())
488+
sensor_mode = nxt.sensor.Mode(tgram.parse_u8())
469489
raw_value = tgram.parse_u16()
470490
normalized_value = tgram.parse_u16()
471491
scaled_value = tgram.parse_s16()
@@ -485,12 +505,15 @@ def get_input_values(self, port):
485505
def reset_input_scaled_value(self, port):
486506
"""Reset scaled value for an input port on the brick.
487507
488-
:param int port: Input port constant.
508+
:param nxt.sensor.Port port: Input port identifier.
489509
490510
This can be used to reset accumulated value for some sensor modes.
511+
512+
.. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor`
513+
class.
491514
"""
492515
tgram = Telegram(Opcode.DIRECT_RESET_IN_VAL)
493-
tgram.add_u8(port)
516+
tgram.add_u8(port.value)
494517
self._cmd(tgram)
495518

496519
def message_write(self, inbox, message):
@@ -553,30 +576,36 @@ def keep_alive(self):
553576
def ls_get_status(self, port):
554577
"""Get status of last low-speed transaction to a brick input port.
555578
556-
:param int port: Input port constant.
579+
:param nxt.sensor.Port port: Input port identifier.
557580
:return: Number of bytes to read as a result of the transaction.
558581
:rtype: int
559582
:raises nxt.error.I2CPendingError: When transaction is still in progress.
560583
:raises nxt.error.DirectProtocolError: When there is an error on the bus.
584+
585+
.. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor`
586+
class.
561587
"""
562588
tgram = Telegram(Opcode.DIRECT_LS_GET_STATUS)
563-
tgram.add_u8(port)
589+
tgram.add_u8(port.value)
564590
tgram = self._cmd(tgram)
565591
size = tgram.parse_u8()
566592
return size
567593

568594
def ls_write(self, port, tx_data, rx_bytes):
569595
"""Write data to a brick input port using low speed transaction.
570596
571-
:param int port: Input port constant.
597+
:param nxt.sensor.Port port: Input port identifier.
572598
:param bytes tx_data: Data to send.
573599
:param int rx_bytes: Number of bytes to receive.
574600
575601
Function returns immediately. Transaction status can be retrieved using
576602
:meth:`ls_get_status` and result must be read using :meth:`ls_read`.
603+
604+
.. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor`
605+
class.
577606
"""
578607
tgram = Telegram(Opcode.DIRECT_LS_WRITE)
579-
tgram.add_u8(port)
608+
tgram.add_u8(port.value)
580609
tgram.add_u8(len(tx_data))
581610
tgram.add_u8(rx_bytes)
582611
tgram.add_bytes(tx_data)
@@ -585,16 +614,19 @@ def ls_write(self, port, tx_data, rx_bytes):
585614
def ls_read(self, port):
586615
"""Read result of low speed transaction.
587616
588-
:param int port: Input port constant.
617+
:param nxt.sensor.Port port: Input port identifier.
589618
:return: Data received.
590619
:rtype: bytes
591620
:raises nxt.error.I2CPendingError: When transaction is still in progress.
592621
:raises nxt.error.DirectProtocolError: When there is an error on the bus.
593622
594623
The :meth:`ls_write` function must be called to initiate the transaction.
624+
625+
.. warning:: This is a low level function, prefer to use a :mod:`nxt.sensor`
626+
class.
595627
"""
596628
tgram = Telegram(Opcode.DIRECT_LS_READ)
597-
tgram.add_u8(port)
629+
tgram.add_u8(port.value)
598630
tgram = self._cmd(tgram)
599631
size = tgram.parse_u8()
600632
rx_data = tgram.parse_bytes(size)

0 commit comments

Comments
 (0)