add an unsafe read

This commit is contained in:
ladyada 2019-03-07 16:23:12 -05:00
parent 428cdb3dbd
commit a5bfc4ffa8
3 changed files with 23 additions and 2 deletions

View File

@ -36,6 +36,27 @@ bool Adafruit_I2CRegister::write(uint32_t value, uint8_t numbytes=0) {
return write(_buffer, numbytes);
}
// This does not do any error checking! returns 0xFFFFFFFF on failure
uint32_t Adafruit_I2CRegister::read(void) {
if (! read(_buffer, _width)) {
return -1;
}
uint32_t value = 0;
for (int i=0; i < _width; i++) {
value <<= 8;
if (_bitorder == MSBFIRST) {
value |= _buffer[_width-i-1];
} else {
value |= _buffer[i];
}
}
return value;
}
bool Adafruit_I2CRegister::read(uint8_t *buffer, uint8_t len) {
_buffer[0] = _address;
if (! _device->write_then_read(_buffer, 1, buffer, len)) {

View File

@ -16,6 +16,7 @@ class Adafruit_I2CRegister {
bool read(uint8_t *value);
bool read(uint16_t *value);
bool read(uint32_t *value);
uint32_t read(void);
bool write(uint8_t *buffer, uint8_t len);
bool write(uint32_t value, uint8_t numbytes=0);

View File

@ -30,8 +30,7 @@ void setup() {
thresh_reg.write(~thresh);
thresh_reg.read(&thresh);
Serial.print("Post threshold register = 0x"); Serial.println(thresh, HEX);
Serial.print("Post threshold register = 0x"); Serial.println(thresh_reg.read(), HEX);
}
void loop() {