-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCA9548.cpp
145 lines (125 loc) · 2.21 KB
/
TCA9548.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "TCA9548.h"
//
// FILE: TCA9548.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2021-03-16
// PURPOSE: Library for TCA9548 I2C multiplexer
//
// HISTORY:
// 0.1.0 2021-03-16 initial version
#include "TCA9548.h"
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
extern "C" {
#include <linux/i2c-dev.h>
}
#include <sys/ioctl.h>
TCA9548::TCA9548(const char *i2c_device_filepath, const uint8_t deviceAddress)
: _address(deviceAddress), i2c_file(0), i2c_filepath(i2c_device_filepath)
{
_mask = 0x00;
_resetPin = -1;
_forced = false;
}
bool TCA9548::begin(uint8_t mask)
{
//Open i2c device
int file;
if ((file = open(i2c_filepath.data(), O_RDWR)) < 0)
{
return false;
}
i2c_file = file;
//Set address
if (ioctl(file, I2C_SLAVE_FORCE, _address) < 0)
{
return false;
}
if (!isConnected())
return false;
setChannelMask(mask);
return true;
}
int TCA9548::i2c_read()
{
uint8_t reg_val = 0;
int result = write(i2c_file, &_address, 1);
if (result < 0)
{
return result;
}
uint8_t data;
result = read(i2c_file, &data, 1);
if (result < 0)
{
return result;
}
else
{
reg_val = data;
}
return reg_val;
}
int TCA9548::i2c_write(uint8_t data)
{
uint8_t buff[2];
buff[0] = _address;
buff[1] = data;
int result = write(i2c_file, buff, 2);
if (result < 0)
{
return result;
}
return 0;
}
bool TCA9548::isConnected()
{
int retval = i2c_read();
return (retval >= 0);
}
void TCA9548::enableChannel(uint8_t channel)
{
if (isEnabled(channel))
return;
setChannelMask(_mask | (0x01 << channel));
}
void TCA9548::disableChannel(uint8_t channel)
{
if (!isEnabled(channel))
return;
setChannelMask(_mask & ~(0x01 << channel));
}
void TCA9548::selectChannel(uint8_t channel)
{
setChannelMask(0x01 << channel);
}
bool TCA9548::isEnabled(uint8_t channel)
{
if (channel > 7)
return false;
return (_mask & (0x01 << channel));
}
void TCA9548::setChannelMask(uint8_t mask)
{
if ((_mask == mask) && (!_forced))
return;
_mask = mask;
i2c_write(mask);
}
uint8_t TCA9548::getChannelMask()
{
return _mask;
}
int TCA9548::getError()
{
int e = _error;
_error = 0;
return e;
}
// -- END OF FILE --