How to merge instances of a Library into new class

Hello,

in my project I'm using multiple ADS7828 chips. There is a library available. The address bits allow to have four chips on one i2c bus. Is it possible to "merge" them into a new class?
One Chip has eight channels (from 0 to 7)

I would like to create a class which has 32 channels. Inside the new class, there should be four instances of the ADS7828 class (each one with different address). Depending on the provided channel id, the new class should then execute ADS7828 by using the right instance. like a "switch case".

You could leave the existing class as it is and create a class that will contain 4 instances of the base object. The new class can do some management to map the channel/chips before executing the right operation on the appropriate base object.

yes, that's what i have in mind, but no idea how to build it tho.

Define a class X (lots to look up on internet id you don't know how)
Inside X you have an array of 4 of the ADS7828 classes defined as a variable.
X constructor initializes the array with whatever is appropriate (eg, properties of the base object you need set).
X.begin() may do something, but good to have itr even if nothing in the function.
X destructor does whatever close off is required for the classes in the array

Then you need to define X.methods that do what you want. For example, X.setDigital(z) where z is between 0 and 31 that maps to device z/4 and appropriate digital. A good starting point is replicating the methods that you would call in the base class but in your own class.

If you would provide a short sketch for one ADS7828 it might be possible that people help you to write a wrapper class for 4 ADS7828 .
If you are using a Library - link the exact library so others can use the same library as you do.

/*
    combine an array of 4 objects into one class
*/
#include <ADS7828.h> // https://github.com/eebothobby/ADS7828/


class SeveralADS7828 {
    ADS7828 adc[4];
  public:

    void begin() {
      for (uint8_t i = 0; i < 4; i++) {
        adc[i].begin(i); // replaces adc.begin(0);
      }
    }

    uint16_t read(uint8_t port) {
      if (port >= 8 * 4) return 0; // Error
      uint8_t device = port / 8;
      uint8_t channel = port % 8;
      return adc[device].read(channel);
    }
};

SeveralADS7828 adc; // replaces ADS7828 adc;

void setup() {
  Serial.begin(115200);
  delay(300);

  Wire.begin();
  Wire.setClock(400000L);
  adc.begin();
}

void loop()
{
  int adcData[8 * 4];
  for (int channel = 0; channel < 8 * 4; channel++) {
    adcData[channel] = adc.read(channel);
    Serial.print(channel); Serial.print(" "); Serial.print(adcData[channel]); Serial.print(" ");
  }
  Serial.println();
}

untested.
Imho a simple function will also do the trick when you have your 4 ADS objects in an array.

By the way, this has nothing to do with "Library". It is just a common programming question.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.