DUDS
Distributed Update of Data from Something
ChipPinSelectManager.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  */
12 
13 namespace duds { namespace hardware { namespace interface {
14 
15 bool ChipPinSelectManager::validChip(int chipId) const noexcept {
16  return selpin && (chipId == 1);
17 }
18 
20  selpin->output(selstate);
21 }
22 
24  selpin->output(!selstate);
25 }
26 
28  std::unique_ptr<DigitalPinAccess> &&dpa,
29  SelectState selectState
30 ) {
31  if (!dpa || !dpa->havePin()) {
33  }
34  // require exclusive access
35  std::unique_lock<std::mutex> lock(block);
36  // is a chip in use?
37  if (inUse()) {
38  assert(selpin);
39  // fail; provide the pin and chip IDs for what is currently in use
41  PinErrorId(selpin->globalId()) << ChipSelectIdError(1)
42  );
43  }
44  // get the capabilities for inspection
45  DigitalPinCap cap = dpa->capabilities();
46  // check for no output ability
47  if (!cap.canOutput()) {
49  PinErrorId(dpa->globalId())
50  );
51  }
52  // assure a deselected state prior to requesting output
53  dpa->output(!selectState);
54  // work out the actual output config
55  dpa->modifyConfig(DigitalPinConfig(cap.firstOutputDriveConfigFlags()));
56  // store this access object; seems good if it got this far without
57  // an exception
58  selpin = std::move(dpa);
59  selstate = selectState;
60 }
61 
63  std::unique_ptr<DigitalPinAccess> &&dpa,
64  SelectState selectState
65 ) {
66  setSelectPin(std::move(dpa), selectState);
67 }
68 
70  shutdown();
71 }
72 
73 } } }
74 
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.
SelectState
The list of possible pin states that can be used to select a chip.
Indicates that a request to configure a pin to input was made of a pin that cannot intput...
std::unique_ptr< DigitalPinAccess > selpin
The access object for the select pin.
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.
Flags capabilities
The capabilities of a digital pin.
virtual bool validChip(int chipId) const noexcept
The only valid chip ID for this manager is 1, and it is only valid once a DigitalPinAccess object has...
SelectState selstate
True when the chip is selected with a high logic level.
bool inUse() const
Returns true if an access object provided by this manager exists.
void setSelectPin(std::unique_ptr< DigitalPinAccess > &&dpa, SelectState selectState=SelectLow)
Sets the DigitalPinAccess object to use for the chip select line.
Defines the capabilites of a digital general purpose I/O pin.
#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
virtual void deselect()
Deselects the chip identified by cid.
void shutdown()
Waits on a ChipAccess object if one is in use, then begins forcing any threads waiting on access to w...
virtual void select()
Selects the chip identified by cid.
An attempt was made to change the set of valid chips or exactly how a particluar chip might be select...