In / Out

ADS1115

class crappy.inout.ads1115.Ads1115(backend: str, device_address: int = 72, i2c_port: int = 1, sample_rate: int = 128, v_range: float = 2.048, multiplexer: str = 'A1', dry_pin: Optional[Union[str, int]] = None, gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None)[source]

A class for controlling Adafruit’s ADS1115 16-bits ADC.

The Ads1115 InOut block is meant for reading output values from a 16-bits ADS1115 ADC, using the I2C protocol. The output is in Volts by default, but a gain and an offset can be specified.

__init__(backend: str, device_address: int = 72, i2c_port: int = 1, sample_rate: int = 128, v_range: float = 2.048, multiplexer: str = 'A1', dry_pin: Optional[Union[str, int]] = None, gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None) None[source]

Checks arguments validity.

Parameters
  • backend (str) –

    The backend for communicating with the ADS1115. Should be one of:

    'Pi4', 'ft232h', 'blinka'
    

    The ‘Pi4’ backend is optimized but only works on boards supporting the smbus2 module, like the Raspberry Pis. The ‘blinka’ backend may be less performant and requires installing Adafruit’s modules, but these modules are compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the ADS1115 from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.

  • device_address (int, optional) – The I2C address of the ADS1115. The default address is 0x48, but it is possible to change this setting using the ADDR pin.

  • i2c_port (int, optional) – The I2C port over which the ADS1115 should communicate. On most Raspberry Pi models the default I2C port is 1.

  • sample_rate (int, optional) –

    The sample rate for data conversion (in SPS). Available sample rates are:

    8, 16, 32, 64, 128, 250, 475, 860
    

  • v_range (float, optional) –

    The value (in Volts) of the measured signal corresponding to the 0x7FFF output in bits, i.e. that saturates the sensor. A signal of -v_range Volts gives a 0x8000 output in bits. Available v_range values are:

    0.256, 0.512, 1.024, 2.048, 4.096, 6.144
    

  • multiplexer (str, optional) –

    Choice of the inputs to consider. Single-input modes actually measure Ax - GND. The available multiplexer values are:

    'A0', 'A1', 'A2', 'A3',
    'A0 - A1',
    'A0 - A3',
    'A1 - A3',
    'A2 - A3'
    

  • dry_pin (int or str, optional) – Optionally, reads the end of conversion signal from a GPIO rather than from an I2C message. Speeds up the reading and decreases the traffic on the bus, but requires one extra wire. With the backend ‘Pi4’, give the index of the GPIO in BCM convention. With the ‘ft232h’ backend, give the name of the GPIO in the format Dx or Cx. This feature is not available with the ‘blinka’ backend.

  • gain (float, optional) –

    Allows to tune the output value according to the formula:

    output = gain * tension + offset.
    

  • offset (float, optional) –

    Allows to tune the output value according to the formula:

    output = gain * tension + offset.
    

  • ft232h_ser_num (str, optional) – If backend is ‘ft232h’, the serial number of the FT232H to use for communication.

Warning

AINx voltages should not be higher than VDD+0.3V nor lower than GND-0.3V. Setting high v_range values does not allow measuring voltages higher than VDD !!

close() None[source]

Closes the I2C bus

get_data() list[source]

Reads the registers containing the conversion result.

The output is in Volts, unless a gain and offset are applied.

Returns

list – A list containing the timeframe and the voltage value

open() None[source]

Sets the I2C communication and device.

Agilent 34420A

class crappy.inout.agilent34420A.Agilent34420a(mode: bytes = b'VOLT', device: str = '/dev/ttyUSB0', baudrate: int = 9600, timeout: float = 1)[source]

Sensor class for Agilent34420A devices.

This class contains method to measure values of resistance or voltage on Agilent34420A devices.

Note

May work for other devices too, but not tested.

If you have issues with this class returning a lot of ‘bad serial’, make sure you have the last version of serial.

__init__(mode: bytes = b'VOLT', device: str = '/dev/ttyUSB0', baudrate: int = 9600, timeout: float = 1) None[source]

Sets the args and initializes parent class.

Parameters
  • mode (bytes, optional) – Desired value to measure. Should be either b’VOLT’ or b’RES’.

  • device (str, optional) – Path to the device.

  • baudrate (int, optional) – Desired baudrate.

  • timeout (float, optional) – Timeout for the serial connection.

close() None[source]

Closes the serial port.

get_data() list[source]

Reads the signal, returns False if error and prints ‘bad serial’.

Comedi

class crappy.inout.comedi.Comedi(**kwargs)[source]

Comedi object, for IO with cards using comedi driver.

Note

The channel-specific args can be given as a list to set it for each channel or as a single value to apply it to every channel.

Parameters
  • (str (- device) – “/dev/comedi0”): The address of the device.

  • default – “/dev/comedi0”): The address of the device.

  • (int (- out_subdevice) – 0): The id of the subdevice.

  • default – 0): The id of the subdevice.

  • (list (- channels) – [0]): The list of the input channels.

  • default – [0]): The list of the input channels.

  • (list/int (- out_range_num) – 0): The range to use on each channel.

  • default – 0): The range to use on each channel.

  • (list/float (- out_offset) – 1): The return value for each chan will be multiplied by the gain.

  • default – 1): The return value for each chan will be multiplied by the gain.

  • (list/float – 0): The offset will be added to the return value for each chan.

  • default – 0): The offset will be added to the return value for each chan.

  • (list/bool (- make_zero) – True): If True, the value read at the beginning will be removed to the offset to take it as a reference.

  • default – True): If True, the value read at the beginning will be removed to the offset to take it as a reference.

  • (list – []): The list of the output channels.

  • default – []): The list of the output channels.

  • (list/int – 0): The range to use on each output.

  • default – 0): The range to use on each output.

  • (list/float – 1): The output value for each chan will be multiplied by the gain.

  • default – 1): The output value for each chan will be multiplied by the gain.

  • (list/float – 0): The offset will be added to the output value for each chan.

  • default – 0): The offset will be added to the output value for each chan.

  • (int – 1): The id of the output subdevice.

  • default – 1): The id of the output subdevice.

