Exemplo n.º 1
0
    def stop_program(self):
        """Stop the running program on the brick.

        :raises nxt.error.DirectProtocolError: When no program is running.
        """
        tgram = Telegram(Opcode.DIRECT_STOP_PROGRAM)
        self._cmd(tgram)
Exemplo n.º 2
0
    def bluetooth_factory_reset(self):
        """Reset brick Bluetooth to factory settings.

        This only works on USB connection.
        """
        tgram = Telegram(Opcode.SYSTEM_BTFACTORYRESET)
        self._cmd(tgram)
Exemplo n.º 3
0
    def set_output_state(self, port, power, mode, regulation_mode, turn_ratio,
                         run_state, tacho_limit):
        """Set output port state on the brick.

        :param nxt.motor.Port port: Output port identifier.
        :param int power: Motor speed or power level (-100 to 100).
        :param nxt.motor.Mode mode: Motor power mode.
        :param nxt.motor.RegulationMode regulation_mode: Motor regulation mode.
        :param int turn_ratio: Turn ratio (-100 to 100). Negative value shift power to
           the left motor.
        :param nxt.motor.RunState run_state: Motor run state.
        :param int tacho_limit: Number of degrees the motor should rotate relative to
           the current position.

        .. warning:: This is a low level function, prefer to use
           :meth:`nxt.motor.Motor`, you can get one from :meth:`get_motor`.
        """
        tgram = Telegram(Opcode.DIRECT_SET_OUT_STATE, reply_req=False)
        tgram.add_u8(port.value)
        tgram.add_s8(power)
        tgram.add_u8(mode.value)
        tgram.add_u8(regulation_mode.value)
        tgram.add_s8(turn_ratio)
        tgram.add_u8(run_state.value)
        tgram.add_u32(tacho_limit)
        self._cmd(tgram)
Exemplo n.º 4
0
    def get_device_info(self):
        """Get brick information.

        :return: The brick name, Bluetooth address, Bluetooth signal strengths and free
           user flash.
        :rtype: (str, str, (int, int, int, int), int)

        Bluetooth address uses this notation: ``00:16:53:xx:xx:xx``, where `xx` is the
        brick unique address part (`00:16:53` is the LEGO OUI used for the NXT bricks).
        """
        tgram = Telegram(Opcode.SYSTEM_DEVICEINFO)
        tgram = self._cmd(tgram)
        name = tgram.parse_string(15)
        a0 = tgram.parse_u8()
        a1 = tgram.parse_u8()
        a2 = tgram.parse_u8()
        a3 = tgram.parse_u8()
        a4 = tgram.parse_u8()
        a5 = tgram.parse_u8()
        # a6 is not used, should be zero.
        tgram.parse_u8()
        address = "%02X:%02X:%02X:%02X:%02X:%02X" % (a0, a1, a2, a3, a4, a5)
        signal_strengths = (
            tgram.parse_u8(),
            tgram.parse_u8(),
            tgram.parse_u8(),
            tgram.parse_u8(),
        )
        user_flash = tgram.parse_u32()
        return name, address, signal_strengths, user_flash
Exemplo n.º 5
0
    def start_program(self, name):
        """Start a program on the brick.

        :param str name: Program file name (example: ``"myprogram.rxe"``).
        """
        tgram = Telegram(Opcode.DIRECT_START_PROGRAM)
        tgram.add_filename(name)
        self._cmd(tgram)
Exemplo n.º 6
0
    def set_brick_name(self, name):
        """Set brick name.

        :param str name: New brick name.
        """
        tgram = Telegram(Opcode.SYSTEM_SETBRICKNAME)
        tgram.add_string(15, name)
        self._cmd(tgram)
Exemplo n.º 7
0
 def poll(self, *args, **kwargs):
     ogram = poll_func(opcode, *args, **kwargs)
     with self.lock:
         self.sock.send(ogram.bytes())
         if ogram.reply:
             igram = Telegram(opcode=opcode, pkt=self.sock.recv())
     if ogram.reply:
         return parse_func(igram)
     else:
         return None
Exemplo n.º 8
0
    def get_battery_level(self):
        """Get brick battery voltage.

        :return: Battery voltage in millivolt.
        :rtype: int
        """
        tgram = Telegram(Opcode.DIRECT_GET_BATT_LVL)
        tgram = self._cmd(tgram)
        millivolts = tgram.parse_u16()
        return millivolts
Exemplo n.º 9
0
    def play_sound_file(self, loop, name):
        """Play a sound file on the brick.

        :param bool loop: Loop mode, play continuously.
        :param str name: Sound file name.
        """
        tgram = Telegram(Opcode.DIRECT_PLAY_SOUND_FILE, reply_req=False)
        tgram.add_bool(loop)
        tgram.add_filename(name)
        self._cmd(tgram)
Exemplo n.º 10
0
    def keep_alive(self):
        """Reset the brick standby timer.

        :return: Sleep timeout in milliseconds.
        :rtype: int
        """
        tgram = Telegram(Opcode.DIRECT_KEEP_ALIVE)
        tgram = self._cmd(tgram)
        sleep_timeout = tgram.parse_u32()
        return sleep_timeout
Exemplo n.º 11
0
    def get_current_program_name(self):
        """Return name of program currently running on the brick.

        :return: Program file name
        :rtype: str
        :raises nxt.error.DirectProtocolError: When no program is running.
        """
        tgram = Telegram(Opcode.DIRECT_GET_CURR_PROGRAM)
        tgram = self._cmd(tgram)
        name = tgram.parse_filename()
        return name
Exemplo n.º 12
0
    def get_output_state(self, port):
        """Get output port state from the brick.

        :param nxt.motor.Port port: Output port identifier.
        :return: A tuple with `port`, `power`, `mode`, `regulation_mode`, `turn_ratio`,
           `run_state`, `tacho_limit`, `tacho_count`, `block_tacho_count`, and
           `rotation_count`.
        :rtype: (nxt.motor.Port, int, nxt.motor.Mode, nxt.motor.RegulationMode, int,
           nxt.motor.RunState, int, int, int, int)

        Return value details:

        - **port** Output port identifier.
        - **power** Motor speed or power level (-100 to 100).
        - **mode** Motor power mode.
        - **regulation_mode** Motor regulation mode.
        - **turn_ratio** Turn ratio (-100 to 100). Negative value shift power to the
          left motor.
        - **run_state** Motor run state.
        - **tacho_limit** Number of degrees the motor should rotate.
        - **block_tacho_count** Number of degrees the motor rotated relative to the
          "block" start.
        - **rotation_count** Number of degrees the motor rotated relative to the program
          start.

        .. warning:: This is a low level function, prefer to use
           :meth:`nxt.motor.Motor`, you can get one from :meth:`get_motor`.
        """
        tgram = Telegram(Opcode.DIRECT_GET_OUT_STATE)
        tgram.add_u8(port.value)
        tgram = self._cmd(tgram)
        port = nxt.motor.Port(tgram.parse_u8())
        power = tgram.parse_s8()
        mode = nxt.motor.Mode(tgram.parse_u8())
        regulation_mode = nxt.motor.RegulationMode(tgram.parse_u8())
        turn_ratio = tgram.parse_s8()
        run_state = nxt.motor.RunState(tgram.parse_u8())
        tacho_limit = tgram.parse_u32()
        tacho_count = tgram.parse_s32()
        block_tacho_count = tgram.parse_s32()
        rotation_count = tgram.parse_s32()
        return (
            port,
            power,
            mode,
            regulation_mode,
            turn_ratio,
            run_state,
            tacho_limit,
            tacho_count,
            block_tacho_count,
            rotation_count,
        )
Exemplo n.º 13
0
    def poll_command_length(self, buf_num):
        """Get number of bytes available in brick poll buffer.

        :param int buf_num: Buffer number, 0 for USB, 1 for high speed.
        :return: Buffer number and number of available bytes.
        :rtype: (int, int)
        """
        tgram = Telegram(Opcode.SYSTEM_POLLCMDLEN)
        tgram.add_u8(buf_num)
        tgram = self._cmd(tgram)
        buf_num = tgram.parse_u8()
        size = tgram.parse_u8()
        return buf_num, size
Exemplo n.º 14
0
    def reset_input_scaled_value(self, port):
        """Reset scaled value for an input port on the brick.

        :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.value)
        self._cmd(tgram)
Exemplo n.º 15
0
    def file_delete(self, name):
        """Delete a file on the brick.

        :param str name: File name.
        :return: The deleted file name.
        :rtype: str
        :raises nxt.error.FileNotFoundError: When file does not exists.
        """
        tgram = Telegram(Opcode.SYSTEM_DELETE)
        tgram.add_filename(name)
        tgram = self._cmd(tgram)
        name = tgram.parse_filename()
        return name
Exemplo n.º 16
0
    def play_tone(self, frequency_hz, duration_ms):
        """Play a tone on the brick, do not wait until finished.

        :param int frequency_hz: Tone frequency in Hertz.
        :param int duration_ms: Tone duration in milliseconds.

        This function do not wait until finished, if you want to play several notes, you
        may need :func:`play_tone_and_wait`.
        """
        tgram = Telegram(Opcode.DIRECT_PLAY_TONE, reply_req=False)
        tgram.add_u16(frequency_hz)
        tgram.add_u16(duration_ms)
        self._cmd(tgram)
Exemplo n.º 17
0
    def reset_motor_position(self, port, relative):
        """Reset block or program motor position for a brick output port.

        :param nxt.motor.Port port: Output port identifier.
        :param bool relative: If ``True``, reset block position, if ``False``, reset
           program position.

        .. warning:: This is a low level function, prefer to use
           :meth:`nxt.motor.Motor`, you can get one from :meth:`get_motor`.
        """
        tgram = Telegram(Opcode.DIRECT_RESET_POSITION)
        tgram.add_u8(port.value)
        tgram.add_bool(relative)
        self._cmd(tgram)
Exemplo n.º 18
0
    def message_write(self, inbox, message):
        """Send a message to a brick mailbox.

        :param int inbox: Mailbox number (0 to 19).
        :param bytes message: Message to send (58 bytes maximum).
        """
        if len(message) > 58:
            raise ValueError("message too long")
        tgram = Telegram(Opcode.DIRECT_MESSAGE_WRITE)
        tgram.add_u8(inbox)
        tgram.add_u8(len(message) + 1)
        tgram.add_bytes(message)
        tgram.add_u8(0)
        self._cmd(tgram)
Exemplo n.º 19
0
    def file_close(self, handle):
        """Close open file.

        :param int handle: Open file handle.
        :return: The closed file handle.
        :rtype: int

        .. warning:: This is a low level function, prefer to use :meth:`open_file`.
        """
        tgram = Telegram(Opcode.SYSTEM_CLOSE)
        tgram.add_u8(handle)
        tgram = self._cmd(tgram)
        handle = tgram.parse_u8()
        return handle
Exemplo n.º 20
0
    def module_close(self, handle):
        """Close module search.

        :param int handle: Open module handle.
        :return: The closed module handle.
        :rtype: int

        .. warning:: This is a low level function, prefer to use :meth:`find_modules`.
        """
        tgram = Telegram(Opcode.SYSTEM_CLOSEMODHANDLE)
        tgram.add_u8(handle)
        tgram = self._cmd(tgram)
        handle = tgram.parse_u8()
        return handle
Exemplo n.º 21
0
    def get_input_values(self, port):
        """Get input port values from the brick.

        :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: (nxt.sensor.Port, bool, bool, nxt.sensor.Type, nxt.sensor.Mode, int,
           int, int, int)

        Return value details:

        - **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.
        - **sensor_mode** Sensor mode.
        - **raw_value** Raw analog to digital converter value.
        - **normalized_value** Normalized value.
        - **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.value)
        tgram = self._cmd(tgram)
        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()
        calibrated_value = tgram.parse_s16()
        return (
            port,
            valid,
            calibrated,
            sensor_type,
            sensor_mode,
            raw_value,
            normalized_value,
            scaled_value,
            calibrated_value,
        )
