#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss

byte _myEndCommand = 227;



void setup()
{
    // open the serial port at 9600 bps:
  Serial.begin(9600);
  
  byte clr;

  pinMode(4, OUTPUT); // D/C low = command
  pinMode(2, OUTPUT); // reset 

  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  
  // RESET THE OLED
  delay(500);
  digitalWrite (2, HIGH);
  delay(100);
  digitalWrite (2, LOW);
  delay(100);
  digitalWrite (2, HIGH);
    delay(100);


 WriteCommand(0xAE); /* Display OFF Command */
  Serial.print("jetzt Vcc einstecken");  
 delay (15000); /* Turn on Vcc in this break */
  WriteCommand(0xAF); /* Display ON Command */
  delay (500);
 

  // Lower Column Address  
  WriteCommand(0x04); /* Set Lower Column Address */

  // High Column Address  
  WriteCommand(0x12); /* Set Higher Column Address   
   */

  // Horzontal Scroll Setup  
  WriteCommand(0x26); 

  // Activate Horizontal Scroll  
  WriteCommand(0x2F); 
  
    // Deactivate Horizontal Scroll  
  WriteCommand(0x2E); 
  
    // Contrast Control Register 
  WriteCommand(0x81);  /* Set Contrast Control */
  WriteCommand(0x15);  /* 0 ~ 127  (1)  */
  
    // Brightness for color banks 
  WriteCommand(0x82); 
  WriteCommand(0x80);
  
  //not used 
 WriteCommand(0x91);
 WriteCommand(0x92);
 WriteCommand(0x93);
 
   // Re-map 
  WriteCommand(0xA1); /* [A0]:column address 0 is  
   map to SEG0 , [A1]: column  
   address 131 is map to SEG0*/
  
  // Display Start Line  
  WriteCommand(0x40); /* Set Display Start Line */
  
  //SEt display offset
  WriteCommand(0xD3);
  WriteCommand(0x00);
  
    // Multiplex Ratio 
  WriteCommand(0xA8); /* Set Multiplex Ratio */
  WriteCommand(0x0F); /* Set to 16 Mux*/
   

  // Entire Display ON/OFF 
  WriteCommand(0xA4); /* A4=ON */

  //  Normal or Inverse Display 
  WriteCommand(0xA6);       /* Normal Display*/

 /* 8B=ON, 8A=OFF */
  // Display ON/OFF 
  WriteCommand(0xAF); /* AF=ON , AE=OFF*/
  
    // Page Address
  WriteCommand(0xB0);
  
  // set com outout scan direction
  WriteCommand(0xC8);
  
// set clock divide
WriteCommand(0xD5);
WriteCommand(0x72);

// set Area Color Mode
WriteCommand(0xD8);
WriteCommand(0x00);

//Set pre charged periode
WriteCommand(0xD9);
WriteCommand(0x22);

 // COM Pins Hardware Configuration 
 WriteCommand(0xDA);
  WriteCommand(0x12); /* Set  Pins Hardware   
   Configuration */

//Set vcom level
WriteCommand(0xDB);
WriteCommand(04);

//Set DC DC on off
WriteCommand(0xAD);
WriteCommand(0x8A);
  
  
}



void loop()
{       
    WriteData(0xFF); // All Pixels on
    WriteCommand (0xB2);
    delay (1000);
    WriteData(0xFF); // All Pixels on
    WriteCommand (0xB1);
        delay (1000);

    Serial.println("Bin  durch"); 

}

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

byte WriteCommand(byte theCommand)
{
  digitalWrite(SLAVESELECT,LOW);
  digitalWrite (4, LOW); //turn to command mode 
  //1 byte command and the command for the end of the transmission
  spi_transfer(theCommand);
    spi_transfer(_myEndCommand);
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}

byte WriteData(byte theData)
{
  digitalWrite(SLAVESELECT,LOW);
  digitalWrite (4, HIGH); //turn Pixel mode 
  //1 byte data and the command for the end of the transmission
  spi_transfer(theData);
  digitalWrite (4, LOW); //turn to command mode 
  spi_transfer(_myEndCommand);
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
}
