5using roo_time::Duration;
14 result = extender_.readPort(port_);
19 : extender_(extender), port_(port) {}
22 return extender_.writePort(port_, state);
32 : extender_(extender), port_(port) {}
39 last_read_time_(Uptime::Start()),
40 last_read_cache_duration_(Millis(20)) {}
58 uint8_t mask = (1 << port);
64 uint8_t mask = (1 << port);
65 if ((last_written_ & mask) != 0) {
69 return write(last_written_ | mask);
71 uint8_t mask = ~(1 << port);
72 if ((last_written_ & mask) == 0) {
76 return write(last_written_ & mask);
81 Uptime now = Uptime::Now();
82 if (last_read_time_ + last_read_cache_duration_ > now &&
83 last_read_time_ > Uptime::Start()) {
88 wire_.requestFrom(address_, (uint8_t)1);
89 if (!wire_.available()) {
90 LOG(ERROR) <<
"I2C read error";
93 last_read_ = wire_.read();
94 last_read_time_ = now;
100 wire_.beginTransmission(address_);
102 if (wire_.endTransmission() != 0) {
103 LOG(ERROR) <<
"I2C write error";
106 last_written_ = data;
109 last_read_time_ = Uptime::Start();
112 last_read_ &= last_written_;
bool setState(BinaryLogicalState state) override
Updates the state of the switch. Returns true on success.
bool getState(BinaryLogicalState &result) const override
OutputPort(Pcf8574 &extender, uint8_t port)
bool getState(BinaryLogicalState &result) const override
Port(Pcf8574 &extender, uint8_t port)
bool setState(BinaryLogicalState state) override
Updates the state of the switch. Returns true on success.
PCF8574 I2C-controlled 8-bit port extender.
bool write(uint8_t data)
Writes the levels of all ports.
bool writePort(uint8_t port, BinaryLogicalState state)
Writes the level of the specified port.
Pcf8574(TwoWire &wire, uint8_t addr)
Creates the extender on the specified TwoWire bus and I2C address.
bool read(uint8_t &data)
Reads, caches, and returns levels of all ports.
BinaryLogicalState readPort(uint8_t port)
Reads the level of the specified port.
uint8_t last_read() const
Returns the most recently read byte, or 0xFF if never read.
BinaryLogicalState
Binary logical state used by selectors and switches.