DUDS
Distributed Update of Data from Something
ChipSelect.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 #include <duds/general/Errors.hpp>
13 
14 namespace duds { namespace hardware { namespace interface {
15 
16 ChipSelect::ChipSelect() noexcept : cid(-1) { }
17 
19  const std::shared_ptr<ChipSelectManager> &csm,
20  int chipId
21 ) {
22  modify(csm, chipId);
23 }
24 
26  std::shared_ptr<ChipSelectManager> &&csm,
27  int chipId
28 ) {
29  modify(std::move(csm), chipId);
30 }
31 
32 std::unique_ptr<ChipAccess> ChipSelect::access() {
33  if (!mgr) {
36  }
37  return mgr->access(cid);
38 }
39 
41  if (!mgr) {
44  }
45  mgr->access(acc, cid);
46 }
47 
48 std::unique_ptr<ChipAccess> ChipSelect::select() {
49  if (!mgr) {
52  }
53  return mgr->select(cid);
54 }
55 
57  if (!mgr) {
60  }
61  mgr->select(acc, cid);
62 }
63 
64 void ChipSelect::modify(const std::shared_ptr<ChipSelectManager> &csm, int chipId) {
65  if (csm && (chipId >= 0)) {
66  if (!csm->validChip(chipId)) {
68  ChipSelectIdError(chipId));
69  }
70  mgr = csm;
71  cid = chipId;
72  }
73  else {
74  reset();
75  }
76 }
77 
78 void ChipSelect::modify(std::shared_ptr<ChipSelectManager> &&csm, int chipId) {
79  if (csm && (chipId >= 0)) {
80  if (!csm->validChip(chipId)) {
82  ChipSelectIdError(chipId));
83  }
84  mgr = csm;
85  cid = chipId;
86  }
87  else {
88  reset();
89  }
90 }
91 
92 void ChipSelect::reset() noexcept {
93  mgr.reset();
94  cid = -1;
95 }
96 
97 } } }
98 
int chipId() const
Returns the chip ID of the chip this object will select.
Definition: ChipSelect.hpp:125
boost::error_info< struct Info_ChipId, int > ChipSelectIdError
The chip select ID relavent to the error.
void modify(const std::shared_ptr< ChipSelectManager > &csm, int chipId)
Changes the manager and chip to select.
Definition: ChipSelect.cpp:64
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
An object used to provide chip select control to a single user at a time.
Definition: ChipAccess.hpp:22
A ChipSelectManager is required for the operation but is not set.
ChipSelect() noexcept
Initializes the object to a non-configured state.
Definition: ChipSelect.cpp:16
std::unique_ptr< ChipAccess > access()
Obtains a ChipAccess object.
Definition: ChipSelect.cpp:32
void reset() noexcept
Returns the object to the default constructed state of not having a manager or a valid chip ID...
Definition: ChipSelect.cpp:92
std::unique_ptr< ChipAccess > select()
Obtains an access object and selects the chip.
Definition: ChipSelect.cpp:48
Indicates an attempt to select a non-existant chip.
General errors.
#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
std::shared_ptr< ChipSelectManager > mgr
The manager that will handle the selection.
Definition: ChipSelect.hpp:28