Python Driver

Command decorator

The command decorator is available in the Koheron Python library.

Use the command decorator @command(classname='', funcname='') where classname is the C++ driver class name and funcname the C++ driver function name for the Python driver to work in line with the C++ driver.

Example

For the following C++ driver:

class LedBlinker {
    #...
    void set_led(uint32_t led_value) {
        ctl.write<reg::led>(led_value);
    }
    #...
}

The matching Python driver is:

from koheron import command

class LedBlinker(object):
    def __init__(self, client):
        self.client = client

        @command(classname='LedBlinker', funcname='set_led')
        def my_set_led(self, value):
            pass


If the Python driver class name matches the C++ driver class name and the Python driver function name matches the C++ driver function name, you can simply use @command().

Available functions

Receive a single value

recv(format)

Available format:

  • 'I': unsigned 32-bit integer
  • 'i': signed 32-bit integer
  • 'f': 32-bit floating point
  • 'd': 64-bit double precision
  • '?': boolean

Example

C++ driver:

uint32_t get_forty_two() {
    return sts.read<reg::forty_two>();
}

Python driver:

@command()
def get_forty_two(self):
    return self.client.recv('I')

Receive a tuple of values

recv_tuple(format)

Available format:

  • 'I': unsigned 32-bit integer
  • 'i': signed 32-bit integer
  • 'f': 32-bit floating point
  • 'd': 64-bit double precision
  • '?': boolean

Example

C++ driver:

auto get_adc() {
    // Convert from two-complement to int32
    int32_t adc0 = ((sts.read<reg::adc0>()-8192)%16384)-8192;
    int32_t adc1 = ((sts.read<reg::adc1>()-8192)%16384)-8192;
    return std::make_tuple(adc0, adc1);
}

Python driver:

@command()
def get_adc(self):
    return self.client.recv_tuple('ii')

Receive arrays

C++ standard arrays and vectors are mapped with Python NumPy arrays.

recv_vector(dtype='')

Available dtype:

  • 'uint32': unsigned 32-bit integer
  • 'int32': signed 32-bit integer
  • 'float32': 32-bit floating point
  • 'float64': 64-bit double precision
  • 'bool': boolean

Example

C++ driver:

std:vector<float>& get_vector() {
    return my_vector;
}
};

Python driver:

import numpy

#...

@command()
def get_vector(self):
    return self.client.recv_vector(dtype='float32')

See also