DUDS
Distributed Update of Data from Something
ChipBinarySelectManager.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 ChipBinarySelectManager::validChip(int chipId) const noexcept {
16  return selpin && (chipId >= 0) && (chipId <= 1);
17 }
18 
20  selpin->output(cid > 0);
21 }
22 
24  selpin->output(cid == 0);
25 }
26 
28  std::unique_ptr<DigitalPinAccess> &&dpa,
29  int initSel
30 ) {
31  if (!dpa) {
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 the requested state prior to begining output
53  dpa->output(initSel > 0);
54  // work out the actual output config
56  dpa->modifyConfig(DigitalPinConfig(
58  ));
59  } else if (cap & DigitalPinCap::OutputDriveLow) {
60  dpa->modifyConfig(DigitalPinConfig(
62  ));
63  } else {
64  dpa->modifyConfig(DigitalPinConfig(
66  ));
67  }
68  // store this access object; seems good if it got this far without
69  // an exception
70  selpin = std::move(dpa);
71 }
72 
74 
76  std::unique_ptr<DigitalPinAccess> &&dpa, int initSel
77 ) {
78  setSelectPin(std::move(dpa), initSel);
79 }
80 
82  shutdown();
83 }
84 
85 } } }
86 
std::unique_ptr< DigitalPinAccess > selpin
The access object for the select pin.
boost::error_info< struct Info_ChipId, int > ChipSelectIdError
The chip select ID relavent to the error.
static constexpr Flags OutputDriveLow
The output is or can be an open collector or open drain type.
Defines the configuration for a digital general purpose I/O pin.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
virtual bool validChip(int chipId) const noexcept
The only valid chip IDs for this manager are 0 and 1, and they are only valid once a DigitalPinAccess...
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
static constexpr Flags OutputPushPull
Configure the pin to drive output both high and low.
std::mutex block
Used to synchonize access.
Flags capabilities
The capabilities of a digital pin.
static constexpr Flags OutputDriveLow
Configure the pin to be able to drive the output low.
static constexpr Flags OutputDriveHigh
Configure the pin to be able to drive the output high.
int cid
Selected chip ID, or -1 to terminate.
void setSelectPin(std::unique_ptr< DigitalPinAccess > &&dpa, int initSel=0)
Sets the DigitalPinAccess object to use for the chip select line.
virtual void deselect()
Deselects the chip identified by cid.
bool inUse() const
Returns true if an access object provided by this manager exists.
static constexpr Flags OutputPushPull
The output can drive the line either low or high.
Indicates that a request to configure a pin to output was made of a pin that cannot output...
static constexpr Flags DirOutput
Configure the pin for output.
Defines the capabilites of a digital general purpose I/O pin.
virtual void select()
Selects the chip identified by cid.
#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
void shutdown()
Waits on a ChipAccess object if one is in use, then begins forcing any threads waiting on access to w...
An attempt was made to change the set of valid chips or exactly how a particluar chip might be select...