Code:
#include <EncoderTool.h>
#include <ILI9341_t3n.h>
#include <ili9341_t3n_font_Arial.h>
#include <ili9341_t3n_font_ArialBold.h>
#define thisGREY 0x3A07
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST -1
#define TFT_SCK 13
#define TFT_MISO 12
#define TFT_MOSI 11
using namespace EncoderTool;
Encoder DROZ_Enc;
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO);
int errorcount, myacc;
float conversionfactor;
volatile float Zval, oldZval;
uint16_t cxg, cyg;
void onDROZ_Changed( int value, int delta )
{
if (delta >0)
{
if (delta>1)
{
Serial.printf("Fault! delta =% i\n", delta);
errorcount+= 1;
}
else
{
myacc = myacc + delta;
}
oldZval = Zval;
Zval = myacc * conversionfactor;
}
if (delta<0)
{
if (delta<-1)
{
Serial.printf("Fault! delta = %i\n", delta);
errorcount += 1;
}
else
{
myacc = myacc + delta;
}
oldZval = Zval;
Zval = myacc * conversionfactor;
}
}
bool initMyDisplay(int rotation)
{
tft.begin();
tft.setRotation(rotation);
tft.fillScreen(ILI9341_BLACK);
return true;
}
void mainMenu()
{
tft.fillScreen(thisGREY);
tft.setCursor(0,0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextDatum(TL_DATUM);
tft.setFont(Arial_18);
tft.drawString("Digital Read Out V0.00", 0,0);
tft.println();
uint16_t cx = tft.getCursorX();
uint16_t cy = tft.getCursorY();
tft.setTextColor(ILI9341_GREEN);
tft.drawString("Z axis", cx, cy);
tft.println();
cx = tft.getCursorX();
cy = tft.getCursorY();
cxg = cx;
cyg = cy;
tft.drawFloat(Zval, 4, cx, cy);
}
void setup() {
// put your setup code here, to run once:
while(!Serial && millis() < 4000);
for(int i=5; i<8; i++) pinMode(i, OUTPUT); // init these pins to outputs
DROZ_Enc.begin(0,1, CountMode::full);
DROZ_Enc.attachCallback(nullptr); // don't attach the callback yet until everything is ready!
DROZ_Enc.setValue(0); // set initial value
myacc = 0;
errorcount = 0;
conversionfactor = 1.0/5080.00;
oldZval = 0.0;
Zval = 0.0;
if (!initMyDisplay(3))
{
Serial.println("Display failed to initialize");
while(1);
}
mainMenu();
DROZ_Enc.attachCallback(onDROZ_Changed); // enable callback
}
void loop() {
// put your main code here, to run repeatedly:
if (Zval != oldZval)
{
// if count changed, update the display
uint16_t x1, y1, w, h;
oldZval = Zval;
String newstr = "XXXXXXXX";
tft.getTextBounds(newstr, cxg, cyg, &x1, &y1, &w, &h);
tft.fillRect(x1, y1, w, h, thisGREY);
tft.drawFloat( Zval, 4, cxg, cyg );
//Serial.printf("Zval = %+7f4\n", Zval); // uncomment for serial output
}
delay(10);
}
Hope this helps.