Access Control Using RFID MIFARE CARD
Dependencies: MFRC522 NextionLCD
Time.cpp@0:a5d3db2f2625, 2018-08-21 (annotated)
- Committer:
- shivanandgowdakr
- Date:
- Tue Aug 21 11:18:56 2018 +0000
- Revision:
- 0:a5d3db2f2625
Access Control Using MIFARE RFID Card, and with details displayed on Nextion LCD;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shivanandgowdakr | 0:a5d3db2f2625 | 1 | #include "mbed.h" |
shivanandgowdakr | 0:a5d3db2f2625 | 2 | #include "Time.h" |
shivanandgowdakr | 0:a5d3db2f2625 | 3 | #include <string> |
shivanandgowdakr | 0:a5d3db2f2625 | 4 | |
shivanandgowdakr | 0:a5d3db2f2625 | 5 | |
shivanandgowdakr | 0:a5d3db2f2625 | 6 | |
shivanandgowdakr | 0:a5d3db2f2625 | 7 | |
shivanandgowdakr | 0:a5d3db2f2625 | 8 | |
shivanandgowdakr | 0:a5d3db2f2625 | 9 | int Date_Time_Setting(struct tm curt,char *str) |
shivanandgowdakr | 0:a5d3db2f2625 | 10 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 11 | int len=0; |
shivanandgowdakr | 0:a5d3db2f2625 | 12 | len= strlen(str); |
shivanandgowdakr | 0:a5d3db2f2625 | 13 | // printf("len = %d",len); |
shivanandgowdakr | 0:a5d3db2f2625 | 14 | if(len < 14) |
shivanandgowdakr | 0:a5d3db2f2625 | 15 | return 0; |
shivanandgowdakr | 0:a5d3db2f2625 | 16 | |
shivanandgowdakr | 0:a5d3db2f2625 | 17 | curt.tm_mday = ( str[0]-0x30)*10+(str[1]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 18 | curt.tm_mon = ( str[2]-0x30)*10+(str[3]-0x30) -1; |
shivanandgowdakr | 0:a5d3db2f2625 | 19 | curt.tm_year = (( str[4]-0x30)*1000 + (str[5]-0x30)*100 + (str[6]-0x30)*10 + (str[7]-0x30)) -1900 ; |
shivanandgowdakr | 0:a5d3db2f2625 | 20 | |
shivanandgowdakr | 0:a5d3db2f2625 | 21 | curt.tm_hour=(str[8]-0x30)*10+(str[9]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 22 | curt.tm_min=(str[10]-0x30)*10+(str[11]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 23 | curt.tm_sec=(str[12]-0x30)*10+(str[13]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 24 | |
shivanandgowdakr | 0:a5d3db2f2625 | 25 | time_t epoch = mktime(&curt); |
shivanandgowdakr | 0:a5d3db2f2625 | 26 | if (epoch == (time_t) -1) { |
shivanandgowdakr | 0:a5d3db2f2625 | 27 | error("Error in clock setting\n"); |
shivanandgowdakr | 0:a5d3db2f2625 | 28 | // Stop here |
shivanandgowdakr | 0:a5d3db2f2625 | 29 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 30 | set_time(epoch); |
shivanandgowdakr | 0:a5d3db2f2625 | 31 | return 1; |
shivanandgowdakr | 0:a5d3db2f2625 | 32 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 33 | |
shivanandgowdakr | 0:a5d3db2f2625 | 34 | |
shivanandgowdakr | 0:a5d3db2f2625 | 35 | int iSetTerminalTime(char *tstring) |
shivanandgowdakr | 0:a5d3db2f2625 | 36 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 37 | int ret=0; |
shivanandgowdakr | 0:a5d3db2f2625 | 38 | struct tm curt; |
shivanandgowdakr | 0:a5d3db2f2625 | 39 | |
shivanandgowdakr | 0:a5d3db2f2625 | 40 | ret = Date_Time_Setting(curt,tstring); |
shivanandgowdakr | 0:a5d3db2f2625 | 41 | |
shivanandgowdakr | 0:a5d3db2f2625 | 42 | if(ret == 1) { |
shivanandgowdakr | 0:a5d3db2f2625 | 43 | wait(1); |
shivanandgowdakr | 0:a5d3db2f2625 | 44 | |
shivanandgowdakr | 0:a5d3db2f2625 | 45 | // Display_LCD(0,0," DATE-TIME "); |
shivanandgowdakr | 0:a5d3db2f2625 | 46 | // Display_LCD(0,1," SET SUCCESS "); |
shivanandgowdakr | 0:a5d3db2f2625 | 47 | printf("Date Time Set succesfully\r\n"); |
shivanandgowdakr | 0:a5d3db2f2625 | 48 | |
shivanandgowdakr | 0:a5d3db2f2625 | 49 | } else { |
shivanandgowdakr | 0:a5d3db2f2625 | 50 | // Display_LCD(0,0," DATE-TIME "); |
shivanandgowdakr | 0:a5d3db2f2625 | 51 | // Display_LCD(0,1," SET FAILURE "); |
shivanandgowdakr | 0:a5d3db2f2625 | 52 | printf("Date Time Set Failure \r\n"); |
shivanandgowdakr | 0:a5d3db2f2625 | 53 | |
shivanandgowdakr | 0:a5d3db2f2625 | 54 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 55 | wait(2); |
shivanandgowdakr | 0:a5d3db2f2625 | 56 | // Clear_LCD(); |
shivanandgowdakr | 0:a5d3db2f2625 | 57 | |
shivanandgowdakr | 0:a5d3db2f2625 | 58 | return ret; |
shivanandgowdakr | 0:a5d3db2f2625 | 59 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 60 | |
shivanandgowdakr | 0:a5d3db2f2625 | 61 | |
shivanandgowdakr | 0:a5d3db2f2625 | 62 | int chk_time (char *str) |
shivanandgowdakr | 0:a5d3db2f2625 | 63 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 64 | int HH,MM,SS; |
shivanandgowdakr | 0:a5d3db2f2625 | 65 | HH=(str[0]-0x30)*10+(str[1]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 66 | MM=(str[2]-0x30)*10+(str[3]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 67 | SS=(str[4]-0x30)*10+(str[5]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 68 | if ( HH < 0 || HH > 23 || MM < 0 || MM > 59 || SS < 0 || SS > 59 ) |
shivanandgowdakr | 0:a5d3db2f2625 | 69 | return -1; |
shivanandgowdakr | 0:a5d3db2f2625 | 70 | return 1; |
shivanandgowdakr | 0:a5d3db2f2625 | 71 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 72 | |
shivanandgowdakr | 0:a5d3db2f2625 | 73 | int chk_date (char *str) |
shivanandgowdakr | 0:a5d3db2f2625 | 74 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 75 | int epos_date=0,epos_month=0,epos_year=0; |
shivanandgowdakr | 0:a5d3db2f2625 | 76 | epos_date = ( str[0]-0x30)*10+(str[1]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 77 | epos_month = ( str[2]-0x30)*10+(str[3]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 78 | epos_year = ( str[4]-0x30)*1000+ (str[5]-0x30)*100 + (str[6]-0x30)*10 + (str[7]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 79 | |
shivanandgowdakr | 0:a5d3db2f2625 | 80 | |
shivanandgowdakr | 0:a5d3db2f2625 | 81 | if ( epos_month < 1 || epos_date < 1 || epos_date > 31 || epos_month > 12 || epos_year < 2008 ) return ERROR ; |
shivanandgowdakr | 0:a5d3db2f2625 | 82 | |
shivanandgowdakr | 0:a5d3db2f2625 | 83 | else if(epos_month == 1 || epos_month == 3 || epos_month == 5 || epos_month == 7 || epos_month == 8 || epos_month ==10 ||epos_month == 12) { |
shivanandgowdakr | 0:a5d3db2f2625 | 84 | |
shivanandgowdakr | 0:a5d3db2f2625 | 85 | if (epos_date > 31) |
shivanandgowdakr | 0:a5d3db2f2625 | 86 | return -1; |
shivanandgowdakr | 0:a5d3db2f2625 | 87 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 88 | |
shivanandgowdakr | 0:a5d3db2f2625 | 89 | else if (epos_month == 4 || epos_month == 6 || epos_month == 9 || epos_month == 11) { |
shivanandgowdakr | 0:a5d3db2f2625 | 90 | |
shivanandgowdakr | 0:a5d3db2f2625 | 91 | if (epos_date > 30) |
shivanandgowdakr | 0:a5d3db2f2625 | 92 | return -1; |
shivanandgowdakr | 0:a5d3db2f2625 | 93 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 94 | |
shivanandgowdakr | 0:a5d3db2f2625 | 95 | else if (epos_month == 2 ) |
shivanandgowdakr | 0:a5d3db2f2625 | 96 | |
shivanandgowdakr | 0:a5d3db2f2625 | 97 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 98 | if ( !(epos_year%400) || (epos_year%100 != 0 && epos_year%4==0 ) ) { |
shivanandgowdakr | 0:a5d3db2f2625 | 99 | if (epos_date > 29 ) return -1; |
shivanandgowdakr | 0:a5d3db2f2625 | 100 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 101 | |
shivanandgowdakr | 0:a5d3db2f2625 | 102 | else if( epos_date > 28 ) return -1; |
shivanandgowdakr | 0:a5d3db2f2625 | 103 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 104 | return 1; |
shivanandgowdakr | 0:a5d3db2f2625 | 105 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 106 | |
shivanandgowdakr | 0:a5d3db2f2625 | 107 | void Get_Date_Time(char *date_string,char *time_string, char *DTSTRING) |
shivanandgowdakr | 0:a5d3db2f2625 | 108 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 109 | time_t curr_time; |
shivanandgowdakr | 0:a5d3db2f2625 | 110 | tm * curr_tm; |
shivanandgowdakr | 0:a5d3db2f2625 | 111 | time(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 112 | curr_tm = localtime(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 113 | strftime(date_string,10,"%Y%m%d",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 114 | strftime(time_string,10,"%H%M%S",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 115 | strftime(DTSTRING,20,"%Y%m%d%H%M%S",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 116 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 117 | |
shivanandgowdakr | 0:a5d3db2f2625 | 118 | void Get_Date_Time(char *date_string,char *time_string) |
shivanandgowdakr | 0:a5d3db2f2625 | 119 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 120 | time_t curr_time; |
shivanandgowdakr | 0:a5d3db2f2625 | 121 | tm * curr_tm; |
shivanandgowdakr | 0:a5d3db2f2625 | 122 | time(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 123 | curr_tm = localtime(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 124 | strftime(date_string,10,"%Y%m%d",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 125 | strftime(time_string,10,"%H%M%S",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 126 | |
shivanandgowdakr | 0:a5d3db2f2625 | 127 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 128 | |
shivanandgowdakr | 0:a5d3db2f2625 | 129 | void Get_Date_Time_Display( char *DTSTRING) |
shivanandgowdakr | 0:a5d3db2f2625 | 130 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 131 | time_t curr_time; |
shivanandgowdakr | 0:a5d3db2f2625 | 132 | tm * curr_tm; |
shivanandgowdakr | 0:a5d3db2f2625 | 133 | time(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 134 | curr_tm = localtime(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 135 | strftime(DTSTRING,29,"%Y/%m/%d %H:%M",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 136 | |
shivanandgowdakr | 0:a5d3db2f2625 | 137 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 138 | |
shivanandgowdakr | 0:a5d3db2f2625 | 139 | void Get_Date_Time( char *DTSTRING) |
shivanandgowdakr | 0:a5d3db2f2625 | 140 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 141 | time_t curr_time; |
shivanandgowdakr | 0:a5d3db2f2625 | 142 | tm * curr_tm; |
shivanandgowdakr | 0:a5d3db2f2625 | 143 | time(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 144 | curr_tm = localtime(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 145 | strftime(DTSTRING,20,"%Y%m%d%H%M%S",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 146 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 147 | |
shivanandgowdakr | 0:a5d3db2f2625 | 148 | void Get_Date_Time_Trns( char *DTSTRING) |
shivanandgowdakr | 0:a5d3db2f2625 | 149 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 150 | time_t curr_time; |
shivanandgowdakr | 0:a5d3db2f2625 | 151 | tm * curr_tm; |
shivanandgowdakr | 0:a5d3db2f2625 | 152 | time(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 153 | curr_tm = localtime(&curr_time); |
shivanandgowdakr | 0:a5d3db2f2625 | 154 | strftime(DTSTRING,20,"%d%m%Y%H%M%S",curr_tm); |
shivanandgowdakr | 0:a5d3db2f2625 | 155 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 156 | |
shivanandgowdakr | 0:a5d3db2f2625 | 157 | |
shivanandgowdakr | 0:a5d3db2f2625 | 158 | |
shivanandgowdakr | 0:a5d3db2f2625 | 159 | int is_6_O_Clock(void) |
shivanandgowdakr | 0:a5d3db2f2625 | 160 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 161 | char dat[9]={'\0'}; |
shivanandgowdakr | 0:a5d3db2f2625 | 162 | char tim[9]={'\0'}; |
shivanandgowdakr | 0:a5d3db2f2625 | 163 | int HH,MM; |
shivanandgowdakr | 0:a5d3db2f2625 | 164 | Get_Date_Time(dat,tim); |
shivanandgowdakr | 0:a5d3db2f2625 | 165 | HH=(tim[0]-0x30)*10+(tim[1]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 166 | MM=(tim[2]-0x30)*10+(tim[3]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 167 | |
shivanandgowdakr | 0:a5d3db2f2625 | 168 | if((HH==6) &&(MM <30)) |
shivanandgowdakr | 0:a5d3db2f2625 | 169 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 170 | // printf("%d:%d\r\n",HH,MM); |
shivanandgowdakr | 0:a5d3db2f2625 | 171 | return 1; |
shivanandgowdakr | 0:a5d3db2f2625 | 172 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 173 | else |
shivanandgowdakr | 0:a5d3db2f2625 | 174 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 175 | |
shivanandgowdakr | 0:a5d3db2f2625 | 176 | // printf("%d:%d\r\n",HH,MM); |
shivanandgowdakr | 0:a5d3db2f2625 | 177 | return 0; |
shivanandgowdakr | 0:a5d3db2f2625 | 178 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 179 | } |
shivanandgowdakr | 0:a5d3db2f2625 | 180 | |
shivanandgowdakr | 0:a5d3db2f2625 | 181 | int Get_Hours(void) |
shivanandgowdakr | 0:a5d3db2f2625 | 182 | { |
shivanandgowdakr | 0:a5d3db2f2625 | 183 | char dat[9]={'\0'}; |
shivanandgowdakr | 0:a5d3db2f2625 | 184 | char tim[9]={'\0'}; |
shivanandgowdakr | 0:a5d3db2f2625 | 185 | int HH,MM; |
shivanandgowdakr | 0:a5d3db2f2625 | 186 | Get_Date_Time(dat,tim); |
shivanandgowdakr | 0:a5d3db2f2625 | 187 | HH=(tim[0]-0x30)*10+(tim[1]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 188 | MM=(tim[2]-0x30)*10+(tim[3]-0x30); |
shivanandgowdakr | 0:a5d3db2f2625 | 189 | return HH; |
shivanandgowdakr | 0:a5d3db2f2625 | 190 | } |