10 #include <boost/exception/errinfo_file_name.hpp> 15 namespace duds {
namespace hardware {
namespace interface {
namespace linux {
17 static const char *
prefix =
"/sys/class/gpio/gpio";
21 PinVector::iterator piter =
pins.begin();
22 FsPinVector::iterator fiter =
fspins.begin();
23 std::vector<unsigned int>::const_iterator iiter = ids.cbegin();
24 for (; piter !=
pins.end(); ++firstid, ++iiter, ++fiter, ++piter) {
27 piter->markNonexistent();
31 fiter->open(piter->conf, piter->cap, *iiter);
42 const std::string &name
47 std::vector<unsigned int> gpios;
49 gpios.reserve(port.
pins.size());
50 for (
auto const &pin : port.
gidIndex()) {
54 gpios.insert(gpios.end(), pin.gid - next, -1);
57 gpios.push_back(pin.pid);
60 std::shared_ptr<SysFsPort> sp = std::make_shared<SysFsPort>(
78 unsigned int localPinId,
85 if (
pins[localPinId]) {
126 std::ostringstream fname;
127 fname << prefix << pin <<
"/value";
128 value.open(fname.str().c_str());
129 bool noOutput =
false;
130 if (!value.is_open()) {
132 value.open(fname.str().c_str(), std::ios_base::in);
133 if (!value.is_open()) {
135 boost::errinfo_file_name(fname.str())
142 curoutval = reqoutval = read();
145 fname.seekp(-5, std::ios_base::cur);
146 fname <<
"direction";
148 direction.open(fname.str().c_str());
149 if (!direction.is_open()) {
151 direction.open(fname.str().c_str(), std::ios_base::in);
152 if (!direction.is_open()) {
155 boost::errinfo_file_name(fname.str())
170 if (!direction.is_open()) {
175 }
else if (dir ==
"out") {
178 if (!direction.is_open()) {
206 if (output != isoutput) {
208 direction <<
"out" << std::endl;
210 direction <<
"in" << std::endl;
212 if (direction.fail()) {
219 curoutval = !reqoutval;
246 if (isoutput && (w != curoutval)) {
251 value << v << std::endl;
void write(bool state)
Changes the output value of the pin.
const PinConfiguration::Pins::index< index_gid >::type & gidIndex() const
Convenience function that provides the pin global ID index for the port's pins.
static constexpr Flags Input
Input operation is supported.
unsigned int idOffset
The pin ID offset for the port; used to translate between global and port pin IDs.
An I/O error prevented the operation on the pin from succeeding.
static constexpr Flags DirInput
Configure the pin for input.
Defines the configuration for a digital general purpose I/O pin.
constexpr DigitalPinCap NonexistentDigitalPin
The capabilities of a non-existent pin.
Holds configuration data for a single digital port.
unsigned int localId(unsigned int globalId) const
Returns the local ID for a pin given the global ID.
The requested operation is not supported by the specific pin.
unsigned int globalId(unsigned int localId) const
Returns the global ID for a pin given the local ID.
Pins pins
The pins described by the configuration file.
boost::error_info< struct Info_PinId, unsigned int > PinErrorId
The pin global ID involved in the error.
void open(DigitalPinConfig &conf, DigitalPinCap &cap, unsigned int pin)
Opens the value and direction files for the pin.
static constexpr Flags OutputPushPull
Configure the pin to drive output both high and low.
A type for holding arbitrary port-specific data within a DigitalPinAccess or DigitalPinSetAccess obje...
virtual void configurePort(unsigned int localPinId, const DigitalPinConfig &cfg, DigitalPinAccessBase::PortData *)
Changes the hardware configuration for a single pin.
Flags capabilities
The capabilities of a digital pin.
void attachPort(const std::shared_ptr< DigitalPort > &dp, const std::string &name="default")
Attaches the given DigitalPort to the named port in the configuration.
bool read()
Reads from the value file of the pin and returns the result.
FsPinVector fspins
Internal pin objects for each pin that will be made available through this port object.
void shutdown()
Waits for access to all pins so that any user of access objects may finish with their operation...
Parses configuration data for DigitalPort, DigitalPin, DigitalPinSet, ChipSelectManager, and ChipSelect objects.
const Port & port(const std::string &name="default") const
Finds the configuration data for the named DigitalPort.
boost::error_info< struct Info_PinPortId, unsigned int > PinErrorPortId
The port (hardware) specific pin ID involved in the error.
void output(unsigned int gid, bool state, DigitalPinAccessBase::PortData *pdata)
Does error checking in advance of calling outputImpl(unsigned int, bool) to change the output of the ...
static constexpr Flags OutputPushPull
The output can drive the line either low or high.
virtual bool simultaneousOperations() const
The sysfs interface does not support simultaneous operations; returns false.
virtual void outputImpl(unsigned int lid, bool state, DigitalPinAccessBase::PortData *)
Changes the output state of the given pin.
static constexpr Flags DirOutput
Configure the pin for output.
Defines the capabilites of a digital general purpose I/O pin.
void setDirection(bool output)
Changes the pin's direction between input and output.
Flags options
The control options requested for a digital pin.
static std::shared_ptr< SysFsPort > makeConfiguredPort(PinConfiguration &pc, const std::string &name="default")
Make a SysFsPort object according to the given configuration, and attach to the configuration.
A partial DigitalPort implementation for ports where the configuration of each pin is independent of ...
Base exception type for all errors about pins.
static const char * prefix
PinVector pins
Data on each pin handled by the port.
virtual bool inputImpl(unsigned int gid, DigitalPinAccessBase::PortData *)
Reads input from the given pin.
Construction option for initializing all fields to defaul values.
SysFsPort(const std::vector< unsigned int > &ids, unsigned int firstid)
Make a SysFsPort object with the given pins.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...