Skip to content

Commit 6401b50

Browse files
committed
dont always print address, reorder conditional for performance boost marginal
1 parent 8987325 commit 6401b50

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ WipperSnapper_Component_I2C::scanAddresses() {
156156
// those that respond.
157157
WS_DEBUG_PRINTLN("EXEC: I2C Scan");
158158
for (uint8_t address = 1; address < 127; ++address) {
159-
WS_DEBUG_PRINT("Address: 0x");
160-
WS_DEBUG_PRINTLN(address);
161159
_i2c->beginTransmission(address);
162160
uint8_t endTransmissionRC = _i2c->endTransmission();
163161

@@ -170,24 +168,35 @@ WipperSnapper_Component_I2C::scanAddresses() {
170168
}
171169
#if defined(ARDUINO_ARCH_ESP32)
172170
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
173-
else if (endTransmissionRC == 1) {
174-
WS_DEBUG_PRINTLN("[i2c] ERROR: data too long to fit in transmit buffer!");
171+
else if (endTransmissionRC == 3) {
172+
// NOTE: The printf below is commented out for performance, this is the
173+
// default case and should typically be hit if the address is not found.
174+
// WS_DEBUG_PRINTLN("[i2c] ERROR: received NACK on transmit of data!");
175175
continue;
176176
} else if (endTransmissionRC == 2) {
177177
WS_DEBUG_PRINTLN("[i2c] ERROR: received NACK on transmit of address!");
178+
scanResp.bus_response =
179+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_UNSPECIFIED;
178180
continue;
179181
} else if (endTransmissionRC == 3) {
180-
WS_DEBUG_PRINTLN("[i2c] ERROR: received NACK on transmit of data!");
182+
WS_DEBUG_PRINTLN("[i2c] ERROR: data too long to fit in transmit buffer!");
183+
scanResp.bus_response =
184+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_UNSPECIFIED;
181185
continue;
182186
} else if (endTransmissionRC == 4) {
183-
WS_DEBUG_PRINTLN("[i2c] ERROR: Other error!");
187+
WS_DEBUG_PRINTLN("[i2c] ERROR: Unspecified bus error occured!");
188+
scanResp.bus_response =
189+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_UNSPECIFIED;
184190
continue;
185191
} else if (endTransmissionRC == 5) {
186192
WS_DEBUG_PRINTLN("[i2c] ERROR: I2C Bus has timed out!");
187-
scanResp.bus_response = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_ERROR_HANG;
193+
scanResp.bus_response =
194+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_ERROR_HANG;
188195
continue;
189196
} else {
190-
WS_DEBUG_PRINTLN("[i2c] ERROR: Unknown error!");
197+
WS_DEBUG_PRINTLN("[i2c] ERROR: An unknown bus error has occured!");
198+
scanResp.bus_response =
199+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_UNSPECIFIED;
191200
continue;
192201
}
193202
#endif

0 commit comments

Comments
 (0)