TRC
Dependents: RTC_terminal HorlogeSimple
RTC_IUT.cpp@1:b85409cf9f7b, 2020-05-30 (annotated)
- Committer:
- gr91
- Date:
- Sat May 30 16:09:00 2020 +0000
- Revision:
- 1:b85409cf9f7b
- Parent:
- 0:f17b3622fae1
v2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gr91 | 0:f17b3622fae1 | 1 | /// @file TimeUtilities.cpp contains a real time clock interface for the mbed |
gr91 | 0:f17b3622fae1 | 2 | /// |
gr91 | 0:f17b3622fae1 | 3 | /// |
gr91 | 0:f17b3622fae1 | 4 | |
gr91 | 0:f17b3622fae1 | 5 | #include "mbed.h" |
gr91 | 0:f17b3622fae1 | 6 | |
gr91 | 0:f17b3622fae1 | 7 | |
gr91 | 0:f17b3622fae1 | 8 | #define VERSION "1.04" |
gr91 | 0:f17b3622fae1 | 9 | |
gr91 | 0:f17b3622fae1 | 10 | #include "RTC_IUT.h" |
gr91 | 0:f17b3622fae1 | 11 | #include <time.h> |
gr91 | 0:f17b3622fae1 | 12 | #include <string.h> |
gr91 | 0:f17b3622fae1 | 13 | #include <stdio.h> |
gr91 | 0:f17b3622fae1 | 14 | #include <stdlib.h> |
gr91 | 0:f17b3622fae1 | 15 | |
gr91 | 0:f17b3622fae1 | 16 | Rtc::Rtc() |
gr91 | 0:f17b3622fae1 | 17 | { |
gr91 | 0:f17b3622fae1 | 18 | } |
gr91 | 0:f17b3622fae1 | 19 | |
gr91 | 0:f17b3622fae1 | 20 | Rtc::~Rtc() |
gr91 | 0:f17b3622fae1 | 21 | { |
gr91 | 0:f17b3622fae1 | 22 | } |
gr91 | 0:f17b3622fae1 | 23 | |
gr91 | 0:f17b3622fae1 | 24 | time_t Rtc::GetTimeValue() |
gr91 | 0:f17b3622fae1 | 25 | { |
gr91 | 0:f17b3622fae1 | 26 | return time(NULL); |
gr91 | 0:f17b3622fae1 | 27 | } |
gr91 | 0:f17b3622fae1 | 28 | |
gr91 | 0:f17b3622fae1 | 29 | void Rtc::SetTimeValue(time_t t) |
gr91 | 0:f17b3622fae1 | 30 | { |
gr91 | 0:f17b3622fae1 | 31 | set_time(t); |
gr91 | 0:f17b3622fae1 | 32 | } |
gr91 | 0:f17b3622fae1 | 33 | |
gr91 | 0:f17b3622fae1 | 34 | void Rtc::GetTimeString(char *buf) |
gr91 | 0:f17b3622fae1 | 35 | { |
gr91 | 0:f17b3622fae1 | 36 | time_t seconds = time(NULL); |
gr91 | 0:f17b3622fae1 | 37 | strftime(buf, 32, "%d/%m/%Y %H:%M:%S", localtime(&seconds)); |
gr91 | 0:f17b3622fae1 | 38 | } |
gr91 | 0:f17b3622fae1 | 39 | |
gr91 | 0:f17b3622fae1 | 40 | void Rtc::SetYear(unsigned short yyyy) |
gr91 | 0:f17b3622fae1 | 41 | { |
gr91 | 0:f17b3622fae1 | 42 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 43 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 44 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 45 | date->tm_year=yyyy-1900; |
gr91 | 0:f17b3622fae1 | 46 | epoch=mktime(date); |
gr91 | 0:f17b3622fae1 | 47 | //printf("epoch = %ud \n",epoch); |
gr91 | 0:f17b3622fae1 | 48 | set_time(epoch); |
gr91 | 0:f17b3622fae1 | 49 | } |
gr91 | 0:f17b3622fae1 | 50 | |
gr91 | 0:f17b3622fae1 | 51 | unsigned short Rtc::GetYear(void) |
gr91 | 0:f17b3622fae1 | 52 | { |
gr91 | 0:f17b3622fae1 | 53 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 54 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 55 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 56 | return date->tm_year+1900; |
gr91 | 0:f17b3622fae1 | 57 | } |
gr91 | 0:f17b3622fae1 | 58 | |
gr91 | 0:f17b3622fae1 | 59 | void Rtc::SetMon(unsigned char y) |
gr91 | 0:f17b3622fae1 | 60 | { |
gr91 | 0:f17b3622fae1 | 61 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 62 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 63 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 64 | date->tm_mon=y-1; |
gr91 | 0:f17b3622fae1 | 65 | epoch=mktime(date); |
gr91 | 0:f17b3622fae1 | 66 | //printf("epoch = %ud \n",epoch); |
gr91 | 0:f17b3622fae1 | 67 | set_time(epoch); |
gr91 | 0:f17b3622fae1 | 68 | } |
gr91 | 0:f17b3622fae1 | 69 | |
gr91 | 0:f17b3622fae1 | 70 | unsigned char Rtc::GetMon(void) |
gr91 | 0:f17b3622fae1 | 71 | { |
gr91 | 0:f17b3622fae1 | 72 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 73 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 74 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 75 | return date->tm_mon+1; |
gr91 | 0:f17b3622fae1 | 76 | } |
gr91 | 0:f17b3622fae1 | 77 | |
gr91 | 0:f17b3622fae1 | 78 | void Rtc::SetDay(unsigned char y) |
gr91 | 0:f17b3622fae1 | 79 | { |
gr91 | 0:f17b3622fae1 | 80 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 81 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 82 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 83 | date->tm_mday=y; |
gr91 | 0:f17b3622fae1 | 84 | epoch=mktime(date); |
gr91 | 0:f17b3622fae1 | 85 | //printf("epoch = %ud \n",epoch); |
gr91 | 0:f17b3622fae1 | 86 | set_time(epoch); |
gr91 | 0:f17b3622fae1 | 87 | } |
gr91 | 0:f17b3622fae1 | 88 | |
gr91 | 0:f17b3622fae1 | 89 | unsigned char Rtc::GetDay(void) |
gr91 | 0:f17b3622fae1 | 90 | { |
gr91 | 0:f17b3622fae1 | 91 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 92 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 93 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 94 | return date->tm_mday; |
gr91 | 0:f17b3622fae1 | 95 | } |
gr91 | 0:f17b3622fae1 | 96 | |
gr91 | 0:f17b3622fae1 | 97 | void Rtc::SetHour(unsigned char y) |
gr91 | 0:f17b3622fae1 | 98 | { |
gr91 | 0:f17b3622fae1 | 99 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 100 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 101 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 102 | date->tm_hour=y; |
gr91 | 0:f17b3622fae1 | 103 | epoch=mktime(date); |
gr91 | 0:f17b3622fae1 | 104 | //printf("epoch = %ud \n",epoch); |
gr91 | 0:f17b3622fae1 | 105 | set_time(epoch); |
gr91 | 0:f17b3622fae1 | 106 | } |
gr91 | 0:f17b3622fae1 | 107 | |
gr91 | 0:f17b3622fae1 | 108 | unsigned char Rtc::GetHour(void) |
gr91 | 0:f17b3622fae1 | 109 | { |
gr91 | 0:f17b3622fae1 | 110 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 111 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 112 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 113 | return date->tm_hour; |
gr91 | 0:f17b3622fae1 | 114 | } |
gr91 | 0:f17b3622fae1 | 115 | |
gr91 | 0:f17b3622fae1 | 116 | void Rtc::SetMin(unsigned char y) |
gr91 | 0:f17b3622fae1 | 117 | { |
gr91 | 0:f17b3622fae1 | 118 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 119 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 120 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 121 | date->tm_min=y; |
gr91 | 0:f17b3622fae1 | 122 | epoch=mktime(date); |
gr91 | 0:f17b3622fae1 | 123 | //printf("epoch = %ud \n",epoch); |
gr91 | 0:f17b3622fae1 | 124 | set_time(epoch); |
gr91 | 0:f17b3622fae1 | 125 | } |
gr91 | 0:f17b3622fae1 | 126 | |
gr91 | 0:f17b3622fae1 | 127 | |
gr91 | 0:f17b3622fae1 | 128 | unsigned char Rtc::GetMin(void) |
gr91 | 0:f17b3622fae1 | 129 | { |
gr91 | 0:f17b3622fae1 | 130 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 131 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 132 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 133 | return date->tm_min; |
gr91 | 0:f17b3622fae1 | 134 | } |
gr91 | 0:f17b3622fae1 | 135 | |
gr91 | 0:f17b3622fae1 | 136 | void Rtc::SetSec(unsigned char y) |
gr91 | 0:f17b3622fae1 | 137 | { |
gr91 | 0:f17b3622fae1 | 138 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 139 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 140 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 141 | date->tm_sec=y; |
gr91 | 0:f17b3622fae1 | 142 | epoch=mktime(date); |
gr91 | 0:f17b3622fae1 | 143 | //printf("epoch = %ud \n",epoch); |
gr91 | 0:f17b3622fae1 | 144 | set_time(epoch); |
gr91 | 0:f17b3622fae1 | 145 | } |
gr91 | 0:f17b3622fae1 | 146 | |
gr91 | 0:f17b3622fae1 | 147 | unsigned char Rtc::GetSec(void) |
gr91 | 0:f17b3622fae1 | 148 | { |
gr91 | 0:f17b3622fae1 | 149 | struct tm *date; |
gr91 | 0:f17b3622fae1 | 150 | time_t epoch=time(NULL); |
gr91 | 0:f17b3622fae1 | 151 | date=localtime(&epoch); |
gr91 | 0:f17b3622fae1 | 152 | return date->tm_sec; |
gr91 | 0:f17b3622fae1 | 153 | } |
gr91 | 0:f17b3622fae1 | 154 | |
gr91 | 0:f17b3622fae1 | 155 | // DD/MM/YYYY HH:MM:SS |
gr91 | 0:f17b3622fae1 | 156 | bool Rtc::SetTimeString(char * timestring) |
gr91 | 0:f17b3622fae1 | 157 | { |
gr91 | 0:f17b3622fae1 | 158 | bool success = false; |
gr91 | 0:f17b3622fae1 | 159 | time_t seconds = time(NULL); |
gr91 | 0:f17b3622fae1 | 160 | struct tm *t = localtime(&seconds); |
gr91 | 1:b85409cf9f7b | 161 | unsigned int dd,MM,yyyy,hh,mm,ss; |
gr91 | 1:b85409cf9f7b | 162 | //printf("str : %s\r\n",timestring); |
gr91 | 1:b85409cf9f7b | 163 | success=sscanf(timestring,"%d/%d/%d %d:%d:%d",&dd,&MM,&yyyy,&hh,&mm,&ss); |
gr91 | 1:b85409cf9f7b | 164 | if(success) { |
gr91 | 1:b85409cf9f7b | 165 | //printf("dd=%d\r\n",dd); |
gr91 | 1:b85409cf9f7b | 166 | t->tm_mday = dd ; |
gr91 | 1:b85409cf9f7b | 167 | t->tm_mon = MM-1; |
gr91 | 1:b85409cf9f7b | 168 | t->tm_year = yyyy - 1900; |
gr91 | 1:b85409cf9f7b | 169 | t->tm_hour = hh; |
gr91 | 1:b85409cf9f7b | 170 | t->tm_min = mm; |
gr91 | 1:b85409cf9f7b | 171 | t->tm_sec = ss; |
gr91 | 0:f17b3622fae1 | 172 | } |
gr91 | 1:b85409cf9f7b | 173 | seconds = mktime(t); |
gr91 | 1:b85409cf9f7b | 174 | set_time(seconds); |
gr91 | 0:f17b3622fae1 | 175 | return success; |
gr91 | 0:f17b3622fae1 | 176 | } |