supla-device
Loading...
Searching...
No Matches
io_pin.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_IO_IO_PIN_H_
5#define SRC_SUPLA_IO_IO_PIN_H_
6
7#include <stdint.h>
8
9namespace Supla {
10namespace Io {
11
12class Base;
13
14struct IoPin {
15 enum Flag : uint8_t {
16 IsSet = 1 << 0,
17 PullUp = 1 << 1,
18 ActiveHigh = 1 << 2,
19 };
20
21 uint8_t pin = 0;
22 uint8_t flags = ActiveHigh;
23 uint8_t mode = 0;
24 Base *io = nullptr;
25
26 IoPin() = default;
27 explicit IoPin(int pin, Base *io = nullptr) : io(io) {
28 setPin(pin);
29 }
30
31 bool isSet() const {
32 return (flags & IsSet) != 0;
33 }
34 void setIsSet(bool value) {
35 if (value) {
36 flags |= IsSet;
37 } else {
38 flags &= ~IsSet;
39 }
40 }
41
42 int getPin() const {
43 return isSet() ? static_cast<int>(pin) : -1;
44 }
45 void setPin(int value) {
46 if (value < 0) {
47 pin = 0;
48 setIsSet(false);
49 } else {
50 pin = static_cast<uint8_t>(value);
51 setIsSet(true);
52 }
53 }
54
55 bool isPullUp() const {
56 return (flags & PullUp) != 0;
57 }
58 void setPullUp(bool value) {
59 if (value) {
60 flags |= PullUp;
61 } else {
62 flags &= ~PullUp;
63 }
64 }
65
66 bool isActiveHigh() const {
67 return (flags & ActiveHigh) != 0;
68 }
69 void setActiveHigh(bool value) {
70 if (value) {
71 flags |= ActiveHigh;
72 } else {
73 flags &= ~ActiveHigh;
74 }
75 }
76
77 void setMode(uint8_t value) {
78 mode = value;
79 }
80 uint8_t getMode() const {
81 return mode;
82 }
83
84 bool operator==(const IoPin &other) const {
85 return io == other.io && getPin() == other.getPin();
86 }
87 bool operator!=(const IoPin &other) const {
88 return !(*this == other);
89 }
90
91 void setPwmResolutionBits(uint8_t resolutionBits);
92 void setPwmFrequency(uint32_t frequencyHz);
93 void configureAnalogOutput(int channelNumber = -1) const;
94 void pinMode(int channelNumber = -1) const;
95 int digitalRead(int channelNumber = -1) const;
96 void digitalWrite(uint8_t value, int channelNumber = -1) const;
97 void analogWrite(int value, int channelNumber = -1) const;
98 uint8_t pwmResolutionBits() const;
99 uint32_t pwmMaxValue() const;
100 void writeActive(int channelNumber = -1) const;
101 void writeInactive(int channelNumber = -1) const;
102 bool readActive(int channelNumber = -1) const;
103};
104
105} // namespace Io
106} // namespace Supla
107
108#endif // SRC_SUPLA_IO_IO_PIN_H_
Definition io.h:23
Definition io_pin.h:14