// 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
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
relay1:NO2
relay1:NC2
relay1:P2
relay1:COIL2
relay1:NO1
relay1:NC1
relay1:P1
relay1:COIL1
relay2:NO2
relay2:NC2
relay2:P2
relay2:COIL2
relay2:NO1
relay2:NC1
relay2:P1
relay2:COIL1
relay3:NO2
relay3:NC2
relay3:P2
relay3:COIL2
relay3:NO1
relay3:NC1
relay3:P1
relay3:COIL1
oled1:DATA
oled1:CLK
oled1:DC
oled1:RST
oled1:CS
oled1:3V3
oled1:VIN
oled1:GND
oled2:DATA
oled2:CLK
oled2:DC
oled2:RST
oled2:CS
oled2:3V3
oled2:VIN
oled2:GND
oled3:DATA
oled3:CLK
oled3:DC
oled3:RST
oled3:CS
oled3:3V3
oled3:VIN
oled3:GND
oled4:DATA
oled4:CLK
oled4:DC
oled4:RST
oled4:CS
oled4:3V3
oled4:VIN
oled4:GND