-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathI2C.cpp
100 lines (82 loc) · 2.58 KB
/
I2C.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
/*
I2C.cpp - Convenience library for dealing with I2C-connected sensors
Created by Myles Grant <[email protected]>
See also: https://github.com/grantmd/QuadCopter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 "WProgram.h"
#include "I2C.h"
#include <Wire.h>
I2C::I2C(){
}
void I2C::setAddress(byte address){
_address = address;
}
// The two sensors I have both let you read their address from register 0
byte I2C::getAddressFromDevice(){
sendReadRequest(0x00);
return readByte();
}
// Verify that the address we get back from the device matches what we have
boolean I2C::validateDevice(){
byte got = getAddressFromDevice();
return got == _address ? true : false;
}
// Write a setting to the device at register data_address
byte I2C::writeSetting(byte data_address, byte data_value){
Wire.beginTransmission(_address);
Wire.send(data_address);
Wire.send(data_value);
return Wire.endTransmission();
}
// Tell the device that we will be reading from register data_address
byte I2C::sendReadRequest(byte data_address){
Wire.beginTransmission(_address);
Wire.send(data_address);
return Wire.endTransmission();
}
// Request 2 bytes and read it
word I2C::readWord(){
requestBytes(2);
return ((Wire.receive() << 8) | Wire.receive());
}
// Request 2 bytes and read it
word I2C::readWordFlip(){
requestBytes(2);
byte msb = Wire.receive();
byte lsb = Wire.receive();
return ((lsb << 8) | msb);
}
// Request a byte and read it
byte I2C::readByte(){
requestBytes(1);
return Wire.receive();
}
// Request some number of bytes
void I2C::requestBytes(byte bytes){
Wire.beginTransmission(_address);
Wire.requestFrom(_address, bytes);
}
// Read the next available byte
byte I2C::readNextByte(){
return Wire.receive();
}
// Read the next available 2 bytes
word I2C::readNextWord(){
return ((Wire.receive() << 8) | Wire.receive());
}
// Read the next available 2 bytes
word I2C::readNextWordFlip(){
byte msb = Wire.receive();
byte lsb = Wire.receive();
return ((lsb << 8) | msb);
}