/*********************************************************************
This is an example for the adafruit Monochrome OLEDs used on the FPGA_VGA Board
Written by John Monahan (S100Computers.com).  
*********************************************************************/

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);  // 12 characters across for TextSize(1); 

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
    
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

  pinMode(2, INPUT);       
  pinMode(3, INPUT);       
  pinMode(4, INPUT);       
  pinMode(5, INPUT);       
  pinMode(21, INPUT);       
  pinMode(0, INPUT);      
  pinMode(6, INPUT);  
  pinMode(7, INPUT);  

  pinMode(36, INPUT);       
  pinMode(37, OUTPUT);       
  pinMode(35, OUTPUT);    

   display.clearDisplay();
   display.setTextSize(1);
   display.setTextColor(WHITE);
   display.setCursor(0,0);

  digitalWrite(34,LOW);          // Clear status bit upon startup
  digitalWrite(34,HIGH);
  display.clearDisplay();   
}
 
    byte Char;
    int status;
    int screen_X = 0;
    int screen_Y = 0;

    void loop() {
    status = digitalRead(36);               // Read the status bit to see if a character is ready
    if (status == HIGH) {
        Char = digitalRead(2) +
            (digitalRead(3) << 1) +
            (digitalRead(4) << 2) +
            (digitalRead(5) << 3) +
            (digitalRead(21) << 4) +
            (digitalRead(0) << 5) +
            (digitalRead(6) << 6) +
            (digitalRead(7) << 7);
        if(Char == 0x0d)                    // CR special case
          screen_X = 0;
        else if(Char == 0x0a)                    // LF special case
          screen_Y += 10;
        else if(Char == 0x0c)                    // Clear screen with FF
          {
          screen_X = 0;
          screen_Y = 0;
          display.clearDisplay(); 
          }
        else
          {
           display.setCursor(screen_X,screen_Y);
           display.print((char)Char);
           display.display(); 
           digitalWrite(34,LOW);
           digitalWrite(34,HIGH);
           if((Char != 0x0d) && (Char != 0x0a) && (Char != 0x0c)) 
             {
             if(screen_X >= 120)               // Update the cursor
              {
              screen_X = 0;
              screen_Y += 10;
              }
              else screen_X += 10;
             }
          } 
    }
}

    
  