/*
  IL 1941 pinout 
CS pin is connected to Arduino digital pin 8, - via 2.2k
RST pin is connected to Arduino digital pin 9, - via 2.2k
D/C pin is connected to Arduino digital pin 10, - via 2.2k
MOSI pin is connected to Arduino digital pin 11, - via 2.2k
SCK pin is connected to Arduino digital pin 13. - via 2.2k 
pinout as per wokwi Adafruit demo not as above
    resistors to drop v to 3.3.--
Other pins are connected as follows:
VCC pin is connected to Arduino 5V pin,
GND pin is connected to Arduino GND pin,
BL (LED) pin is connected to Arduino 5V pin, ??
MISO pin is not connected. ??
ILI 9341	NANO			Orig
1 	Vcc		3.3V
2 	Gnd 	Gnd
3 	CS  	D10	SS
4 	RST
5 	D/C 	D09	----
6 	MOSI 	D11	MOSI
7 	SCK		D13 SCK
8 	LED
9 	MISO	D12	MISO
10 	SCL		A5	SCL
11 	SDA		A4	SDA

		*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_RST		8
#define TFT_DC 		9
#define TFT_CS 		10
#define TFT_MOSI 	11
#define TFT_MISO 	12
#define TFT_CLK 	13  // (SCK)

#define buttonX 	2
#define buttonY 	3

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);



		 int X_clk = 6; // CLK
		 int X_dt = 7; // DT
		 int X_clkLast;
		 int X_counter = 0; //  Min of 0 and Max of 19 due to one full encoder turn ?? necessary ??
		
		 int Y_clk = 4; // CLK
		 int Y_dt = 5; // DT
		 int Y_clkLast;
		 int Y_counter = 0;    //  ?? necessary ??
		
		 unsigned long currentTime;
		 unsigned long loopTime;
		 //  Bed traverse 
		 unsigned char X_clk_Signal;
		 unsigned char X_dt_Signal;
		 unsigned char X_clk_Signal_prev = 0;
		 // cross slide traverse
		 unsigned char Y_clk_Signal;
		 unsigned char Y_dt_Signal;
		 unsigned char Y_clk_Signal_prev = 0;


		 void PosPrint() {
			tft.fillScreen(ILI9341_BLACK);
			tft.setCursor(0, 0);
  		tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(2);
  		tft.print("X Bed traverse = ");
			tft.println(X_counter);
			tft.print("Y Cross slide = ");
			tft.println(Y_counter);
			Serial.print("X Bed =");
			Serial.print(X_counter);
			Serial.print("Y (cross feed) = ");
			Serial.print(Y_counter);
			Serial.println();

		 }
		 void setup() {
			pinMode(X_clk,	INPUT);
		 	pinMode(X_dt,		INPUT);
			pinMode(Y_clk,	INPUT);
		 	pinMode(Y_dt, 	INPUT);
			X_clkLast = digitalRead(X_clk);
		 	Y_clkLast = digitalRead(Y_clk);
			pinMode(buttonX, INPUT_PULLUP);
			pinMode(buttonY, INPUT_PULLUP);
			
		
		 	currentTime = millis();
		 	loopTime = currentTime;
			tft.begin();
			tft.setRotation(1);
			Serial.begin(115200);
		 	Serial.println("X and Y need setting to sensor travel distance !");
		 }
		
		 void loop() {
				int sensorReset = digitalRead(buttonX);
				if (sensorReset == LOW) {
					X_counter = 0;
					Serial.println(" X button pressed");
					PosPrint();
				}
				sensorReset = digitalRead(buttonY);	
				if (sensorReset == LOW) {
					Y_counter = 0;
					Serial.println(" Y button pressed");
					PosPrint();
				}
		 // get the current elapsed time
		 currentTime = millis();
		// delay(5);

		// if (currentTime >= (loopTime + 5)) {  // why ??
		// Serial.print(Y_counter);
		// Serial.println();

		 	X_clk_Signal = digitalRead(X_clk); // Read bed encoder pins
		 	X_dt_Signal = digitalRead(X_dt);
			
			Y_clk_Signal = digitalRead(Y_clk);  // Read cross slide pins
			Y_dt_Signal = digitalRead(Y_dt);
			
		 	if ((!X_clk_Signal) && (X_clk_Signal_prev)) {	// A has gone from high to low
		 		if (X_dt_Signal) {		// B is high so X_counter-clockwise
		 			X_counter--;
		 		} else {		 // B is low so clockwise
					X_counter++;
		 		}
				PosPrint();
		 	}
		 	X_clk_Signal_prev = X_clk_Signal; // Store value of A for next time
		 	loopTime = currentTime; // Updates loopTime
			if ((!Y_clk_Signal) && (Y_clk_Signal_prev)) {		// A has gone from high to low
				if (Y_dt_Signal) {			// B is high so X_counter-clockwise
		 			Y_counter--;
		 		} else {		 // B is low so clockwise
					Y_counter++;
		 		}
				PosPrint();
		 	}
		 	Y_clk_Signal_prev = Y_clk_Signal; // Store value of A for next time
		 	loopTime = currentTime; // Updates loopTime
		 }