Skip to content

Commit 4711c5a

Browse files
committed
Added I2C Scanner
1 parent 096de85 commit 4711c5a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/utility/CommUtil.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,37 @@ void CommUtil::scanID(bool *result) {
151151
*(result+i)=writeCommand(i,0x00);
152152
}
153153
}
154+
155+
// I2C Scanner, scavengered from https://github.com/MartyMacGyver/Arduino_I2C_Scanner
156+
void CommUtil::scan() {
157+
Serial.println();
158+
Serial.println("Scanning I2C Bus: ");
159+
Serial.println();
160+
Serial.print(" ");
161+
for (int i = 0; i < 0x10; i++) {
162+
Serial.printf(" %2x", i);
163+
}
164+
Serial.println();
165+
Serial.print(" ");
166+
for(uint8_t addr = 0x03; addr <= 0x77; addr++ ) {
167+
// Address the device
168+
Wire.beginTransmission(addr);
169+
// Check for ACK (detection of device), NACK or error
170+
uint8_t deviceStatus = Wire.endTransmission();
171+
172+
if (!(addr % 0x10)) { // Start of a line
173+
Serial.printf("%02x:", addr / 0x10);
174+
}
175+
if (deviceStatus == 0) {
176+
Serial.printf(" %02x", addr);
177+
} else if (deviceStatus == 4) {
178+
Serial.printf(" %02s", "??");
179+
} else {
180+
Serial.printf(" %02s", "--");
181+
}
182+
if (!((addr+1) % 0x10) ) {
183+
Serial.println();
184+
}
185+
}
186+
Serial.println();
187+
}

src/utility/CommUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CommUtil {
2222
bool readBytes(uint8_t address, uint8_t count,uint8_t * dest);
2323
bool readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest);
2424
void scanID(bool *result);
25+
void scan();
2526
private:
2627
};
2728
#endif

0 commit comments

Comments
 (0)