UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Tue Apr 23 08:47:11 2019 +0000
Revision:
2:a694440145e9
Child:
4:bec3f80dc49c
add TimeManager

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 2:a694440145e9 1 #include "TimeManager.h"
Inscape_ao 2:a694440145e9 2 #include "global.h"
Inscape_ao 2:a694440145e9 3
Inscape_ao 2:a694440145e9 4
Inscape_ao 2:a694440145e9 5 /* Cyclic handler (for tick) */
Inscape_ao 2:a694440145e9 6 void flip() {
Inscape_ao 2:a694440145e9 7 pTM->tick();
Inscape_ao 2:a694440145e9 8 }
Inscape_ao 2:a694440145e9 9
Inscape_ao 2:a694440145e9 10 /* Constractor */
Inscape_ao 2:a694440145e9 11 TimeManager::TimeManager(void)
Inscape_ao 2:a694440145e9 12 {
Inscape_ao 2:a694440145e9 13 current.tm_sec = 0;
Inscape_ao 2:a694440145e9 14 current.tm_min = 0;
Inscape_ao 2:a694440145e9 15 current.tm_hour = 0;
Inscape_ao 2:a694440145e9 16 current.tm_mday = 0;
Inscape_ao 2:a694440145e9 17 current.tm_mon = 0;
Inscape_ao 2:a694440145e9 18 current.tm_year = 0;
Inscape_ao 2:a694440145e9 19 baseclock.attach(flip, 1.0);
Inscape_ao 2:a694440145e9 20 }
Inscape_ao 2:a694440145e9 21
Inscape_ao 2:a694440145e9 22 /* tick (currenttime++) */
Inscape_ao 2:a694440145e9 23 void TimeManager::tick(void)
Inscape_ao 2:a694440145e9 24 {
Inscape_ao 2:a694440145e9 25 int daysInMonth = getDaysInMonth(current.tm_mon, current.tm_year);
Inscape_ao 2:a694440145e9 26 current.tm_sec++;
Inscape_ao 2:a694440145e9 27 if (current.tm_sec >= 60) {
Inscape_ao 2:a694440145e9 28 current.tm_sec = 0;
Inscape_ao 2:a694440145e9 29 current.tm_min++;
Inscape_ao 2:a694440145e9 30 } else { return; }
Inscape_ao 2:a694440145e9 31 if (current.tm_min >= 60) {
Inscape_ao 2:a694440145e9 32 current.tm_min = 0;
Inscape_ao 2:a694440145e9 33 current.tm_hour++;
Inscape_ao 2:a694440145e9 34 } else { return; }
Inscape_ao 2:a694440145e9 35 if (current.tm_hour >= 24) {
Inscape_ao 2:a694440145e9 36 current.tm_hour = 0;
Inscape_ao 2:a694440145e9 37 current.tm_mday++;
Inscape_ao 2:a694440145e9 38 } else { return; }
Inscape_ao 2:a694440145e9 39 if (current.tm_mday > daysInMonth) {
Inscape_ao 2:a694440145e9 40 current.tm_mday = 0;
Inscape_ao 2:a694440145e9 41 current.tm_mon++;
Inscape_ao 2:a694440145e9 42 } else { return; }
Inscape_ao 2:a694440145e9 43 if (current.tm_mon > 12) {
Inscape_ao 2:a694440145e9 44 current.tm_mon = 1;
Inscape_ao 2:a694440145e9 45 current.tm_year++;
Inscape_ao 2:a694440145e9 46 } else { return; }
Inscape_ao 2:a694440145e9 47 }
Inscape_ao 2:a694440145e9 48
Inscape_ao 2:a694440145e9 49 /* copy Struct Time to arg */
Inscape_ao 2:a694440145e9 50 void TimeManager::getCurrentTime(struct tm *ret)
Inscape_ao 2:a694440145e9 51 {
Inscape_ao 2:a694440145e9 52 *ret = current;
Inscape_ao 2:a694440145e9 53 }
Inscape_ao 2:a694440145e9 54
Inscape_ao 2:a694440145e9 55 /* get string of timestamp */
Inscape_ao 2:a694440145e9 56 int TimeManager::getTimeStamp(char *dst)
Inscape_ao 2:a694440145e9 57 {
Inscape_ao 2:a694440145e9 58 char dummy[TimeStampLength + 1];
Inscape_ao 2:a694440145e9 59 sprintf(dummy, "%04d-%02d-%02d_%02d:%02d:%02d",
Inscape_ao 2:a694440145e9 60 current.tm_year + 1900, current.tm_mon, current.tm_mday,
Inscape_ao 2:a694440145e9 61 current.tm_hour, current.tm_min, current.tm_sec);
Inscape_ao 2:a694440145e9 62 memcpy(dst, dummy, TimeStampLength);
Inscape_ao 2:a694440145e9 63 return TimeStampLength;
Inscape_ao 2:a694440145e9 64 }
Inscape_ao 2:a694440145e9 65
Inscape_ao 2:a694440145e9 66 /* set time parameters */
Inscape_ao 2:a694440145e9 67 bool TimeManager::setCurrentTime(int selector, int set)
Inscape_ao 2:a694440145e9 68 {
Inscape_ao 2:a694440145e9 69 bool finish = false;
Inscape_ao 2:a694440145e9 70 int daysInMonth = getDaysInMonth(current.tm_mon, current.tm_year);
Inscape_ao 2:a694440145e9 71 switch (selector) {
Inscape_ao 2:a694440145e9 72 case SetTimeMethod::Year:
Inscape_ao 2:a694440145e9 73 if (set >= 0) {
Inscape_ao 2:a694440145e9 74 current.tm_year = set;
Inscape_ao 2:a694440145e9 75 finish = true;
Inscape_ao 2:a694440145e9 76 }
Inscape_ao 2:a694440145e9 77 break;
Inscape_ao 2:a694440145e9 78 case SetTimeMethod::Month:
Inscape_ao 2:a694440145e9 79 if (set > 0 && set <= 12) {
Inscape_ao 2:a694440145e9 80 current.tm_mon = set;
Inscape_ao 2:a694440145e9 81 finish = true;
Inscape_ao 2:a694440145e9 82 }
Inscape_ao 2:a694440145e9 83 break;
Inscape_ao 2:a694440145e9 84 case SetTimeMethod::Day:
Inscape_ao 2:a694440145e9 85 if (set > 0 && set <= daysInMonth) {
Inscape_ao 2:a694440145e9 86 current.tm_mday = set;
Inscape_ao 2:a694440145e9 87 finish = true;
Inscape_ao 2:a694440145e9 88 }
Inscape_ao 2:a694440145e9 89 break;
Inscape_ao 2:a694440145e9 90 case SetTimeMethod::Hour:
Inscape_ao 2:a694440145e9 91 if (set >= 0 && set < 24) {
Inscape_ao 2:a694440145e9 92 current.tm_hour = set;
Inscape_ao 2:a694440145e9 93 finish = true;
Inscape_ao 2:a694440145e9 94 }
Inscape_ao 2:a694440145e9 95 break;
Inscape_ao 2:a694440145e9 96 case SetTimeMethod::Min:
Inscape_ao 2:a694440145e9 97 if (set >= 0 && set < 60) {
Inscape_ao 2:a694440145e9 98 current.tm_min = set;
Inscape_ao 2:a694440145e9 99 finish = true;
Inscape_ao 2:a694440145e9 100 }
Inscape_ao 2:a694440145e9 101 break;
Inscape_ao 2:a694440145e9 102 case SetTimeMethod::Sec:
Inscape_ao 2:a694440145e9 103 if (set >= 0 && set < 60) {
Inscape_ao 2:a694440145e9 104 current.tm_sec = set;
Inscape_ao 2:a694440145e9 105 finish = true;
Inscape_ao 2:a694440145e9 106 }
Inscape_ao 2:a694440145e9 107 break;
Inscape_ao 2:a694440145e9 108 };
Inscape_ao 2:a694440145e9 109 return finish;
Inscape_ao 2:a694440145e9 110 }
Inscape_ao 2:a694440145e9 111
Inscape_ao 2:a694440145e9 112 /* calcurate Number of Days in month */
Inscape_ao 2:a694440145e9 113 int TimeManager::getDaysInMonth(int month, int year)
Inscape_ao 2:a694440145e9 114 {
Inscape_ao 2:a694440145e9 115 int daysInMonth;
Inscape_ao 2:a694440145e9 116 bool leap;
Inscape_ao 2:a694440145e9 117 year += 1900;
Inscape_ao 2:a694440145e9 118 leap = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))? true : false;
Inscape_ao 2:a694440145e9 119
Inscape_ao 2:a694440145e9 120 if (((month % 2 == 1) && (month <= 7)) ||
Inscape_ao 2:a694440145e9 121 ((month % 2 == 0) && (month >= 8))) {
Inscape_ao 2:a694440145e9 122 /* 31days/month */
Inscape_ao 2:a694440145e9 123 daysInMonth = 31;
Inscape_ao 2:a694440145e9 124 } else if (month == 2) {
Inscape_ao 2:a694440145e9 125 /* 28days OR 29days/month */
Inscape_ao 2:a694440145e9 126 if (leap) {
Inscape_ao 2:a694440145e9 127 daysInMonth = 29;
Inscape_ao 2:a694440145e9 128 } else {
Inscape_ao 2:a694440145e9 129 daysInMonth = 28;
Inscape_ao 2:a694440145e9 130 }
Inscape_ao 2:a694440145e9 131 } else {
Inscape_ao 2:a694440145e9 132 /* 30days/month */
Inscape_ao 2:a694440145e9 133 daysInMonth = 30;
Inscape_ao 2:a694440145e9 134 }
Inscape_ao 2:a694440145e9 135 return daysInMonth;
Inscape_ao 2:a694440145e9 136 }