DUDS
Distributed Update of Data from Something
ChipMultiplexerSelectManager.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) 2017 Jeff Jackowski
9  */
11 
12 namespace duds { namespace hardware { namespace interface {
13 
15  std::unique_ptr<DigitalPinSetAccess> &&acc
16 ) {
17  setAccess(std::move(acc));
18 }
19 
20 bool ChipMultiplexerSelectManager::validChip(int chipId) const noexcept {
21  if (outacc) {
22  return (chipId > 0) && (chipId < (1 << outacc->size()));
23  }
24  return false;
25 }
26 
28  std::unique_ptr<DigitalPinSetAccess> &&acc
29 ) {
30  if (!acc || !acc->havePins()) {
32  }
33  // require exclusive access
34  std::unique_lock<std::mutex> lock(block);
35  // is a chip in use?
36  if (inUse()) {
37  assert(outacc);
38  // fail; provide the pin and chip IDs for what is currently in use
41  );
42  }
43  // get the capabilities for inspection
44  std::vector<DigitalPinCap> caps = acc->capabilities();
45  // prepare to configure each pin
46  std::vector<DigitalPinConfig> conf;
47  conf.reserve(caps.size());
48  // iteratate over all the pins
49  std::vector<DigitalPinCap>::const_iterator iter = caps.cbegin();
50  unsigned int pos = 0;
51  for (; iter != caps.cend(); ++pos, ++iter) {
52  // check for no output ability
53  if (!iter->canOutput()) {
55  PinErrorId(acc->globalId(pos))
56  );
57  }
58  // work out the actual output config
59  conf.push_back(DigitalPinConfig(iter->firstOutputDriveConfigFlags()));
60  }
61  // assure a deselected state prior to requesting output
62  acc->output(false);
63  // make all pins outputs
64  acc->modifyConfig(conf);
65  // store this access object; seems good if it got this far without
66  // an exception
67  outacc = std::move(acc);
68 }
69 
70 std::unique_ptr<DigitalPinSetAccess>
72  // get exclusive access to manager data
73  std::unique_lock<std::mutex> lock(block);
74  // is a chip in use?
75  if (inUse()) {
76  // fail; provide the chip ID for what is currently in use
78  }
79  return std::move(outacc);
80 }
81 
83  outacc->write(cid);
84 }
85 
87  outacc->write((std::int32_t)0);
88 }
89 
90 } } }
boost::error_info< struct Info_ChipId, int > ChipSelectIdError
The chip select ID relavent to the error.
Defines the configuration for a digital general purpose I/O pin.
virtual void select()
Selects the chip identified by cid.
void setAccess(std::unique_ptr< DigitalPinSetAccess > &&acc)
Sets the access object used to output the number of the chip to select.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
A pin required for the operation does not exist or is unavailable to the process. ...
Definition: PinErrors.hpp:70
boost::error_info< struct Info_PinId, unsigned int > PinErrorId
The pin global ID involved in the error.
Definition: PinErrors.hpp:97
std::mutex block
Used to synchonize access.
std::unique_ptr< DigitalPinSetAccess > outacc
Access used for parallel output.
virtual void deselect()
Deselects the chip identified by cid.
std::unique_ptr< DigitalPinSetAccess > releaseAccess()
Returns the access object that was used by this chip select manager.
int cid
Selected chip ID, or -1 to terminate.
ChipMultiplexerSelectManager()=default
Default constructor.
virtual bool validChip(int chipId) const noexcept
Valid chip IDs are greater than zero and can be represented in the same number of bits as there are p...
bool inUse() const
Returns true if an access object provided by this manager exists.
Indicates that a request to configure a pin to output was made of a pin that cannot output...
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
An attempt was made to change the set of valid chips or exactly how a particluar chip might be select...