get_data(channel: str = 'all') list[source]

To read the value on input_channels.

Note

If channel is specified, it will only read and return these channels.

‘all’ (default) will read all opened channels.

open() None[source]

Starts communication with the device, must be called before any set_cmd or get_data.

Note

It reads channel properties from the device, those will be used in data_read/data_write.

set_cmd(*cmd: float) None[source]

To set the value of the outputs (when specified). Takes as many argument as opened output channels.

DaQMX

class crappy.inout.daqmx.Daqmx(device: str = 'Dev1', channels: Optional[Union[list, str]] = None, gain: Union[list, float] = 1, offset: Union[list, float] = 0, range: Union[float, list] = 5, make_zero: Union[list, bool] = True, nperscan: int = 1000, sample_rate: float = 10000, out_channels: Optional[Union[list, str]] = None, out_gain: Union[list, float] = 1, out_offset: Union[list, float] = 0, out_range: Union[list, float] = 5)[source]

Class to use DAQmx devices.

__init__(device: str = 'Dev1', channels: Optional[Union[list, str]] = None, gain: Union[list, float] = 1, offset: Union[list, float] = 0, range: Union[float, list] = 5, make_zero: Union[list, bool] = True, nperscan: int = 1000, sample_rate: float = 10000, out_channels: Optional[Union[list, str]] = None, out_gain: Union[list, float] = 1, out_offset: Union[list, float] = 0, out_range: Union[list, float] = 5) None[source]

Sets the args and initializes the parent class.

Parameters
  • device (str, optional) – Name of the device to open.

  • channels (list, optional) – Names or ids of the channels to read.

  • gain (list, optional) – Gains to apply to each reading.

  • offset (list, optional) – Offset to apply to each reading.

  • range (list, optional) –

    Max value for the reading. Should be a list of float in:

    0.5, 1., 2.5, 5.
    

    Refer to the niDAQ api for more details.

  • make_zero (list, optional) – If True, the average value on the channel at opening will be evaluated and subtracted to the actual reading.

  • nperscan (int, optional) – If using streamer mode, number of readings to acquire on each get_stream() call.

  • sample_rate (float, optional) – If using streamer mode, frequency of acquisition when calling get_stream().

  • out_channels (list, optional) – Names or ids of the output channels.

  • out_gain (list, optional) – Gains to apply to the commands.

  • out_offset (list, optional) – Offset to apply to the commands.

  • out_range (list, optional) –

    Max value of the output. Should be a list of float in:

    0.5, 1., 2.5, 5.
    

    Refer to the niDAQ api for more details.

Note

If an argument supposed to be a list is given as a single value, this value will be applied to all channels.

check_vars() None[source]

Turns the settings into list of the same length, each index standing for one channel.

Note

If a list is given, simply checks the length. Else make a list of the correct length containing only the given value.

close() None[source]

Closes the connection.

get_data() list[source]

Returns a tuple of length len(self.channels) + 1.

First element is the time, others are readings of each channel.

get_single(npoints: Optional[int] = None) list[source]

Reads the analog voltage on specified channels.

Parameters
  • npoints – Number of values to read. If None, will use the value of

  • self.nperscan.

Returns

A tuple of len(self.channels) + 1 list of length npoints. First list is the time, the others are the read voltages.

set_cmd(*args: float) None[source]

Set the output(s) to the specified value.

Note

Takes n arguments, n being the number of channels opened at __init__(). The ith argument is the value to set to the ith channel.

crappy.inout.daqmx.get_daqmx_devices_names() list[source]

Get all connected daqmx devices.

Returns

A list of all connected daqmx devices.

Fake Inout

class crappy.inout.fake_inout.Fake_inout[source]

A class demonstrating the usage of an inout abject without requiring any hardware.

It can read and/or modify the current memory usage on the computer.

__init__() None[source]

Not much to do here.

close() None[source]

Deletes the buffer.

static get_data() list[source]

Just returns time and the current memory usage.

open() None[source]

Creates the buffer allowing to modify the memory usage.

set_cmd(*cmd: float) None[source]

Modifies the computer memory usage.

If the command is lower than the current, empties the buffer. If the command is higher than the current, adds big lists to the buffer in order to use more memory.

Parameters

*cmd (float) – The target memory usage to set.

GPIO PWM

class crappy.inout.gpio_pwm.Gpio_pwm(pin_out: int, duty_cycle: Optional[float] = None, frequency: Optional[float] = None)[source]

Class for performing PWM on a Raspberry Pi.

The Gpio_pwn InOut block is meant for controlling a PWM output from a Raspberry Pi GPIO. It allows to control the duty cycle, the frequency, or both at the same time. When controlling both, the duty cycle should be first and the frequency second in the list of inputs.

Warning

Only works on Raspberry Pi !

__init__(pin_out: int, duty_cycle: Optional[float] = None, frequency: Optional[float] = None) None[source]

Checks the arguments validity.

Parameters
  • pin_out (int) – The GPIO pin to be controlled (BCM convention).

  • duty_cycle (float) – If provided, sets a fixed duty cycle for the entire assay. Only the frequency can then be tuned. If not provided, the block will expect the duty cycle values to be given as the first input. It will also start the PWM with a duty cycle of 0% before the first value is received and set.

  • frequency (float) – If provided, sets a fixed PWM frequency for the entire assay. Only the duty cycle can then be tuned. If not provided, the block will expect the frequency values to be given as the first input if the duty_cycle argument is provided, or else as the second input. It will also start the PWM with a frequency of 10kHz before the first value is received and set.

