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:
Fri Jul 05 01:26:19 2019 +0000
Revision:
19:36072b9b79f3
Parent:
16:602bc04e3cb5
add LICENSE.txt

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 12:a45a9c65dc03 16 current.tm_mday = 1;
Inscape_ao 12:a45a9c65dc03 17 current.tm_mon = 1;
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 4:bec3f80dc49c 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 16:602bc04e3cb5 71 dbgprintf("setCurrentTime:%d-%d(%d)\n", selector, set, daysInMonth);
Inscape_ao 2:a694440145e9 72 switch (selector) {
Inscape_ao 2:a694440145e9 73 case SetTimeMethod::Year:
Inscape_ao 2:a694440145e9 74 if (set >= 0) {
Inscape_ao 2:a694440145e9 75 current.tm_year = set;
Inscape_ao 2:a694440145e9 76 finish = true;
Inscape_ao 2:a694440145e9 77 }
Inscape_ao 2:a694440145e9 78 break;
Inscape_ao 2:a694440145e9 79 case SetTimeMethod::Month:
Inscape_ao 2:a694440145e9 80 if (set > 0 && set <= 12) {
Inscape_ao 2:a694440145e9 81 current.tm_mon = set;
Inscape_ao 2:a694440145e9 82 finish = true;
Inscape_ao 2:a694440145e9 83 }
Inscape_ao 2:a694440145e9 84 break;
Inscape_ao 2:a694440145e9 85 case SetTimeMethod::Day:
Inscape_ao 2:a694440145e9 86 if (set > 0 && set <= daysInMonth) {
Inscape_ao 2:a694440145e9 87 current.tm_mday = set;
Inscape_ao 2:a694440145e9 88 finish = true;
Inscape_ao 2:a694440145e9 89 }
Inscape_ao 2:a694440145e9 90 break;
Inscape_ao 2:a694440145e9 91 case SetTimeMethod::Hour:
Inscape_ao 2:a694440145e9 92 if (set >= 0 && set < 24) {
Inscape_ao 2:a694440145e9 93 current.tm_hour = set;
Inscape_ao 2:a694440145e9 94 finish = true;
Inscape_ao 2:a694440145e9 95 }
Inscape_ao 2:a694440145e9 96 break;
Inscape_ao 2:a694440145e9 97 case SetTimeMethod::Min:
Inscape_ao 2:a694440145e9 98 if (set >= 0 && set < 60) {
Inscape_ao 2:a694440145e9 99 current.tm_min = set;
Inscape_ao 2:a694440145e9 100 finish = true;
Inscape_ao 2:a694440145e9 101 }
Inscape_ao 2:a694440145e9 102 break;
Inscape_ao 2:a694440145e9 103 case SetTimeMethod::Sec:
Inscape_ao 2:a694440145e9 104 if (set >= 0 && set < 60) {
Inscape_ao 2:a694440145e9 105 current.tm_sec = set;
Inscape_ao 2:a694440145e9 106 finish = true;
Inscape_ao 2:a694440145e9 107 }
Inscape_ao 2:a694440145e9 108 break;
Inscape_ao 2:a694440145e9 109 };
Inscape_ao 2:a694440145e9 110 return finish;
Inscape_ao 2:a694440145e9 111 }
Inscape_ao 2:a694440145e9 112
Inscape_ao 2:a694440145e9 113 /* calcurate Number of Days in month */
Inscape_ao 2:a694440145e9 114 int TimeManager::getDaysInMonth(int month, int year)
Inscape_ao 2:a694440145e9 115 {
Inscape_ao 2:a694440145e9 116 int daysInMonth;
Inscape_ao 2:a694440145e9 117 bool leap;
Inscape_ao 2:a694440145e9 118 year += 1900;
Inscape_ao 2:a694440145e9 119 leap = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))? true : false;
Inscape_ao 2:a694440145e9 120
Inscape_ao 2:a694440145e9 121 if (((month % 2 == 1) && (month <= 7)) ||
Inscape_ao 2:a694440145e9 122 ((month % 2 == 0) && (month >= 8))) {
Inscape_ao 2:a694440145e9 123 /* 31days/month */
Inscape_ao 2:a694440145e9 124 daysInMonth = 31;
Inscape_ao 2:a694440145e9 125 } else if (month == 2) {
Inscape_ao 2:a694440145e9 126 /* 28days OR 29days/month */
Inscape_ao 2:a694440145e9 127 if (leap) {
Inscape_ao 2:a694440145e9 128 daysInMonth = 29;
Inscape_ao 2:a694440145e9 129 } else {
Inscape_ao 2:a694440145e9 130 daysInMonth = 28;
Inscape_ao 2:a694440145e9 131 }
Inscape_ao 2:a694440145e9 132 } else {
Inscape_ao 2:a694440145e9 133 /* 30days/month */
Inscape_ao 2:a694440145e9 134 daysInMonth = 30;
Inscape_ao 2:a694440145e9 135 }
Inscape_ao 2:a694440145e9 136 return daysInMonth;
Inscape_ao 2:a694440145e9 137 }