forked from NorthernWidget/TCA9534
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCA9534.cpp
More file actions
110 lines (90 loc) · 2.39 KB
/
TCA9534.cpp
File metadata and controls
110 lines (90 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <Arduino.h>
#include <TCA9534.h>
#include <Wire.h>
TCA9534::TCA9534(int _ADR) {
ADR = _ADR;
Wire.begin();
}
// Begin comms with the device and test for presence
int TCA9534::Begin(void) {
Wire.beginTransmission(ADR);
if(Wire.endTransmission() != 0) return -1;
else return 1;
}
// Set pin mode to input or output
int TCA9534::PinMode(int Pin, boolean PinType) {
// Check if specified pin is in range of avalable pins
if(Pin > 7 || Pin < 0) {
return -1;
}
// Set pin to input
if(PinType == INPUT) {
PinModeConf = PinModeConf | (0x01 << Pin);
SetDirection(PinModeConf);
return 1;
}
// Set pin to output
else if(PinType == OUTPUT)
{
PinModeConf = PinModeConf & ~(0x01 << Pin);
SetDirection(PinModeConf);
return 0;
}
// Returns -1 (fail) if pin type was not or wrongly specified
else return -1;
}
// Write state to specified pin
int TCA9534::DigitalWrite(int Pin, boolean State)
{
// Check if specified pin is in range of avalable pins
if(Pin > 7 || Pin < 0) {
return -1;
}
// If state is HIGH write according shifted port via SetPort()
if(State == HIGH) {
Port = Port | (0x01 << Pin);
SetPort(Port);
return 1;
}
// If state is LOW write according shifted port via SetPort()
else if(State == LOW) {
Port = Port & ~(0x01 << Pin);
SetPort(Port);
return 0;
}
// Returns -1 (fail) if pin type was not or wrongly specified
else return -1;
}
// Read state from specified pin
int TCA9534::DigitalRead(int Pin)
{
// Check if specified pin is in range of avalable pins
if(Pin > 7 || Pin < 0) {
return -1;
}
uint8_t inRegister = ReadAll();
return (( -inRegister >> Pin) & 0x01 );
}
// Read state of the whole input port register
uint8_t TCA9534::ReadAll() {
// Initiate transmission, set input port and the read from register
Wire.beginTransmission(ADR);
Wire.write(0x00); // 0x00 = Input Port
Wire.endTransmission();
Wire.requestFrom(ADR, 1);
return Wire.read();
}
// Set port via output register (0x01)
int TCA9534::SetPort(int Config) {
Wire.beginTransmission(ADR);
Wire.write(0x01); // 0x01 = Output Port
Wire.write(Config);
return Wire.endTransmission();
}
// Set direction via configuration register (0x03)
int TCA9534::SetDirection(int Config) {
Wire.beginTransmission(ADR);
Wire.write(0x03); // 0x03 = Configuration
Wire.write(Config);
return Wire.endTransmission();
}