Skip to content
Permalink
Browse files

driver: add FileDigitalOutputDriver

The FileDigitalOutputDriver can write and read configurable strings as boolean
representations on a file, e.g. for sysfs operations.

Signed-off-by: Bastian Germann <bage@linutronix.de>
  • Loading branch information...
Bastian Germann authored and jluebbe committed Mar 27, 2019
1 parent bede899 commit 53a893cbe65cb3afed62f476d007dda1a0d96723
Showing with 74 additions and 0 deletions.
  1. +1 −0 CHANGES.rst
  2. +26 −0 doc/configuration.rst
  3. +1 −0 labgrid/driver/__init__.py
  4. +46 −0 labgrid/driver/filedigitaloutput.py
@@ -4,6 +4,7 @@ Release 0.3.0 (unreleased)
New Features in 0.3.0
~~~~~~~~~~~~~~~~~~~~~

- The new `FileDigitalOutputDriver` respresents a digital signal with a file.
- The new `GpioDigitalOutputDriver` controls the state of a GPIO via the sysfs interface.
- Crossbar and autobahn have been updated to 19.3.3 and 19.3.5 respectively.
- The InfoDriver was removed. The functions have been integrated into the
@@ -1130,6 +1130,32 @@ Arguments:
SerialDriver in your environment (what is likely to be the case
if you are using this driver).

FileDigitalOutputDriver
~~~~~~~~~~~~~~~~~~~~~~~
The FileDigitalOutputDriver uses a file
to write arbitrary string representations of booleans
to a file and read from it.

The file is checked to exist at configuration time.

If the file's content does not match any of the representations
reading defaults to False.

A prime example for using this driver is Linux's sysfs.

Implements:
- :any:`DigitalOutputProtocol`

.. code-block:: yaml
FileDigitalOutputDriver:
filepath: "/sys/class/leds/myled/brightness"
Arguments:
- filepath (str): A file that is used for reads and writes.
- false_repr (str): A representation for False (default: "0\n")
- true_repr (str): A representation for True (default: "1\n")

ModbusCoilDriver
~~~~~~~~~~~~~~~~
A ModbusCoilDriver controls a `ModbusTCPCoil` resource.
@@ -24,5 +24,6 @@
from .networkusbstoragedriver import NetworkUSBStorageDriver, Mode
from .resetdriver import DigitalOutputResetDriver
from .gpiodriver import GpioDigitalOutputDriver
from .filedigitaloutput import FileDigitalOutputDriver
from .serialdigitaloutput import SerialPortDigitalOutputDriver
from .xenadriver import XenaDriver
@@ -0,0 +1,46 @@
import os

import attr

from ..factory import target_factory
from ..protocol import DigitalOutputProtocol
from ..step import step
from .common import Driver


@target_factory.reg_driver
@attr.s(cmp=False)
class FileDigitalOutputDriver(Driver, DigitalOutputProtocol):
"""
Two arbitrary string values false_repr and true_repr
are defined as representations for False and True.
These values are written to a file and read from it.
If the file's content does not match any of the
representations it defaults to False.
A prime example for using this driver is Linux's sysfs.
"""

bindings = {}
filepath = attr.ib(validator=attr.validators.instance_of(str))
false_repr = attr.ib(default='0\n', validator=attr.validators.instance_of(str))
true_repr = attr.ib(default='1\n', validator=attr.validators.instance_of(str))

def __attrs_post_init__(self):
super().__attrs_post_init__()

if not os.path.isfile(self.filepath):
raise Exception("{} is not a file.".format(self.filepath))

@Driver.check_active
@step()
def get(self):
with open(self.filepath) as fdes:
from_file = fdes.read()
return from_file == self.true_repr

@Driver.check_active
@step()
def set(self, status):
out = self.true_repr if status else self.false_repr
with open(self.filepath, "w") as fdes:
fdes.write(out)

0 comments on commit 53a893c

Please sign in to comment.
You can’t perform that action at this time.