2121import nxt .error
2222import nxt .motor
2323import nxt .sensor
24+ import nxt .sensor .digital
2425from 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