upyt.cmd package

Submodules

upyt.cmd.can module

upyt.cmd.can.can_state(send, bus)

Get the state of a previously configured CAN bus.

Parameters

bus (int) – 1 or 2 (according to the pyboard’s CAN controllers)

Response:

{
    'r': 'can_state',  # in response to can_state request
    'state': x,  # CAN bus state, as string (or None if not configured)
}
upyt.cmd.can.can_tx(send, bus, id, data, rtr=False)
upyt.cmd.can.can_tx_p(send, bus, id, data, period)

Send a CAN message periodically

Parameters
  • bus (int) – bus number (1 or 2)

  • id (int) – arbitration id of frame

  • data (list) – list of bytes (integers between 0 and 255)

  • period (int) – time between transmissions (ms)

upyt.cmd.can.can_tx_p_stop(send, bus, id)
upyt.cmd.can.can_tx_p_stopall(send, bus)
upyt.cmd.can.config_can(send, bus, mode='normal', extframe=False, prescaler=100, sjw=1, bs1=6, bs2=8, auto_restart=False)

Configure CAN bus

Parameters

bus (int) – 1 or 2 (for each CAN controller on the pyboard)

upyt.cmd.io module

upyt.cmd.io.config_pin(id, mode, pull=None, value=None)
Parameters
  • id (str) – name of pin (eg: SW, X7)

  • mode (str) – pin mode (see table below)

  • pull (str) – pull up/down resistor (if any), (see table below)

  • value (int) – if pin is an output, initial value can be set

Parameter: mode

mode Value

Pin Behaviour

'in'

input

'out'

(synonymn for ‘out_pp’)

'out_pp'

output, with push-pull control

'out_od'

output, with open-drain control

'af_pp'

alternate function, pull-pull

'af_od'

alternate function, open-drain

'analog'

analog

Parameter: pull

pull Value

Pin Behaviour

None

no pull up/down

'none'

no pull up/down

'up'

pull up resistor

'down'

pull down resistor

upyt.cmd.io.get_pin(id)
upyt.cmd.io.set_pin(id, v)

upyt.cmd.leds module

Turn LED on for a set duration

Parameters
  • led (int) – index of LED 1=red, 2=green, 3=yellow, and 4=blue

  • intensity (int) – brightness of LED (0-0xff)

  • duration (int) – time to leave LED on

upyt.cmd.leds.set_led(led=1, intensity=255)

Turn LED on for a set duration

Parameters
  • led (int) – index of LED 1=red, 2=green, 3=yellow, and 4=blue

  • intensity (int) – brightness of LED (0-0xff)

upyt.cmd.mapping module

upyt.cmd.mapping.instruction(func)

Maps a function to an incoming instruction

Usage:

@instruction
def ping(value=0):
    return {'r': 'ping', 'value': value + 1}

With this implemented, when a host sends {'i': 'ping', 'k': {'value': 10}}, it will receive {'r': 'ping', 'value': 11}

Format of Request When transmitting a request from a host machine, the dict format is:

{
    'i': 'some_instruction',  # name of the method
    'a': [1, 'abc', -5.4],  # optional list arguments
    'k': {  # optional keywords argument list
        'value': True,
        'pin': 'X3',
    }
}

Instruction Design for Serializing Because the method name, and keyword arguments are serialized and transmitted, consider keeping argument & method names short.

upyt.cmd.mapping.interpret(obj)

Perform the action defined in the given object

Parameters

obj (dict) – deserialized JSON object received from host

upyt.cmd.mapping.list_instructions()
upyt.cmd.mapping.send(obj)

Send serial data over the preset serial port

Usage:

>>> import pyb
>>> from cmd import set_serial_port, send

# Setup Port >>> com_port = pyb.USB_VCP() >>> set_serial_port(com_port)

# Sender transmits json encoding of given obj over VCP >>> send(‘abc’) >>> send(123) >>> send([1, 2, 3]) >>> send({‘a’: 1, ‘b’: 2})

upyt.cmd.mapping.set_serial_port(given_port)

Set the serial object through which to send serialized data. See send() for more details.

Parameters

given_port (pyb.USB_VCP) – Port through which to send serialized data

upyt.cmd.spi module

upyt.cmd.spi.config_spi(idx)

Configure SPI bus

Parameters

idx (int (1 or 2)) – pyboard SPI index (1 or 2)

upyt.cmd.spi.spi_send(idx, data)

Note: SPI must be configured via instruction config_spi() before sending with this method.

Parameters
  • idx (int) – pyboard SPI index (1 or 2)

  • data (list of int) –

upyt.cmd.system module

upyt.cmd.system.bootloader_mode(t=50)

Reets pyboard, and it will boot into bootloader mode

Parameters

t (int) – time until reset (unit: ms) (default: 50ms)

upyt.cmd.system.break_loop()
upyt.cmd.system.get_system_info()
upyt.cmd.system.get_ticks_ms()

Get ticks since boot

upyt.cmd.system.machine_reset(t=50)

Resets the pyboard

Parameters

t (int) – time until reset (unit: ms) (default: 50ms)

upyt.cmd.test module

upyt.cmd.test.get_switch()
upyt.cmd.test.ping(value=0)

Module contents

upyt.cmd.instruction(func)

Maps a function to an incoming instruction

Usage:

@instruction
def ping(value=0):
    return {'r': 'ping', 'value': value + 1}

With this implemented, when a host sends {'i': 'ping', 'k': {'value': 10}}, it will receive {'r': 'ping', 'value': 11}

Format of Request When transmitting a request from a host machine, the dict format is:

{
    'i': 'some_instruction',  # name of the method
    'a': [1, 'abc', -5.4],  # optional list arguments
    'k': {  # optional keywords argument list
        'value': True,
        'pin': 'X3',
    }
}

Instruction Design for Serializing Because the method name, and keyword arguments are serialized and transmitted, consider keeping argument & method names short.

upyt.cmd.interpret(obj)

Perform the action defined in the given object

Parameters

obj (dict) – deserialized JSON object received from host

upyt.cmd.send(obj)

Send serial data over the preset serial port

Usage:

>>> import pyb
>>> from cmd import set_serial_port, send

# Setup Port >>> com_port = pyb.USB_VCP() >>> set_serial_port(com_port)

# Sender transmits json encoding of given obj over VCP >>> send(‘abc’) >>> send(123) >>> send([1, 2, 3]) >>> send({‘a’: 1, ‘b’: 2})