// A multiplexer-relay-switching-something-experiment-thingy OLED version
//
// All OLED displays are at the same I2C address.
// The relay on the left switches address bit 1.
// The two relays on the right switch address bit 0.
//
// 3 March 2022, Version 1, by Koepel, additions by Koepel are Public Domain
//
// This started as a simple dual I2C bus multiplexer:
//   https://wokwi.com/arduino/projects/325057312456704596
//
// Using this tutorial as a guide:
//   https://randomnerdtutorials.com/esp32-ssd1306-oled-display-arduino-ide/
//
// Notes:
//   Four displays need four objects.
//   The PROGMEM and F() macro are not needed for a ESP32.
//
//
//
// When copying this sketch, please do not remove this Adafruit note:
/*********
  Complete project details at https://randomnerdtutorials.com
  
  This is an example for our Monochrome OLEDs based on SSD1306 drivers. Pick one up today in the adafruit shop! ------> http://www.adafruit.com/category/63_98
  This example is for a 128x32 pixel display using I2C to communicate 3 pins are required to interface (two I2C and one reset).
  Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
  Written by Limor Fried/Ladyada for Adafruit Industries, with contributions from the open source community. BSD license, check license.txt for more information All text above, and the splash screen below must be included in any redistribution. 
*********/



#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const int selectPin0 = 4;         // select pin bit 0 for relays on the right
const int selectPin1 = 5;         // select pin bit 1 for relay on the left


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display[4] =
{
  Adafruit_SSD1306 (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET),
  Adafruit_SSD1306 (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET),
  Adafruit_SSD1306 (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET),
  Adafruit_SSD1306 (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET),
};

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

void setup() {
  Serial.begin(115200);
  Serial.println( "The sketch has started");

  pinMode( selectPin0, OUTPUT);
  pinMode( selectPin1, OUTPUT);

  for( int i=0; i<4; i++)
  {
    select( i);
    // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    bool valid = display[i].begin(SSD1306_SWITCHCAPVCC, 0x3C);
    if( !valid) 
    { 
      Serial.println(F("SSD1306 allocation failed"));
      for(;;); // Don't proceed, loop forever
    }
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  for( int i=0; i<4; i++)
  {
    select( i);
    display[i].display();
  }

  delay( 2000);

}

void loop() 
{
  for( int i=0; i<4; i++)
  {
    select( i);               // select a I2C (sub)bus

    switch( i)
    {
      case 0:
        testdrawcircle( i);    // Draw circles (outlines)
        break;
      case 1:
        testfillcircle( i);    // Draw circles (filled)
        break;
      case 2:
        testdrawtriangle( i);  // Draw triangles (outlines)
        break;
      case 3:
        testfilltriangle( i);  // Draw triangles (filled)
        break;
    }
  }
}

void testdrawline( int x) {
  int16_t i;

  display[x].clearDisplay(); // Clear display buffer

  for(i=0; i<display[x].width(); i+=4) {
    display[x].drawLine(0, 0, i, display[x].height()-1, WHITE);
    display[x].display(); // Update screen with each newly-drawn line
    delay(1);
  }
  for(i=0; i<display[x].height(); i+=4) {
    display[x].drawLine(0, 0, display[x].width()-1, i, WHITE);
    display[x].display();
    delay(1);
  }
  delay(250);

  display[x].clearDisplay();

  for(i=0; i<display[x].width(); i+=4) {
    display[x].drawLine(0, display[x].height()-1, i, 0, WHITE);
    display[x].display();
    delay(1);
  }
  for(i=display[x].height()-1; i>=0; i-=4) {
    display[x].drawLine(0, display[x].height()-1, display[x].width()-1, i, WHITE);
    display[x].display();
    delay(1);
  }
  delay(250);

  display[x].clearDisplay();

  for(i=display[x].width()-1; i>=0; i-=4) {
    display[x].drawLine(display[x].width()-1, display[x].height()-1, i, 0, WHITE);
    display[x].display();
    delay(1);
  }
  for(i=display[x].height()-1; i>=0; i-=4) {
    display[x].drawLine(display[x].width()-1, display[x].height()-1, 0, i, WHITE);
    display[x].display();
    delay(1);
  }
  delay(250);

  display[x].clearDisplay();

  for(i=0; i<display[x].height(); i+=4) {
    display[x].drawLine(display[x].width()-1, 0, 0, i, WHITE);
    display[x].display();
    delay(1);
  }
  for(i=0; i<display[x].width(); i+=4) {
    display[x].drawLine(display[x].width()-1, 0, i, display[x].height()-1, WHITE);
    display[x].display();
    delay(1);
  }

  delay(2000); // Pause for 2 seconds
}

void testdrawrect(int x) {
  display[x].clearDisplay();

  for(int16_t i=0; i<display[x].height()/2; i+=2) {
    display[x].drawRect(i, i, display[x].width()-2*i, display[x].height()-2*i, WHITE);
    display[x].display(); // Update screen with each newly-drawn rectangle
    delay(1);
  }

  delay(2000);
}

void testfillrect(int x) {
  display[x].clearDisplay();

  for(int16_t i=0; i<display[x].height()/2; i+=3) {
    // The INVERSE color is used so rectangles alternate white/black
    display[x].fillRect(i, i, display[x].width()-i*2, display[x].height()-i*2, INVERSE);
    display[x].display(); // Update screen with each newly-drawn rectangle
    delay(1);
  }

  delay(2000);
}

void testdrawcircle(int x) {
  display[x].clearDisplay();

  for(int16_t i=0; i<max(display[x].width(),display[x].height())/2; i+=2) {
    display[x].drawCircle(display[x].width()/2, display[x].height()/2, i, WHITE);
    display[x].display();
    delay(1);
  }

  delay(2000);
}

void testfillcircle(int x) {
  display[x].clearDisplay();

  for(int16_t i=max(display[x].width(),display[x].height())/2; i>0; i-=3) {
    // The INVERSE color is used so circles alternate white/black
    display[x].fillCircle(display[x].width() / 2, display[x].height() / 2, i, INVERSE);
    display[x].display(); // Update screen with each newly-drawn circle
    delay(1);
  }

  delay(2000);
}

void testdrawroundrect(int x) {
  display[x].clearDisplay();

  for(int16_t i=0; i<display[x].height()/2-2; i+=2) {
    display[x].drawRoundRect(i, i, display[x].width()-2*i, display[x].height()-2*i,
      display[x].height()/4, WHITE);
    display[x].display();
    delay(1);
  }

  delay(2000);
}

void testfillroundrect(int x) {
  display[x].clearDisplay();

  for(int16_t i=0; i<display[x].height()/2-2; i+=2) {
    // The INVERSE color is used so round-rects alternate white/black
    display[x].fillRoundRect(i, i, display[x].width()-2*i, display[x].height()-2*i,
      display[x].height()/4, INVERSE);
    display[x].display();
    delay(1);
  }

  delay(2000);
}

void testdrawtriangle(int x) {
  display[x].clearDisplay();

  for(int16_t i=0; i<max(display[x].width(),display[x].height())/2; i+=5) {
    display[x].drawTriangle(
      display[x].width()/2  , display[x].height()/2-i,
      display[x].width()/2-i, display[x].height()/2+i,
      display[x].width()/2+i, display[x].height()/2+i, WHITE);
    display[x].display();
    delay(1);
  }

  delay(2000);
}

void testfilltriangle(int x) {
  display[x].clearDisplay();

  for(int16_t i=max(display[x].width(),display[x].height())/2; i>0; i-=5) {
    // The INVERSE color is used so triangles alternate white/black
    display[x].fillTriangle(
      display[x].width()/2  , display[x].height()/2-i,
      display[x].width()/2-i, display[x].height()/2+i,
      display[x].width()/2+i, display[x].height()/2+i, INVERSE);
    display[x].display();
    delay(1);
  }

  delay(2000);
}

void testdrawchar(int x) {
  display[x].clearDisplay();

  display[x].setTextSize(1);      // Normal 1:1 pixel scale
  display[x].setTextColor(WHITE); // Draw white text
  display[x].setCursor(0, 0);     // Start at top-left corner
  display[x].cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display[x]. This is normal.
  // Library will draw what it can and the rest will be clipped.
  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display[x].write(' ');
    else          display[x].write(i);
  }

  display[x].display();
  delay(2000);
}

