/*
Based on VU Meter Sketch From Instructables by Bodmer
http://www.instructables.com/id/Arduino-sketch-for-a-retro-analogue-meter-graphic-/
An example analogue meter using a ILI9341 TFT LCD screen
This example uses the hardware SPI only
Alan Senior 23/2/2015
Stripped out the special library functions
Wozzy 09/01/2015
*/
#include "SPI.h"
#include "ILI9341_t3.h"
#include "font_DroidSans.h"
#include "font_DroidSans_Bold.h"
#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST);
#define RGB(r,g,b) (r<<11|g<<5|b) // R = 5 bits 0 to 31, G = 6 bits 0 to 63, B = 5 bits 0 to 31
#define ILI9341_GREY 0x5AEB
float ltx = 0; // Saved x coord of bottom of needle
uint16_t osx = 120, osy = 120; // Saved x & y coords
uint32_t updateTime = 0; // time for next update
int old_analog = -999; // Value last displayed
int old_digital = -999; // Value last displayed
void setup(void) {
tft.begin();
tft.setRotation(2);
tft.fillScreen(ILI9341_BLACK);
analogMeter(); // Draw analogue meter
updateTime = millis(); // Next update time
}
void loop() {
if (updateTime <= millis()) {
updateTime = millis() + 500;
int reading = 0;
reading = random(0, 100); // Test with random value
//reading = map(analogRead(A0),0,1023,0,100); // Test with value form Analogue 0
plotNeedle(reading, 8); // Update analogue meter, 8ms delay per needle increment
}
}
// #########################################################################
// Draw the analogue meter on the screen
// #########################################################################
void analogMeter()
{
// Meter outline
tft.setRotation(1);
tft.fillRect(0, 0, 239, 126, ILI9341_GREY);
tft.fillRect(5, 3, 230, 119, ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK); // Text colour
tft.setRotation(1);
// Draw ticks every 5 degrees from -50 to +50 degrees (100 deg. FSD swing)
for (int i = -50; i < 51; i += 5) {
// Long scale tick length
int tl = 15;
// Coodinates of tick to draw
float sx = cos((i - 90) * 0.0174532925);
float sy = sin((i - 90) * 0.0174532925);
uint16_t x0 = sx * (100 + tl) + 120;
uint16_t y0 = sy * (100 + tl) + 140;
uint16_t x1 = sx * 100 + 120;
uint16_t y1 = sy * 100 + 140;
// Coordinates of next tick for zone fill
float sx2 = cos((i + 5 - 90) * 0.0174532925);
float sy2 = sin((i + 5 - 90) * 0.0174532925);
int x2 = sx2 * (100 + tl) + 120;
int y2 = sy2 * (100 + tl) + 140;
int x3 = sx2 * 100 + 120;
int y3 = sy2 * 100 + 140;
// White zone limits
//if (i >= -50 && i < -45) {
// tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_WHITE);
// tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_WHITE);
//}
// Green zone limits
if (i >= -50 && i < 30) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_GREEN);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_GREEN);
}
// Yellow zone limits
if (i >= 30 && i < 40) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_YELLOW);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_YELLOW);
}
// Orange zone limits
if (i >= 40 && i < 45) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_ORANGE);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_ORANGE);
}
// Red zone limits
if (i >= 45 && i < 50) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_RED);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_RED);
}
// Short scale tick length
if (i % 25 != 0) tl = 8;
// Recalculate coords incase tick lenght changed
x0 = sx * (100 + tl) + 120;
y0 = sy * (100 + tl) + 140;
x1 = sx * 100 + 120;
y1 = sy * 100 + 140;
// Draw tick
tft.drawLine(x0, y0, x1, y1, ILI9341_BLACK);
tft.setFont(DroidSans_12);
tft.setCursor(25,50);
tft.print("0");
tft.setCursor(60,20);
tft.print("25");
tft.setCursor(110,9);
tft.print("50");
tft.setCursor(160,20);
tft.print("75");
tft.setCursor(202,50);
tft.print("100");
tft.setFont(DroidSans_14_Bold);
tft.setCursor(8,100);
tft.print("TEMP");
tft.setCursor(210,100);
tft.print("°F");
// Now draw the arc of the scale
sx = cos((i + 5 - 90) * 0.0174532925);
sy = sin((i + 5 - 90) * 0.0174532925);
x0 = sx * 100 + 120;
y0 = sy * 100 + 140;
// Draw scale arc, don't draw the last part
if (i < 50) tft.drawLine(x0, y0, x1, y1, ILI9341_BLACK);
}
tft.drawRect(5, 3, 230, 119, ILI9341_BLACK); // Draw bezel line
plotNeedle(0,0); // Put meter needle at 0
}
// #########################################################################
// Update needle position
// This function is blocking while needle moves, time depends on ms_delay
// 10ms minimises needle flicker if text is drawn within needle sweep area
// Smaller values OK if text not in sweep area, zero for instant movement but
// does not look realistic... (note: 100 increments for full scale deflection)
// #########################################################################
void plotNeedle(int value, byte ms_delay)
{
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
char buf[8]; dtostrf(value, 4, 0, buf);
// tft.drawRightString(buf, 40, 119 - 20, 2);
if (value < -10) value = -10; // Limit value to emulate needle end stops
if (value > 110) value = 110;
// Move the needle util new value reached
while (!(value == old_analog)) {
if (old_analog < value) old_analog++;
else old_analog--;
if (ms_delay == 0) old_analog = value; // Update immediately id delay is 0
float sdeg = map(old_analog, -10, 110, -150, -30); // Map value to angle
// Calcualte tip of needle coords
float sx = cos(sdeg * 0.0174532925);
float sy = sin(sdeg * 0.0174532925);
// Calculate x delta of needle start (does not start at pivot point)
float tx = tan((sdeg+90) * 0.0174532925);
// Erase old needle image
tft.drawLine(120 + 20 * ltx - 1, 140 - 20, osx - 1, osy, ILI9341_WHITE);
tft.drawLine(120 + 20 * ltx, 140 - 20, osx, osy, ILI9341_WHITE);
tft.drawLine(120 + 20 * ltx + 1, 140 - 20, osx + 1, osy, ILI9341_WHITE);
// Re-plot text under needle
tft.setTextColor(ILI9341_BLACK);
// tft.drawCentreString("%RH", 120, 70, 4); // // Comment out to avoid font 4
// Store new needle end coords for next erase
ltx = tx;
osx = sx * 98 + 120;
osy = sy * 98 + 140;
// Draw the needle in the new postion, magenta makes needle a bit bolder
// draws 3 lines to thicken needle
tft.drawLine(120 + 20 * ltx - 1, 140 - 20, osx - 1, osy, ILI9341_RED);
tft.drawLine(120 + 20 * ltx, 140 - 20, osx, osy, ILI9341_MAGENTA);
tft.drawLine(120 + 20 * ltx + 1, 140 - 20, osx + 1, osy, ILI9341_RED);
// Slow needle down slightly as it approaches new postion
if (abs(old_analog - value) < 10) ms_delay += ms_delay / 5;
// Wait before next update
delay(ms_delay);
}
}