Note

  • duty_cycle: The duty cycle inputs are clamped between 0 and 100.

  • frequency: The frequency inputs are clamped between 10Hz and 1MhZ. Sending other values to the bloc doesn’t raise any error, but the assay may not run as expected.

  • Hardware PWM pins: On the Raspberry Pi 4, only the GPIO pins 12, 13, 18 and 19 support hardware PWM. Trying to get a PWM output from other pins might work but may decrease the available frequency range.

close() None[source]

Stops PWM and releases GPIOs.

open() None[source]

Sets the GPIOs and starts the PWM.

set_cmd(*cmd: float) None[source]

Modifies the PWM frequency and/or duty cycle.

Parameters

*cmd (float) – Values of duty cycle and/or frequency to set

GPIO switch

class crappy.inout.gpio_switch.Gpio_switch(pin_out: int)[source]

Class for setting a GPIO high or low.

The Gpio_switch InOut block is meant for switching a Raspberry Pi GPIO high or low according to the input signal value. When the input signal is 1 the GPIO is turned high, when the signal is 0 it is turned low. Any value other than 0 and 1 raises an error.

Warning

Only works on Raspberry Pi !

__init__(pin_out: int) None[source]

Checks the argument validity.

Parameters

pin_out (int) – The GPIO pin to be controlled (BCM convention).

static close() None[source]

Releases the GPIO.

open() None[source]

Sets the GPIO.

set_cmd(*cmd: int) None[source]

Drives the GPIO according to the command.

Parameters

cmd (int) – 1 for driving the GPIO high, 0 for driving it low

GSM

class crappy.inout.gsm.Gsm(numbers: list, port: str = '/dev/ttyUSB0', baudrate: int = 115200)[source]

Block for sending messages by SMS to given phone numbers.

Important

This block should be associated with a modifier to manage the messages to send.

__init__(numbers: list, port: str = '/dev/ttyUSB0', baudrate: int = 115200) None[source]

Checks arguments validity.

Parameters
  • numbers (list) –

    The list of numbers the messages will be sent to. The syntax is the following :

    ["0611223344"]
    

  • port (str, optional) – Serial port the GSM is connected to.

  • baudrate (int, optional) – Serial baudrate, between 1200 and 115200.

close() None[source]

Closes the serial port.

open() None[source]

Sends "AT" to the GSM and waits for the response : "OK".

set_cmd(*cmd: str) None[source]

Sends an SMS whose text is the str received as command to all the phone numbers.

Doesn’t send anything if the string is empty, and raises a TypeError if the command is not a str.

InOut

class crappy.inout.inout.MetaIO(name: str, bases: tuple, dict_: dict)[source]

Metaclass that will define all IO objects.

All IO classes should be of this type.

To do so, simply add __metaclass__ = MetaIO in the class definition. (Obviously, you must import this Metaclass first.)

MetaIO is a MetaClass: we will NEVER do c = MetaIO(...).

The __init__() is used to init the classes of type MetaIO (with __metaclass__ = MetaIO as a class attribute) and NOT an instance of MetaClass.

Kollmorgen

class crappy.inout.kollmorgen.Koll(data: str = 'position', axis: Union[str, int] = 'all', speed: float = 360, acc: float = 3600, decc: float = 3600, labels: Optional[list] = None, host: str = '192.168.0.109', port: int = 502)[source]

Class to communicate to Kollmorgen devices via Crappy.

class crappy.inout.kollmorgen.KollMorgenVariator(host: str = '192.168.0.109', port: int = 502)[source]

Main class to test communication with kollmorgen variator.

Every variable and its address has been defined in the Kollmorgen Integrated Suite.

To add or remove some, update the dictionaries of __init__().

There are 3 motors, so the tens are for each motor, the units for the address.

__init__(host: str = '192.168.0.109', port: int = 502) None[source]

Sets the variator and defines the bits.

Parameters
  • host (str, optional) – Variator’s IP address.

  • port (int, optional) – Port for modbus communication

clear_errors() None[source]

If errors occurred, it must be clear in order to continue the program.

read_position(motor: Union[str, int]) list[source]

To read position of motor. Returns a float.

read_speed(motor: Union[str, int]) list[source]

Reads speed of each motor.

set_accelerations(motor: int, **kwargs) None[source]

To set acceleration, deceleration (for positioning) and fast deceleration (boolean stop).

set_position(motor: int, position: float) None[source]

To set a position (in degrees), absolute value.

set_rotation(motor: int, rotation: float) None[source]

To set a rotation (in degrees) of the motor axis. Rotation is signed.

set_speed(motor: int, speed: float) None[source]

Writes to variator desired speed (signed), and its direction. Applies to every motor movement (rotations, positioning…).

start_rotation(motor: int) None[source]

Sets the rotation of specified motor at specified speed (signed).

stop(motor: int) None[source]

Stops the motor movement.

toggle_power(motor: int) None[source]

Toggles power of given motor. Maybe not the most intelligent way to handle power, though…

Labjack T7

