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