forked from hotchpotch/Arduino-i2cdetect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2cdetect.cpp
More file actions
57 lines (50 loc) · 1.2 KB
/
i2cdetect.cpp
File metadata and controls
57 lines (50 loc) · 1.2 KB
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
#include <Wire.h>
#include <Arduino.h>
#include "i2cdetect.h"
// _printf base code: http://playground.arduino.cc/Main/Printf
#include <stdarg.h>
#define PRINTF_BUF 80 // define the tmp buffer size (change if desired)
void _printf(const char *format, ...)
{
char buf[PRINTF_BUF];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap);
for(char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines
{
if(*p == '\n')
Serial.write('\r');
Serial.write(*p);
}
va_end(ap);
}
void i2cdetect(TwoWire &wire, uint8_t first, uint8_t last) {
uint8_t i, j, address, error;
// header
Serial.print(" ");
for (i = 0; i < 16; i++) {
printf("%3x", i);
}
Serial.println("");
for (j = 0; j < 8; j++) {
_printf("%02d:", j*10);
for (i = 0; i < 16; i++) {
address = i + j*16;
if (address >= first && address <= last) {
wire.beginTransmission(address);
error = wire.endTransmission();
if (error) {
Serial.print(" --");
} else {
_printf(" %02x", address);
}
} else {
Serial.print(" ");
}
}
Serial.println("");
}
}
void i2cdetect() {
i2cdetect(0x03, 0x77);
}