forked from AlexisTM/LIDAREnhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2CFunctions.h
179 lines (154 loc) · 5.69 KB
/
I2CFunctions.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef I2C_FUNCTIONS_H
#define I2C_FUNCTIONS_H
#include <Arduino.h>
#include <Wire.h>
#define STOP_CONDITION_I2C true
class I2CFunctions {
public:
I2CFunctions() {};
/*******************************************************************************
begin : Begin the I2C master device
If fasti2c is true, use 400kHz I2C
*******************************************************************************/
void begin(bool fasti2c = false){
Wire.begin();
if (fasti2c) {
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set I2C frequency to 400kHz, for the Due
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif
}
};
/*******************************************************************************
isOnline : check if something is connected at this address
returns true if online
flse if offline
*******************************************************************************/
bool isOnline(uint8_t Device = 0x62){
Wire.beginTransmission(Device);
if(Wire.endTransmission(STOP_CONDITION_I2C))
return false;
return true;
};
/*******************************************************************************
whoisOnline : check the array of Device to know if they are online
Debug only, it prints on Serial1
*******************************************************************************/
void whoisOnline(int number, uint8_t * Devices){
for(int i = 0; i < number; i++){
Serial.print("Device ");
Serial.print(Devices[i],HEX);
if(isOnline(Devices[i]))
Serial.println(" is ONLINE");
else
Serial.println(" is OFFLINE");
}
};
/*******************************************************************************
write : write a uint8_t to one I2C device at a certain address
Device : the I2C device address
regAdr : The I2C foreign register
data : The data to put in the regAdr register of the i2c (Device) device
returns the nack packet
*******************************************************************************/
uint8_t write(uint8_t Device, uint8_t regAdr, uint8_t data){
Wire.beginTransmission(Device);
Wire.write(regAdr);
Wire.write(data);
uint8_t nackCatcher = Wire.endTransmission(STOP_CONDITION_I2C);
return nackCatcher;
};
/*******************************************************************************
readByte : read one 8-bit uint8_t from one I2C device
Device : the I2C device address
regAdr : The I2C foreign register
data : The data array where to put data
returns the nack packet
*******************************************************************************/
uint8_t readByte(uint8_t Device, uint8_t regAdr, uint8_t * data){
Wire.beginTransmission(Device);
Wire.write(regAdr);
uint8_t nackCatcher = Wire.endTransmission(STOP_CONDITION_I2C);
Wire.requestFrom(Device, uint8_t(1), uint8_t(1));
data[0] = Wire.read();
return nackCatcher;
};
/*******************************************************************************
readWord : read two 8-bit uint8_t from one I2C device
Device : the I2C device address
regAdr : The I2C foreign register
data : The data array where to put data
returns the nack packet
*******************************************************************************/
uint8_t readWord(uint8_t Device, uint8_t regAdr, uint8_t * data){
Wire.beginTransmission(Device);
Wire.write(regAdr);
int nackCatcher = Wire.endTransmission(STOP_CONDITION_I2C);
Wire.requestFrom(Device, uint8_t(2), uint8_t(1));
data[0] = Wire.read();
data[1] = Wire.read();
return nackCatcher;
};
/*******************************************************************************
scan : debug function used to show which device is currently on the I2C bus
*******************************************************************************/
void scan(){
uint8_t error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission(STOP_CONDITION_I2C);
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
};
/*******************************************************************************
nackError : debug function, show the error if we got a NACK error
error : the uint8_t of error from the I2C device
returns the error
*******************************************************************************/
uint8_t nackError(uint8_t error) {
if (error) {
Serial.print("NackError : ");
switch (error) {
case 1:
Serial.println("Message is too long in transmit buffer");
break;
case 2:
Serial.println("NACK on transmit address");
break;
case 3:
Serial.println("NACK on transmit data");
break;
case 4:
Serial.println("Other error");
break;
default:
Serial.println("No Error");
}
}
return error;
};
};
I2CFunctions I2C;
extern I2CFunctions I2C;
#endif