/*
  Autor: Michael Springwald
  Datum: Dienstag, 02.08.2016 

  Steht beim Sofa
*/

// Für das Nokia LCD 5110 
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

#define ONE_WIRE_BUS 9
#define cmd_TextOut         2
#define cmd_ID              3
#define cmd_ClearLine       4
#define cmd_ClearScreen     5
#define cmd_LightToggle     6
#define cmd_AutoLightChange 7
#define cmd_FillRect        8

const long interval = 5000;

unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
boolean StartTimer = false;

int ButtonValue=0;
int ButtonValueHigh=0;
byte OldButtonIndex = 2;
byte ButtonIndex =0;

boolean WriteSerial = false;
boolean AutoLighOff = true;
boolean LightStatus = false;
boolean IDOK = false;
boolean ButtonRead = true;

Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);


int index = 0;
int z = 0;
boolean isEnde=false;

char Data[50]; // Funktion
char ch;
int CommandIndex=-1;
int P1=-1;
int P2=-1;
int P3=-1;
int P4=-1;
int P5=-1;

void setup() {
  Serial.begin(9600);
//  Serial.println("25,3,Sensor");

  pinMode(6,OUTPUT);
  analogWrite(6,100);
  LightStatus=true;   
  StartTimer=true;

  pinMode(A0, INPUT_PULLUP);
    
   
  display.begin();  
  display.setContrast(50);
  display.setRotation(2);    
  display.clearDisplay();  
  display.display();  
}

void loop() {
  if (Serial.available() ) {    
    ch=Serial.read(); 
    if (ch == ',' || ch == 10) {
      isEnde=ch == 10;
      
      Command();
      if (ch == 10) {
        Serial.println("27,OK");
        index=0;         
        P1=-1; P2=-1; P3=-1;                
      }
      else 
        index=index+1;       
      
      for (int x=0; x <z; x++) Data[x]='\0'; 
      z=0; 
    }
    else {
      Data[z]=ch; 
      z=z+1; 
    }
  }     

  unsigned long currentMillis = millis(); 
 
  if ( (AutoLighOff) and (StartTimer) and (currentMillis - previousMillis2 >= 10000) ) {
    previousMillis2 = currentMillis;
    analogWrite(6,0);
    StartTimer=false;
    LightStatus=false;
  } 

  CheckButtons(); 
} // loop


void TextOut(int x, int y, char aData[]) {  
  display.setCursor(x,y);          
  display.println(aData);             
  display.display();    
}

void CheckButtons() {
  unsigned long currentMillis = millis();        
  
  if (ButtonRead) ButtonValue=analogRead(A0);    
  
  if (ButtonValue < 800) {
    ButtonRead=false; ButtonIndex=0;          
   // Serial.println(ButtonValue);
    if (ButtonValue >= 72 && ButtonValue <= 79) ButtonIndex = 1;                     
    if (ButtonValue >= 127 && ButtonValue <= 132) ButtonIndex = 2;                
    if (ButtonValue >= 174 && ButtonValue <= 181) ButtonIndex = 3;                
    if (ButtonValue >= 216 && ButtonValue <= 223) ButtonIndex = 4;             
    if (ButtonValue >= 253 && ButtonValue <= 259) ButtonIndex = 5; 
    if (ButtonValue >= 288 && ButtonValue <= 295) ButtonIndex = 6;
    if (ButtonValue >= 320 && ButtonValue <= 325) ButtonIndex = 7; 
    if (ButtonValue >= 345 && ButtonValue <= 355) ButtonIndex = 8;
    
    if (ButtonIndex != OldButtonIndex) {                    
      if (LightStatus) {
        Serial.print("271,"); 
        Serial.print(ButtonIndex);
        Serial.println();  
      }
      
      if (!StartTimer) { 
        analogWrite(6,100);
        StartTimer=true;
        LightStatus=true;        
        previousMillis2=currentMillis;
      }
      else
        previousMillis2=currentMillis;  
    }
    OldButtonIndex=ButtonIndex;
  } 
  else 
    OldButtonIndex=0;
    
  
  if (currentMillis - previousMillis1 >= 60) {
    previousMillis1=currentMillis;
    ButtonRead=true;
  }     
}

int GetCommandIndex(const char* aCommand) {
  int Temp=-1; 
  if (strcmp(aCommand, "ID") == 0) { Temp=cmd_ID; return Temp;}   
  if (strcmp(aCommand, "TO") == 0) { Temp=cmd_TextOut; return Temp;}   
  if (strcmp(aCommand, "CL") == 0) { Temp=cmd_ClearLine; return Temp;}       
  
  if (strcmp(aCommand, "CS") == 0) { Temp=cmd_ClearScreen; return Temp;}       
  if (strcmp(aCommand, "LT") == 0) { Temp=cmd_LightToggle; return Temp;}       
  if (strcmp(aCommand, "ALC") == 0) { Temp=cmd_AutoLightChange; return Temp;}         
  if (strcmp(aCommand, "FR") == 0) { Temp=cmd_FillRect; return Temp;}             
}

//Debug();
void SetParameter () {
  switch (index)  {       
    case 2: { // X
      P1=atoi(Data); 
      break;
    }
         
    case 3: { // Y
      P2=atoi(Data); 
      break;
    }

    case 4: {
      P3=atoi(Data); 
      break;
    }

    case 5: {
      P4=atoi(Data); 
      break;
    }

    case 6: {
      P5 = atoi(Data); 
      break;
    }
  }
}

void Command() {
  if (index == 0) {  
    if (atoi(Data) == 27) 
      IDOK=true;
    else
      IDOK=false;
  }
  else {  
    if (IDOK) {
      if (index == 1) 
        CommandIndex=GetCommandIndex(Data);

      switch (CommandIndex) {
        case cmd_ID: {
          Serial.println("ModulID:27");   
          break;
        } // cmd_ID

        case cmd_TextOut: {
          if (index <=3  )
            SetParameter();
          else {
            TextOut(P1,P2,Data); 
          } 
          break;
        } // cmd_TextOut

        case cmd_ClearLine: {
          SetParameter();     
          if (P1 > -1) {       
            display.fillRect(0,P1,display.width(),10,WHITE); 
            display.display();            
          }          
          break;
        } // cmd_ClearLine        

        case cmd_ClearScreen: {
          display.clearDisplay();  
          display.display();  
          
          break;
        } // cmd_ClearScreen

        case cmd_LightToggle: {
          LightStatus=!LightStatus;          
          AutoLighOff=false;                    
          if (LightStatus) 
            analogWrite(6,100);
          else 
            analogWrite(6,0);            
          break;
        } // cmd_LightToggle

        case cmd_AutoLightChange: {
          if (strcmp(Data, "ON") == 0) 
            AutoLighOff=true;
          else
            AutoLighOff=false;

          break;
        } // cmd_AutoLightChange                   

        case cmd_FillRect: {
          SetParameter();  
          if (index == 4 ) {
            display.fillRect(P1,P2,P3,P4,WHITE);
            display.display();
          } 
          break;
        } // // cmd_FillRect        
      }     
    }
  }    
}
