-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSparFun_AS7341X_IO.cpp
126 lines (106 loc) · 3.56 KB
/
SparFun_AS7341X_IO.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
/*
This is a library written for the AMS AS7341X 10-Channel Spectral Sensor Frontend
SparkFun sells these at its website:
https://www.sparkfun.com/products/17719
Do you like this library? Help support open source hardware. Buy a board!
Written by Ricardo Ramos @ SparkFun Electronics, March 15th, 2021
This file defines all I2C communication functions implementations used in the AS7341X sensor library.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SparkFun_AS7341X_Constants.h"
#include "SparkFun_AS7341X_IO.h"
bool SparkFun_AS7341X_IO::begin(byte AS7341X_address, TwoWire& wirePort)
{
_i2cPort = &wirePort;
_address = AS7341X_address;
return isConnected();
}
bool SparkFun_AS7341X_IO::isConnected()
{
_i2cPort->beginTransmission(_address);
if (_i2cPort->endTransmission() != 0)
return (false);
return (true);
}
void SparkFun_AS7341X_IO::writeMultipleBytes(byte registerAddress, const byte* buffer, byte const packetLength)
{
setBankConfiguration(registerAddress);
_i2cPort->beginTransmission(_address);
_i2cPort->write(registerAddress);
for (byte i = 0; i < packetLength; i++)
_i2cPort->write(buffer[i]);
_i2cPort->endTransmission();
}
void SparkFun_AS7341X_IO::readMultipleBytes(byte registerAddress, byte* buffer, byte const packetLength)
{
setBankConfiguration(registerAddress);
_i2cPort->beginTransmission(_address);
_i2cPort->write(registerAddress);
_i2cPort->endTransmission();
_i2cPort->requestFrom(_address, packetLength);
for (byte i = 0; (i < packetLength) && _i2cPort->available(); i++)
buffer[i] = _i2cPort->read();
}
byte SparkFun_AS7341X_IO::readSingleByte(byte registerAddress)
{
setBankConfiguration(registerAddress);
byte result;
_i2cPort->beginTransmission(_address);
_i2cPort->write(registerAddress);
_i2cPort->endTransmission();
_i2cPort->requestFrom(_address, 1U);
result = _i2cPort->read();
return result;
}
void SparkFun_AS7341X_IO::writeSingleByte(byte registerAddress, byte const value)
{
setBankConfiguration(registerAddress);
_i2cPort->beginTransmission(_address);
_i2cPort->write(registerAddress);
_i2cPort->write(value);
_i2cPort->endTransmission();
}
void SparkFun_AS7341X_IO::setRegisterBit(byte registerAddress, byte const bitPosition)
{
byte value = readSingleByte(registerAddress);
value |= (1 << bitPosition);
writeSingleByte(registerAddress, value);
}
void SparkFun_AS7341X_IO::clearRegisterBit(byte registerAddress, byte const bitPosition)
{
byte value = readSingleByte(registerAddress);
value &= ~(1 << bitPosition);
writeSingleByte(registerAddress, value);
}
bool SparkFun_AS7341X_IO::isBitSet(byte registerAddress, byte const bitPosition)
{
byte value = readSingleByte(registerAddress);
byte mask = 1 << bitPosition;
if ((value & mask) != 0)
return true;
else
return false;
}
void SparkFun_AS7341X_IO::setBankConfiguration(byte regAddress)
{
byte result;
_i2cPort->beginTransmission(_address);
_i2cPort->write(REGISTER_CFG_0);
_i2cPort->endTransmission();
_i2cPort->requestFrom(_address, 1U);
result = _i2cPort->read();
byte value;
if (regAddress >= 0x60 && regAddress <= 0x74)
value |= (1 << 4);
else
value &= ~(1 << 4);
_i2cPort->beginTransmission(_address);
_i2cPort->write(REGISTER_CFG_0);
_i2cPort->write(value);
_i2cPort->endTransmission();
}