 /**** state functions ****/
 
 void nextState() {
-  current_state++;
-  if (current_state == STATE_END) {
-    current_state = STATE_RESET;
-  }
+	current_state++;
+	if (current_state == STATE_END) {
+		current_state = STATE_RESET;
+	}
 }
 
 void doStateReset() {
-  // show the splash screen
-  reset();  
+	// show the splash screen
+	reset();  
 }
 
 void doStateNfcKeyLogin() {
-  // wait for the touch and show it
-  if (screen.getTouchScreen().bufferEmpty()) {
-    return;
-  }
-  if (! screen.getTouchScreen().touched()) {
-    return;
-  }
-
-
-  // Retrieve a point  
-  TS_Point p = screen.touchscreenGetPoint();
-  /*
+	// wait for the touch and show it
+	if (screen.getTouchScreen().bufferEmpty()) {
+		return;
+	}
+	if (! screen.getTouchScreen().touched()) {
+		return;
+	}
+
+
+	// Retrieve a point  
+	TS_Point p = screen.touchscreenGetPoint();
+	/*
     if (p.z == 0) {
-   return;
-   }
-   */
-
-  // act on touchscreen interaction
-  actOnTouchScreenInteraction(p.x, p.y, true);
+	 return;
+	 }
+	 */
+
+	// act on touchscreen interaction
+	actOnTouchScreenInteraction(p.x, p.y, true);
 }
 
 void doStateNfcCardLogin() {
 
-  // read the nfc card
-  //uint8_t success;
-  uint8_t uid[] = { 
-    0, 0, 0, 0, 0, 0, 0   };                    // Buffer to store the returned UID
-  uint8_t uidLength = nfclib.initTarget(&*uid);               // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
-  uidLength = nfclib.printTarget();   
-
-  // show the data
-  showDataScreen();
-
-  // reset everything()
-  reset();  
+	// read the nfc card
+	//uint8_t success;
+	uint8_t uid[] = { 
+		0, 0, 0, 0, 0, 0, 0   	};                    // Buffer to store the returned UID
+	uint8_t uidLength = nfclib.initTarget(&*uid);               // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
+	uidLength = nfclib.printTarget();   
+
+	// show the data
+	showDataScreen();
+
+	// reset everything()
+	reset();  
 }
 
 void doStateShowData() {
-  // show the data
-  showDataScreen();
-
-  nextState();
+	// show the data
+	showDataScreen();
+
+	nextState();
 }
 
 

/**** touchscreen action functions ****/

void actOnTouchScreenInteraction(uint16_t x, uint16_t y, boolean keyboardOn) {
  uint16_t millis_now = millis();
  uint16_t millis_delta = millis_now - last_key_press_ms;  
  char c;

  // check polling
  if (millis_delta <= KEYBOARD_POLLING_MS) {
    return;
  }

  // actions
  actOnButtonPress(x, y);

  if (keyboardOn) {
    if ( nfc_key_count > 5) {
      return;
    }
    c = screen.actOnKeyboardKeyPress(x, y);
    if (c != 0) {
      nfc_key[nfc_key_count] = c;
      nfc_key_count++;

      char dest[12];
      strcpy(dest, "key: ");
      strcat(dest, nfc_key);
      dest[11] = '\0';
      screen.writeTextToBottom(dest);


      // show the nfc screen when password is fullfilled
      if (nfc_key_count > 5) {
        showNFCCardScreen();
      }

    }
  }


  // set last key press for polling
  last_key_press_ms = millis_now;
}

void actOnButtonPress(uint16_t x, uint16_t y) {
  if (SCREEN_BUTTON_MINY + SCREEN_TEXT_PADDING*2.7 >= y && y <= SCREEN_BUTTON_MINY + SCREEN_TEXT_PADDING*2.7 + SCREEN_BUTTON_SIZE * 2) {
    if (SCREEN_TEXT_PADDING*3 >= x && x <= SCREEN_TEXT_PADDING*3 + SCREEN_BUTTON_SIZE * 2) {
	  Serial.print("Button pressed: ");   
	  Serial.print(x); 
	  Serial.print(" / "); 
	  Serial.print(y);
	  Serial.println("");
      reset();  
    }
  }
}



/**** display prepare functions ****/

void clearScreen() {
	screen.clearScreen();
}


void reset() {
	// reset all global variables
	current_state = STATE_RESET;
	//  last_key_press_ms = 0;
	//  last_key = 0;
	memset(nfc_key, 0, sizeof(nfc_key));
	nfc_key_count = 0;

	// show the splash screen
	showSplashScreen();

	// reset the sd card controller
	// sd.setup();

	// reset the touchscreen
	uint16_t x, y;
	uint8_t z;  
	while (!screen.getTouchScreen().bufferEmpty()) {
		screen.getTouchScreen().readData(&x, &y, &z);
		Serial.println(screen.getTouchScreen().bufferSize());
	}
	screen.getTouchScreen().writeRegister8(STMPE_INT_STA, 0xFF);

	// setup nfc
	nfclib.setupHost();

	// show the nfc screen
	showNFCKeyScreen();
}


