ahio  1.0.0
I/O Communication Library
ahio.abstract_driver.AbstractDriver Class Reference

Base class for drivers. More...

Inheritance diagram for ahio.abstract_driver.AbstractDriver:
object ahio.drivers.arduino.Driver ahio.drivers.generic_tcp_io.Driver ahio.drivers.raspberry.Driver ahio.drivers.snap7.Driver

Public Member Functions

def available_pins (self)
 Returns available pins. More...
 
def map_pin (self, abstract_pin_id, physical_pin_id)
 Maps a pin number to a physical device pin. More...
 
def mapped_pins (self)
 Returns a dictionary containing the mapped pins. More...
 
def set_pin_interpolation (self, pin, read_min, read_max, write_min, write_max)
 Interpolates input and output values for pin. More...
 
def set_pin_direction (self, pin, direction)
 Sets pin pin to direction. More...
 
def pin_direction (self, pin)
 Gets the ahio.Direction this pin was set to. More...
 
def set_pin_type (self, pin, ptype)
 Sets pin pin to type. More...
 
def pin_type (self, pin)
 Gets the ahio.PortType this pin was set to. More...
 
def write (self, pin, value, pwm=False)
 Sets the output to the given value. More...
 
def read (self, pin)
 Reads value from pin pin. More...
 
def analog_references (self)
 Possible values for analog reference. More...
 
def set_analog_reference (self, reference, pin=None)
 Sets the analog reference to reference More...
 
def analog_reference (self, pin=None)
 Returns the analog reference. More...
 
def set_pwm_frequency (self, frequency, pin=None)
 Sets PWM frequency, if supported by hardware. More...
 

Detailed Description

Base class for drivers.

Read the documentation carefully if you're developing a driver. Some functions require you to implement a private function with similar signature instead of the function itself.

The driver can have a setup method with special signature to initialize itself using user provided parameters, like port to use for communication, for example. If no user parameters are needed, the initialization should occur in the __init__ method.

Definition at line 55 of file abstract_driver.py.

Member Function Documentation

§ analog_reference()

def ahio.abstract_driver.AbstractDriver.analog_reference (   self,
  pin = None 
)

Returns the analog reference.

If the driver supports per pin analog reference setting, returns the reference for pin pin. If pin is None, returns the global analog reference. If only per pin reference is supported and pin is None, raise RuntimeError.

If you're developing a driver, implement _analog_reference(self, pin)

  • pin if the the driver supports it, the pin that will use reference as reference. None for all.
Returns
the reference used for pin
Exceptions
RuntimeErrorif pin is None on a per pin only hardware, or if it's a valid pin on a global only analog reference hardware.
KeyErrorif pin isn't mapped.

Definition at line 424 of file abstract_driver.py.

§ analog_references()

def ahio.abstract_driver.AbstractDriver.analog_references (   self)

Possible values for analog reference.

If you're developing a driver, override this function.

Returns
a list of values that can be passed to set_analog_reference or returned from analog_reference(). Very driver specific.

Definition at line 371 of file abstract_driver.py.

§ available_pins()

def ahio.abstract_driver.AbstractDriver.available_pins (   self)

Returns available pins.

Returns a list of dictionaries indicating the available pins and it's capabilities. It should follow this format:

         [ {
             'id': 1, # some value that represents this pin in your
                      # implementation.
                      # prefer numbers and Enums. This value will be used
                      # in `map_pin(a,p)`
             'name': 'Pin 1', # a name that can be shown to the user, if needed
             'analog': {
                 'input': True, # if analog input is available
                 'output': False, # if analog output is available
                 'read_range': (0, 1023), # if input is supported, what is the
                                          # valid range (both inclusive)
                 'write_range': (0, 5) # if output is supported, what is the
                                       #valid range (both inclusive)
             },
             'digital': {
                 'input': True, # if digital input is available
                 'output': True, # if digital output is available
                 'pwm': True # if pwm generation is available
             }
         }]

If you're developing a driver, you should override this function.

Returns
a list of dictionaries

Definition at line 92 of file abstract_driver.py.

§ map_pin()

def ahio.abstract_driver.AbstractDriver.map_pin (   self,
  abstract_pin_id,
  physical_pin_id 
)

Maps a pin number to a physical device pin.

To make it easy to change drivers without having to refactor a lot of code, this library does not use the names set by the driver to identify a pin. This function will map a number, that will be used by other functions, to a physical pin represented by the drivers pin id. That way, if you need to use another pin or change the underlying driver completly, you only need to redo the mapping.

If you're developing a driver, keep in mind that your driver will not know about this. The other functions will translate the mapped pin to your id before calling your function.

  • abstract_pin_id the id that will identify this pin in the other function calls. You can choose what you want.

Definition at line 116 of file abstract_driver.py.

§ mapped_pins()

def ahio.abstract_driver.AbstractDriver.mapped_pins (   self)

Returns a dictionary containing the mapped pins.

Each key of the dictionary is the ID you set with map_pin, and each value is the driver-specific ID.

Returns
a dictionary of mapped pins

Definition at line 130 of file abstract_driver.py.

§ pin_direction()

def ahio.abstract_driver.AbstractDriver.pin_direction (   self,
  pin 
)

Gets the ahio.Direction this pin was set to.

If you're developing a driver, implement _pin_direction(self, pin)

  • pin the pin you want to see the mode
    Returns
    the ahio.Direction the pin is set to
    Exceptions
    KeyErrorif pin isn't mapped.

Definition at line 229 of file abstract_driver.py.

§ pin_type()

def ahio.abstract_driver.AbstractDriver.pin_type (   self,
  pin 
)

