In / Out — Crappy 1.5.3 documentation

# 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', gain: float = 1, offset: float = 0, ft232h_ser_num: Optional[str] = None)

  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.

  Warning

  Only available on Raspberry Pi for now !

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

    Checks arguments validity.

    Parameters

      - backend (`str`) –

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

            'Pi4', 'ft232h'

      - 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. 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'

      - 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 to measure voltages higher than VDD !!

  close() → None

    Closes the I2C bus

  get_data() → list

    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

    Sets the I2C communication and device.

## Agilent 34420A

class crappy.inout.agilent34420A.Agilent34420a(mode=b'VOLT', device='/dev/ttyUSB0', baudrate=9600, timeout=1)

  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=b'VOLT', device='/dev/ttyUSB0', baudrate=9600, timeout=1)

    Sets the args and initializes parent class.

    Parameters

      - mode (`str`, optional) – Desired value to measure. Should be either ‘VOLT’ or ‘RES’.

      - device (`str`, optional) – Path to the device.

      - baudrate (`int`, optional) – Desired baudrate.

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

  close()

    Closes the serial port.

  get_data()

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

## Arduino

class crappy.inout.arduino.Arduino(port=None, baudrate=9600, labels=None, frames=None, width=100, fontsize=11)

  Main class used to interface Arduino, its GUI and crappy.

  Note

  For reusability, make sure the program inside the arduino sends to the serial port a python dictionary formated string.

  __init__(port=None, baudrate=9600, labels=None, frames=None, width=100, fontsize=11)

    Sets the args and initializes the parent class.

    Parameters

      - port – Serial port of the arduino.

      - baudrate – Baudrate defined inside the arduino program.

      - labels –

      - frames –

        Which frames to show. Available frames are :

            'monitor', 'submit', 'minitens'

      - width – Width of the GUI.

      - fontsize – Size of the font inside the GUI.

  get_data()

    Gets data from `ArduinoHandler`, or the minitens GUI.

  open()

    Opens `ArduinoHandler`.

class crappy.inout.arduino.ArduinoHandler(*args)

  This class creates every object (GUIs, Arduino) and handles communication between them. Inputs/outputs of arduino and GUIs.

  Note

  The user doesn’t interact directly with it, the Arduino IOBlock will create this handler.

  The ArduinoHandler lives on a separate process from the `Arduino` class.

  close()

    Exit main loop.

  init_main_window()

    Creates every frame specified by user, and creates links between proper objects.

  main_loop()

    Update GUI, inputs and outputs.

  send_crappy(serial_received)

    Depending on which GUI is created, multiple cases can occur.

    If monitor and/or submit GUI is created, the arduino string returned must be evaluated as a `dict`.

    If minitens GUI is created, it returns a `dict`.

  send_guis(serial_received)

    Send to every created GUI information received from arduino (if applicable).

  update_serial()

    Collects serial and writes in it (if applicable).

    Returns

      Received information, or `None` if nothing received in 0.01 secs.

crappy.inout.arduino.collect_serial(arduino, queue)

  Collect serial information, in a parallel way.

## Comedi

class crappy.inout.comedi.Comedi(**kwargs)

  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

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

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

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

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

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

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

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

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

    - gain (list/float (-) – 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.

    - offset (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.

    - make_zero (list/bool (-) – 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.

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

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

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

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

    - out_gain (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.

    - out_offset (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.

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

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

  get_data(channel='all')

    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()

    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)

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

## DaQMX

class crappy.inout.daqmx.Daqmx(device='Dev1', channels=None, gain=1, offset=0, range=5, make_zero=True, nperscan=1000, sample_rate=10000, out_channels=None, out_gain=1, out_offset=0, out_range=5)

  Class to use DAQmx devices.

  __init__(device='Dev1', channels=None, gain=1, offset=0, range=5, make_zero=True, nperscan=1000, sample_rate=10000, out_channels=None, out_gain=1, out_offset=0, out_range=5)

    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()

    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()

    Closes the connection.

  get_data()

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

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

  get_single(npoints=None)

    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)

    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()

  Get all connected daqmx devices.

  Returns

    A `list` of all connected daqmx devices.

## GPIO PWM

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

  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

    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

    Stops PWM and releases GPIOs.

  open() → None

    Sets the GPIOs and starts the PWM.

  set_cmd(*cmd: float) → None

    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)

  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

    Checks the argument validity.

    Parameters

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

  static close() → None

    Releases the GPIO.

  open() → None

    Sets the GPIO.

  set_cmd(*cmd: int) → None

    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: Optional[list] = None, port: str = '/dev/ttyUSB0', baudrate: int = 115200)

  Block for sending any messages by SMS to given phone numbers.

  Important

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

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

    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 of the GSM.

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

  close() → None

    Closes the serial port.

  open() → None

    Calls the `_is_connected()` method.

  set_cmd(*cmd: str) → None

    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, bases, dict_)

  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='position', axis='all', speed=360, acc=3600, decc=3600, labels=None, host='192.168.0.109', port=502)

  Class to communicate to Kollmorgen devices via Crappy.

