Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: how to create a structured array of diplay objects

  1. #1

    how to create a structured array of diplay objects

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

    Code:
    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:

    Code:
    display[0].print("etc");
    display[1].print("etc");
    display[2].print("etc");
    How would i define these objects?

  2. #2
    Quote Originally Posted by snowsh View Post
    I want to be able to access these using an identifier like this:
    Code:
    display[0].print("etc");
    display[1].print("etc");
    display[2].print("etc");
    With modern C++ there may be a syntax to define/initialize as an array, but you could also do this:

    Code:
    // define the individual display objects as you already have
    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);
    
    // define an array of pointers to those objects
    Adafruit_SSD1306 *display[5] = { &display1, &display2, &display3, &display4, &display5 };
    
    // access the objects via the array, but use the "->" syntax because it's an array of pointers
    display[0]->print("etc");
    display[1]->print("etc");
    display[2]->print("etc");

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,849
    Try this:

    Code:
    Adafruit_SSD1306 dispay[]
    {
        {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}
    };

  4. #4
    Thanks for the help, got it running now.

    Here is a working example sketch:

    Code:
    /*
       example sketch to drive up to 1 + 8 monochome OLED displays using a TCA9548A i2c MUX
    
       display on wire 0 ic2 . use this as usual for higher speed updates. teensy 4.1 SCL pin 19, SDA pin 18
    
       displayMulti on wire2 i2c.
       use a TCA9548A i2c MUX on wire2 - teensy 4.1 scl2 pin 24, sda2 pin 25
    */
    
    //-------------------------------------------------------------------------  includes
    
    #include <Wire.h>
    
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    
    //-------------------------------------------------------------------------  OLED display
    
    #define SCREEN_WIDTH 128                                // OLED display width, in pixels
    #define SCREEN_HEIGHT 64                                // OLED display height, in pixels
    #define OLED_RESET     -1                               // Reset pin # (or -1 if sharing Arduino reset pin)
    #define SCREEN_ADDRESS 0x3c                             // < See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 - well the 128 x 64 yellow / blue are on 0x3c....
    #define NUMBER_OF_MULTI_SCREENS 8
    
    //-------------------------------------------------------------------------  OLED display - this is the principle display for high framerate updates, clocks etc
    
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
    
    //-------------------------------------------------------------------------  OLED displayMulti ic2 wire2
    
    Adafruit_SSD1306 displayMulti[] {                       // define for each NUMBER_OF_MULTI_SCREENS
      {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},
      {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
      {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
      {SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2, OLED_RESET},
    };
    
    //-------------------------------------------------------------------------  i2c mux
    
    void TCA9548A(uint8_t bus)
    {
      Wire2.beginTransmission(0x70);                        // TCA9548A i2c address
      Wire2.write(1 << bus);
      Wire2.endTransmission();
    }
    
    //-------------------------------------------------------------------------  setup functions
    
    void displayMultiStartup()//multiDisplayStartup()
    {
      for (uint8_t i = 0; i < NUMBER_OF_MULTI_SCREENS; i++)
      { 
        TCA9548A(i);                                        // set the i2c mux address  // IMPORTANT !! need to call TCA9548A(uint8_t bus) any time send to these displays
        if (!displayMulti[i].begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
        {
          Serial.print(F("SSD1306 screen #"));
          Serial.print(i);
          Serial.println(F(" allocation failed"));
    
          for (;;);                                         // Don't proceed, loop forever
        }
        Serial.print(F("screen #"));
        Serial.print(i);
        Serial.println(F(" sucessfully initialised"));
    
        displayMultiStartupText(i);
      }
    }
    
    void displayMultiStartupText(uint8_t i)
    {
      TCA9548A(i);                                          // set the i2c mux address
      displayMulti[i].clearDisplay();
      displayMulti[i].setTextColor(SSD1306_WHITE, SSD1306_BLACK);
      displayMulti[i].setCursor(0, 0);
      displayMulti[i].setTextSize(1);
      displayMulti[i].print(F("displayMulti["));
      displayMulti[i].print(i);
      displayMulti[i].print(F("]"));
      displayMulti[i].setTextSize(1);
      displayMulti[i].setCursor(0, 16);
      displayMulti[i].display();
    }
    
    //-------------------------------------------------------------------------   loop functions
    
    void displayMultiLoop(uint8_t i)
    {
      TCA9548A(i);                                          // set the i2c mux address
      displayMulti[i].setTextColor(SSD1306_WHITE, SSD1306_BLACK);
      displayMulti[i].setTextSize(1);
      displayMulti[i].setCursor(0, 26);
      displayMulti[i].print(millis());
      displayMulti[i].display();
    }
    
    
    //-------------------------------------------------------------------------  setup
    
    void setup()
    {
      Serial.begin(57600);
    
      Serial.println(F("serial ready"));
    
      //-----------------------------------------------------------------------  display
    
      Serial.println(F("begin display:"));
    
      if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
      {
        Serial.println(F("SSD1306 allocation failed"));
    
        for (;;); // Don't proceed, loop forever
      }
    
      Serial.println(F("display started"));
    
      display.clearDisplay();
      display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
      display.setCursor(0, 0);
      display.setTextSize(1);
      display.println("primary display");
      display.display();
      
      displayMultiStartup();
    }
    
    //-------------------------------------------------------------------------  loop
    
    uint8_t counter = 0;
    
    void loop()
    {
      display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);   // display to the primary display
      display.setCursor(0, 25);
      display.print(millis());
      display.display();
    
      displayMultiLoop(counter);                             // display to each of the multi displays one per loop.
      counter++;
      if (counter >= NUMBER_OF_MULTI_SCREENS ) counter = 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •