///< CONFIG: Must be configured for proper platform
///< CONFIG: Config 0=IDE MODE, Config 1=WOKII MODE
#define CONFIG 2
#if CONFIG == 0
#define CONFIG_TEXT "IDE"
#elif CONFIG == 1
#define CONFIG_TEXT "WOKII"
#elif CONFIG == 2
#define CONFIG_TEXT "TFT_eSPI"
#endif
///< INCLUDE: This includes all variables, defintions, libraries & drivers required
///< INCLUDE:
#include "include.h"
float center_x = 120; // center x of tachometer - 240x240 TFT
float center_y = 120; // center y of tachometer - 240x240 TFT
float edge_x = 0;
float edge_y = 0;
float edge_x_old = 0;
float edge_y_old = 0;
float edge_x_out = 0;
float edge_y_out = 0;
float circle0x=0, circle0y=0, circle50x=0,circle50y=0, circle100x=0,circle100y=0,circle150x=0,circle150y=0,circle180x=0,circle180y=0;
float needletail_x = 0;
float needletail_y = 0;
float needletail_x_old = 0;
float needletail_y_old = 0;
int j;
float angle = 0;
int radius = 87;
float angle_circle = 0;
float angle_needle = 0;
int iteration = 0;
int stopper = 90; // moment to reverse direction
int v = 1; // needle in accerator or decelerator mode
// for for-next loops
float needle_x = 0; // needle pivot = center
float needle_y = 0; // needle pivot = center
float needle_x_old = 120;
float needle_y_old = 120;
int tachoSpeed = 0;
int cycleTime = 200;
void setup(void) {
Serial.begin (9600);
tft.begin ();
tft.setRotation (0); // display in portrait
tft.fillScreen (BLACK);
drawBigDial ();
needle ();
numericDial ();
}
void loop (){
if (iteration == 0)
{
v=+1;
delay (5*cycleTime);
}
iteration = iteration+v;
tachoSpeed = iteration*2;
serial_print_stuff();
needle ();
numericDial ();
if (iteration == stopper)
{
v =-1;
delay (5*cycleTime);
}
delay (cycleTime);
}
void drawBigDial (){
tft.drawCircle (center_x, center_y, radius+10, MAGENTA); // outer margin ring
tft.drawCircle (center_x, center_y, radius+11, MAGENTA); // outer margin ring
tft.drawCircle (center_x, center_y, radius+7, BLUE); // outer margin ring
tft.drawCircle (center_x, center_y, radius+8, BLUE); // outer margin ring
tft.drawCircle (center_x, center_y, radius+4, CYAN); // outer margin ring
tft.drawCircle (center_x, center_y, radius+5, CYAN); // outer margin ring
for (j=0; j<360; j=(j+6)) // scale markers set 6 degrees apart
{
angle_circle = (j*DEG2RAD);
edge_x = (center_x + (radius*cos (angle_circle)));
edge_y = (center_y + (radius*sin (angle_circle)));
edge_x_out = (center_x + ((radius+8)*cos (angle_circle)));
edge_y_out = (center_y + ((radius+8)*sin (angle_circle)));
tft.drawLine (edge_x, edge_y, edge_x_out, edge_y_out, WHITE);
}
for (j=0; j<271; j=(j+90)) // scale markers set 6 degrees apart
{
angle_circle = (j*DEG2RAD);
edge_x = (center_x + ((radius-5)*cos (angle_circle)));
edge_y = (center_y + ((radius-5)*sin (angle_circle)));
edge_x_out = (center_x + ((radius-1)*cos (angle_circle)));
edge_y_out = (center_y + ((radius-1)*sin (angle_circle)));
tft.fillCircle (edge_x, edge_y, 3, LIGHTYELLOW);
if (j==0) {
circle0x=edge_x;
circle0y=edge_y;
}
if (j==90) {
circle50x=edge_x;
circle50y=edge_y;
}
if (j==180) {
circle100x=edge_x;
circle100y=edge_y;
}
if (j==270) {
circle150x=edge_x;
circle150y=edge_y;
}
if (j==340) {
circle180x=edge_x;
circle180y=edge_y;
}
tft.drawLine (edge_x, edge_y, edge_x_out, edge_y_out, LIGHTYELLOW);
}
tft.setTextSize (1);
tft.setTextColor (WHITE);
tft.setCursor (center_x-110, center_y-5);
tft.print ("0");
tft.setCursor (center_x-5, center_y-111);
tft.print ("50");
tft.setCursor (center_x+100, center_y-5);
tft.print ("100");
tft.setCursor (center_x-10, center_y+104);
tft.print ("150");
tft.setCursor (center_x-100, center_y+50);
tft.print ("180");
tft.fillCircle (center_x-70,center_y+50, 3, LIGHTYELLOW);
tft.drawRoundRect ( (center_x-100),(center_y+5), 50, 22, 3, CYAN); // small numeric tacho dial
}
void needle (){
if (tachoSpeed >180) tachoSpeed = 180; // needle limiter
if (trunc(tachoSpeed) == 0) {
tft.fillCircle (circle0x, circle0y, 3, LIGHTYELLOW);
}
if (trunc(tachoSpeed) == 50) {
tft.fillCircle (circle50x, circle50y, 3, LIGHTYELLOW);
}
if (trunc(tachoSpeed) == 100) {
tft.fillCircle (circle100x, circle100y, 3, LIGHTYELLOW);
}
if (trunc(tachoSpeed) == 150) {
tft.fillCircle (circle150x, circle150y, 3, LIGHTYELLOW);
}
if (trunc(tachoSpeed) == 180) {
tft.fillCircle (circle180x, circle180y, 3, LIGHTYELLOW);
}
tft.drawLine (needle_x_old, needle_y_old, needletail_x_old, needletail_y_old, BLACK);
angle_needle = (((tachoSpeed)*DEG2RAD*1.8)-3.14); // unit of angle is radian
needle_x = (center_x + ((radius-10)*cos(angle_needle)));
needle_y = (center_y + ((radius-10)*sin(angle_needle)));
needletail_x = (center_x - ((radius-60)*cos(angle_needle-6.28)));
needletail_y = (center_x - ((radius-60)*sin(angle_needle-6.28)));
needle_x_old = needle_x; // remember previous needle position
needle_y_old = needle_y;
needletail_x_old = needletail_x;
needletail_y_old = needletail_y;
tft.drawLine (needle_x, needle_y, needletail_x, needletail_y,RED);
tft.fillCircle (center_x, center_y, 6, BLACK); // restore needle pivot
tft.fillCircle (center_x, center_y, 6, RED); // restore needle pivot
}
void serial_print_stuff(){
if (tachoSpeed < 100) Serial.print ("speed: ");
if (tachoSpeed >= 100) Serial.print ("speed: ");
Serial.print (tachoSpeed);
Serial.println (" km/h");
}
void numericDial (){
tft.fillRect ((center_x-93), (center_y+9), 40, 16, GREY);
tft.setTextColor (YELLOW, BLACK);
tft.setTextSize (2);
if (tachoSpeed >99)
{
tft.setCursor (center_x-93, center_y+9);
}
else tft.setCursor (center_x-83, center_y+9);
tft.print (tachoSpeed,1);
}