Read multiple bytes from an 8-bit device register. * @param devAddr I2C slave device address * @param regAddr First register regAddr to read from * @param length Number of bytes to read * @param data Buffer to store read data in * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Number of bytes read (-1
| 207 | * @return Number of bytes read (-1 indicates failure) |
| 208 | */ |
| 209 | int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout, void *wireObj) { |
| 210 | #ifdef I2CDEV_SERIAL_DEBUG |
| 211 | Serial.print("I2C (0x"); |
| 212 | Serial.print(devAddr, HEX); |
| 213 | Serial.print(") reading "); |
| 214 | Serial.print(length, DEC); |
| 215 | Serial.print(" bytes from 0x"); |
| 216 | Serial.print(regAddr, HEX); |
| 217 | Serial.print("..."); |
| 218 | #endif |
| 219 | |
| 220 | int8_t count = 0; |
| 221 | uint32_t t1 = millis(); |
| 222 | |
| 223 | #if (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) |
| 224 | TwoWire *useWire = &Wire; |
| 225 | if (wireObj) useWire = (TwoWire *)wireObj; |
| 226 | |
| 227 | #if (ARDUINO < 100) |
| 228 | // Arduino v00xx (before v1.0), Wire library |
| 229 | |
| 230 | // I2C/TWI subsystem uses internal buffer that breaks with large data requests |
| 231 | // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in |
| 232 | // smaller chunks instead of all at once |
| 233 | for (int k = 0; k < length; k += min((int)length, I2CDEVLIB_WIRE_BUFFER_LENGTH)) { |
| 234 | useWire->beginTransmission(devAddr); |
| 235 | useWire->send(regAddr); |
| 236 | useWire->endTransmission(); |
| 237 | useWire->beginTransmission(devAddr); |
| 238 | useWire->requestFrom((uint8_t)devAddr, (uint8_t)min((int)length - k, I2CDEVLIB_WIRE_BUFFER_LENGTH)); |
| 239 | |
| 240 | for (; useWire->available() && (timeout == 0 || millis() - t1 < timeout); count++) { |
| 241 | data[count] = useWire->receive(); |
| 242 | #ifdef I2CDEV_SERIAL_DEBUG |
| 243 | Serial.print(data[count], HEX); |
| 244 | if (count + 1 < length) Serial.print(" "); |
| 245 | #endif |
| 246 | } |
| 247 | } |
| 248 | #elif (ARDUINO == 100) |
| 249 | // Arduino v1.0.0, Wire library |
| 250 | // Adds standardized write() and read() stream methods instead of send() and receive() |
| 251 | |
| 252 | // I2C/TWI subsystem uses internal buffer that breaks with large data requests |
| 253 | // so if user requests more than I2CDEVLIB_WIRE_BUFFER_LENGTH bytes, we have to do it in |
| 254 | // smaller chunks instead of all at once |
| 255 | for (int k = 0; k < length; k += min((int)length, I2CDEVLIB_WIRE_BUFFER_LENGTH)) { |
| 256 | useWire->beginTransmission(devAddr); |
| 257 | useWire->write(regAddr); |
| 258 | useWire->endTransmission(); |
| 259 | useWire->beginTransmission(devAddr); |
| 260 | useWire->requestFrom((uint8_t)devAddr, (uint8_t)min((int)length - k, I2CDEVLIB_WIRE_BUFFER_LENGTH)); |
| 261 | |
| 262 | for (; useWire->available() && (timeout == 0 || millis() - t1 < timeout); count++) { |
| 263 | data[count] = useWire->read(); |
| 264 | #ifdef I2CDEV_SERIAL_DEBUG |
| 265 | Serial.print(data[count], HEX); |
| 266 | if (count + 1 < length) Serial.print(" "); |
nothing calls this directly
no test coverage detected