Home > OS >  c arduino multiple display objects in a structured array
c arduino multiple display objects in a structured array

Time:04-07

I have declarations for multiple OLED displays running via an 8 channel i2c Mux:

Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);
Adafruit_SSD1306 display5(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET);

I want to be able to access these using an identifier like this:

display[0].print("etc");
display[1].print("etc");
display[2].print("etc");

so I can then call a function like this:

sendTitleDisplay(1);

void sendTitleDisplay(uint8_t id)
{
   display[id].print("title example");
}

How would i define these objects so I can access them in this array format?

CodePudding user response:

You could initialize your array like this:

Adafruit_SSD1306 display[]{
    {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
    {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
    {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
    {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
    {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
};

This would use the first of the new recommended constructors for each of the Adafruit_SSD1306 instances, with clkDuring using the default value 400000UL and clkAfter using the default value 100000UL.

// NEW CONSTRUCTORS -- recommended for new projects
Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
                 int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
                 uint32_t clkAfter = 100000UL);

Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
                 int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);

Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
                 int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);

// DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
                 int8_t rst_pin, int8_t cs_pin);

Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);

Adafruit_SSD1306(int8_t rst_pin = -1);
  • Related