From 8d82e659fd8756ff3ace392aaf3033f6bfbd8485 Mon Sep 17 00:00:00 2001 From: Maik Fischer Date: Thu, 2 Nov 2017 11:44:01 +0100 Subject: [PATCH] track day of event and increment it when clock wraps --- include/c128time.h | 10 +++++++++- include/globals.h | 2 ++ src/c128time.c | 30 +++++++++++++++++++++++------- src/globals.c | 2 ++ src/kasse.c | 21 ++++++++++++--------- 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/include/c128time.h b/include/c128time.h index af76f84..e962912 100644 --- a/include/c128time.h +++ b/include/c128time.h @@ -2,7 +2,15 @@ #define TIME_H #include -void set_time(uint8_t hrs, uint8_t min, uint8_t sec); +void set_time(uint8_t day, uint8_t hrs, uint8_t min, uint8_t sec); char *get_time(void); +void update_time(void); + +struct daytime_t { + uint8_t day; + uint8_t hour; + uint8_t min; + uint8_t sec; +}; #endif /* TIME_H */ diff --git a/include/globals.h b/include/globals.h index 8a64063..9f98e11 100644 --- a/include/globals.h +++ b/include/globals.h @@ -2,6 +2,7 @@ #define GLOBALS_H #include +#include "c128time.h" #ifdef IS_GLOBALS_C #define GLOBAL @@ -12,5 +13,6 @@ void init_globals(void); GLOBAL uint8_t printing; +GLOBAL struct daytime_t daytime; #endif /* GLOBALS_H */ diff --git a/src/c128time.c b/src/c128time.c index e268133..f4b1e4a 100644 --- a/src/c128time.c +++ b/src/c128time.c @@ -14,9 +14,8 @@ #include "general.h" #include "globals.h" -char *get_time(void) { - static char buffer[9]; - uint8_t bcd_hour, hour, bcd_min, bcd_sec, tenth; +void update_time(void) { + uint8_t bcd_hour, hour, min, sec, tenth; /* Read the hour register first to stop the clock from updating the external * registers from the internal (still ticking!) CIA registers. */ @@ -34,20 +33,32 @@ char *get_time(void) { hour = bcd2dec(bcd_hour); } - bcd_sec = CIA1.tod_sec; - bcd_min = CIA1.tod_min; + sec = bcd2dec(CIA1.tod_sec); + min = bcd2dec(CIA1.tod_min); /* MUST read tod_10 to enable the clock latch again */ tenth = CIA1.tod_10; - sprintf(buffer, "%02d:%02x:%02x", hour, bcd_min, bcd_sec); + if (daytime.hour > hour) { + daytime.day++; + } + + daytime.hour = hour; + daytime.min = min; + daytime.sec = sec; +} + +char *get_time(void) { + static char buffer[9]; + update_time(); + sprintf(buffer, "%02d:%02d:%02d", daytime.hour, daytime.min, daytime.sec); return buffer; } /* divide by 10; put quotient in high nibble, reminder in low nibble */ uint8_t dec2bcd(uint8_t dec) { return (((dec / 10) << 4) | (dec % 10)); } -void set_time(uint8_t hour, uint8_t min, uint8_t sec) { +void set_time(uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) { uint8_t bcd_hour; /* CIA TOD will always flip the pm bit @@ -64,6 +75,11 @@ void set_time(uint8_t hour, uint8_t min, uint8_t sec) { bcd_hour = dec2bcd(hour); } + daytime.day = day; + daytime.hour = hour; + daytime.min = min; + daytime.sec = sec; + CIA1.tod_hour = bcd_hour; CIA1.tod_min = dec2bcd(min); CIA1.tod_sec = dec2bcd(sec); diff --git a/src/globals.c b/src/globals.c index c1032e2..51533ea 100644 --- a/src/globals.c +++ b/src/globals.c @@ -3,5 +3,7 @@ void init_globals(void) { printing = 1; + /* initialize daytime global, start the CIA TOD */ + set_time(0, 0, 0, 0); return; } diff --git a/src/kasse.c b/src/kasse.c index f063a97..41a7a7d 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -269,21 +269,24 @@ void buy_custom(void) { } void set_time_interactive(void) { - BYTE part[3] = {'0', '0', '\0'}; - BYTE tp1, tp2, tp3; + char part[3] = {'\0', '\0', '\0'}; + uint8_t day, tp1, tp2, tp3; char *time_input, *time; - cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n"); + cprintf("Gib den aktuellen Tag des Events und Uhrzeit ein\r\n" + "(Format DHHMMSS):\r\n"); time_input = get_input(); part[0] = time_input[0]; - part[1] = time_input[1]; + day = atoi(part); + part[0] = time_input[1]; + part[1] = time_input[2]; tp1 = atoi(part); - part[0] = time_input[2]; - part[1] = time_input[3]; + part[0] = time_input[3]; + part[1] = time_input[4]; tp2 = atoi(part); - part[0] = time_input[4]; - part[1] = time_input[5]; + part[0] = time_input[5]; + part[1] = time_input[6]; tp3 = atoi(part); - set_time(tp1, tp2, tp3); + set_time(day, tp1, tp2, tp3); time = get_time(); cprintf("\r\nZeit gesetzt: %s\r\n", time); -- 2.39.5