202 lines
6.5 KiB
C++
Executable File
202 lines
6.5 KiB
C++
Executable File
/*
|
|
An example analogue clock using a TFT LCD screen to show the time
|
|
use of some of the drawing commands with the modified Adafruit_TFT_AS library.
|
|
For a more accurate clock, it would be better to use the RTClib library.
|
|
But this is just a demo.
|
|
|
|
This sketch uses font 4 only.
|
|
|
|
Make sure all the required fonts are loaded by editting the
|
|
User_Setup.h file in the TFT_ILI9341 library folder.
|
|
|
|
If using an UNO or Mega (ATmega328 or ATmega2560 processor) then for best
|
|
performance use the F_AS_T option found in the User_Setup.h file in the
|
|
TFT_ILI9341 library folder.
|
|
|
|
The library uses the hardware SPI pins only:
|
|
For UNO, Nano, Micro Pro ATmega328 based processors
|
|
MOSI = pin 11, SCK = pin 13
|
|
For Mega:
|
|
MOSI = pin 51, SCK = pin 52
|
|
|
|
The pins used for the TFT chip select (CS) and Data/command (DC) and Reset (RST)
|
|
signal lines to the TFT must also be defined in the library User_Setup.h file.
|
|
|
|
Sugested TFT connections for UNO and Atmega328 based boards
|
|
sclk 13 // Don't change, this is the hardware SPI SCLK line
|
|
mosi 11 // Don't change, this is the hardware SPI MOSI line
|
|
cs 10 // Chip select for TFT display
|
|
dc 9 // Data/command line
|
|
rst 7 // Reset, you could connect this to the Arduino reset pin
|
|
|
|
Suggested TFT connections for the MEGA and ATmega2560 based boards
|
|
sclk 52 // Don't change, this is the hardware SPI SCLK line
|
|
mosi 51 // Don't change, this is the hardware SPI MOSI line
|
|
cs 47 // TFT chip select line
|
|
dc 48 // TFT data/command line
|
|
rst 44 // you could alternatively connect this to the Arduino reset
|
|
|
|
#########################################################################
|
|
###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
|
|
###### TO SELECT THE FONTS AND PINS YOU USE, SEE ABOVE ######
|
|
#########################################################################
|
|
|
|
Based on a sketch by Gilchrist 6/2/2014 1.0
|
|
*/
|
|
|
|
// These are the pins for the shield!
|
|
#define YP A1 // must be an analog pin, use "An" notation!
|
|
#define XM A2 // must be an analog pin, use "An" notation!
|
|
#define YM 7 // can be a digital pin
|
|
#define XP 6 // can be a digital pin
|
|
|
|
#include <Adafruit_GFX_AS.h> // Core graphics library
|
|
#include <Adafruit_ILI9341_8bit_AS.h>// Hardware-specific library
|
|
|
|
#include <SPI.h>
|
|
|
|
//#define TFT_GREY 0x5AEB
|
|
|
|
#define LCD_CS A3 // Chip Select goes to Analog 3
|
|
#define LCD_CD A2 // Command/Data goes to Analog 2
|
|
#define LCD_WR A1 // LCD Write goes to Analog 1
|
|
#define LCD_RD A0 // LCD Read goes to Analog 0
|
|
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
|
|
Adafruit_ILI9341_8bit_AS tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
|
|
|
|
|
|
float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0; // Saved H, M, S x & y multipliers
|
|
float sdeg=0, mdeg=0, hdeg=0;
|
|
uint16_t osx=120, osy=120, omx=120, omy=120, ohx=120, ohy=120; // Saved H, M, S x & y coords
|
|
uint16_t x0=0, x1=0, y0=0, y1=0;
|
|
uint32_t targetTime = 0; // for next 1 second timeout
|
|
uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6); // Get H, M, S from compile time
|
|
boolean initial = 1;
|
|
|
|
// Assign human-readable names to some common 16-bit color values:
|
|
#define BLACK 0x0000
|
|
#define BLUE 0x001F
|
|
#define RED 0xF800
|
|
#define GREEN 0x07E0
|
|
#define CYAN 0x07FF
|
|
#define MAGENTA 0xF81F
|
|
#define YELLOW 0xFFE0
|
|
#define WHITE 0xFFFF
|
|
#define ORANGE 0xF400
|
|
#define GREY 0x5AEB
|
|
|
|
void setup(void) {
|
|
// tft.init();
|
|
tft.reset();
|
|
delay(10);
|
|
tft.begin(0x9341);
|
|
tft.setRotation(2);
|
|
|
|
//tft.fillScreen(TFT_BLACK);
|
|
//tft.fillScreen(TFT_RED);
|
|
//tft.fillScreen(TFT_GREEN);
|
|
//tft.fillScreen(TFT_BLUE);
|
|
//tft.fillScreen(TFT_BLACK);
|
|
tft.fillScreen(GREY);
|
|
|
|
tft.setTextColor(WHITE, GREY); // Adding a background colour erases previous text automatically
|
|
|
|
// Draw clock face
|
|
tft.fillCircle(120, 120, 118, GREEN);
|
|
tft.fillCircle(120, 120, 110, BLACK);
|
|
|
|
// Draw 12 lines
|
|
for(int i = 0; i<360; i+= 30) {
|
|
sx = cos((i-90)*0.0174532925);
|
|
sy = sin((i-90)*0.0174532925);
|
|
x0 = sx*114+120;
|
|
y0 = sy*114+120;
|
|
x1 = sx*100+120;
|
|
y1 = sy*100+120;
|
|
|
|
tft.drawLine(x0, y0, x1, y1, GREEN);
|
|
}
|
|
|
|
// Draw 60 dots
|
|
for(int i = 0; i<360; i+= 6) {
|
|
sx = cos((i-90)*0.0174532925);
|
|
sy = sin((i-90)*0.0174532925);
|
|
x0 = sx*102+120;
|
|
y0 = sy*102+120;
|
|
// Draw minute markers
|
|
tft.drawPixel(x0, y0, WHITE);
|
|
|
|
// Draw main quadrant dots
|
|
if(i==0 || i==180) tft.fillCircle(x0, y0, 2, WHITE);
|
|
if(i==90 || i==270) tft.fillCircle(x0, y0, 2, WHITE);
|
|
}
|
|
|
|
tft.fillCircle(120, 121, 3, WHITE);
|
|
|
|
// Draw text at position 120,260 using fonts 4
|
|
// Only font numbers 2,4,6,7 are valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : . a p m
|
|
// Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .
|
|
tft.drawCentreString("Time flies",120,260,4);
|
|
|
|
targetTime = millis() + 1000;
|
|
}
|
|
|
|
void loop() {
|
|
if (targetTime < millis()) {
|
|
targetTime = millis()+1000;
|
|
ss++; // Advance second
|
|
if (ss==60) {
|
|
ss=0;
|
|
mm++; // Advance minute
|
|
if(mm>59) {
|
|
mm=0;
|
|
hh++; // Advance hour
|
|
if (hh>23) {
|
|
hh=0;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pre-compute hand degrees, x & y coords for a fast screen update
|
|
sdeg = ss*6; // 0-59 -> 0-354
|
|
mdeg = mm*6+sdeg*0.01666667; // 0-59 -> 0-360 - includes seconds
|
|
hdeg = hh*30+mdeg*0.0833333; // 0-11 -> 0-360 - includes minutes and seconds
|
|
hx = cos((hdeg-90)*0.0174532925);
|
|
hy = sin((hdeg-90)*0.0174532925);
|
|
mx = cos((mdeg-90)*0.0174532925);
|
|
my = sin((mdeg-90)*0.0174532925);
|
|
sx = cos((sdeg-90)*0.0174532925);
|
|
sy = sin((sdeg-90)*0.0174532925);
|
|
|
|
if (ss==0 || initial) {
|
|
initial = 0;
|
|
// Erase hour and minute hand positions every minute
|
|
tft.drawLine(ohx, ohy, 120, 121, BLACK);
|
|
ohx = hx*62+121;
|
|
ohy = hy*62+121;
|
|
tft.drawLine(omx, omy, 120, 121, BLACK);
|
|
omx = mx*84+120;
|
|
omy = my*84+121;
|
|
}
|
|
|
|
// Redraw new hand positions, hour and minute hands not erased here to avoid flicker
|
|
tft.drawLine(osx, osy, 120, 121, BLACK);
|
|
osx = sx*90+121;
|
|
osy = sy*90+121;
|
|
tft.drawLine(osx, osy, 120, 121, RED);
|
|
tft.drawLine(ohx, ohy, 120, 121, WHITE);
|
|
tft.drawLine(omx, omy, 120, 121, WHITE);
|
|
tft.drawLine(osx, osy, 120, 121, RED);
|
|
|
|
tft.fillCircle(120, 121, 3, RED);
|
|
}
|
|
}
|
|
|
|
static uint8_t conv2d(const char* p) {
|
|
uint8_t v = 0;
|
|
if ('0' <= *p && *p <= '9')
|
|
v = *p - '0';
|
|
return 10 * v + *++p - '0';
|
|
}
|
|
|