DUDS
Distributed Update of Data from Something
VirtualPort.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2018 Jeff Jackowski
9  */
12 
13 namespace duds { namespace hardware { namespace interface { namespace test {
14 
16  unsigned int numpins,
17  unsigned int firstid
18 ) : DigitalPortIndependentPins(numpins, firstid) {
19  for (std::uint32_t pidx = 0; pidx < numpins; ++pidx) {
20  initPin(pidx, pidx);
21  }
22 }
23 
25  const std::vector<unsigned int> &ids,
26  unsigned int firstid
27 ) : DigitalPortIndependentPins(ids.size(), firstid) {
28  std::vector<unsigned int>::const_iterator iter = ids.begin();
29  for (unsigned int pid = 0; iter != ids.end(); ++iter, ++pid) {
30  initPin(*iter, pid);
31  }
32 }
33 
34 std::shared_ptr<VirtualPort> VirtualPort::makeConfiguredPort(
35  PinConfiguration &pc,
36  const std::string &name
37 ) {
38  // find the port's config object
39  const PinConfiguration::Port &port = pc.port(name);
40  // enumerate the pins
41  std::vector<unsigned int> gpios;
42  unsigned int next = port.idOffset;
43  gpios.reserve(port.pins.size());
44  for (auto const &pin : port.gidIndex()) {
45  // need empty spots?
46  if (pin.gid > next) {
47  // add unavailable pins
48  gpios.insert(gpios.end(), pin.gid - next, -1);
49  }
50  // add available pin
51  gpios.push_back(pin.pid);
52  next = pin.gid + 1;
53  }
54  std::shared_ptr<VirtualPort> sp = std::make_shared<VirtualPort>(
55  gpios,
56  port.idOffset
57  );
58  pc.attachPort(sp, name);
59  return sp;
60 }
61 
63  shutdown();
64 }
65 
66 void VirtualPort::initPin(std::uint32_t offset, unsigned int pid) {
67 
68  //std::cout << "VirtualPort::initPin(), offset(gid) = " << offset <<
69  //" pid = " << pid << std::endl;
70 
71  if (offset == -1) {
72  // line cannot be used
73  pins[pid].markNonexistent();
74  return;
75  }
76  pins[pid].conf.options = DigitalPinConfig::DirInput;
77  pins[pid].cap.capabilities =
80  DigitalPinCap::EventEdgeFalling | // theses are not yet supported
81  DigitalPinCap::EventEdgeRising |
82  DigitalPinCap::EventEdgeChange |
83  DigitalPinCap::InterruptOnEvent */;
84  // no data on output currents
85  pins[pid].cap.maxOutputCurrent = 0;
86 }
87 
89  return true;
90 }
91 
93  unsigned int lid,
94  const DigitalPinConfig &cfg,
96 ) {
97  DigitalPinConfig &dpc = pins[lid].conf;
98  // change in config?
99  if (
102  ) {
103  if (cfg & DigitalPinConfig::DirInput) {
104  //gr->inputOffset(chipFd, lid);
105  } else if (cfg & DigitalPinConfig::DirOutput) {
106  /*gr->outputOffset(
107  chipFd,
108  lid,
109  dpc.options & DigitalPinConfig::OutputState
110  );*/
111  }
112  }
113 }
114 
116  unsigned int gid,
118 ) {
120  int lid = localId(gid);
121  pins[lid].conf.options.setTo(DigitalPinConfig::InputState, true);
122  return true;
123 }
124 
125 std::vector<bool> VirtualPort::inputImpl(
126  const std::vector<unsigned int> &pvec,
128 ) {
130  // return input states
131  std::vector<bool> outv;
132  outv.reserve(pvec.size());
133  int idx = 0;
134  for (const unsigned int &gid : pvec) {
135  outv.push_back(
136  (pins[localId(gid)].conf.options & DigitalPinConfig::InputState) > 0
137  );
138  }
139  return outv;
140 }
141 
143  unsigned int lid,
144  bool state,
146 ) {
147  // store new state
148  pins[lid].conf.options.setTo(DigitalPinConfig::OutputState, state);
149 }
150 
152  const std::vector<unsigned int> &pvec,
153  const std::vector<bool> &state,
155 ) {
156  // loop through all pins to alter
157  std::vector<unsigned int>::const_iterator piter = pvec.begin();
158  std::vector<bool>::const_iterator siter = state.begin();
159  for (; piter != pvec.end(); ++piter, ++siter) {
160  // store new state
161  pins[*piter].conf.options.setTo(DigitalPinConfig::OutputState, *siter);
162  }
163 }
164 
165 } } } } // namespaces
const PinConfiguration::Pins::index< index_gid >::type & gidIndex() const
Convenience function that provides the pin global ID index for the port&#39;s pins.
static constexpr Flags Input
Input operation is supported.
static constexpr Flags DirMask
A bit mask for all direction flags.
unsigned int offset() const
Returns the offset for the port&#39;s pins.
unsigned int idOffset
The pin ID offset for the port; used to translate between global and port pin IDs.
static constexpr Flags DirInput
Configure the pin for input.
Defines the configuration for a digital general purpose I/O 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.
Pins pins
The pins described by the configuration file.
A type for holding arbitrary port-specific data within a DigitalPinAccess or DigitalPinSetAccess obje...
virtual void outputImpl(unsigned int lid, bool state, DigitalPinAccessBase::PortData *pdata)
Changes the output state of the given pin.
virtual bool simultaneousOperations() const
Simultaneous operations are supported; returns true.
Definition: VirtualPort.cpp:88
virtual bool inputImpl(unsigned int gid, DigitalPinAccessBase::PortData *pdata)
Reads input from the given 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.
VirtualPort(unsigned int numpins, unsigned int firstid=0)
Make a VirtualPort object.
Definition: VirtualPort.cpp:15
void shutdown()
Waits for access to all pins so that any user of access objects may finish with their operation...
Definition: DigitalPort.cpp:20
Parses configuration data for DigitalPort, DigitalPin, DigitalPinSet, ChipSelectManager, and ChipSelect objects.
static std::shared_ptr< VirtualPort > makeConfiguredPort(PinConfiguration &pc, const std::string &name="default")
Make a VirtualPort object according to the given configuration, and attach to the configuration...
Definition: VirtualPort.cpp:34
const Port & port(const std::string &name="default") const
Finds the configuration data for the named DigitalPort.
unsigned int size() const
The maximum number of pins on the port.
void initPin(std::uint32_t offset, unsigned int pid)
Initializes a PinEntry object.
Definition: VirtualPort.cpp:66
static constexpr Flags OutputPushPull
The output can drive the line either low or high.
static constexpr Flags DirOutput
Configure the pin for output.
Flags options
The control options requested for a digital pin.
static constexpr Flags OutputState
The set output state for the pin.
A partial DigitalPort implementation for ports where the configuration of each pin is independent of ...
PinVector pins
Data on each pin handled by the port.
virtual void configurePort(unsigned int localPinId, const DigitalPinConfig &cfg, DigitalPinAccessBase::PortData *pdata)
Changes the hardware configuration for a single pin.
Definition: VirtualPort.cpp:92
static constexpr Flags InputState
The last known input state from the pin.