Base class for drivers. More...
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... | |
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.
| 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)
reference as reference. None for all.| RuntimeError | if pin is None on a per pin only hardware, or if it's a valid pin on a global only analog reference hardware. |
| KeyError | if pin isn't mapped. |
Definition at line 424 of file abstract_driver.py.
| def ahio.abstract_driver.AbstractDriver.analog_references | ( | self | ) |
Possible values for analog reference.
If you're developing a driver, override this function.
Definition at line 371 of file abstract_driver.py.
| 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.
Definition at line 92 of file abstract_driver.py.
| 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.
AbstractDriver.available_pins. Setting it to None removes the mapping. Definition at line 116 of file abstract_driver.py.
| 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.
Definition at line 130 of file abstract_driver.py.
| 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)
ahio.Direction the pin is set to| KeyError | if pin isn't mapped. |
Definition at line 229 of file abstract_driver.py.
| 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)
ahio.PortType the pin is set to| KeyError | if pin isn't mapped. |
Definition at line 280 of file abstract_driver.py.
| 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)
| KeyError | if pin isn't mapped. |
Definition at line 348 of file abstract_driver.py.
| 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.
AbstractDriver.analog_references reference as reference. None for all.| RuntimeError | if pin is None on a per pin only hardware, or if it's a valid pin on a global only analog reference hardware. |
| KeyError | if pin isn't mapped. |
Definition at line 395 of file abstract_driver.py.
| 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.
AbstractDriver.map_pin AbstractDriver.Direction| KeyError | if pin isn't mapped. |
| RuntimeError | if direction is not supported by pin. |
Definition at line 207 of file abstract_driver.py.
| 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.
AbstractDriver.map_pin AbstractDriver.read. AbstractDriver.read. AbstractDriver.write. AbstractDriver.write. Definition at line 164 of file abstract_driver.py.
| 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.
AbstractDriver.map_pin AbstractDriver.PortType| KeyError | if pin isn't mapped. |
| RuntimeError | if type is not supported by pin. |
Definition at line 256 of file abstract_driver.py.
| 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 as pwm frequency. None for all/global.| RuntimeError | if pin is None on a per pin only hardware, or if it's a valid pin on a global only hardware. |
| KeyError | if pin isn't mapped. |
Definition at line 453 of file abstract_driver.py.
| 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)
| RuntimeError | if the pin does not support PWM and pwm is True. |
| TypeError | if value is not valid for this pin's mode and pwm value. |
| KeyError | if pin isn't mapped. |
Definition at line 315 of file abstract_driver.py.