Exemplo n.º 22
0
    def set_input_mode(self, port, sensor_type, sensor_mode):
        """Set input port mode on the brick.

        :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.value)
        tgram.add_u8(sensor_type.value)
        tgram.add_u8(sensor_mode.value)
        self._cmd(tgram)
Exemplo n.º 23
0
    def get_firmware_version(self):
        """Get firmware version information.

        :return: Protocol and firmware versions, as two tuples with major and minor for
           each version.
        :rtype: ((int, int), (int, int))
        """
        tgram = Telegram(Opcode.SYSTEM_VERSIONS)
        tgram = self._cmd(tgram)
        prot_minor = tgram.parse_u8()
        prot_major = tgram.parse_u8()
        prot_version = (prot_major, prot_minor)
        fw_minor = tgram.parse_u8()
        fw_major = tgram.parse_u8()
        fw_version = (fw_major, fw_minor)
        return prot_version, fw_version
Exemplo n.º 24
0
    def poll_command(self, buf_num, size):
        """Get bytes from brick poll buffer.

        :param int buf_num: Buffer number, 0 for USB, 1 for high speed.
        :param int size: Number of bytes to read.
        :return: Buffer number and read bytes.
        :rtype: (int, bytes)
        """
        tgram = Telegram(Opcode.SYSTEM_POLLCMD)
        tgram.add_u8(buf_num)
        tgram.add_u8(size)
        tgram = self._cmd(tgram)
        buf_num = tgram.parse_u8()
        size = tgram.parse_u8()
        command = tgram.parse_bytes(size)
        return buf_num, command
Exemplo n.º 25
0
    def file_open_read(self, name):
        """Open file for reading.

        :param str name: File name.
        :return: The file handle and the file size.
        :rtype: (int, int)
        :raises nxt.error.FileNotFoundError: When file does not exists.

        .. warning:: This is a low level function, prefer to use :meth:`open_file`.
        """
        tgram = Telegram(Opcode.SYSTEM_OPENREAD)
        tgram.add_filename(name)
        tgram = self._cmd(tgram)
        handle = tgram.parse_u8()
        size = tgram.parse_u32()
        return handle, size
Exemplo n.º 26
0
    def _cmd(self, tgram):
        """Send a message to the NXT brick and read reply.

        :param nxt.telegram.Telegram tgram: Message to send.
        :return: Reply message after status has been checked, or None if no reply
           requested.
        :rtype: nxt.telegram.Telegram or None
        """
        reply_tgram = None
        with self._lock:
            self._sock.send(tgram.bytes())
            if tgram.reply_req:
                reply_tgram = Telegram(opcode=tgram.opcode,
                                       pkt=self._sock.recv())
        if reply_tgram:
            reply_tgram.check_status()
        return reply_tgram
Exemplo n.º 27
0
    def file_find_next(self, handle):
        """Continue finding files.

        :param int handle: Handle open with :meth:`file_find_first`.
        :return: The handle, next file found name and size.
        :rtype: (int, str, int)
        :raises nxt.error.FileNotFoundError: When no more file is found.

        .. warning:: This is a low level function, prefer to use :meth:`find_files`.
        """
        tgram = Telegram(Opcode.SYSTEM_FINDNEXT)
        tgram.add_u8(handle)
        tgram = self._cmd(tgram)
        handle = tgram.parse_u8()
        name = tgram.parse_filename()
        size = tgram.parse_u32()
        return handle, name, size
Exemplo n.º 28
0
    def file_find_first(self, pattern):
        """Start finding files matching a pattern.

        :param str pattern: Pattern to match files against.
        :return: A handle for the search, first file found name and size.
        :rtype: (int, str, int)
        :raises nxt.error.FileNotFoundError: When no file is found.

        .. warning:: This is a low level function, prefer to use :meth:`find_files`.
        """
        tgram = Telegram(Opcode.SYSTEM_FINDFIRST)
        tgram.add_filename(pattern)
        tgram = self._cmd(tgram)
        handle = tgram.parse_u8()
        name = tgram.parse_filename()
        size = tgram.parse_u32()
        return handle, name, size
Exemplo n.º 29
0
    def file_write(self, handle, data):
        """Write data to open file.

        :param int handle: Open file handle.
        :param bytes data: Data to write.
        :return: The file handle and the number of bytes written.
        :rtype: (int, int)

        .. warning:: This is a low level function, prefer to use :meth:`open_file`.
        """
        tgram = Telegram(Opcode.SYSTEM_WRITE)
        tgram.add_u8(handle)
        tgram.add_bytes(data)
        tgram = self._cmd(tgram)
        handle = tgram.parse_u8()
        size = tgram.parse_u16()
        return handle, size
Exemplo n.º 30
0
    def ls_get_status(self, port):
        """Get status of last low-speed transaction to a brick input port.

        :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.value)
        tgram = self._cmd(tgram)
        size = tgram.parse_u8()
        return size