google / cyanobyte

Machine-readable datasheets
https://cyanobyte.dev
Apache License 2.0
79 stars 31 forks source link

Add function to arbitrarily read data #184

Closed Fleker closed 4 years ago

Fleker commented 4 years ago

In the case of the BH1750FVI light sensor, you cannot read from a register address as it's data-only. This means that you cannot do a standard read on the light intensity. Rather we need a way to read data arbitrarily off the bus (or somehow without sending the register?)

Fleker commented 4 years ago
uint16_t BH1750FVI::readLightIntensity() {
    uint8_t datum;
    uint16_t value;
    _wire->beginTransmission(DEVICE_ADDRESS);
    //_wire->write(REGISTER_LIGHTINTENSITY);
    if (_wire->endTransmission(false) != 0) {
        return -1;
    }

    if (_wire->requestFrom(DEVICE_ADDRESS, 2) != 2) {
        return 0;
    }

    datum = _wire->read();
    value = value << 8 | datum;
    datum = _wire->read();
    value = value << 8 | datum;

    return value;
}
Fleker commented 4 years ago

Add a commandOnly to register? To i2c? Commands top-level?

Fleker commented 4 years ago

i2c.commandOnly: true

Then, turn the BH1750FVI registers into fields.

Fleker commented 4 years ago

That doesn't result in clean code as now the field logic will need to handle 0-bits. And I do like the existing writeX() code output. Appending it for a single register for reading is probably the best solution here for now.

Fleker commented 4 years ago

image