roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
pcf8574.cpp
Go to the documentation of this file.
2
3namespace roo_control {
4
5using roo_time::Duration;
6using roo_time::Millis;
7using roo_time::Uptime;
8
10 return extender_.writePort(port_, state);
11}
12
14 result = extender_.readPort(port_);
15 return true;
16}
17
18Pcf8574::Port::Port(Pcf8574& extender, uint8_t port)
19 : extender_(extender), port_(port) {}
20
22 return extender_.writePort(port_, state);
23}
24
26 result = ((extender_.last_written_ & (1 << port_)) != 0) ? BINARY_STATE_HIGH
28 return true;
29}
30
32 : extender_(extender), port_(port) {}
33
34Pcf8574::Pcf8574(TwoWire& wire, uint8_t addr)
35 : wire_(wire),
36 address_(addr),
37 last_written_(0xFF),
38 last_read_(0xFF),
39 last_read_time_(Uptime::Start()),
40 last_read_cache_duration_(Millis(20)) {}
41
42// // Reads the level of the specified port. For ports used as output, the value
43// // will reflect what was last written.
44// // On a communication failure, returns the last known state.
45// BinaryLogicalState readPort(uint8_t port);
46
47// // Writes the level of the specified output port. Returns false on a
48// // communication failure. For input ports, it is OK to write HIGH - that will
49// // allow the slave to continue driving the level actaully seen by readPort.
50// // Writing LOW will force the LOW state.
51// bool writePort(uint8_t port, BinaryLogicalState state);
52
54 uint8_t data;
55 if (!read(data)) {
56 data = last_read();
57 }
58 uint8_t mask = (1 << port);
59 return ((data & mask) != 0) ? BINARY_STATE_HIGH : BINARY_STATE_LOW;
60}
61
62bool Pcf8574::writePort(uint8_t port, BinaryLogicalState state) {
63 if (state == BINARY_STATE_HIGH) {
64 uint8_t mask = (1 << port);
65 if ((last_written_ & mask) != 0) {
66 // No change since last write.
67 return true;
68 }
69 return write(last_written_ | mask);
70 } else {
71 uint8_t mask = ~(1 << port);
72 if ((last_written_ & mask) == 0) {
73 // No change since last write.
74 return true;
75 }
76 return write(last_written_ & mask);
77 }
78}
79
80bool Pcf8574::read(uint8_t& data) {
81 Uptime now = Uptime::Now();
82 if (last_read_time_ + last_read_cache_duration_ > now &&
83 last_read_time_ > Uptime::Start()) {
84 // Cached value is valid; return it.
85 data = last_read_;
86 return true;
87 }
88 wire_.requestFrom(address_, (uint8_t)1);
89 if (!wire_.available()) {
90 LOG(ERROR) << "I2C read error";
91 return false;
92 }
93 last_read_ = wire_.read();
94 last_read_time_ = now;
95 data = last_read_;
96 return true;
97}
98
99bool Pcf8574::write(uint8_t data) {
100 wire_.beginTransmission(address_);
101 wire_.write(data);
102 if (wire_.endTransmission() != 0) {
103 LOG(ERROR) << "I2C write error";
104 return false;
105 }
106 last_written_ = data;
107 // Invalidate the read cache, since the change of external state is likely
108 // to have an effect also on the inputs.
109 last_read_time_ = Uptime::Start();
110 // Still, update the cached value to incorporate zeroed bits, in case it is
111 // returned due to I2C read failure.
112 last_read_ &= last_written_;
113 return true;
114}
115
116} // namespace roo_control
bool setState(BinaryLogicalState state) override
Updates the state of the switch. Returns true on success.
Definition pcf8574.cpp:21
bool getState(BinaryLogicalState &result) const override
Definition pcf8574.cpp:25
OutputPort(Pcf8574 &extender, uint8_t port)
Definition pcf8574.cpp:31
bool getState(BinaryLogicalState &result) const override
Definition pcf8574.cpp:13
Port(Pcf8574 &extender, uint8_t port)
Definition pcf8574.cpp:18
bool setState(BinaryLogicalState state) override
Updates the state of the switch. Returns true on success.
Definition pcf8574.cpp:9
PCF8574 I2C-controlled 8-bit port extender.
Definition pcf8574.h:21
bool write(uint8_t data)
Writes the levels of all ports.
Definition pcf8574.cpp:99
bool writePort(uint8_t port, BinaryLogicalState state)
Writes the level of the specified port.
Definition pcf8574.cpp:62
Pcf8574(TwoWire &wire, uint8_t addr)
Creates the extender on the specified TwoWire bus and I2C address.
Definition pcf8574.cpp:34
bool read(uint8_t &data)
Reads, caches, and returns levels of all ports.
Definition pcf8574.cpp:80
BinaryLogicalState readPort(uint8_t port)
Reads the level of the specified port.
Definition pcf8574.cpp:53
uint8_t last_read() const
Returns the most recently read byte, or 0xFF if never read.
Definition pcf8574.h:83
BinaryLogicalState
Binary logical state used by selectors and switches.