adafruit / Adafruit_SSD1306

Arduino library for SSD1306 monochrome 128x64 and 128x32 OLEDs
http://www.adafruit.com/category/63_98
Other
1.78k stars 976 forks source link

[Feature Request] One instance - multiple devices #272

Open edhinard opened 4 months ago

edhinard commented 4 months ago

If I understand correctly how the class works, displaying is a two stages process:

1) Preparation of the pixel map in MCU's RAM

  1. Transfer of the pixel map to the hardware with the display() method
    • whether by I2C if a Wire reference is given in constructor or SPI
    • when I2C, the device address is given as a parameter of the begin() method
    • when SPI the method (hardware or software) and the pins are given in the constructor

Scroll methods do not change the pixel map in RAM and just send commands to the display that do the job.

With minor code changes, it should be possible to manage 2 or more I2C displays of the same width and height with a single class. Thus reducing the need of RAM. We just need to allow user code to change i2caddr attribute. For that:

Another solution, which has my preference since it should work with different size displays and for SPI as well, is to:

An additional logic which manage peripheral's begin function would be a plus. For example with the help of a periphHasBegun protected attribute. From user perspective the code will be:

A first example with only one instance for 2 devices (same dimensions) sharing the same I2C bus:

Adafruit_SSD1306 display1(WIDTH, HEIGHT, &Wire);

display.begin(SSD1306_SWITCHCAPVCC, ADDR1);
...   
display.reset();

display.begin(SSD1306_SWITCHCAPVCC, ADDR2); // no need to "periphBegin=0" after first call of begin()
...   
display.reset();

A second example with two instances, SPI and I2C and different dimensions, but only one pixel map in RAM at a time:

Adafruit_SSD1306 display1(WIDTH1, HEIGHT1, &Wire);
Adafruit_SSD1306 display2(WIDTH2, HEIGHT2, &SPI...);

display1.begin(SSD1306_SWITCHCAPVCC, ADDR);
...   
display1.reset();

display2.begin(SSD1306_SWITCHCAPVCC);
...   
display2.reset();

Question to the library maintainer:

Would you accept a modification in this direction (reset method)?

I would like to propose a modification. The problem is that I don't have anything to test it at the moment. And certainly not with varied configurations.