/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Fonts/FreeMonoBoldOblique12pt7b.h>
#include "batteria.h"
Battery battery(Battery::Size::Unit);
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define pinPot A0
byte X = 0;
byte Y = 0;
byte W = 160;
byte H = 10;
double progress1 = 0.0;
int i=0;
int giri=0;
bool crescita=0;
int value=0;
int periodo = 200;
unsigned long tempoPrec= 0;
class GFXField {
public:
void begin(int16_t row, int16_t col, byte len) {
m_row = row;
m_col = col;
m_len = len;
if (m_ptrBuffer != nullptr) {
m_ptrBuffer = new char[len+1];
}
}
void setTrackVar(byte *var, byte varT) {
m_ptrTrackingVar = reinterpret_cast<int32_t*>(var);
//&var;
m_varType = varT;
//copyValue = *m_ptrTrackingVar;
//Serial.println(copyValue);
}
void setTrackVar(int *var) {
m_ptrTrackingVar = reinterpret_cast<int32_t*>(var);
}
void setColor(uint16_t fg, uint16_t bg) {
m_fg = fg;
m_bg = bg;
}
void show() {
int32_t cv = static_cast<int32_t>(*m_ptrTrackingVar);
if (copyValue != cv) {
if (copyValue != 0xFFFFFFFF) {
Serial.println("pulisce");
} else {
Serial.println("non pulisce");
}
}
copyValue = cv;
}
private:
int32_t copyValue = 0xFFFFFFFF;
byte m_varType;
int32_t *m_ptrTrackingVar;
byte m_len;
int16_t m_row;
int16_t m_col;
uint16_t m_fg;
uint16_t m_bg;
char *m_ptrBuffer = nullptr;
};
GFXField giriField;
void test() {
Serial.println("i\tVolts\tVbatt");
char batBuff[9];
for (uint16_t i = 0; i<1024; i++) {
int Vbatt = map(i,0,1023,0,51);// / 100; // nota il diviso 10 che è fondamentale
Serial.print(i);
Serial.print('\t');
sprintf(batBuff, "V.%u,%01u", Vbatt/10, Vbatt%10); // non pasticciare a caso, deve essere / 100 e %100 uguale uguale.
Serial.print(batBuff);
Serial.print('\t');
Serial.println(Vbatt);
}
}
void setup() {
Serial.begin(115200);
Serial.println("Hello");
tft.begin();
//battery.begin();
battery.setPos(200, 0);
battery.draw();
/*battery.drawEnergy(5);
delay(200);
battery.drawEnergy(4);
delay(200);*/
battery.drawEnergy(3);
delay(200);
battery.drawEnergy(2);
delay(200);
battery.drawEnergy(1);
delay(200);
battery.drawEnergy(0);
delay(200);
battery.drawEnergy(1);
delay(200);
battery.drawEnergy(2);
delay(200);
battery.drawEnergy(3);
battery.clear();
delay(1000);
battery.draw();
return;
test();
giriField.begin(60, 80, 4);
giriField.setTrackVar(&W, 0);
giriField.show();
W = 50;
giriField.show();
Serial.println((uint16_t)&PIND, HEX);
i = 1909;
if(1999>=i && i>1000) {
Serial.println(i);
}
for (i=0; i<6000; i++)
if(1999>=i>1000 || 2999>=i>=2000 || 3999>=i>=3000 || 4999>=i>=4000 || 5999>=i>=5000)
Serial.println("OK");
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello, TFT!");
tft.setCursor(20, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("I can has colors?");
delay(1000);
//tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.fillScreen(0x0000); //black
tft.setFont(&FreeMonoBoldOblique12pt7b);
tft.setTextColor(0xFFFF); //bianco
tft.setCursor(10,100);
tft.setTextSize(1);
tft.print("RPM");
tft.setFont();
tft.setTextSize(1);
tft.setCursor(0, 20);
tft.print("0");
tft.setCursor(25, 20);
tft.print("1");
tft.setCursor(50, 20);
tft.print("2");
tft.setCursor(75, 20);
tft.print("3");
tft.setCursor(100, 20);
tft.print("4");
tft.setCursor(125, 20);
tft.print("5");
tft.setCursor(150, 20);
tft.print("6");
//tft.setCursor(0, 25);
tft.drawLine(0, H, 160, H, 0xFFFF);
delay(1000);
// Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
}
float fMap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void contatore() {
/*if(1999>=i>1000 || 2999>=i>=2000 || 3999>=i>=3000 || 4999>=i>=4000 || 5999>=i>=5000) {
tft.fillRect(80, 80, 20, 30, 0x0000);
} else {*/
//tft.fillRect(60, 80, 20, 30, 0x0000);
//tft.fillRect(81, 80, 25, 30, 0x0000);
//}
/*if(i<=1000) {
tft.setCursor(60, 80);
tft.fillRect(60, 80, 25, 30, 0x0000);
//tft.fillRect(100, 80, 20, 30, 0x0000);
tft.fillRect(130, 80, 40, 30, 0x0000);
}
if(i==0)
tft.fillRect(80, 80, 80, 30, 0x0000);
*/
}
int giriOld = 6100;
void barra() {
char buff[5] = {0};
progress1= fMap(value, 0, 1023, 0, 160);
i= map(value, 0, 1023, 0, 6000);
tft.setTextSize(4);
//tft.setFont(&FreeMonoBoldOblique12pt7b);
// sprintf e il colore di background si possono usare solo
// con il font standard, cioè con il font sopra non funge.
//contatore();
giri= (i+50)- ((i+50)%100);
/*
if (giri != giriOld) {
tft.setCursor(60, 80);
tft.setTextColor(0);
tft.print(giriOld);
giriOld = giri;
tft.setCursor(60, 80);
tft.setTextColor(ILI9341_WHITE);
tft.print(giri);
}
*/
tft.setCursor(60, 80);
tft.setTextColor(ILI9341_WHITE, 0);
sprintf(buff, "%#4u", giri);
tft.print(buff);
}
void loop() {
value = analogRead(pinPot);
if(millis()-tempoPrec>=500) {
//barra();
battery.recharge();
tempoPrec=millis();
}
}