roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
pcf8574.h
Go to the documentation of this file.
1#pragma once
2
3#include "Wire.h"
5#include "roo_time.h"
6
7namespace roo_control {
8
9/// PCF8574 I2C-controlled 8-bit port extender.
10///
11/// Datasheet: https://www.ti.com/lit/ds/symlink/pcf8574.pdf
12///
13/// Each port can be used as digital input, digital output, or
14/// quasi-bidirectional.
15///
16/// To use a given port as output, drive the corresponding bit from the
17/// extender, and read it from the slave. See the datasheet for details.
18///
19/// To use a given port as input, set the corresponding bit to HIGH (the
20/// initial value), and then use the slave to drive the line to VCC or GND.
21class Pcf8574 {
22 public:
23 /// Quasi-bidirectional port usable as both input and output.
24 ///
25 /// If your port is output-only, prefer `OutputPort` instead.
26 class Port : public BinarySwitch {
27 public:
28 Port(Pcf8574& extender, uint8_t port);
29
30 bool setState(BinaryLogicalState state) override;
31 bool getState(BinaryLogicalState& result) const override;
32
33 private:
34 Pcf8574& extender_;
35 const uint8_t port_;
36 };
37
38 /// Output-only port.
39 ///
40 /// Reading the state (getState()) returns the last set value without
41 /// communicating with the extender. Prefer this to `Port` for output-only
42 /// ports.
43 class OutputPort : public BinarySwitch {
44 public:
45 OutputPort(Pcf8574& extender, uint8_t port);
46
47 bool setState(BinaryLogicalState state) override;
48 bool getState(BinaryLogicalState& result) const override;
49
50 private:
51 Pcf8574& extender_;
52 const uint8_t port_;
53 };
54
55 /// Creates the extender on the specified TwoWire bus and I2C address.
56 Pcf8574(TwoWire& wire, uint8_t addr);
57
58 /// Returns the maximum allowed staleness of cached reads.
59 ///
60 /// Useful to reduce unnecessary repetitive I2C reads. Defaults to 20 ms.
61 roo_time::Duration getReadCacheDuration() const {
62 return last_read_cache_duration_;
63 }
64
65 /// Sets the maximum allowed staleness of cached reads.
66 void setReadCacheDuration(roo_time::Duration duration) {
67 last_read_cache_duration_ = duration;
68 }
69
70 /// Reads, caches, and returns levels of all ports.
71 ///
72 /// If called within the read-cache interval, may return cached results.
73 /// Returns false on a communication failure.
74 bool read(uint8_t& data);
75
76 /// Writes the levels of all ports.
77 ///
78 /// For input ports, the corresponding bits should be HIGH to allow the slave
79 /// to keep driving the actual line level. Returns false on failure.
80 bool write(uint8_t data);
81
82 /// Returns the most recently read byte, or 0xFF if never read.
83 uint8_t last_read() const { return last_read_; }
84
85 /// Returns the most recently written byte, or 0xFF if never written.
86 uint8_t last_written() const { return last_written_; }
87
88 /// Reads the level of the specified port.
89 ///
90 /// For output ports, the value generally reflects what was last written, but
91 /// it is always read from the extender. On failure, returns the last known
92 /// state.
93 BinaryLogicalState readPort(uint8_t port);
94
95 /// Writes the level of the specified port.
96 ///
97 /// Returns false on a communication failure. For input ports, it is OK to
98 /// write HIGH to allow the slave to keep driving the level seen by readPort.
99 /// Writing LOW will force the LOW state.
100 bool writePort(uint8_t port, BinaryLogicalState state);
101
102 private:
103 // The I2C interface.
104 TwoWire& wire_;
105
106 // The I2C device address.
107 uint8_t address_;
108
109 // The byte that was last written (initially 0xFF).
110 uint8_t last_written_;
111
112 // The byte that was last read (initially 0xFF).
113 uint8_t last_read_;
114
115 // The time of last read, or Uptime::Start if never read or if the cached
116 // value (last_read_) is considered invalid.
117 roo_time::Uptime last_read_time_;
118
119 // Maximum staleness of values returned by read().
120 roo_time::Duration last_read_cache_duration_;
121};
122
123} // 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
Quasi-bidirectional port usable as both input and output.
Definition pcf8574.h:26
bool getState(BinaryLogicalState &result) const override
Definition pcf8574.cpp:13
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
void setReadCacheDuration(roo_time::Duration duration)
Sets the maximum allowed staleness of cached reads.
Definition pcf8574.h:66
uint8_t last_written() const
Returns the most recently written byte, or 0xFF if never written.
Definition pcf8574.h:86
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
roo_time::Duration getReadCacheDuration() const
Returns the maximum allowed staleness of cached reads.
Definition pcf8574.h:61
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.