class crappy.inout.labjackT7.Labjack_t7(device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', channels: Optional[list] = None, write_at_open: Optional[list] = None, no_led: bool = False)[source]

Class for LabJack T7 devices.

It can use any channel as input/output, it can be used with an IOBlock.

This class is NOT capable of streaming. For higher frequency, see T7 Streamer class.

The keyword argument channels is used to specify the channels.

Each channel must be represented as a dict including all the parameters. See below for more details on the parameters of the channels.

__getitem__(chan: str) tuple[source]

Allows reading of an input chan by calling lj[chan].

__init__(device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', channels: Optional[list] = None, write_at_open: Optional[list] = None, no_led: bool = False) None[source]

Sets the args and initializes the parent class.

Parameters
  • device (str, optional) –

    The type of the device to open. Possible values include:

    'ANY', 'T7', 'T4', 'DIGIT'
    

    Only tested with ‘T7’ in Crappy.

  • connection (str, optional) –

    How is the Labjack connected ? Possible values include:

    'ANY', 'TCP', 'USB', 'ETHERNET', 'WIFI'
    

  • identifier (str, optional) – Something to identify the Labjack. It can be a serial number, an IP address, or a device name.

  • channels (list, optional) – Channels to use and their settings. It must be a list of dict.

  • write_at_open (list, optional) – If you need to write specific names or registers when opening the channel, you can give them as a list of tuple. They will be written in the order of the list.

  • no_led (list, optional) – If True, turns off the LED on the Labjack. This led can cause noise on the channels AIN0 and AIN1.

Note

  • channels keys:

    • name (str): The name of the channel according to Labjack’s naming convention. Ex: ‘AIN0’. This will be used to define the direction (in/out) and the available settings.

      It can be:
      • AINx: An analog input, if gain and/or offset is given, the integrated slope mechanism will be used with the extended features registers. It can also be used for thermocouples (see below). You can use any EF by using the write_at_open and to_read keys if necessary.

      • (T)DACx: An analog output, you can specify gain and/or offset.

      • (E/F/C/M IOx): Digital in/outputs. You can specify the direction.

    • gain (float, default: 1): A numeric value that will multiply the given value for inputs and outputs.

    • offset (float, default: 0): Will be added to the value.

      For inputs:

      returned_value = gain * measured_value + offset
      

      For outputs:

      set_value = gain * given_value + offset.
      

      Where measured_value and set_values are in Volts.

    • make_zero (bool): AIN only, if True the input value will be evaluated at startup and the offset will be adjusted to return 0 (or the offset if any).

    • direction (bool): DIO only, if True (or 1), the port will be used as an output else as an input.

    • resolution (int, default: 1): The resolution of the acquisition, see Labjack documentation for more details. The bigger this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 according to the model.

    • range (float, default: 10): The range of the acquisition in Volts. A range of x means that values can be read between -x and x Volts. The possible values are:

      0.01, 0.1, 1, 10
      
    • limits (tuple, default: None): To clamp the output values to a given range. The tuple should contain two values: the min and the max limit.

    • thermocouple (str): The type of thermocouple (AIN only). Possible values are:

      'E', 'J', 'K', 'R', 'T', 'S', 'C'
      

      If specified, it will use the EF to read a temperature directly from the thermocouples.

    • write_at_open (list): If you need to write specific names or registers when opening the channel, you can give them as a list of tuple. They will be written in the order of the list.

      The tuples can either be (name (str), value (int/float)) or (register (int), type (int), value (float/int)).

Warning

DO NOT CONSIDER the limits KEY AS A SAFETY IMPLEMENTATION. It should not go beyond/below the given values, but this is not meant to replace hardware safety!

__setitem__(chan: str, val: float) None[source]

Allows setting of an output chan by calling lj[chan] = val.

close() None[source]

Closes the device.

get_data() list[source]

Read the signal on all pre-defined input channels.

read(address: int, dtype: Optional[str] = None)[source]

To read data directly from a register.

set_cmd(*cmd: float) None[source]

Converts the tension value to a digital value and send it to the output.

Note

Once a value has been written, it will not be written again until it changes! This is meant to lower the communication and Labjack activity.

It relies on the fact that these registers will not change between writes (Which is true unless the card runs a lua script writing the same registers as the user).

write(value: float, address: int, dtype: Optional[str] = None) None[source]

To write data directly into a register.

Labjack UE9

class crappy.inout.labjackUE9.Labjack_ue9(channels: Union[int, list] = 0, gain: Union[float, list] = 1, offset: Union[float, list] = 0, make_zero: Union[bool, list] = True, resolution: Union[int, list] = 12)[source]

Can read data from a LabJack UE9.

Important

Streamer mode and DAC are not supported yet.

crappy.inout.labjackUE9.format_lists(list_to_format: Union[list, float, int, bool], length: int) list[source]

In case the user only specifies one parameter, and wants it applied to all inputs.

crappy.inout.labjackUE9.get_channel_number(channels: list) None[source]

Register needs to be called with the channel name as int.

MCP9600

class crappy.inout.mcp9600.Mcp9600(backend: str, thermocouple_type: str, i2c_port: int = 1, device_address: int = 103, adc_resolution: int = 18, sensor_resolution: float = 0.0625, filter_coefficient: int = 0, mode: str = 'Hot Junction Temperature', ft232h_ser_num: Optional[str] = None)[source]

Class for controlling Adafruit’s MCP9600 thermocouple reader.

The Mcp9600 InOut block is meant for reading temperature from an MCP9600 board, using the I2C protocol. The output is in °C, except for one operating mode that returns Volts.

__init__(backend: str, thermocouple_type: str, i2c_port: int = 1, device_address: int = 103, adc_resolution: int = 18, sensor_resolution: float = 0.0625, filter_coefficient: int = 0, mode: str = 'Hot Junction Temperature', ft232h_ser_num: Optional[str] = None) None[source]

Checks arguments validity.

Parameters
  • backend (str) –

    The backend for communicating with the MCP9600. Should be one of:

    'Pi4', 'ft232h', 'blinka'
    

    The ‘Pi4’ backend is optimized but only works on boards supporting the smbus2 module, like the Raspberry Pis. The ‘blinka’ backend may be less performant and requires installing Adafruit’s modules, but these modules are compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the ADS1115 from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.

  • thermocouple_type (str) –

    The type of thermocouple plugged in the MCP9600. The available types are:

    'J', 'K', 'T', 'N', 'S', 'E', 'B', 'R'
    

  • i2c_port (int, optional) – The I2C port over which the MCP9600 should communicate. On most Raspberry Pi models the default I2C port is 1.

  • device_address (int, optional) – The I2C address of the MCP9600. The default address is 0x67, but it is possible to change this setting using a specific setup involving the ADDR pin.

  • adc_resolution (int, optional) –

    The number of bits the ADC output is encoded on. The greater the resolution, the lower the sample rate. The available resolutions are:

    12, 14, 16, 18
    

  • sensor_resolution (float, optional) – The temperature measurement resolution in °C. It should be either 0.0625 or 0.25. Setting the resolution to 0.25 will increase the sample rate, but the output temperature will be encoded on two bits less.

  • filter_coefficient (int, optional) – The MCP9600 features an integrated filter (see its documentation for the exact filter formula). When set to 0, the filter is inactive. It is maximal when set to 7. When active, the filter will prohibit fast temperature changes, thus limiting noise and smoothening the signal.

  • mode (str, optional) –

    Four different values can be accessed when measuring a temperature: the temperature of the thermocouple (hot junction temperature), the temperature of the MCP9600 board (cold junction temperature), the temperature calculated from the ADC data and thermocouple type but not yet cold junction-compensated (junction temperature delta), and the raw ADC measurement of the voltage difference in the thermocouple (raw data ADC, in Volts). The available modes are thus:

    'Hot Junction Temperature',
    'Junction Temperature Delta',
    'Cold Junction Temperature',
    'Raw Data ADC'
    

  • ft232h_ser_num (str, optional) – If backend is ‘ft232h’, the serial number of the ft232h to use for communication.

close() None[source]

Switches the MCP9600 to shutdown mode and closes the I2C bus..

get_data() list[source]

Reads the registers containing the conversion result.

The output is in °C for all modes except the raw data ADC one, which outputs Volts.

Returns

list – A list containing the timeframe and the output value

open() None[source]

Sets the I2C communication and device.

MPRLS

class crappy.inout.mprls.Mprls(backend: str, eoc_pin: Optional[Union[str, int]] = None, device_address: int = 24, i2c_port: int = 1, ft232h_ser_num: Optional[str] = None)[source]

The Mprls inout is meant for reading pressure from Adafruit’s Mprls pressure sensor.

It communicates over I2C with the sensor.

__init__(backend: str, eoc_pin: Optional[Union[str, int]] = None, device_address: int = 24, i2c_port: int = 1, ft232h_ser_num: Optional[str] = None) None[source]

Initializes the parent class and opens the I2C bus.

Parameters
  • backend (str) –

    Should be one of :

    'Pi4', 'blinka', 'ft232h'
    

    The ‘Pi4’ backend is optimized but only works on boards supporting the smbus2 module, like the Raspberry Pis. The ‘blinka’ backend may be less performant and requires installing Adafruit’s modules, but these modules are compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the MPRLS from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.

  • eoc_pin (int or str, optional) – Optionally, reads the end of conversion signal from a GPIO rather than from an I2C message. Speeds up the reading and decreases the traffic on the bus, but requires one extra wire. With the backend ‘Pi4’, give the index of the GPIO in BCM convention. With the ‘ft232h’ backend, give the name of the GPIO in the format Dx or Cx. With the backend ‘blinka’, it should be a string but the syntax varies according to the board. Refer to blinka’s documentation for more information.

  • device_address (int, optional) – The I2C address of the MPRLS. The address of the devices sold by Adafruit is 0x18, but other suppliers may sell it with another address.

  • i2c_port (int, optional) – The I2C port over which the MPRLS should communicate. On most Raspberry Pi models the default I2C port is 1.

  • ft232h_ser_num (str, optional) – If backend is ‘ft232h’, the serial number of the FT232H to use for communication.

close()[source]

Closes the I2C bus.

get_data() list[source]

Reads the pressure value.

Returns

The timestamp and the pressure value in hPa.

open() None[source]

Opens the I2C bus.

NAU7802

class crappy.inout.nau7802.Nau7802(backend: str, i2c_port: int = 1, device_address: int = 42, gain_hardware: int = 128, sample_rate: int = 80, int_pin: Optional[Union[str, int]] = None, gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None)[source]

Class for controlling Sparkfun’s NAU7802 load cell conditioner.

The Nau7802 InOut block is meant for reading output values from a NAU7802 load cell conditioner, using the I2C protocol. The output is in Volts by default, but can be converted to Newtons using gain and offset.

__init__(backend: str, i2c_port: int = 1, device_address: int = 42, gain_hardware: int = 128, sample_rate: int = 80, int_pin: Optional[Union[str, int]] = None, gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None) None[source]

Checks the validity of the arguments..

Parameters
  • backend (str) –

    The backend for communicating with the NAU7802. Should be one of:

    'Pi4', 'ft232h'
    

    to use the device from a Raspberry Pi or from a PC through an FT232H USB to I2C converter, respectively. See Crappy for embedded hardware for details.

  • i2c_port (int, optional) – The I2C port over which the NAU7802 should communicate. On most Raspberry Pi models the default I2C port is 1.

  • device_address (int, optional) – The I2C address of the NAU7802. It is impossible to change this address, so it is not possible to have several NAU7802 on the same i2c bus.

  • gain_hardware (int, optional) –

    The gain to be used by the programmable gain amplifier. Setting a high gain allows reading small voltages with a better precision, but it might saturate the sensor for higher voltages. Available gains are:

    1, 2, 4, 8, 16, 32, 64, 128
    

  • sample_rate (int, optional) –

    The sample rate for data conversion. The higher the rate, the greater the noise. Available sample rates are:

    10, 20, 40, 80, 320
    

  • int_pin (int or str, optional) – Optionally, reads the end of conversion signal from a GPIO rather than from an I2C message. Speeds up the reading and decreases the traffic on the bus, but requires one extra wire. With the backend ‘Pi4’, give the index of the GPIO in BCM convention. With the ‘ft232h’ backend, give the name of the GPIO in the format Dx or Cx.

  • gain (float, optional) –

    Allows to tune the output value according to the formula:

    output = gain * tension + offset.
    

  • offset (float, optional) –

    Allows to tune the output value according to the formula:

    output = gain * tension + offset.
    

  • ft232h_ser_num (str, optional) – If backend is ‘ft232h’, the serial number of the ft232h to use for communication.

close() None[source]

Powers down the device.

get_data() list[source]

Reads the registers containing the conversion result.

The output is in Volts by default, and can be converted to Newtons using gain and offset.

Returns

list – A list containing the timeframe and the output value

open() None[source]

Sets the I2C communication and device.

NI DaQMX

class crappy.inout.ni_daqmx.Nidaqmx(channels: Optional[list] = None, samplerate: float = 100, nsamples: Optional[int] = None)[source]

Opens National Instrument devices using the NiDAQmx driver (Windows only).

This class can open ai, ao and dio channels on NI devices. It supports continuous streaming on ai channels only. Streaming is not supported with ai channels of different types.

It uses the nidaqmx python module by NI.

__init__(channels: Optional[list] = None, samplerate: float = 100, nsamples: Optional[int] = None) None[source]

Builds the different channels lists.

Parameters
  • channels (list, optional) –

    A list of dict describing all the channels to open. See the note below for details.

    Note

    For dio, use DevX/d[i/o] Y to select port(Y//8)/line(Y%8) on DevX.

  • samplerate (float, optional) – If using stream mode, the samplerate of the stream.

  • nsamples (int, optional) – If using stream mode, the stream array will be returned after reading nsamples samples. Defaults to samplerate // 5.

Note

  • channels keys:

    • name (str): The name of the channel to open. For dio, use diX for digital input on line X and doX for digital output.

    • type (str, default: ‘voltage’): The type of channel to open. Ex: ‘thrmcpl’.

    • All the other args will be given as kwargs to nidaqmx.Task.add_[a/d][i/o]_[type]_chan.

Open DaQ

class crappy.inout.open_daq.Opendaq(channels: Union[int, list] = 1, port: str = '/dev/ttyUSB0', gain: Union[float, int] = 1, offset: Union[float, list] = 0, cmd_label: str = 'cmd', out_gain: float = 1, out_offset: float = 0, make_zero: Union[bool, list] = True, mode: str = 'single', nsamples: int = 20)[source]

Can read data from an OpenDAQ card.

Opsens

class crappy.inout.opsens.Opsens(device: str = '/dev/ttyUSB0')[source]

Sensor class for opsens conditioner.

PiJuice

class crappy.inout.piJuice.Pijuice(i2c_port: int = 1, address: int = 20, backend: str = 'Pi4')[source]

Block getting various information about a piJuice power platform, including the charge level and the power supply status.

Warning

Only available on Raspberry Pi !

__init__(i2c_port: int = 1, address: int = 20, backend: str = 'Pi4') None[source]

Checks arguments validity.

Parameters
  • backend (str, optional) –

    Should be one of :

    'Pi4', 'pijuice'
    

    The ‘Pi4’ backend is based on the smbus2 module, while the ‘pijuice’ backend is based on the pijuice.

  • i2c_port (int, optional) – The I2C port over which the PiJuice should communicate.

  • address (int, optional) – The I2C address of the piJuice. The default address is 0x14.

close() None[source]

Closes the I2C bus.

get_data() dict[source]

Reads all the available information on the battery status.

Returns

dict

Returns a dict containing:

  • the timeframe in seconds as a float in label t(s)

  • the battery status as a str in label battery_status

  • the USB status as a str in label USB_status

  • the GPIO status as a str in label GPIO_status

  • the charge level as an int in label charge_level

  • the battery temperature in °C as an int in label battery_temperature

  • the battery voltage in mV as an int in label battery_voltage

  • the battery current in mA as an int in label battery_current

  • the GPIO voltage in mV as an int in label GPIO_voltage

  • the GPIO current in mA as an int in label GPIO_current

open() None[source]

Opens the I2C port.

Spectrum

class crappy.inout.spectrum.Spectrum(device: bytes = b'/dev/spcm0', channels: Optional[list] = None, ranges: Optional[list] = None, samplerate: int = 100000, buff_size: int = 67108864, notify_size: int = 65536, split_chan: bool = False)[source]

Acquire data from a Spectrum device.

__init__(device: bytes = b'/dev/spcm0', channels: Optional[list] = None, ranges: Optional[list] = None, samplerate: int = 100000, buff_size: int = 67108864, notify_size: int = 65536, split_chan: bool = False) None[source]

Sets the args and the instance attributes.

Parameters
  • (str (device) – “/dev/spcm0”): The address of the device to use.

  • default – “/dev/spcm0”): The address of the device to use.

  • (list (ranges) – [0]): The channels to open. See doc for the allowed combinations!

  • default – [0]): The channels to open. See doc for the allowed combinations!

  • (list – [10000]): The ranges of the channels in mV.

  • default – [10000]): The ranges of the channels in mV.

  • (int (notify_size) – 100000): The samplerate for all channels in Hz.

  • default – 100000): The samplerate for all channels in Hz.

  • (int – 2**16 (64MB)): The size of the memory allocated as a rolling buffer to copy the data from the card

  • default – 2**16 (64MB)): The size of the memory allocated as a rolling buffer to copy the data from the card

  • (int – 2**16 (64kB)): The size of the chunks of data to copy from the card.

  • default – 2**16 (64kB)): The size of the chunks of data to copy from the card.

  • split_chan – split_chan (bool, default: False): If False, it will return a single 2D array, else each chan will be a 1D array.

exception crappy.inout.spectrum.SpectrumError[source]

T7 Streamer

class crappy.inout.t7Streamer.T7_streamer(device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', channels: Optional[list] = None, scan_rate: int = 100000, scan_per_read: int = 10000, resolution: int = 1)[source]

Class to use stream mode with Labjack T7 devices.

Note

For single modes, see Labjack T7.

You can use IOBlock with streamer=True to read data at high frequency from the Labjack. Streamer mode makes the Labjack unavailable for any other operation (single acquisition, DAC or DIO).

You can specify each channel as a dict, allowing to set channel-specific settings such as gain, offset (computed on the host machine as this feature is not available on board with streamer mode), range and ability to zero the reading at startup.

__init__(device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', channels: Optional[list] = None, scan_rate: int = 100000, scan_per_read: int = 10000, resolution: int = 1) None[source]

Sets the args and initializes the parent class.

Parameters
  • device (str, optional) –

    The type of the device to open. Possible values include:

    'ANY', 'T7', 'T4', 'DIGIT'
    

    Only tested with ‘T7’ in Crappy.

  • connection (str, optional) –

    How is the Labjack connected ? Possible values include:

    'ANY', 'TCP', 'USB', 'ETHERNET', 'WIFI'
    

  • identifier (str, optional) – Something to identify the Labjack. It can be a serial number, an IP address, or a device name.

  • channels (list, optional) – Channels to use and their settings. It must be a list of dict.

  • scan_rate (int, optional) – The acquisition frequency in Hz for the channels. Note that the sample rate (scan_rate * num of chan) cannot exceed 100000. If it is too high it will be lowered to the highest possible value.

  • scan_per_read (int, optional) – The number of points to read during each loop.

  • resolution (int, optional) – The resolution index for all channels. The bigger this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 according to the model.

Note

  • channels keys:

    • name (str): The name of the channel according to Labjack’s naming convention. Ex: ‘AIN0’. This will be used to define the direction (in/out) and the available settings.

      It can be:
      • AINx: An analog input, if gain and/or offset is given, the integrated slope mechanism will be used with the extended features registers. It can also be used for thermocouples (see below). You can use any EF by using the write_at_open and to_read keys if necessary.

      • (T)DACx: An analog output, you can specify gain and/or offset.

      • (E/F/C/M IOx): Digital in/outputs. You can specify the direction.

    • gain (float, default: 1): A numeric value that will multiply the given value for inputs and outputs.

    • offset (float, default: 0): Will be added to the value.

      For inputs:

      returned_value = gain * measured_value + offset
      

      For outputs:

      set_value = gain * given_value + offset.
      

      Where measured_value and set_values are in Volts.

    • make_zero (bool): AIN only, if True the input value will be evaluated at startup and the offset will be adjusted to return 0 (or the offset if any).

    • range (float, default: 10): The range of the acquisition in Volts. A range of x means that values can be read between -x and x Volts. The possible values are:

      0.01, 0.1, 1, 10
      
get_data() None[source]

Short version, only used for InOut.eval_offset().

Waveshare AD/DA

class crappy.inout.waveshare_ad_da.Waveshare_ad_da(dac_channels: Optional[List[str]] = None, adc_channels: Optional[List[str]] = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: Union[int, float] = 100)[source]

Class for controlling Waveshare’s AD/DA hat.

The Waveshare_ad_da InOut block is meant for communicating with Waveshare’s AD/DA Raspberry Pi hat, using the SPI protocol and the GPIOs. It allows to read values from the 8-channels ADC and/or to set the 2-channels DAC.

Warning

This class is specifically meant to be used on a Raspberry Pi. See Waveshare AD/DA FT232H for use with FTDI’s FT232H.

__init__(dac_channels: Optional[List[str]] = None, adc_channels: Optional[List[str]] = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: Union[int, float] = 100) None[source]

Checks the validity of the arguments.

Parameters
  • dac_channels (list, optional) – A list of str representing the channels to be set. The syntax for each string is ‘DACi’ with i being either 0 or 1.

  • adc_channels (list, optional) –

    A list of str representing the channels to read. The syntax for all strings is either:

    'ADi' (i in range(8))
    

    or else:

    'ADi - ADj' (i, j in range(8))
    

  • gain_hardware (int, optional) –

    The gain to be used by the programmable gain amplifier. Setting a high gain allows to read small voltages with a better precision, but it might saturate the sensor for higher voltages. The available gain values are:

    1, 2, 4, 8, 16, 32, 64
    

  • v_ref (float, optional) – The voltage reference set by the VREF jumper. When reading single inputs, v_ref is the value the ADC compares the signals with. In a similar way, the maximum output voltage of the DAC is v_ref. 3.3 and 5 are the only possible values for this setting, as the Raspberry Pi can only provide 3.3V and 5V.

  • gain (float, optional) –

    Allows to tune the output values of the DAC according to the formula:

    output = gain * tension + offset.
    

    The same gain applies to all the outputs.

  • offset (float, optional) –

    Allows to tune the output values of the ADC according to the formula:

    output = gain * tension + offset.
    

    The same offset applies to all the outputs.

  • sample_rate (optional) –

    The ADC data output rate in SPS. The available values are:

    2.5, 5, 10, 15, 25, 30, 50, 60, 100, 500,
    1000, 2000, 3750, 7500, 15000, 30000
    

Warning

  • adc_channels: For reading single inputs the JMP_AGND jumper should normally be connected, whereas it should be disconnected for reading differential inputs. It is however possible to set a different reference than AGND for single input measurements, in which case the JMP_AGND jumper should not be connected and the voltage reference should be plugged in the AINCOM pin.

    The AD/DA offers the possibility to read single inputs or differential inputs, but not both at the same time ! This is due to the JMP_AGND jumper. For measuring both input types simultaneously, is it necessary to connect AGND to one of the channels (for example AD0). Then all single inputs ‘ADi’ should be replaced by ‘ADi - AD0’. They are then considered as differential inputs.

    The ADC channels voltages should not be lower than AGND-0.1V, and not be greater than AGND+5.1V. This is independent of VREF value.

Note

  • adc_channels: If multiple channels to read are given, they are read in a sequential way. This means that there’s a small delay between each acquisition, and the timeframe is thus less accurate for the last channels than for the first ones. If time precision matters it is preferable to read as few channels as possible !

  • vref: VREF can be set independently of the chosen VCC value. The VCC value has no influence on the ADC behaviour as it is always powered up with 5V. Same goes for the DAC.

close() None[source]

Releases the GPIOs.

get_data() list[source]

Reads data from all the user-specified ADC channels, in a sequential way.

Data is returned in Volts, but this can be tuned using gain and offset.

Returns

list – A list containing the timeframe, and then the values for each channel to read

open() None[source]

Sets the SPI communication, the GPIOs and the device.

set_cmd(*cmd: float) None[source]

Sets the user-specified DAC channels according to the input values.

Parameters

cmd (float) – The input values, in Volts

Waveshare AD/DA FT232H

class crappy.inout.waveshare_ad_da_ft232h.Waveshare_ad_da_ft232h(dac_channels: Optional[List[str]] = None, adc_channels: Optional[List[str]] = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: Union[int, float] = 100, ft232h_ser_num: Optional[str] = None, rst_pin_ads: str = 'D7', cs_pin_ads: str = 'D4', drdy_pin_ads: str = 'D6', cs_pin_dac: str = 'D5')[source]

Class for controlling Waveshare’s AD/DA hat from an FTDI FT232H.

The Waveshare_ad_da InOut block is meant for communicating with Waveshare’s AD/DA Raspberry Pi hat from an FT232H, using the SPI protocol and the GPIOs. It allows reading values from the 8-channels ADC and/or to set the 2-channels DAC.

Warning

This class is specifically meant to be used with an FT232H. See Waveshare AD/DA for use with a Raspberry Pi.

__init__(dac_channels: Optional[List[str]] = None, adc_channels: Optional[List[str]] = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: Union[int, float] = 100, ft232h_ser_num: Optional[str] = None, rst_pin_ads: str = 'D7', cs_pin_ads: str = 'D4', drdy_pin_ads: str = 'D6', cs_pin_dac: str = 'D5') None[source]

Checks the validity of the arguments.

Parameters
  • dac_channels (list, optional) – A list of str representing the channels to be set. The syntax for each string is ‘DACi’ with i being either 0 or 1.

  • adc_channels (list, optional) –

    A list of str representing the channels to read. The syntax for all strings is either:

    'ADi' (i in range(8))
    

    or else:

    'ADi - ADj' (i, j in range(8))
    

  • gain_hardware (int, optional) –

    The gain to be used by the programmable gain amplifier. Setting a high gain allows reading small voltages with a better precision, but it might saturate the sensor for higher voltages. The available gain values are:

    1, 2, 4, 8, 16, 32, 64
    

  • v_ref (float, optional) – The voltage reference set by the VREF jumper. When reading single inputs, v_ref is the value the ADC compares the signals with. In a similar way, the maximum output voltage of the DAC is v_ref. 3.3 and 5 are the only possible values for this setting, as the Raspberry Pi can only provide 3.3V and 5V.

  • gain (float, optional) –

    Allows to tune the output values of the DAC according to the formula:

    output = gain * tension + offset.
    

    The same gain applies to all the outputs.

  • offset (float, optional) –

    Allows to tune the output values of the ADC according to the formula:

    output = gain * tension + offset.
    

    The same offset applies to all the outputs.

  • sample_rate (optional) –

    The ADC data output rate in SPS. The available values are:

    2.5, 5, 10, 15, 25, 30, 50, 60, 100, 500,
    1000, 2000, 3750, 7500, 15000, 30000
    

  • ft232h_ser_num (str, optional) – The serial number of the ft232h to use for communication.

  • rst_pin_ads (str, optional) – The pin for resetting the ADS1256.

  • cs_pin_ads (str, optional) – The chip select pin for the ADS1256.

  • drdy_pin_ads (str, optional) – The pin for knowing when a conversion result in ready.

  • cs_pin_dac (str, optional) – The chip select pin for the DAC8552.

Warning

  • adc_channels: For reading single inputs the JMP_AGND jumper should normally be connected, whereas it should be disconnected for reading differential inputs. It is however possible to set a different reference than AGND for single input measurements, in which case the JMP_AGND jumper should not be connected and the voltage reference should be plugged in the AINCOM pin.

    The AD/DA offers the possibility to read single inputs or differential inputs, but not both at the same time ! This is due to the JMP_AGND jumper. For measuring both input types simultaneously, is it necessary to connect AGND to one of the channels (for example AD0). Then all single inputs ‘ADi’ should be replaced by ‘ADi - AD0’. They are then considered as differential inputs.

    The ADC channels voltages should not be lower than AGND-0.1V, and not be greater than AGND+5.1V. This is independent of VREF value.

Note

  • adc_channels: If multiple channels to read are given, they are read in a sequential way. This means that there’s a small delay between each acquisition, and the timeframe is thus less accurate for the last channels than for the first ones. If time precision matters it is preferable to read as few channels as possible !

  • vref: VREF can be set independently of the chosen VCC value. The VCC value has no influence on the ADC behaviour as it is always powered up with 5V. Same goes for the DAC.

close() None[source]

Releases the GPIOs.

get_data() list[source]

Reads data from all the user-specified ADC channels, in a sequential way.

Data is returned in Volts, but this can be tuned using gain and offset.

Returns

list – A list containing the timeframe, and then the values for each channel to read

open() None[source]

Sets the SPI communication, the GPIOs and the device.

set_cmd(*cmd: float) None[source]

Sets the user-specified DAC channels according to the input values.

Parameters

cmd (float) – The input values, in Volts