Change the value of a const variable that's in the private member of library from the main code

I’ve been playing with this library for a DAC chip that I found on github. The default I2c addr for the DAC is 0x60, which is declared in the private member of the library. (It is also a parameter in multiple functions in the library) I was just using the example code hideakitai provided on github.


    struct DACInputData
        VREF vref;
        PWR_DOWN pd;
        GAIN gain;
        uint16_t data;

    const uint8_t I2C_ADDR {0x60};
    uint8_t addr_ {I2C_ADDR};
    uint8_t pin_ldac_;

    DACInputData reg_[4];
    DACInputData eep_[4];
    DACInputData read_reg_[4];
    DACInputData read_eep_[4];

    TwoWire* wire_;

Now I have multiple same DACs hooked up to photon, and I renamed their address so it’s ranging from 0x60 to 0x67. I would like to be able to change the address from my main code , and then call the function to read the specific DAC.

So far, I have tried the following methods.

  1. Moving the “uint8_t I2C_ADDR;” from the private to the public, and then declare it in global in my main code “uint8_t I2C_ADDR = {0x63};”. This returns the error

uninitialized const member in ‘class MCP4728’

  1. I removed the const and tried again, this time the code compiles but from the serial monitor, it seems like it’s not reading the address and cannot print out any readings.

  2. I used “extern uint8_t I2C_ADDR;” instead in the library. The error this time is

storage class specified for ‘I2C_ADDR’

which is probably because there are lots of functions in the library that’s like "function(I2C_ADDR){} which is illegal.

I’m really new to c++, any advice would be appreciated, thanks!

@poolnoodle, the MCP4728 class has a public function which lets you change the address of the device:

void setID(uint8_t id) { addr_ = I2C_ADDR + id; }

The address addr_ ,used throughout the code, is based on I2C_ADDR plus the 8-bit offset you provide when calling setID(). Once you create an MCP4728 object for all your devices, call setID() with the proper offset and then attach() each object.


Thank you so much! It worked:)

1 Like