-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpca9555.cpp
103 lines (89 loc) · 2.4 KB
/
pca9555.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
/*
* author : Shuichi TAKANO
* since : Mon Aug 29 2022 02:42:51
*/
#include "pca9555.h"
#include "i2c_manager.h"
#include "debug.h"
#include <cassert>
namespace device
{
namespace
{
enum class Command
{
INPUT_PORT_0,
INPUT_PORT_1,
OUTPUT_PORT_0,
OUTPUT_PORT_1,
POLARITY_INVERSION_0,
POLARITY_INVERSION_1,
CONFIGURATION_0,
CONFIGURATION_1,
};
inline constexpr int I2C_PRIO = 1;
}
bool
PCA9555::init(int addrSel)
{
int addr = 0b0100000 | addrSel;
addr_ = addr;
setPortDir(0xffff);
uint8_t rxdata{};
auto r = getI2CManager().readBlocking(addr, &rxdata, 1);
DPRINT(("PCA9555 init[%02x]: %d\n", addr, r));
if (r < 0)
{
addr_ = 0;
return false;
}
addr_ = addr;
return true;
}
bool
PCA9555::setPortDirNonBlocking(uint16_t inputPortBits) const
{
assert(*this);
uint8_t data[] =
{
static_cast<uint8_t>(Command::CONFIGURATION_0),
static_cast<uint8_t>(inputPortBits & 0xFF),
static_cast<uint8_t>(inputPortBits >> 8),
};
return getI2CManager().sendNonBlocking(I2C_PRIO, addr_, data, 3);
}
void
PCA9555::setPortDir(uint16_t inputPortBits) const
{
assert(*this);
uint8_t data[] =
{
static_cast<uint8_t>(Command::CONFIGURATION_0),
static_cast<uint8_t>(inputPortBits & 0xFF),
static_cast<uint8_t>(inputPortBits >> 8),
};
getI2CManager().sendBlocking(addr_, data, 3, false);
}
void
PCA9555::output(uint16_t bits) const
{
assert(*this);
uint8_t data[] =
{
static_cast<uint8_t>(Command::OUTPUT_PORT_0),
static_cast<uint8_t>(bits & 0xFF),
static_cast<uint8_t>(bits >> 8),
};
getI2CManager().sendBlocking(addr_, data, 3, false);
}
int
PCA9555::input() const
{
assert(*this);
uint8_t cmd = static_cast<uint8_t>(Command::INPUT_PORT_0);
getI2CManager().sendBlocking(addr_, &cmd, 1, true);
uint8_t data[2] = {};
getI2CManager().readBlocking(addr_, data, 2);
return data[0] | (data[1] << 8);
}
}