Added hand set buttons

This commit is contained in:
Peter-Martijn 2024-03-13 00:04:03 +01:00
parent c6e022aa1e
commit 9361814e3f

View File

@ -3,6 +3,7 @@
// Anti-aliased lines can be drawn with sub-pixel resolution and permit lines to be
// drawn with less jaggedness.
// Requires MBed OS 2040 RAspberry pi pico board
// Based on a sketch by DavyLandman:
// https://github.com/Bodmer/TFT_eSPI/issues/905
@ -21,11 +22,14 @@
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
TFT_eSprite face = TFT_eSprite(&tft);
const int BtnFwd = 6;
const int BtnMode = 8;
const int BtnBack = 7;
#define CLOCK_FG TFT_BLACK
#define CLOCK_BG TFT_WHITE
#define SECCOND_FG TFT_RED
#define HILITE_FG TFT_RED
#define LABEL_FG TFT_NAVY
#define CLOCK_R 240.0f / 2.0f // Clock face radius (float type)
@ -47,9 +51,13 @@ TFT_eSprite face = TFT_eSprite(&tft);
#define FACE_W CLOCK_R * 2 + 1
#define FACE_H CLOCK_R * 2 + 1
const int ModeNormal = 0;
const int ModeMinutes = 1;
const int ModeHours = 2;
// Time h:m:s
uint8_t h = 0, m = 0, s = 0;
uint8_t mode = ModeNormal;
float time_secs = h * 3600 + m * 60 + s;
// Init RPI_PICO_Timer, can use any from 0-15 pseudo-hardware timers
@ -78,6 +86,10 @@ void TimerHandler(uint alarm_num)
// =========================================================================
void setup() {
Serial.begin(115200);
pinMode(BtnFwd,INPUT_PULLUP);
pinMode(BtnBack,INPUT_PULLUP);
pinMode(BtnMode,INPUT_PULLUP);
delay(500);
Serial.println("Booting...");
@ -116,8 +128,9 @@ void setup() {
void loop() {
char time[20];
static unsigned long targetTime = millis() + 1000;
static long d = -1;
long e = 0;
static uint8_t lastMode = PinStatus::HIGH;
uint8_t val = PinStatus::HIGH;
// Update time periodically
if (targetTime < millis()) {
@ -127,41 +140,43 @@ void loop() {
// All graphics are drawn in sprite to stop flicker
renderFace(time_secs);
if (d > 0 && time_secs < 5) {
d = -1;
}
e = ((unsigned long) time_secs) / (60*5); // 5 minute update
if ( e > d) {
d = e;
long r = (unsigned long) time_secs;
int h = (int)(r/3600);
r = r%3600;
int m = (int)(r/60);
int s = (int)(r % 60);
sprintf(time,"%02d:%02d:%02d",h,m,s);
Serial.print(F("Time "));
Serial.println(time);
}
if ( mode > ModeNormal) {
if (!digitalRead(BtnFwd)) {
if ( mode == ModeHours ) {
time_secs += 60 * 60; // Add one minute
} else {
time_secs += 60; // Add one minute
}
while(Serial.available()) {
String str = Serial.readString();
int h = str.substring(0,2).toInt();
int m = str.substring(3,5).toInt();
int s = str.substring(6,8).toInt();
unsigned long t = s+m*60+h*3600;
time_secs = t;
d = -1;
sprintf(time,"%02d:%02d:%02d",h,m,s);
Serial.print(F("Setting to "));
Serial.println(time);
// Midnight roll-over
if (time_secs >= (60 * 60 * 24)) { time_secs -= (60 * 60 * 24); }
renderFace(time_secs);
} else if (!digitalRead(BtnBack)) {
if ( mode == ModeHours ) {
time_secs -= 60 * 60; // Add one minute
} else {
time_secs -= 60; // Add one minute
}
// Midnight roll-over
if (time_secs < 0 ) { time_secs += (60 * 60 * 24); }
renderFace(time_secs);
}
}
val = digitalRead(BtnMode);
if (!val && val != lastMode) {
mode += 1;
if ( mode > 3) { mode = 0; }
delay(100);
}
lastMode = val;
}
// =========================================================================
@ -177,7 +192,7 @@ static void renderFace(float t) {
r = r%3600;
int m = (int)(r/60);
int s = (int)(r % 60);
int HandColor = CLOCK_FG;
// The face is completely redrawn - this can be done quickly
face.fillSprite(TFT_BLACK);
@ -202,19 +217,30 @@ static void renderFace(float t) {
face.drawNumber(h, xp, 2 + yp);
}
/*
// Add text (could be digital time...)
face.setTextColor(LABEL_FG, CLOCK_BG);
sprintf(time,"%02d:%02d:%02d",h,m,s);
face.drawString(time, CLOCK_XY, CLOCK_XY * 0.75);
*/
if (mode == ModeMinutes) {
HandColor = HILITE_FG;
} else {
HandColor = CLOCK_FG;
}
// Draw minute hand
getCoord( CLOCK_XY, CLOCK_XY, &xp, &yp, M_HAND_LENGTH, m_angle);
face.drawWideLine( CLOCK_XY, CLOCK_XY, xp, yp, 6.0f, CLOCK_FG);
face.drawWideLine( CLOCK_XY, CLOCK_XY, xp, yp, 6.0f, HandColor);
// face.drawWideLine( CLOCK_XY, CLOCK_XY, xp, yp, 2.0f, CLOCK_BG);
if (mode == ModeHours) {
HandColor = HILITE_FG;
} else {
HandColor = CLOCK_FG;
}
// Draw hour hand
getCoord( CLOCK_XY, CLOCK_XY, &xp, &yp, H_HAND_LENGTH, h_angle);
face.drawWideLine( CLOCK_XY, CLOCK_XY, xp, yp, 6.0f, CLOCK_FG);
face.drawWideLine( CLOCK_XY, CLOCK_XY, xp, yp, 6.0f, HandColor);
//face.drawWideLine( CLOCK_XY, CLOCK_XY, xp, yp, 2.0f, CLOCK_BG);
// Draw the central pivot circle