Wombat Environmental Node 1.0
An environmental node for SDI-12 and digital sensors.
Loading...
Searching...
No Matches
TCA9534.h
Go to the documentation of this file.
1// Modified from https://github.com/hideakitai/TCA9534/blob/master/TCA9534.h
2
3#ifndef TCA9534_H
4#define TCA9534_H
5
6#include <Arduino.h>
7#include <Wire.h>
8
9class TCA9534 {
10 uint8_t I2C_ADDR = 0x20;
11
12 TwoWire* wire;
13 uint8_t sts;
14
15public:
17 enum class Config { OUT, IN };
18 enum class Polarity { ORIGINAL, INVERSE };
19 enum Level { L = 0x00, H = 0xFF };
20
21 void attach(TwoWire& w) {
22 wire = &w;
23 }
24
25 void setDeviceAddress(const uint8_t addr) { I2C_ADDR = addr; }
26
27 uint8_t input(const uint8_t port)
28 {
29 return readBit(I2C_ADDR, (uint8_t)Reg::INPUT_PORT, port);
30 }
31 uint8_t input()
32 {
33 return readByte(I2C_ADDR, (uint8_t)Reg::INPUT_PORT);
34 }
35
36 uint8_t output(const uint8_t port, const uint8_t v)
37 {
38 return writeBit(I2C_ADDR, (uint8_t)Reg::OUTPUT_PORT, port, v & 0x01);
39 }
40 uint8_t output(const uint8_t v)
41 {
42 return writeByte(I2C_ADDR, (uint8_t)Reg::OUTPUT_PORT, v);
43 }
44 uint8_t output()
45 {
46 return readByte(I2C_ADDR, (uint8_t)Reg::OUTPUT_PORT);
47 }
48
49 uint8_t polarity(const uint8_t port, const Polarity pol)
50 {
51 return writeBit(I2C_ADDR, (uint8_t)Reg::POLARITY, port, (uint8_t)pol);
52 }
53 uint8_t polarity(const Polarity pol)
54 {
55 uint8_t d = (pol == Polarity::ORIGINAL) ? 0x00 : 0xFF;
56 return writeByte(I2C_ADDR, (uint8_t)Reg::POLARITY, (uint8_t)d);
57 }
58 uint8_t polarity()
59 {
60 return readByte(I2C_ADDR, (uint8_t)Reg::POLARITY);
61 }
62
63 uint8_t config(const uint8_t port, const Config cfg)
64 {
65 return writeBit(I2C_ADDR, (uint8_t)Reg::CONFIG, port, (uint8_t)cfg);
66 }
67 uint8_t config(const Config cfg)
68 {
69 uint8_t d = (cfg == Config::OUT) ? 0x00 : 0xFF;
70 return writeByte(I2C_ADDR, (uint8_t)Reg::CONFIG, (uint8_t)d);
71 }
72 uint8_t config()
73 {
74 return readByte(I2C_ADDR, (uint8_t)Reg::CONFIG);
75 }
76
77 uint8_t status() const { return sts; }
78
79private:
80
81 uint8_t readBit(uint8_t dev, uint8_t reg, uint8_t bit)
82 {
83 uint8_t b = readByte(dev, reg);
84 b &= (1 << bit);
85 return b;
86 }
87
88 uint8_t readByte(uint8_t dev, uint8_t reg)
89 {
90 uint8_t data;
91 readBytes(dev, reg, 1, &data);
92 return data;
93 }
94
95 int8_t readBytes(uint8_t dev, uint8_t reg, uint8_t size, uint8_t *data)
96 {
97 wire->beginTransmission(dev);
98 wire->write(reg);
99 wire->endTransmission();
100 wire->requestFrom(dev, size);
101 int8_t count = 0;
102 while (wire->available()) data[count++] = wire->read();
103 return count;
104 }
105
106 bool writeBit(uint8_t dev, uint8_t reg, uint8_t bit, uint8_t data)
107 {
108 uint8_t b = readByte(dev, reg);
109 b = (data != 0) ? (b | (1 << bit)) : (b & ~(1 << bit));
110 return writeByte(dev, reg, b);
111 }
112
113 bool writeByte(uint8_t dev, uint8_t reg, uint8_t data)
114 {
115 return writeBytes(dev, reg, 1, &data);
116 }
117
118 bool writeBytes(uint8_t dev, uint8_t reg, uint8_t size, uint8_t* data)
119 {
120 wire->beginTransmission(dev);
121 wire->write(reg);
122 for (uint8_t i = 0; i < size; i++)
123 wire->write(data[i]);
124 sts = wire->endTransmission();
125 if (sts != 0)
126 {
127 Serial.print("I2C ERROR : ");
128 Serial.println(sts);
129 }
130 return (sts == 0);
131 }
132
133};
134
135#endif //TCA9534_H
Definition: TCA9534.h:9
uint8_t config()
Definition: TCA9534.h:72
uint8_t I2C_ADDR
Definition: TCA9534.h:10
uint8_t sts
Definition: TCA9534.h:13
uint8_t config(const Config cfg)
Definition: TCA9534.h:67
uint8_t polarity()
Definition: TCA9534.h:58
Config
Definition: TCA9534.h:17
uint8_t input()
Definition: TCA9534.h:31
uint8_t status() const
Definition: TCA9534.h:77
int8_t readBytes(uint8_t dev, uint8_t reg, uint8_t size, uint8_t *data)
Definition: TCA9534.h:95
Level
Definition: TCA9534.h:19
@ H
Definition: TCA9534.h:19
@ L
Definition: TCA9534.h:19
uint8_t config(const uint8_t port, const Config cfg)
Definition: TCA9534.h:63
uint8_t output(const uint8_t port, const uint8_t v)
Definition: TCA9534.h:36
TwoWire * wire
Definition: TCA9534.h:12
uint8_t input(const uint8_t port)
Definition: TCA9534.h:27
void setDeviceAddress(const uint8_t addr)
Definition: TCA9534.h:25
uint8_t output(const uint8_t v)
Definition: TCA9534.h:40
Reg
Definition: TCA9534.h:16
uint8_t readBit(uint8_t dev, uint8_t reg, uint8_t bit)
Definition: TCA9534.h:81
bool writeBytes(uint8_t dev, uint8_t reg, uint8_t size, uint8_t *data)
Definition: TCA9534.h:118
uint8_t polarity(const Polarity pol)
Definition: TCA9534.h:53
bool writeByte(uint8_t dev, uint8_t reg, uint8_t data)
Definition: TCA9534.h:113
uint8_t readByte(uint8_t dev, uint8_t reg)
Definition: TCA9534.h:88
Polarity
Definition: TCA9534.h:18
uint8_t polarity(const uint8_t port, const Polarity pol)
Definition: TCA9534.h:49
bool writeBit(uint8_t dev, uint8_t reg, uint8_t bit, uint8_t data)
Definition: TCA9534.h:106
uint8_t output()
Definition: TCA9534.h:44
void attach(TwoWire &w)
Definition: TCA9534.h:21