Components

A component is the smallest logical part of some hardware.

A component will have the same basic functionality no matter what hardware it is on.

Battery Sensor

class j5.components.BatterySensor(identifier: int, backend: j5.components.battery_sensor.BatterySensorInterface)[source]

A sensor capable of monitoring a battery.

property current: float

Get the current of the battery sensor.

Returns

current measured by the sensor.

property voltage: float

Get the voltage reported by the battery sensor.

Returns

voltage measured by the sensor.

Button

class j5.components.Button(identifier: int, backend: j5.components.button.ButtonInterface)[source]

A button.

property is_pressed: bool

Get the current pushed state of the button.

Returns

current pushed state of the button.

wait_until_pressed() None[source]

Halt the program until this button is pushed.

GPIOPin

class j5.components.GPIOPin(identifier: int, backend: j5.components.gpio_pin.GPIOPinInterface, *, initial_mode: Union[Type[j5.components.component.DerivedComponent], j5.components.gpio_pin.GPIOPinMode], hardware_modes: Set[j5.components.gpio_pin.GPIOPinMode] = {GPIOPinMode.DIGITAL_OUTPUT}, firmware_modes: Set[Type[j5.components.component.DerivedComponent]] = {})[source]

A GPIO Pin.

analogue_read() float[source]

Get the scaled analogue reading of the pin.

Returns

scaled analogue reading

analogue_write(new_value: float) None[source]

Set the analogue value of the pin.

Parameters

new_value – analogue value

Raises

ValueError – pin value must be between 0 and 1

digital_read() bool[source]

Get the digital state of the pin.

Returns

digital read state of the pin.

digital_write(state: bool) None[source]

Set the digital state of the pin.

Parameters

state – digital state.

property firmware_modes: Set[Type[j5.components.component.DerivedComponent]]

Get the supported firmware modes.

Returns

supported firmware modes.

property last_digital_write: bool

Get the last set digital state of the pin.

This does not perform a read operation, it only gets the last set value, which is usually cached in memory.

Returns

last set digital state of the pin

property mode: Union[Type[j5.components.component.DerivedComponent], j5.components.gpio_pin.GPIOPinMode]

Get the mode of this pin.

Returns

current mode of the pin.

pwm_write(new_value: float) None[source]

Set the PWM value of the pin.

Parameters

new_value – new duty cycle

Raises

ValueError – pin value must be between 0 and 1

LED

class j5.components.LED(identifier: int, backend: j5.components.led.LEDInterface)[source]

A standard Light Emitting Diode.

property state: bool

Get the current state of the LED.

Returns

current state of the LED.

Motor

class j5.components.Motor(identifier: int, backend: j5.components.motor.MotorInterface)[source]

Brushed DC motor output.

property power: Union[float, j5.components.motor.MotorSpecialState]

Get the current power of this output.

Returns

current power of this output.

Piezo

class j5.components.Piezo(identifier: int, backend: j5.components.piezo.PiezoInterface, *, default_blocking: bool = False)[source]

A standard piezo.

buzz(duration: Union[int, float, datetime.timedelta], pitch: Union[int, float, j5.components.piezo.Note], *, blocking: Optional[bool] = None) None[source]

Queue a note to be played.

Float and integer durations are measured in seconds.

A buzz can either be blocking, or non-blocking and will fall back to a default if it is not specified.

Parameters
  • duration – length to play for

  • pitch – pitch of buzz.

  • blocking – whether the code waits for the buzz

static verify_duration(duration: datetime.timedelta) None[source]

Verify that a duration is valid.

Parameters

duration – duration to validate.

Raises
static verify_pitch(pitch: Union[int, float, j5.components.piezo.Note]) None[source]

Verify that a pitch is valid.

Parameters

pitch – pitch to validate.

Raises

PowerOutput

class j5.components.PowerOutput(identifier: int, backend: j5.components.power_output.PowerOutputInterface)[source]

A power output channel.

It can be enabled/disabled, and the current being drawn on this channel can be measured.

property current: float

Get the current being drawn on this power output, in amperes.

Returns

current being drawn on this power output, in amperes.

property is_enabled: bool

Get whether the output is enabled.

Returns

output enabled

PWMLED

class j5.components.PWMLED(identifier: int, backend: j5.components.pwm_led.PWMLEDInterface)[source]

A Light Emitting Diode, driven by a PWM output.

This usually means that the LED is of variable brightness.

property duty_cycle: float

Get the current duty cycle of the LED.

Returns

current duty cycle of the LED.

RGBLED

class j5.components.RGBLED(identifier: int, backend: j5.components.rgb_led.RGBLEDInterface)[source]

A Light Emitting Diode, driven by a PWM output.

This usually means that the LED is of variable brightness.

property blue: float

Get the current value of the blue channel.

Returns

current duty cycle of the blue channel.

get_channel(channel: Union[str, j5.components.rgb_led.RGBColour]) float[source]

Get the current value of a channel.

Parameters

channel – The channel to get the value for.

Returns

The duty cycle for the channel.

Raises

ValueError – channel is not a valid RGB channel.

property green: float

Get the current value of the green channel.

Returns

current duty cycle of the green channel.

property red: float

Get the current value of the red channel.

Returns

current duty cycle of the red channel.

property rgb: Tuple[float, float, float]

Get a tuple of the channel duty cycles.

Returns

tuple of duty cycles (R, G, B).

set_channel(channel: Union[str, j5.components.rgb_led.RGBColour], duty_cycle: float) None[source]

Set the current value of a channel.

Parameters
  • channel – The channel to get the value for.

  • duty_cycle – The duty cycle to set the channel to.

Raises
  • ValueError – channel is not a valid RGB channel.

  • ValueError – duty cycle is not in expected range.

Servo

class j5.components.Servo(identifier: int, backend: j5.components.servo.ServoInterface)[source]

A standard servomotor.

property position: Optional[float]

Get the current position of the Servo.

Returns

current position of the Servo

StringCommand

class j5.components.StringCommandComponent(identifier: int, backend: j5.components.string_command.StringCommandComponentInterface)[source]

A string command component.

This component allows the sending and receiving of commands to a board, so that custom ASCII protocols can be implemented. This is primarily aimed at Boards which can have custom firmware installed by the students that are using them.

execute(command: str) str[source]

Execute the string command and return the result.

This function can be synchronous and blocking.

Parameters

command – command to execute.

Returns

result of command.

Raises

ValueError – command is not valid.

UltrasoundSensor

class j5.components.derived.UltrasoundSensor(gpio_trigger: j5.components.gpio_pin.GPIOPin, gpio_echo: j5.components.gpio_pin.GPIOPin, backend: j5.components.derived.ultrasound.UltrasoundInterface, *, distance_mode: bool = True)[source]

Ultrasonic distance sensor.

A sensor that utilises the reflection of ultrasound to calculate the distance to a nearby object.

distance() Optional[float][source]

Send a pulse and return the distance to the object.

Returns

Distance measured in metres, or None if it timed out.

Raises

Exception – distance mode is disabled.

pulse() Optional[datetime.timedelta][source]

Send a pulse and return the time taken.

Returns

Time taken for the pulse, or None if it timed out.