SpaIot Library
multiplexer.h
1/*
2 * SpaIot Library (c) by epsilonrt - epsilonrt@gmail.com
3 * This file is part of SpaIot library <https://github.com/epsilonrt/spaiot-lib>
4 *
5 * SpaIot library is licensed under a
6 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
7 *
8 * You should have received a copy of the license along with this
9 * work. If not, see <http://creativecommons.org/licenses/by-nc-sa/4.0/>.
10 *
11 * SpaIot library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY;
13 */
14#pragma once
15
16#include <vector>
17#include <initializer_list>
18#include <type_traits>
19#include "spaiotdebug.h"
20
21#include "buttoncontroller.h"
22
23namespace SpaIot {
24
29 template <int W>
30 class Multiplexer : public ButtonController {
31 public:
32
42 Multiplexer (const std::initializer_list<int>& spins, int inhPin) :
43 ButtonController(), m_spin (spins), m_inh (inhPin) {
44
45 SPAIOT_ASSERT ( (1 << spins.size()) >= W, "The size of spins:%d does not allow you to select the number of channels:%d", spins.size(), W);
46 }
47
48 Multiplexer (const String & name, const std::initializer_list<int>& spins, int inhPin) :
49 ButtonController (name), m_spin (spins), m_inh (inhPin) {
50
51 SPAIOT_ASSERT ( (1 << spins.size()) >= W, "The size of spins:%d does not allow you to select the number of channels:%d", spins.size(), W);
52 }
53
59 ButtonController(), m_inh (-1) {
60 }
61
67 int size() const {
68
69 return W;
70 }
71
77 virtual void begin() {
78
79 if ( (isOpened() == false) && (isNull() == false)) {
80 SPAIOT_DBG ("Multiplexer::begin(): opening");
81
82 pinMode (m_inh, OUTPUT);
83 digitalWrite (m_inh, HIGH);
84
85 for (unsigned int i = 0; i < m_spin.size(); i++) {
86
87 pinMode (m_spin.at (i), OUTPUT);
88 digitalWrite (m_spin.at (i), LOW);
89 }
90 m_isopened = true;
91 }
92 }
93
99 virtual void end() {
100
101 for (unsigned int i = 0; i < m_spin.size(); i++) {
102
103 pinMode (m_spin.at (i), INPUT_PULLUP);
104 }
105 ButtonController::end();
106 }
107
115 virtual int select (int button) {
116
117 if (isOpened() && (button >= 0) && (button <= W)) {
118
119 for (unsigned int i = 0; i < m_spin.size(); i++) {
120
121 digitalWrite (m_spin.at (i), (button & (1 << i)) ? HIGH : LOW);
122
123 }
124 digitalWrite (m_inh, LOW);
125 m_selected = button;
126 }
127 return selected();
128 }
129
133 virtual void deselect () {
134
135 if (isOpened()) {
136
137 digitalWrite (m_inh, HIGH);
138 m_selected = -1;
139 }
140 }
141
147 virtual bool isNull() const {
148
149 return m_spin.size() == 0;
150 }
151
159 virtual bool operator== (const ButtonController &other) const {
160
161 if (ButtonController::operator== (other)) {
162
163 const Multiplexer<W>& o = static_cast<const Multiplexer<W>&> (other);
164 return ( (m_spin == o.m_spin) && (m_inh == o.m_inh));
165 }
166 return false;
167 }
168
175 int selectPin (int key) const {
176
177 return m_spin.at (key);
178 }
179
186 void setSelectPin (int key, int pin) {
187
188 m_spin[key] = pin;
189 }
190
191 protected:
192 std::vector<int> m_spin;
193 int m_inh;
194 };
195
196}
Analog multiplexer template.
Definition: multiplexer.h:30
void setSelectPin(int key, int pin)
Definition: multiplexer.h:186
int size() const
Definition: multiplexer.h:67
virtual bool operator==(const ButtonController &other) const
Definition: multiplexer.h:159
Multiplexer()
Definition: multiplexer.h:58
Multiplexer(const std::initializer_list< int > &spins, int inhPin)
Definition: multiplexer.h:42
virtual void end()
Definition: multiplexer.h:99
virtual void begin()
Definition: multiplexer.h:77
virtual bool isNull() const
Definition: multiplexer.h:147
int selectPin(int key) const
Definition: multiplexer.h:175
virtual void deselect()
Definition: multiplexer.h:133
virtual int select(int button)
Definition: multiplexer.h:115
SpaIot name space.
Definition: bussettings.h:21