From 2b61ac1baa7fbd1940c12f8e9c0d6778b56bafd9 Mon Sep 17 00:00:00 2001 From: Brent Bettis Date: Tue, 24 Oct 2023 17:15:39 -0700 Subject: [PATCH] test programs for RTC functions --- CMakeLists.txt | 27 +++++++++++++++ src/clock.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ src/print-time.c | 23 +++++++++++++ src/set-time.c | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 src/clock.c create mode 100644 src/print-time.c create mode 100644 src/set-time.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5819829..3d7d8f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,3 +48,30 @@ target_sources(paint PRIVATE target_link_libraries(paint PRIVATE rp6502 ) + +add_executable(clock) +rp6502_executable(clock) +target_sources(clock PRIVATE + src/clock.c +) +target_link_libraries(clock PRIVATE + rp6502 +) + +add_executable(set-time) +rp6502_executable(set-time) +target_sources(set-time PRIVATE + src/set-time.c +) +target_link_libraries(set-time PRIVATE + rp6502 +) + +add_executable(print-time) +rp6502_executable(print-time) +target_sources(print-time PRIVATE + src/print-time.c +) +target_link_libraries(print-time PRIVATE + rp6502 +) diff --git a/src/clock.c b/src/clock.c new file mode 100644 index 0000000..70e4ee9 --- /dev/null +++ b/src/clock.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +void test_time() +{ + time_t now; + unsigned long res; + res = time(&now); + if (res == -1) + { + printf("errno: %d\n", errno); + printf("_oserror: %d\n", _oserror); + } else + { + printf("%s", ctime(&now)); + + } +} + +void test_clock_getres() +{ + struct timespec resolution; + unsigned long res; + res = clock_getres(CLOCK_REALTIME, &resolution); + if (res == -1) + { + printf("errno: %d\n", errno); + printf("_oserror: %d\n", _oserror); + } else + { + printf("Clock resolution: seconds: %lu, nanoseconds: %lu\n", resolution.tv_sec, resolution.tv_nsec); + } +} + +void test_clock_gettime() +{ + struct timespec now; + unsigned long res; + struct tm time_struct; + char buf[80]; + res = clock_gettime(CLOCK_REALTIME, &now); + if (res == -1) + { + printf("errno: %d\n", errno); + printf("_oserror: %d\n", _oserror); + } else + { + time_struct = *localtime(&(now.tv_sec)); + strftime(buf, sizeof(buf), "%a %m/%d/%Y %I:%M:%S %p", &time_struct); + printf("%s\n", buf); + } +} + +void test_clock_settime() +{ + struct tm current_time; + struct timespec now; + current_time.tm_hour = 5; + current_time.tm_sec = 5; + current_time.tm_min = 5; + current_time.tm_mday = 29; + current_time.tm_mon = 10; + current_time.tm_year = 123; + current_time.tm_isdst = -1; + current_time.tm_wday = 0; + current_time.tm_yday = 0; + now.tv_sec = mktime(¤t_time); + now.tv_nsec = 0; + clock_settime(CLOCK_REALTIME, &now); +} + +void main() +{ + printf("test clock_gettime...\n"); + test_clock_gettime(); + printf("test clock_settime...\n"); + test_clock_settime(); + printf("test clock_getres...\n"); + test_clock_getres(); + printf("test clock_gettime...\n"); + test_clock_gettime(); + printf("test time...\n"); + test_time(); +} diff --git a/src/print-time.c b/src/print-time.c new file mode 100644 index 0000000..3176a40 --- /dev/null +++ b/src/print-time.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +void main() +{ + time_t now; + unsigned long res; + struct tm time_struct; + char buf[80]; + res = time(&now); + if (res == -1) + { + printf("errno: %d\n", errno); + printf("_oserror: %d\n", _oserror); + } else + { + time_struct = *localtime(&now); + strftime(buf, sizeof(buf), "%a %m/%d/%Y %I:%M:%S %p", &time_struct); + printf("%s\n", buf); + } +} diff --git a/src/set-time.c b/src/set-time.c new file mode 100644 index 0000000..3cbecdd --- /dev/null +++ b/src/set-time.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#define IN_BUF_SIZE 8 + +void main() +{ + struct tm current_time; + struct timespec now; + char in_buf[IN_BUF_SIZE]; + + current_time.tm_hour = -1; + current_time.tm_sec = -1; + current_time.tm_min = -1; + current_time.tm_mday = 0; + current_time.tm_mon = -1; + current_time.tm_year = -1; + current_time.tm_isdst = -1; + current_time.tm_wday = 0; + current_time.tm_yday = 0; + + + while (current_time.tm_mon < 0) { + puts("Enter month (1-12): "); + gets(in_buf); + current_time.tm_mon = atoi(in_buf) - 1; + if (current_time.tm_mon > 11) + current_time.tm_mon = -1; + } + while (!current_time.tm_mday) { + puts("Enter day (1-31): "); + gets(in_buf); + current_time.tm_mday = atoi(in_buf); + if (current_time.tm_mday > 31) + current_time.tm_mday = 0; + } + while (current_time.tm_year < 0) { + puts("Enter year: (1970-2038)"); + gets(in_buf); + current_time.tm_year = atoi(in_buf) - 1900; + if (current_time.tm_year > 138 || + current_time.tm_year < 70) + current_time.tm_year = -1; + } + puts("Local time:\n"); + while (current_time.tm_hour < 0) { + puts("Enter hour (0-23): "); + gets(in_buf); + current_time.tm_hour = atoi(in_buf); + if (current_time.tm_hour > 23) + current_time.tm_hour = -1; + } + while (current_time.tm_min < 0) { + puts("Enter minute (0-59): "); + gets(in_buf); + current_time.tm_min = atoi(in_buf); + if (current_time.tm_min > 59) + current_time.tm_min = -1; + } + while (current_time.tm_sec < 0) { + puts("Enter second (0-59): "); + gets(in_buf); + current_time.tm_sec = atoi(in_buf); + if (current_time.tm_sec > 59) + current_time.tm_sec = -1; + } + now.tv_sec = mktime(¤t_time); + now.tv_nsec = 0; + clock_settime(CLOCK_REALTIME, &now); +}