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 <string>
18#include <initializer_list>
19#include <type_traits>
20#include <Arduino.h>
21#include "debug.h"
22
23#include "buttoncontroller.h"
24
25namespace SpaIot {
26
31 template <int W>
32 class Multiplexer : public ButtonController {
33 public:
34
40 Multiplexer (const std::initializer_list<int>& spins, int inhPin) :
41 ButtonController(), m_spin (spins), m_inh (inhPin) {
42
43 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);
44 }
45
46 Multiplexer () :
47 ButtonController(), m_inh (-1) {
48 }
49
54 int size() const {
55
56 return W;
57 }
58
62 virtual void begin() {
63
64 if ( (isOpened() == false) && (isNull() == false)) {
65 SPAIOT_DBG ("Multiplexer::begin(): opening");
66
67 pinMode (m_inh, OUTPUT);
68 digitalWrite (m_inh, HIGH);
69
70 for (int i = 0; i < m_spin.size(); i++) {
71
72 pinMode (m_spin.at (i), OUTPUT);
73 digitalWrite (m_spin.at (i), LOW);
74 }
75 m_isopened = true;
76 }
77 }
78
84 virtual int select (int button) {
85
86 if (isOpened() && (button >= 0) && (button <= W)) {
87
88 for (int i = 0; i < m_spin.size(); i++) {
89
90 digitalWrite (m_spin.at (i), (button & (1 << i)) ? HIGH : LOW);
91
92 }
93 digitalWrite (m_inh, LOW);
94 m_selected = button;
95 }
96 return selected();
97 }
98
102 virtual void deselect () {
103
104 if (isOpened()) {
105
106 digitalWrite (m_inh, HIGH);
107 m_selected = -1;
108 }
109 }
110
115 virtual bool isNull() const {
116
117 return m_spin.size() == 0;
118 }
119
125 virtual bool operator== (const ButtonController &other) const {
126
127 if (ButtonController::operator== (other)) {
128
129 const Multiplexer<W>& o = static_cast<const Multiplexer<W>&> (other);
130 return ( (m_spin == o.m_spin) && (m_inh == o.m_inh));
131 }
132 return false;
133 }
134
140 int selectPin (int key) const {
141
142 return m_spin.at (key);
143 }
144
150 void setSelectPin (int key, int pin) {
151
152 m_spin[key] = pin;
153 }
154
155 protected:
156 std::vector<int> m_spin;
157 int m_inh;
158 };
159
160}
Analog multiplexer template.
Definition: multiplexer.h:32
void setSelectPin(int key, int pin)
Definition: multiplexer.h:150
int size() const
Definition: multiplexer.h:54
virtual bool operator==(const ButtonController &other) const
Definition: multiplexer.h:125
Multiplexer(const std::initializer_list< int > &spins, int inhPin)
Definition: multiplexer.h:40
virtual bool isNull() const
Definition: multiplexer.h:115
int selectPin(int key) const
Definition: multiplexer.h:140
virtual int select(int button)
Definition: multiplexer.h:84
SpaIot name space.
Definition: bussettings.h:21