/*
Written by C4

--Commands for arduino--
Commands should start with a "C" to denote a command
- CBL = Command Backlight (switch the state)
- COD = Command Ouput Display (COD192.168.1.1, CODThis is a test)



 */
// Includes LCD header
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int BLState = 1; // Backlight State
int BLPin = 8; // Pin to control backlight
int ReadState = 0; // Checks to see if the data in passes some cammand requirements
byte Sin; // Serial Input
long PrevMils = 0; //Previous Milliseconds
long interval = 10000; // interval for a delay loop
char CrackArray[] = { 1,'Q','X',6,'*','$','(','L','Y','@','&',8,'G',8,'Z',2,'V',5,'$','N','S','W','P',0,7,'B',6,'%','^','!','I','M',2,'F','R','T','Y','K'};
int CD = 0; //CrackingDisplay Counter


void setup(){
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  pinMode(BLPin, OUTPUT);
  Serial.begin(1200);
  digitalWrite(BLPin, HIGH);

  lcd.print("Booting...");
  
    Serial.println("");
    Serial.print("root");
    Serial.print("admin");

}

void crack(int time, char letter, int position){
  int RandomLetter = 0;
  for(int i=0; i < time; i++){
     
     lcd.print(CrackArray[RandomLetter]);
     RandomLetter++;
     lcd.setCursor(position,1);
     delay(50);
  }
  lcd.setCursor(position,1);
  lcd.print(letter); 
}


void CrackingDisplay(){
    lcd.clear();
    lcd.setCursor(0,0);    
    lcd.print("Cracking...");
    
    lcd.setCursor(3,1);
    //lcd.print("DEFCON 18");
    lcd.print("*********");
    lcd.setCursor(3,1);
    crack(47, 'D', 3);
    crack(23, 'E', 4);
    crack(43, 'F', 5);
    crack(39, 'C', 6);
    crack(21, 'O', 7); 
    crack(25, 'N', 8);  
    lcd.setCursor(9,1);
    lcd.print(' '); 
    crack(26, '1', 10);
    crack(30, '8', 11);
    CD++;
    delay(10000);
  
}

void GetRouterInfo(){
  //Serial.print("echo \"COD`/sbin/ifconfig br0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'`\"\r\n"); 
}

void matrix(){
  char message[] = {'D','E','F','C','O','N',' ','h','a','s',' ','y','o','u','.'};
  lcd.clear();
  lcd.setCursor(0,0);
  for(int a =0; a < 4; a++){
    lcd.cursor();
    delay(300);
    lcd.noCursor();
    delay(300);
  }
  for(int a=0; a <15; a++){
    lcd.print(message[a]);
    delay(200);
  }
  for(int a =0; a < 3; a++){
    lcd.noCursor();
    delay(300);
    lcd.cursor();
    delay(300);
  }
  
}



void credits(){
    char website[] = {'h','t','t','p',':','/','/','w','w','w','.','u','n','s','t','d','i','o','.','o','r','g',' ',' ',' ',' ',' '};
    lcd.clear();
    lcd.setCursor(19,0);  
    lcd.print("Created by: C4");
    lcd.setCursor(16,1);
    lcd.autoscroll();
    for (int c = 0; c < 27; c++){
      lcd.print(website[c]);
      delay(500);
    }
    lcd.noAutoscroll();
    lcd.clear();
    delay(200);
     
    
  
}

void loop(){
 
  if (Serial.available()) {
    Sin = Serial.read();
    if(ReadState > 20) {
      ReadState = 0;
      lcd.clear();
    }
    //lcd.write(Sin);
    
   
    if(Sin =='C' && ReadState == 0){
      ReadState++;
    }
    else if(Sin =='B' && ReadState == 1){
      ReadState++;
    }
    else if(Sin =='O' && ReadState == 1){
      ReadState++;
    }
    else if(Sin =='L' && ReadState == 2){    
      if(BLState == 1){
        digitalWrite(BLPin, LOW);
        BLState = 0;
        Serial.println("Backlight Off");
        ReadState = 0;
      }
      else{
        digitalWrite(BLPin, HIGH);
        BLState = 1;
        Serial.println("Backlight On");
        ReadState = 0;
      }     
    }   
    else if(Sin =='D' && ReadState == 2){
     lcd.clear();
     ReadState = 3;
    }
    else if(ReadState == 3) {    
      if (Sin == '\n' || Sin == '\r') {
        ReadState = 0;
      } else {
        lcd.write(Sin);
      }
    }
    else {
      ReadState = 0;
    }    
    
  }
  
  unsigned long CurMils = millis();
  if(CurMils - PrevMils > interval) {
    PrevMils = CurMils;
    //Serial.println("1234");
    //Serial.println("root");
    //Serial.println("admin");
    //Serial.print("echo \"COD`/sbin/ifconfig br0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'`\"\r\n"); 
    CrackingDisplay(); //calls the cracking function (twice)
 
    if(CD > 1){
      matrix(); //calls matrix function
      credits(); //calls the credits functions
      CD=0;
      
    }
    
    
  }
  
}