class crappy.inout.kollmorgen.KollMorgenVariator(host='192.168.0.109', port=502)

  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='192.168.0.109', port=502)

    Sets the variator and defines the bits.

    Parameters

      - host (`str`, optional) – Variator’s IP address.

      - port (`int`, optional) – Port for modbus communication

  clear_errors()

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

  read_position(motor)

    To read position of motor. Returns a `float`.

  read_speed(motor)

    Reads speed of each motor.

  set_accelerations(motor, **kwargs)

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

  set_position(motor, position)

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

  set_rotation(motor, rotation)

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

  set_speed(motor, speed)

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

  start_rotation(motor)

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

  stop(motor)

    Stops the motor movement.

  toggle_power(motor)

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

## Labjack T7

class crappy.inout.labjackT7.Labjack_t7(device='ANY', connection='ANY', identifier='ANY', channels=None, write_at_open=None, no_led=False)

  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)

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

  __init__(device='ANY', connection='ANY', identifier='ANY', channels=None, write_at_open=None, no_led=False)

    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, val)

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

  close()

    Closes the device.

  get_data()

    Read the signal on all pre-defined input channels.

  read(address, dtype=None)

    To read data directly from a register.

  set_cmd(*cmd)

    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, address, dtype=None)

    To write data directly into a register.

## Labjack UE9

class crappy.inout.labjackUE9.Labjack_ue9(channels=0, gain=1, offset=0, make_zero=True, resolution=12)

  Can read data from a LabJack UE9.

  Important

  Streamer mode and DAC are not supported yet.

crappy.inout.labjackUE9.format_lists(list_to_format, length)

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

crappy.inout.labjackUE9.get_channel_number(channels)

  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)

  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.

  Warning

  Only available on Raspberry Pi for now !

  __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

    Checks arguments validity.

    Parameters

      - backend (`str`) –

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

            'Pi4', 'ft232h'

      - 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

    Switches the MCP9600 to shutdown mode.

  get_data() → list

    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

    Sets the I2C communication and device.

## MPRLS

class crappy.inout.mprls.Mprls

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

  It communicates over I2C with the sensor.

  __init__() → None

    Initializes the parent class.

  close() → None

    Nothing to do here.

  get_data() → list

    Reads the pressure value.

    Returns

      The pressure value in hPa.

  open() → None

    Creates the sensor object.

## NAU7802

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

  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 into Newtons using `gain` and `offset`.

  Warning

  Only available on Raspberry Pi for now !

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

    Checks the arguments validity.

    Parameters

      - backend (`str`) –

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

            'Pi4', 'ft232h'

      - 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 port.

      - 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. 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

      - 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

    Powers down the device

  get_data() → list

    Reads the registers containing the conversion result

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

    Returns

      `list` – A list containing the timeframe and the output value

  open() → None

    Sets the I2C communication and device

## NI DaQMX

class crappy.inout.ni_daqmx.Nidaqmx(channels=None, samplerate=100, nsamples=None)

  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=None, samplerate=100, nsamples=None)

    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=1, port='/dev/ttyUSB0', gain=1, offset=0, cmd_label='cmd', out_gain=1, out_offset=0, make_zero=True, mode='single', nsamples=20)

  Can read data from an OpenDAQ card.

## Opsens

class crappy.inout.opsens.Opsens(device='/dev/ttyUSB0')

  Sensor class for opsens conditioner.

## PiJuice

class crappy.inout.piJuice.Pijuice(i2c_port: int = 1, address: int = 20)

  Block getting the status (plugged or unplugged) and the actual level of charge of the piJuice power platform.

  Warning

  Only available on Raspberry Pi !

  __init__(i2c_port: int = 1, address: int = 20) → None

    Checks arguments validity.

    Parameters

      - 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.

  get_data() → list

    Reads the status and the charge level.

    The output is 0 if unplugged and 1 if plugged for status and between 0 and 100 for charge.

    Returns

      `list` – A list containing the timeframe and the output values for status and charge.

## Spectrum

class crappy.inout.spectrum.Spectrum(device=b'/dev/spcm0', channels=None, ranges=None, samplerate=100000, buff_size=67108864, notify_size=65536, split_chan=False)

  Acquire data from a Spectrum device.

  Parameters

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

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

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

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

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

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

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

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

    - buff_size (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

    - notify_size (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 (bool (-) – False): If False, it will return a single 2D array, else each chan will be a 1D array.

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

exception crappy.inout.spectrum.SpectrumError

## T7 Streamer

class crappy.inout.t7Streamer.T7_streamer(device='ANY', connection='ANY', identifier='ANY', channels=None, scan_rate=100000, scan_per_read=10000, resolution=1)

  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='ANY', connection='ANY', identifier='ANY', channels=None, scan_rate=100000, scan_per_read=10000, resolution=1)

    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()

    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)

  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

    Checks the arguments validity.

    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 be 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 of 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 of 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 from VREF value.

    Note

    - `dac_channels`: As there are 2 DAC channels on the AD/DA, only 1 or 2 strings can be given for the dac_channels argument.

    - `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 from 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

    Releases the GPIOs.

  get_data() → list

    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

    Sets the SPI communication, the GPIOs and the device.

  set_cmd(*cmd: float) → None

    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')

  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 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 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

    Checks the arguments validity.

    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 be 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 of 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 of 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) – If backend is ‘ft232h’, 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 from VREF value.

    Note

    - `dac_channels`: As there are 2 DAC channels on the AD/DA, only 1 or 2 strings can be given for the dac_channels argument.

    - `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 from 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

    Releases the GPIOs.

  get_data() → list

    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

    Sets the SPI communication, the GPIOs and the device.

  set_cmd(*cmd: float) → None

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

    Parameters

      cmd (`float`) – The input values, in Volts