Gets the ahio.PortType this pin was set to.

If you're developing a driver, implement _pin_type(self, pin)

  • pin the pin you want to see the mode
    Returns
    the ahio.PortType the pin is set to
    Exceptions
    KeyErrorif pin isn't mapped.

Definition at line 280 of file abstract_driver.py.

§ read()

def ahio.abstract_driver.AbstractDriver.read (   self,
  pin 
)

Reads value from pin pin.

Returns the value read from pin pin. If it's an analog pin, returns a number in analog.input_range. If it's digital, returns ahio.LogicValue.

If you're developing a driver, implement _read(self, pin)

  • pin the pin to read from
    Returns
    the value read from the pin
    Exceptions
    KeyErrorif pin isn't mapped.

Definition at line 348 of file abstract_driver.py.

§ set_analog_reference()

def ahio.abstract_driver.AbstractDriver.set_analog_reference (   self,
  reference,
  pin = None 
)

Sets the analog reference to reference

If the driver supports per pin reference setting, set pin to the desired reference. If not, passing None means set to all, which is the default in most hardware. If only per pin reference is supported and pin is None, raise RuntimeError.

If you're developing a driver, implement _set_analog_reference(self, reference, pin). Raise RuntimeError if pin was set but is not supported by the platform.

  • reference the value that describes the analog reference. See AbstractDriver.analog_references
  • pin if the the driver supports it, the pin that will use reference as reference. None for all.
Exceptions
RuntimeErrorif pin is None on a per pin only hardware, or if it's a valid pin on a global only analog reference hardware.
KeyErrorif pin isn't mapped.

Definition at line 395 of file abstract_driver.py.

§ set_pin_direction()

def ahio.abstract_driver.AbstractDriver.set_pin_direction (   self,
  pin,
  direction 
)

Sets pin pin to direction.

The pin should support the requested mode. Calling this function on a unmapped pin does nothing. Calling it with a unsupported direction throws RuntimeError.

If you're developing a driver, you should implement _set_pin_direction(self, pin, direction) where pin will be one of your internal IDs. If a pin is set to OUTPUT, put it on LOW state.

Exceptions
KeyErrorif pin isn't mapped.
RuntimeErrorif direction is not supported by pin.

Definition at line 207 of file abstract_driver.py.

§ set_pin_interpolation()

def ahio.abstract_driver.AbstractDriver.set_pin_interpolation (   self,
  pin,
  read_min,
  read_max,
  write_min,
  write_max 
)

Interpolates input and output values for pin.

Changes the output and input of AbstractDriver.read and AbstractDriver.write functions to use a value in range (read_min, read_max) or (write_min, write_max) instead of the values returned by available_pins (analog only). The conversion is done using linear interpolation. If read_min, read_max, write_min and write_max are all None or don't form valid pairs (like, read_min has a value but read_max is None), the pin is deregistered. If you pass a pair but leave the other with None values, only one direction is registered.

Definition at line 164 of file abstract_driver.py.

§ set_pin_type()

def ahio.abstract_driver.AbstractDriver.set_pin_type (   self,
  pin,
  ptype 
)

Sets pin pin to type.

The pin should support the requested mode. Calling this function on a unmapped pin does nothing. Calling it with a unsupported mode throws RuntimeError.

If you're developing a driver, you should implement _set_pin_type(self, pin, ptype) where pin will be one of your internal IDs. If a pin is set to OUTPUT, put it on LOW state.

Exceptions
KeyErrorif pin isn't mapped.
RuntimeErrorif type is not supported by pin.

Definition at line 256 of file abstract_driver.py.

§ set_pwm_frequency()

def ahio.abstract_driver.AbstractDriver.set_pwm_frequency (   self,
  frequency,
  pin = None 
)

Sets PWM frequency, if supported by hardware.

If the driver supports per pin frequency setting, set pin to the desired frequency. If not, passing None means set to all. If only per pin frequency is supported and pin is None, raise RuntimeError.

If you're developing a driver, implement _set_pwm_frequency(self, frequency, pin). Raise RuntimeError if pin was set but is not supported by the platform.

  • frequency pwm frequency to be set, in Hz
  • pin if the the driver supports it, the pin that will use frequency as pwm frequency. None for all/global.
Exceptions
RuntimeErrorif pin is None on a per pin only hardware, or if it's a valid pin on a global only hardware.
KeyErrorif pin isn't mapped.

Definition at line 453 of file abstract_driver.py.

§ write()

def ahio.abstract_driver.AbstractDriver.write (   self,
  pin,
  value,
  pwm = False 
)

Sets the output to the given value.

Sets pin output to given value. If the pin is in INPUT mode, do nothing. If it's an analog pin, value should be in write_range. If it's not in the allowed range, it will be clamped. If pin is in digital mode, value can be ahio.LogicValue if pwm = False, or a number between 0 and 1 if pwm = True. If PWM is False, the pin will be set to HIGH or LOW, if pwm is True, a PWM wave with the given cycle will be created. If the pin does not support PWM and pwm is True, raise RuntimeError. The pwm argument should be ignored in case the pin is analog. If value is not valid for the given pwm/analog|digital combination, raise TypeError.

If you're developing a driver, implement _write(self, pin, value, pwm)

  • pin the pin to write to
  • value the value to write on the pin
  • pwm wether the output should be a pwm wave
Exceptions
RuntimeErrorif the pin does not support PWM and pwm is True.
TypeErrorif value is not valid for this pin's mode and pwm value.
KeyErrorif pin isn't mapped.

Definition at line 315 of file abstract_driver.py.


The documentation for this class was generated from the following file: