Device

This class is the blueprint for the device attributes of the ports in the hub.port module, which in turn are instances of the Port class. You cannot import or instantiate this class manually.

class Device

Read values from a Powered Up device and configure its modes.

Methods

get(format: Optional[int])list

Gets the values that the active device mode provides.

Parameters

format – Format of the data. Choose FORMAT_RAW, FORMAT_PCT, or FORMAT_SI.

Returns

Values or measurements representing the device state.

mode(mode: int)None
mode(mode: int, data: bytes)None
mode(mode: Iterable[Tuple[int, int]])None
mode()Iterable[Tuple[int, int]]

Configures the device mode or multi-mode.

Most Powered Up devices can work in different modes. Each mode makes it do or measure different things. After selecting one mode or a list of modes, the corresponding measured values are accessible via the get method.

Each mode can provide one or more values. For each mode in a multi-mode list, you must select which value of that mode you want. For example, to read value 0 of mode 0 as well as values 2 and 3 of mode 5, the mode argument is: [(0, 0), (5, 2), (5, 3)]. See Port.info to learn which modes can be combined.

Incorrect arguments or incompatible mode settings will be ignored without errors.

Parameters
  • mode – A single mode integer or a list of multi-mode tuples.

  • data – Data to write to the selected mode. When using this argument, modes must be a single integer.

Returns

When setting the mode, it returns None. If you don’t give any arguments, this returns the currently active mode.

pwm(value: int)None

Applies a PWM signal to the power pins of the port or device.

A PWM value of 0 has the same effect as float().

Parameters

value – PWM value between -100 and +100. The polarity of the PWM signal matches the sign of the value. A value of 0 stops the PWM signal and leaves the port driver in the floating state.

write_direct(data: bytes)None

Sends a message to the device using the wired Powered Up protocol.

The data must be formatted using the Powered Up specification, which may include a command or message type and a payload. The required checksum will be added automatically.

Parameters

data – The Powered Up data message. Must not exceed 9 bytes.

Constants

FORMAT_RAW = 0

The data has no particular unit.

FORMAT_PCT = 1

The data is a percentage.

FORMAT_SI = 2

The data has SI units, if available.