supla-device
Loading...
Searching...
No Matches
io_pin.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16*/
17
18#ifndef SRC_SUPLA_IO_IO_PIN_H_
19#define SRC_SUPLA_IO_IO_PIN_H_
20
21#include <stdint.h>
22
23namespace Supla {
24namespace Io {
25
26class Base;
27
28struct IoPin {
29 enum Flag : uint8_t {
30 IsSet = 1 << 0,
31 PullUp = 1 << 1,
32 ActiveHigh = 1 << 2,
33 };
34
35 uint8_t pin = 0;
36 uint8_t flags = ActiveHigh;
37 uint8_t mode = 0;
38 Base *io = nullptr;
39
40 IoPin() = default;
41 explicit IoPin(int pin, Base *io = nullptr) : io(io) {
42 setPin(pin);
43 }
44
45 bool isSet() const {
46 return (flags & IsSet) != 0;
47 }
48 void setIsSet(bool value) {
49 if (value) {
50 flags |= IsSet;
51 } else {
52 flags &= ~IsSet;
53 }
54 }
55
56 int getPin() const {
57 return isSet() ? static_cast<int>(pin) : -1;
58 }
59 void setPin(int value) {
60 if (value < 0) {
61 pin = 0;
62 setIsSet(false);
63 } else {
64 pin = static_cast<uint8_t>(value);
65 setIsSet(true);
66 }
67 }
68
69 bool isPullUp() const {
70 return (flags & PullUp) != 0;
71 }
72 void setPullUp(bool value) {
73 if (value) {
74 flags |= PullUp;
75 } else {
76 flags &= ~PullUp;
77 }
78 }
79
80 bool isActiveHigh() const {
81 return (flags & ActiveHigh) != 0;
82 }
83 void setActiveHigh(bool value) {
84 if (value) {
85 flags |= ActiveHigh;
86 } else {
87 flags &= ~ActiveHigh;
88 }
89 }
90
91 void setMode(uint8_t value) {
92 mode = value;
93 }
94 uint8_t getMode() const {
95 return mode;
96 }
97
98 bool operator==(const IoPin &other) const {
99 return io == other.io && getPin() == other.getPin();
100 }
101 bool operator!=(const IoPin &other) const {
102 return !(*this == other);
103 }
104
105 void setPwmResolutionBits(uint8_t resolutionBits);
106 void setPwmFrequency(uint32_t frequencyHz);
107 void configureAnalogOutput(int channelNumber = -1) const;
108 void pinMode(int channelNumber = -1) const;
109 int digitalRead(int channelNumber = -1) const;
110 void digitalWrite(uint8_t value, int channelNumber = -1) const;
111 void analogWrite(int value, int channelNumber = -1) const;
112 uint8_t pwmResolutionBits() const;
113 uint32_t pwmMaxValue() const;
114 void writeActive(int channelNumber = -1) const;
115 void writeInactive(int channelNumber = -1) const;
116 bool readActive(int channelNumber = -1) const;
117};
118
119} // namespace Io
120} // namespace Supla
121
122#endif // SRC_SUPLA_IO_IO_PIN_H_
Definition io.h:36