ElliottWaterman / ADS1015

Device agnostic library for the ADS1015 (4 channel ADC) written in C (compatible with C++).
MIT License
0 stars 1 forks source link

ADS1015

Device agnostic library for the ADS1015 (4 channel, 12-bit ADC) written in C (compatible with C++ and Arduino).

Configuration

There is only 1 configuration register for all 4 channels, this means each device connected to an input channel must set the config before taking a measurement (the current config could be set up for another device/channel that just used the ADS1015).

Taking a measurement

To take a measurement, call the function ads1015_measurement_get with parameters:

How to implement

Arduino

int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
    Wire.beginTransmission(i2cAddress);
    Wire.write(reg);
    Wire.beginTransmission(false);

    Wire.requestFrom(i2cAddress, len);
    for (uint16_t x = 0; x < len; x++)
    {
        bufp[x] = Wire.read();
    }

    return 0;
}

int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
    Wire.beginTransmission(i2cAddress);
    Wire.write(reg);
    for (uint16_t x = 0; x < len; x++)
    {
        Wire.write(bufp[x]);
    }

    return Wire.endTransmission(); // Will return 0 upon success
}

...

ads1015_ctx_t dev_ctx;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
//dev_ctx.handle = (void*) this; // Used in a wrapper class

...

ads1015_config_reg_t config = ADS1015_DEFAULT_CONFIG();
// customise default config above with device specific settings below
config.mux = ADS1015_MUX_0; // Input channel: 0
config.pga = ADS1015_PGA_3; // Gain: +/- 1.024V
// etc...

...

uint16_t measurement;
int32_t error_response = ads1015_measurement_get(&dev_ctx, config, &measurement);
if (error_response == 0) {
    // Success, use measurement here
}
else {
    // Failure, handle error here
}

C / C++

Create a single wrapper class

Create multiple different wrapper classes