void testdrawstyles(int x) {
  display[x].clearDisplay();

  display[x].setTextSize(1);             // Normal 1:1 pixel scale
  display[x].setTextColor(WHITE);        // Draw white text
  display[x].setCursor(0,0);             // Start at top-left corner
  display[x].println(F("Hello, world!"));

  display[x].setTextColor(BLACK, WHITE); // Draw 'inverse' text
  display[x].println(3.141592);

  display[x].setTextSize(2);             // Draw 2X-scale text
  display[x].setTextColor(WHITE);
  display[x].print(F("0x")); display[x].println(0xDEADBEEF, HEX);

  display[x].display();
  delay(2000);
}

void testscrolltext(int x) {
  display[x].clearDisplay();

  display[x].setTextSize(2); // Draw 2X-scale text
  display[x].setTextColor(WHITE);
  display[x].setCursor(10, 0);
  display[x].println(F("scroll"));
  display[x].display();      // Show initial text
  delay(100);

  // Scroll in various directions, pausing in-between:
  display[x].startscrollright(0x00, 0x0F);
  delay(2000);
  display[x].stopscroll();
  delay(1000);
  display[x].startscrollleft(0x00, 0x0F);
  delay(2000);
  display[x].stopscroll();
  delay(1000);
  display[x].startscrolldiagright(0x00, 0x07);
  delay(2000);
  display[x].startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display[x].stopscroll();
  delay(1000);
}

void testdrawbitmap(int x) {
  display[x].clearDisplay();

  display[x].drawBitmap(
    (display[x].width()  - LOGO_WIDTH ) / 2,
    (display[x].height() - LOGO_HEIGHT) / 2,
    logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
  display[x].display();
  delay(1000);
}



void select( int bus)          // 0 ... 3
{
  if( (bus & 0x01) == 0)       // test bit 0
    digitalWrite( selectPin0, LOW);
  else
    digitalWrite( selectPin0, HIGH);

  if( (bus & 0x02) == 0)       // test bit 1
    digitalWrite( selectPin1, LOW);
  else
    digitalWrite( selectPin1, HIGH);

  delay( 60);                  // the relay is slow
}