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: int | str | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = 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: int | str | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = 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[float][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 – Desired value to measure. Should be either b’VOLT’ or b’RES’.

  • device – Path to the device to open, as a str.

  • baudrate – Desired baudrate for serial communication.

  • timeout – Timeout for the serial connection, as a float.

close() None[source]

Closes the serial port.

get_data() List[float][source]

Asks the Agilent to acquire a reading and returns it, except if an error occurs in which case 0 is returned.

open() None[source]

Opens the serial connection, resets the Agilent and configures it to the desired mode.

Comedi

class crappy.inout.comedi.Comedi(device: str = '/dev/comedi0', subdevice: int = 0, channels: List[int] | None = None, range_num: List[int] | None = None, gain: List[float] | None = None, offset: List[float] | None = None, make_zero: List[bool] | None = None, out_subdevice: int = 1, out_channels: List[int] | None = None, out_range_num: List[int] | None = None, out_gain: List[float] | None = None, out_offset: List[float] | None = None)[source]

This class can control acquisition boards relying on the Comedi driver.

It can read data from ADCs on input channels, and set voltages of DACs on output channels. Each channel can be tuned independently in terms of range, gan, offset, and for input channels it’s possible to decide whether they should be offset to 0 at the beginning of the test.

__init__(device: str = '/dev/comedi0', subdevice: int = 0, channels: List[int] | None = None, range_num: List[int] | None = None, gain: List[float] | None = None, offset: List[float] | None = None, make_zero: List[bool] | None = None, out_subdevice: int = 1, out_channels: List[int] | None = None, out_range_num: List[int] | None = None, out_gain: List[float] | None = None, out_offset: List[float] | None = None) None[source]

Sets the args and initializes the parent class.

Parameters:
  • device – The address of the device, as a str.

  • subdevice – The id of the subdevice to use for input channels, as an int.

  • channels – A list containing the indexes of the channels to use as inputs, given as int.

  • range_num – A list containing for each input channel the index of the range to set for that channel, as an int. Refer to the documentation of the board to get the correspondence between range indexes and Volts. If not given, all input channels will be set to the range 0.

  • gain

    A list containing for each input channel the gain to apply to the measured voltage, as a float. The returned voltage is calculated as follows :

    returned_voltage = gain * measured_voltage + offset
    

    If not given, no gain is applied to the measured values.

  • offset

    A list containing for each input channel the offset to apply to the measured voltage, as a float. The returned voltage is calculated as follows :

    returned_voltage = gain * measured_voltage + offset
    

    If not given, no offset is applied to the measured values.

  • make_zero – A list containing for each input channel a bool indicating whether the channel should be zeroed or not. If so, data will be acquired on this channel before the test starts, and a compensation value will be deduced so that the offset of this channel is 0. It will only take effect if the ``make_zero_delay`` argument of the :ref:`IOBlock` controlling the Comedi is set ! If not given, the channels are by default not zeroed.

  • out_subdevice – The id of the subdevice to use for output channels, as an int.

  • out_channels – A list containing the indexes of the channels to use as outputs, given as int.

  • out_range_num – A list containing for each output channel the index of the range to set for that channel, as an int. Refer to the documentation of the board to get the correspondence between range indexes and Volts. If not given, all output channels will be set to the range 0.

  • out_gain

    A list containing for each output channel the gain to apply to the command voltage, as a float. The set voltage is calculated as follows :

    set_voltage = out_gain * command_voltage + out_offset
    

    If not given, no gain is applied to the command values.

  • out_offset

    A list containing for each output channel the offset to apply to the command voltage, as a float. The set voltage is calculated as follows :

    set_voltage = out_gain * command_voltage + out_offset
    

    If not given, no offset is applied to the command values.

Note

All the list given as arguments for the input channels should have the same length, and same for the output channels. If that’s not the case, all the given lists are treated as if they had the same length as the shortest given list.

close() None[source]

Simply closes the Comedi board and warns the user in case of failure.

get_data() List[float][source]

Simply reads and returns the value of each channel, adjusted with the given gain and offset.

make_zero(delay: float) None[source]

Overriding of the method of the parent class, because the user can choose which channels should be zeroed or not.

It simply performs the regular zeroing, and resets the compensation to zero for the channels that shouldn’t be zeroed.

open() None[source]

Opening the Comedi board and setting up the input and output channels.

set_cmd(*cmd: float) None[source]

Sets the command value on the output channels.

There should be as many commands as there are output channels. In case there would be fewer commands or channels, the extra commands/channels wouldn’t be considered/set.

DaQMX

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.

get_data() List[float][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: float | None = None, frequency: float | None = 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: float | None = None, frequency: float | None = 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 | str, backend: str, ft232h_ser_num: int | None = None)[source]

Class for setting a GPIO high or low.

The Gpio_switch InOut block is meant for switching a 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.

__init__(pin_out: int | str, backend: str, ft232h_ser_num: int | None = None) None[source]

Checks the argument validity.

Parameters:
  • pin_out – The GPIO pin to be controlled. On Raspberry Pi, should be an integer corresponding to a GPIO in BCM convention. On FT232H, should be a string corresponding to the name of a GPIO. With the ‘blinka’ backend, should be a string holding the name of the pin. Refer to blinka’s specific documentation for each board for more information.

  • backend

    Should be one of :

    'Pi4', 'blinka', 'ft232h'
    

    The ‘Pi4’ backend only works on the Raspberry Pis. The ‘blinka’ backend requires installing Adafruit’s modules, but is compatible with and maintained on a wide variety of boards. The ‘ft232h’ backend allows controlling the GPIO from a PC using Adafruit’s FT232H USB to I2C adapter. See Crappy for embedded hardware for details.

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

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[str], 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[str], 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.InOut[source]

Base class for all InOut objects. Implements methods shared by all the these objects, and ensures their dataclass is MetaIO.

__init__() None[source]

Sets the attributes.

close() None[source]

This method should perform any action required for properly ending the test and closing the communication with hardware.

It will be called when the associated IOBlock receives the order to stop, either because the user hit CTRL+C, or because a Generator block reached the end of its path, or because an exception was raised in any of the blocks.

get_data() list | Dict[str, Any] | None[source]

This method should acquire data from a device and return it in a list along with a timestamp.

The timestamp must always be the first returned value, and there can be any number of other acquired channels. The same number of values should always be returned, and they should be in the same order.

Alternatively, the values can be returned in a dict. In that case, the labels argument of the IOBlock is ignored and the returned labels correspond to the keys of the dict.

It is alright for this method to return None if there’s no data to acquire.

get_stream() List[ndarray] | None[source]

This method should acquire a stream as a numpy array, and return it in a list along with an array carrying the timestamps.

The time array must be the first element of the list, the stream array the second element. The time array should have only one column, the stream array can have any number of columns representing the different channels acquired.

It is also possible to return the two arrays in a dict, in which case the labels argument is ignored and the keys of the dict set the returned labels.

It is alright for this method to return None if there’s no data to acquire.

make_zero(delay: float) None[source]

Acquires data for a given delay, averages it for each channel, and stores the average.

Does not work for pure streams, as it requires a get_data() for acquiring the data.

The average values will then be used to remove the offset of the acquired data during the test.

open() None[source]

This method should perform any action that’s required for initializing the hardware and the communication with it.

Communication with hardware should be avoided in the __init__() method, and this method is where it should start happening. This method is called after Crappy’s processes start, i.e. when the associated IOBlock already runs separately from all the other blocks.

return_data() list | Dict[str, Any] | None[source]

Returns the data from get_data(), corrected by an offset if the make_zero_delay argument of the IOBlock is set.

return_stream() List[ndarray] | None[source]

Returns the data from get_stream(), corrected by an offset if the make_zero_delay argument of the IOBlock is set.

set_cmd(*cmd) None[source]

This method should handle commands received from the upstream blocks.

Usually the command is meant to be set on a device, but any other behavior is possible. The commands will be passed to this method as args (not kwargs), in the same order as the cmd_labels are given in the IOBlock.

If the expected number of commands is always the same, you can simply put as many args to your set_cmd method as there are commands. For example for three commands:

def set_cmd(self, cmd0, cmd1, cmd2):
  ...

Alternatively, or if the number of commands may vary from one test to another, you can get all the commands at once in a tuple by putting a single unpacking argument. Example:

def set_cmd(self, *cmds):
  number_of_commands = len(cmds)
  cmd0 = cmds[0]
  ...
start_stream() None[source]

This method should start the acquisition of the stream.

stop_stream() None[source]

This method should stop the acquisition of the stream.

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

Metaclass ensuring that two InOuts don’t have the same name, and that all InOuts define the required methods. Also keeps track of all the InOut classes, including the custom user-defined ones.

Kollmorgen

class crappy.inout.kollmorgen.Koll(axes: List[int], mode: str = 'position', host: str = '192.168.0.109', port: int = 502)[source]

This class can communicate with a KollMorgen AKD PDMM programmable multi-axis controller.

It can either drive it in speed or in position. Multiple axes can be driven. The values of the current speeds or positions can also be retrieved.

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

Sets the args and initializes the parent class.

Parameters:
  • axes – A list containing the motors/axes to drive, given as int.

  • mode – Should be either ‘speed’ or ‘position’. Whether the axes should be driven in speed or in position.

  • host – The IP address of the variator, given as a str.

  • port – The network port over which to communicate with the variator, as an int.

close() None[source]

Closes the modbus connection to the variator.

get_data() List[float][source]

For each motor, reads its current speed or position depending on the selected mode.

The positions or speeds are returned in the same order as the motors were given in the axes argument.

open() None[source]

Connects to the variator over modbus.

set_cmd(*cmd: float) None[source]

Sets either the speed or the position of all motors depending on the selected mode.

If more commands than motors are given, the extra commands are ignored. If there are more motors than commands, only part of the motors will be set.

Labjack T7

class crappy.inout.labjackT7.Labjack_t7(channels: List[Dict[str, Any]], device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', write_at_open: List[tuple] | None = None, no_led: bool = False)[source]

This InOut object allows controlling a Labjack T7 device. It can use any channel as input/output.

The Labjack T7 is a very complete DAQ board. It features several ADC, several DAC, as well as multiple GPIOs. It can also read thermocouples, and run LUA code on an integrated microcontroller. These features can all be controlled from Crappy.

This class is not capable of streaming. For higher frequency, refer to the T7 Streamer class.

__init__(channels: List[Dict[str, Any]], device: str = 'ANY', connection: str = 'ANY', identifier: str = 'ANY', write_at_open: List[tuple] | None = None, no_led: bool = False) None[source]

Sets the args and initializes the parent class.

Parameters:
  • channels – A list of the channels to interface with on the Labjack. Each object in this list should be a dict representing a single channel, and whose keys provide information on the channel to use. Refer to the note below for more information on the possible keys.

  • device

    The type of Labjack to open. Possible values include :

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

    Only tested with ‘T7’ in Crappy.

  • connection

    The type of connection used for interfacing with the Labjack. Possible values include :

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

  • identifier – Any extra information allowing to further identify the Labjack to open, like a serial number, an IP address, or a device name.

  • write_at_open – If specific names or registers have to be written when opening the channel, they can be given here as a list of tuple. They will be written in the same order as in the given list. Refer to the note below for the accepted formats.

  • no_led – If True, turns off the LED on the Labjack. This led can generate noise on the channels AIN0 and AIN1.

Note

  • channels keys:

    • name: The name of the channel to interface with, as written on the Labjack’s case. Ex: ‘AIN0’. The available settings as well as the direction of the channel depend on the given name.

      The name can be:
      • AINx: Analog input, linked to an ADC. A gain and offset can be provided, and the range and resolution can be adjusted. It is always an input. These channels can also be used with thermocouples (see below).

      • (T)DACx: Analog output, linked to a DAC. A gain and an offset can be specified. It is always an output.

      • (E/F/C/M IOx): Digital inputs/outputs. A gain and an offset can be specified. It can be either an input or an output, the default is output.

    • gain: If the channel is an input, the measured value will be modified directly by the Labjack as follows :

      returned_value = gain * measured_value + offset
      

      If the channel is an output, the command value will be modified in Crappy as follows before being sent to the Labjack :

      sent_value = gain * command + offset.
      
    • offset: If the channel is an input, the measured value will be modified directly by the Labjack as follows :

      returned_value = gain * measured_value + offset
      

      If the channel is an output, the command value will be modified in Crappy as follows before being sent to the Labjack :

      sent_value = gain * command + offset
      
    • make_zero: If True, data will be acquired on this channel before the test starts, and a compensation value will be deduced so that the offset of this channel is 0. The compensation is performed directly by the Labjack. This setting only has effect for AIN channels defined as inputs. It will only take effect if the ``make_zero_delay`` argument of the :ref:`IOBlock` controlling the Labjack is set !

    • direction: If True, the channel is considered as an output, else as an input. Only has effect for IO channels, the default is output.

    • resolution: The resolution of the acquisition as an integer, refer to Labjack documentation for more details. The higher this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 depending on the model. The default is 1. Only has effect for AIN channels.

    • range: 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
      

      Only has effect for AIN channels.

    • limits: A tuple containing the minimum and maximum allowed command values to set for this channel. After applying the gain and offset to the command, it is then clamped between these two values before being sent to the Labjack. Only has effect for output channels.

    • thermocouple: The type of thermocouple to read data from. 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. Only has effect for AIN channels.

    • write_at_open: A list containing commands for writing specific names or registers when opening the channel. The commands should be given as tuple either in the format (name (str), value (int/float)) or (register (int), type (int), value (float/int)).

Warning

Do not consider the limits key as a safety feature. It should not go beyond/below the given values, but this is not meant to replace hardware safety !

close() None[source]

Closes the Labjack.

get_data() list | Dict[str, Any] | None[source]

Read the signal on all pre-defined input channels.

make_zero(delay: float) None[source]

Overriding of the method of the parent class, because the Labjack T7 allows setting offsets directly on the board.

Setting the offsets on the Labjack is slightly quicker than correcting the received values afterwards.

Parameters:

delay – The delay during which the data should be acquired for determining the offset.

open() None[source]

Opening the Labjack, parsing the commands to write at open, and sending them.

set_cmd(*cmd) None[source]

Sets the tension commands on the output channels.

The given gain and offset are first applied, then the commands are clamped to the given limits. The commands are then written to the Labjack, only if they differ from the last ones.

Labjack UE9

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: str | None = 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: str | None = 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[float][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: int | str | None = None, device_address: int = 24, i2c_port: int = 1, ft232h_ser_num: str | None = 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: int | str | None = None, device_address: int = 24, i2c_port: int = 1, ft232h_ser_num: str | None = 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() None[source]

Closes the I2C bus.

get_data() List[float][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: int | str | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = 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: int | str | None = None, gain: float = 1, offset: float = 0, ft232h_ser_num: str | None = 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[float][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: List[Dict[str, Any]], sample_rate: float = 100, n_samples: int | None = None)[source]

This class can communicate with NI DAQmx devices using the pydaqmx module.

It can read single data points from digital and analog channels, read streams of data from analog channels, and set the voltage of analog and digital output channels. For analog input channels, several types of acquisition can be performed, like voltage, resistance, current, etc.

__init__(channels: List[Dict[str, Any]], sample_rate: float = 100, n_samples: int | None = None) None[source]

Sets the args and initializes the parent class.

Parameters:
  • channels – A list containing dict holding information on the channels to read data from or write data to. See below for the mandatory and optional keys for the dicts. Note that in streamer mode, the digital input channels are not available for reading. Also, only one type of analog input channel at a time can be read in streamer mode, with no restriction on the number of channels of this type.

  • sample_rate – The target sample rate for data acquisition in streamer mode, given as a float. Default is 100 SPS.

  • n_samples – The number of samples to acquire per chunk of data in streamer mode. Default is 20% of sample_rate.

Note

  • channels keys:

    • name: The name of the channel to interface with, given with the following syntax :

      'DevX/[a/d][i/o]Y'
      

      With X the index of the device, and Y the line on which the channel is. d stands for digital, a for analog, i for input and o for output. For digital channels, DevX/d[i/o]Y is internally converted to DevX/port<Y // 8>/line<Y % 8>. Example of a valid name : Dev1/ao3.

    • type: The type of data to read, for analog input channels. This field can take many different values, refer to the documentation of the nidaqmx for more details. This field is internally used for calling the method : nidaqmx.task.add_ai_[type]_chan(). The default for this field is ‘voltage’, possible values include ‘thrmcpl’, ‘bridge’, ‘current’ and ‘resistance’.

    • All the other keys will be given as kwargs to the nidaqmx.task.add_ai_[type]_chan() method for analog input channels, to the nidaqmx.task.add_ao_voltage_chan() for analog output channels, to nidaqmx.task.add_do_chan() for digital output channels, and to nidaqmx.task.add_di_chan() for digital input channels. Refer to nidaqmx documentation for the possible arguments and values. Note that for the ‘thrmcpl’ analog input channel type, the ‘thermocouple_type’ argument must be given as a letter, same for the ‘units’ argument. They will be parsed internally. Also note that for the analog output channels and the analog input channels of type ‘voltage’, the ‘min_val’ and ‘max_val’ arguments are internally set by default to 0 and 5.

close() None[source]

Stops all the acquisition tasks, and closes the connections to the device.

get_data() List[float][source]

Reads data from the analog and digital input channels, and returns it along with a timestamp.

Data from the analog channels is read first, and then data from the digital channels. Data is returned in the same order as it was acquired.

get_stream() List[ndarray] | None[source]

Reads data from the NI DAQmx, and returns it in an array along with an array holding the timestamps.

Only data from analog input channels can be read, this method cannot read stream data from digital input channels.

open() None[source]

Creates tasks and streams for analog output, digital input, digital output, and each type of analog input channels.

set_cmd(*cmd: float) None[source]

Sets the analog and digital output channels according to the given command values.

The first command values correspond to the analog channels, the remaining ones correspond the the digital channels. It might be that not all channels are set if the number of commands doesn’t match the number of channels.

start_stream() None[source]

Starts the streaming task for analog input channels.

Data can be acquired via streaming for multiple channels, but only for one type of channel.

stop_stream() None[source]

Stops all the acquisition tasks.

Opsens

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

This class allows reading data from an Opsens PicoSens fiber optics signal conditioner.

It can read data from various fiber optics sensors like temperature, pressure, position or strain.

__init__(device: str = '/dev/ttyUSB0') None[source]

Sets the arg and initializes the parent class.

Parameters:

device – Address of the serial connection for communicating with the PicoSens.

close() None[source]

Closes the serial connection if it was opened.

get_data() List[float][source]

Reads data from the PicoSens and returns it.

open() None[source]

Opens the serial connection and configures the PicoSens.

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[str, Any][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(channels: List[int], device: str = '/dev/spcm0', ranges: List[int] | None = None, sample_rate: int = 100000, buff_size: int = 67108864, notify_size: int = 65536)[source]

This class can read data from a Spectrum high speed ADC interfacing over PCIe.

It can acquire data over multiple channels, and set for each channel a different voltage range. It is possible to tune the sample rate, the chunk size and the memory allocated to the data buffer.

This class can only acquire data by streaming, it cannot acquire single data points.

__init__(channels: List[int], device: str = '/dev/spcm0', ranges: List[int] | None = None, sample_rate: int = 100000, buff_size: int = 67108864, notify_size: int = 65536) None[source]

Sets the args and initializes the parent class.

Parameters:
  • channels – A list of all the channels to read data from, given as int. Refer to the documentation to know which combinations of channels are allowed.

  • device – The address of the device to read data from, as a str.

  • ranges – A list indicating for each channel the range of the acquisition in mV, as an int. There should be as many values in this list as there are channels. If not given, all ranges default to 10000 mV.

  • sample_rate – The sample rate of the acquisition for all channels, in Hz. The default is 100KHz.

  • buff_size – The size of the memory allocated as a rolling buffer to copy the data from the card, in bytes. The default is 67MB.

  • notify_size – The size of each chunk of data to copy from the card, in bytes. The default is 65kB.

close() None[source]

Closes the connection to the Spectrum if it was opened.

get_stream() List[ndarray][source]

Waits for data to be available, and returns it along with an array of timestamps.

open() None[source]

Opens and configures the Spectrum, and sets the ranges and the sample rate as requested.

start_stream() None[source]

Starts the streams and saves the corresponding timestamp.

stop_stream() None[source]

Stops the stream, if it was started.

T7 Streamer

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

This InOut object allows controlling a Labjack T7 device in stream mode.

It can only acquire data on the AIN channels. For single point mode, and acquisition on all channels, use the Labjack T7 InOut.

Compared with single point acquisition, the streaming mode can achieve much higher data rates and has a much greater regularity in the frequency of the acquisition. However, fewer options are available and not all types of channels can be read in the streamer mode.

For each channel, the voltage range can be tuned, and a gain and offset can be defined. Also, the user can decide whether the channel should be zeroed before starting the test or not.

Important

The streamer argument of the IOBlock controlling this InOut must be set to True to enable streaming in this class. Otherwise, only single point acquisition can be performed.

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

Sets the args and initializes the parent class.

Parameters:
  • channels – A list of the channels to interface with on the Labjack. Each object in this list should be a dict representing a single channel, and whose keys provide information on the channel to use. Refer to the note below for more information on the possible keys.

  • device

    The type of Labjack to open. Possible values include :

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

    Only tested with ‘T7’ in Crappy.

  • connection

    The type of connection used for interfacing with the Labjack. Possible values include :

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

  • identifier – Any extra information allowing to further identify the Labjack to open, like a serial number, an IP address, or a device name.

  • scan_rate – The acquisition frequency in Hz for all 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 – The number of points to read at each loop.

  • resolution – The resolution of the acquisition as an integer for all channels. Refer to Labjack documentation for more details. The higher this value the better the resolution, but the lower the speed. The possible range is either 1 to 8 or to 12 depending on the model. The default is 1.

Note

  • channels keys:

    • name: The name of the channel to interface with, as written on the Labjack’s case. Ex: ‘AIN0’. In streamer mode, only the AIN channels, i.e. the analog inputs, are available.

    • gain: The measured value will be modified in Crappy as follows :

      returned_value = gain * measured_value + offset
      
    • offset: The measured value will be modified in Crappy as follows :

      returned_value = gain * measured_value + offset
      
    • make_zero: If True, data will be acquired on this channel before the test starts, and a compensation value will be deduced so that the offset of this channel is 0. It will only take effect if the ``make_zero_delay`` argument of the :ref:`IOBlock` controlling the Labjack is set !

    • range: 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
      
close() None[source]

Closes the Labjack if it was opened.

get_data() List[float][source]

Reads single data points, applies the given gains and offsets, and returns the data.

get_stream() List[ndarray] | None[source]

Acquires the stream, reshapes the data, applies the gains and offsets, and returns the data along with a time array.

make_zero(delay: float) None[source]

Overriding of the method of the parent class, because the user can choose which channels should be zeroed or not.

It simply performs the regular zeroing, and resets the compensation for the channels that shouldn’t be zeroed.

open() None[source]

Opens the Labjack, parses the commands to write at open, and sends them.

Also checks whether the scan rate chose nby the Labjack is the same as requested by the user.

start_stream() None[source]

Starts the stream, and saves the timestamp chen the stream started.

stop_stream() None[source]

Stops the stream if it was started.

Waveshare AD/DA

class crappy.inout.waveshare_ad_da.Waveshare_ad_da(dac_channels: List[str] | None = None, adc_channels: List[str] | None = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: 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: List[str] | None = None, adc_channels: List[str] | None = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: 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[float][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: List[str] | None = None, adc_channels: List[str] | None = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: int | float = 100, ft232h_ser_num: str | None = 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: List[str] | None = None, adc_channels: List[str] | None = None, gain_hardware: int = 1, v_ref: float = 3.3, gain: float = 1, offset: float = 0, sample_rate: int | float = 100, ft232h_ser_num: str | None = 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[float][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 High Precision

class crappy.inout.waveshare_high_precision.Waveshare_high_precision(spi_port: int = 0, gain_hardware: int = 16, sample_rate: int | float = 50, channels: List[str] | None = None, digital_filter: int = 4, gain: float = 1, offset: float = 0)[source]

This InOut allows acquiring data from Waveshare’s High Precision HAT.

This board features an ADS1263 32-bits ADC, which is what this InOut actually drives. The main specificities compared with driving just an ADS1263 are that the reference voltage is the board’s 5V supply, and that the differential acquisition is improved when using pairs of channels (0 & 1, 2 & 3, etc.).

The sample rate, hardware gain and digital filter can be adjusted. It is also possible to acquire data sequentially from several channels.

The Waveshare HAT is originally meant to be used with a Raspberry Pi, but it can be used with any device supporting SPI as long as the wiring is correct and the 3.3 and 5V power are supplied.

__init__(spi_port: int = 0, gain_hardware: int = 16, sample_rate: int | float = 50, channels: List[str] | None = None, digital_filter: int = 4, gain: float = 1, offset: float = 0) None[source]

Sets the args and initializes the parent class.

Parameters:
  • spi_port – The SPI port for communicating with the Waveshare HAT.

  • gain_hardware

    A programmable gain for the signal. Should be one of :

    1, 2, 4, 8, 16, 32
    

    Allows increasing the resolution of the ADC, but also divides the full scale range and greatly reduces the noise performance when set greater than 8. This value applies to all the channels.

  • sample_rate

    The number of samples per second to acquire. Should be one of:

    2.5, 5, 10, 16?6, 20, 50, 60, 100, 400, 1200, 2400, 4800, 7200,
    14400, 19200, 38400
    

    The actual achieved sample rate might be lower depending on the capability of the PC and the load on the processor. Note that the greater the sample rate, the greater the noise. For multiple channels, the achieved sample rate is roughly the target sample rate divided by the number of channels.

  • channels

    A list containing strings representing the channels to acquire. Each channel must follow on of the two syntax :

    'INi', i in range(10)
    

    or else

    'INi-INj', i, j in range(10)
    

    With the first syntax, the output is the voltage difference between the acquired channel and the ground of the power supply. With the second syntax, the output value is simply the difference between the voltages of the channels i and j. It is preferable to use the channels in pair (0 & 1, 2 & 3, etc.) for differential acquisition. The data from the different channels is acquired sequentially, not all at once.

  • digital_filter – The Waveshare Hat features a digital filter that can accept different settings. Refer to the documentation of the ADS1263 for more detail.

  • gain

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

    output = gain * tension + offset.
    

    The same gain applies to all the channels.

  • offset

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

    output = gain * tension + offset.
    

    The same offset applies to all the channels.

Important

When the gain_hardware is greater than 1, the PGA cannot amplify above 4.7V or under 0.3V. For example a 2.8V signal read with a gain of 2 would be read as 4.7V after the PGA, not 4.8V ! Beware !

close() None[source]

Closes the SPI bus and resets the GPIOs.

get_data() List[float][source]

Reads the channels sequentially, and returns all the values along with a timestamp.

Time is returned first, and the values are then returned in the same order as the channels were given.

open() None[source]

Sets up the GPIO and the different parameters on the ADS1263.