/************************************** **函数名:UpdateRTC **功能:在LCD上刷新RTC **注意事项:有一定的技巧用于显示RTC,使得LCD不重复刷写一个相同的数字 **************************************/ void UpdateRTC(void) { T_STRUCT time; GetRTCTime(&time); //刷新秒 USART1_Putc(time.sec); //刷新分 if(time.sec==0) { USART1_Putc(time.minute); //刷新时 if(time.minute==0) { USART1_Putc(time.hour); //刷新日 if(time.hour==0) { USART1_Putc(time.day); //刷新月 if(time.day==1) { USART1_Putc(time.month); //刷新年 if(time.month==1) { USART1_Putc(time.year); } } } } } }
void DispatcherTask ( void * pvParameters ) { struct ADispatcherMessage *pxDispatcherMessage; struct ALCDMessage xLCDMessage, *pxLCDMessage = & xLCDMessage; UB_RTC_Init ( ); LedInit ( ); xDispatcherQueue = xQueueCreate ( 8, sizeof ( unsigned long ) ); while ( 1 ) { SendTimeToLCD ( pxLCDMessage ); if ( xQueueReceive ( xDispatcherQueue, & ( pxDispatcherMessage ), 250 / portTICK_RATE_MS ) ) { if ( !strnicmp ( pxDispatcherMessage->Domain, "HELP", 3 ) ) { PrintHelp ( ); } else if ( !strnicmp ( pxDispatcherMessage->Domain, "RTC", 3 ) ) { if ( !strnicmp ( pxDispatcherMessage->Command, "SET", 3 ) ) { SetRTCTime ( pxDispatcherMessage->Parameter ); } else if ( !strnicmp ( pxDispatcherMessage->Command, "GET", 3 ) ) { GetRTCTime ( ); }; } else if ( !strnicmp ( pxDispatcherMessage->Domain, "AUDIO", 5 ) ) { ForwardAudioMessage ( pxDispatcherMessage->Command, pxDispatcherMessage->Parameter ); } else if ( !strnicmp ( pxDispatcherMessage->Domain, "LED", 3 ) ) { if ( !strnicmp ( pxDispatcherMessage->Command, "SET", 3 ) ) { LedSet ( pxDispatcherMessage->Parameter ); } else if ( !strnicmp ( pxDispatcherMessage->Command, "GET", 3 ) ) { LedGet ( pxDispatcherMessage->Parameter ); }; } else { }; }; }; };
void SetTimeWin(u8 param) { T_STRUCT time; if(param==WIDGET_PARAM_SHOW) { TFTShowString16(0,40,"时间设定:",BLACK,0); TFTShowString16(24,68,"/ / : :",BLACK,0); //获取时间 GetRTCTime(&time); //赋值到各个控件上 SetEditTmp(&SET_TIME_WIN_EDIT_YEAR,time.year); SetEditTmp(&SET_TIME_WIN_EDIT_MONTH,time.month); SetEditTmp(&SET_TIME_WIN_EDIT_DAY,time.day); SetEditTmp(&SET_TIME_WIN_EDIT_HOUR,time.hour); SetEditTmp(&SET_TIME_WIN_EDIT_MINUTE,time.minute); } }
bool RalTimeGetCurrent(struct tm* outCurrentTime) { char IS_Y2K = 1; short t_mdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //double newTime; unsigned long newTime; double tmpSec; signed short yy, mm, dd, h, m, s; bool ret; ret = TRUE; newTime = GetRTCTime(); CONSOL_Printf("== Cur Second : %ld\n", newTime); if (newTime >= (double)SEC_1904_2K) { newTime -= (double)SEC_1904_2K; IS_Y2K = 1; } else IS_Y2K = 0; // year for (yy = IS_Y2K ? 2000 : 1904, tmpSec = (double)LEAP_YEAR_SEC; newTime >= tmpSec; ) { newTime -= tmpSec; yy++; tmpSec = (double)(yy%4 == 0) && (yy%100 != 0) || (yy%400 == 0) ? LEAP_YEAR_SEC : YEAR_SEC; } // month for (mm = 1, tmpSec = (double)t_mdays[mm]*86400; newTime >= tmpSec; ) { newTime -= tmpSec; mm++; if (mm==2 && ((yy%4 == 0) && (yy%100 != 0) || (yy%400 == 0))) tmpSec = (double)2505600; //29*24*60*60 else tmpSec = (double)t_mdays[mm]*86400; /*24*60*60*/ } // day for (dd = 1; newTime >= (double)86400; dd++, newTime -= (double)86400); // hour for (h = 0; newTime >= (double)3600; h++, newTime -= (double)3600); // minute for (m = 0; newTime >= (double)60; m++, newTime -= (double)60); s = newTime; // by chc CONSOL_Printf("==-- s(%d)/NewTime(%d)\n", s, newTime ); outCurrentTime->tm_sec = s; outCurrentTime->tm_min = m; outCurrentTime->tm_hour = h; outCurrentTime->tm_mday = dd; outCurrentTime->tm_mon = mm; outCurrentTime->tm_year = yy; outCurrentTime->tm_yday = 0; outCurrentTime->tm_wday = 1; outCurrentTime->tm_isdst = 0; CONSOL_Printf("==-- curTSec(%d)\n", outCurrentTime->tm_sec); return ret; }
int main(int argc, char **argv) { int retVal, rtcFd, fifoFd; struct fifo_param fifo_data; struct rtc_time rtc_tm; char data_buf[30]; // this part works well since then, so no need to notify server if open fifo failed // if we want to notify server, use other ipc tools, such as message, shared memory if ((fifoFd = open(FIFO_DEV, O_WRONLY)) < 0) { // fifo's write-endian block until read-endian open if (mkfifo(FIFO_DEV, 0666) < 0) { db_error("mkfifo failed(%s)\n", strerror(errno)); return -1; } else { fifoFd = open(FIFO_DEV, O_WRONLY); } } if ((rtcFd = open(RTC_DEV, O_RDWR)) < 0) { // db_error("open %s failed(%s)\n", RTC_DEV, strerror(errno)); // fifo_data.type = 'A'; // strlcpy(fifo_data.name, RTC, 10); // fifo_data.result = 'F'; // write(fifoFd, &fifo_data, sizeof(fifo_param_t)); // add notify-info to tell server(fifo's read-endian) that client will close fifo's write-endian // If not, the server should use other method to dectect wheather fifo's write-endian is closed // D--Detect fifo's state // write(fifoFd, "D[RTC] close fifo", 18); // write(fifoFd, &fifo_data, sizeof(fifo_param_t)); strlcpy(data_buf, "F[RTC] fail", 30); write(fifoFd, data_buf, 30); close(fifoFd); return -1; } else { // fifo_data.type = 'A'; // strlcpy(fifo_data.name, RTC, 10); // fifo_data.result = 'W'; // write(fifoFd, &fifo_data, sizeof(fifo_param_t)); strlcpy(data_buf, "W[RTC] waiting", 30); write(fifoFd, data_buf, 30); } db_msg("open rtc dev OK\n"); // use debug level [db_msg] when open dev success, use [db_debug] for get data while (1) { retVal = GetRTCTime(rtcFd, &rtc_tm); // retVal = CheckRTCData(&rtc_tm); // 0-OK; !0-Fail if (retVal == 0) { // fifo_data.type = 'A'; // strlcpy(fifo_data.name, RTC, 10); // fifo_data.result = 'P'; // P-pass; F-fail // fifo_data.val.rtc = rtc_tm; // write(fifoFd, &fifo_data, sizeof(fifo_param_t)); sprintf(data_buf, "P[RTC] %.2d:%.2d:%.2d", rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); write(fifoFd, data_buf, 30); } else { // db_error("rtc data NOT correct\n"); // fifo_data.type = 'A'; // strlcpy(fifo_data.name, RTC, 10); // fifo_data.result = 'F'; // need finish the tester?(2015.01.06) // write(fifoFd, &fifo_data, sizeof(fifo_param_t)); sprintf(data_buf, "F[RTC] rtc data err"); write(fifoFd, data_buf, 30); } // memset(&fifo_data, 0, sizeof(fifo_param_t)); sleep(1); } /* puts("test rtc..."); rtcFd = open(RTC_DEV, O_RDONLY); if (rtcFd < 0) { perror("can NOT open rtc dev"); } else { printf("rtcFd = %d\n", rtcFd); } while (1) { retVal = ioctl(rtcFd, RTC_RD_TIME, &rtc_tm); if (retVal < 0) { perror("ioctl failed"); return -1; } else { printf("retVal: %d\n", retVal); printf("data: %d-%d-%d, time: %d:%d:%d\n", rtc_tm.tm_mon, rtc_tm.tm_mday, rtc_tm.tm_year+1900, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); } sleep(1); } */ close(fifoFd); close(rtcFd); return 0; }
void parseAlarmJson(char* content){ int r; int i = 2; int startidx = 0; int charAmount = 0; int usedAlarms[maxAlarms()]; int j; jsmn_parser p; jsmntok_t token[160]; /* We expect no more than 128 tokens */ jsmn_init(&p); r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0])); if (r <= 0) { printf("Failed to parse JSON: %d \n", r); return; }else{ printf("Aantal tokens found: %d \n", r); } struct _tm time = GetRTCTime(); for(j = 0; j < maxAlarms(); j++){ usedAlarms[j] = 0; } for(i = 2; i < r; i++) { int id = 0; u_short port = 0; char url[24]; char ip[24]; char name[17]; char str2[16]; char st = -1; char oo = -1; memset(url, 0, 24); memset(ip, 0, 24); memset(name, 0, 17); for (i = i; (st == -1 && i < r); i+=2) { //Zodra ST is gevonden, betekent dit de laatste token van een alarm. if (jsoneq(content, &token[i], "YYYY") == 0) { time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900; }else if (jsoneq(content, &token[i], "MM") == 0) { time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1; }else if (jsoneq(content, &token[i], "DD") == 0) { time.tm_mday = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "hh") == 0) { time.tm_hour = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "mm") == 0) { time.tm_min = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "ss") == 0) { time.tm_sec = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "id") == 0) { id = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "port") == 0) { port = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "ip") == 0) { getStringToken(content, &token[i + 1], ip, 24); }else if (jsoneq(content, &token[i], "url") == 0) { getStringToken(content, &token[i + 1], url, 24); }else if (jsoneq(content, &token[i], "name") == 0) { getStringToken(content, &token[i + 1], name, 18); }else if (jsoneq(content, &token[i], "oo") == 0) { oo = getIntegerToken(content, &token[i + 1]); }else if (jsoneq(content, &token[i], "st") == 0) { st = getIntegerToken(content, &token[i + 1]); } } int idx = alarmExist(id); if(idx == -1){ printf("New alarm found!\n"); printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec); printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900)); printf("Alarm stream data is: %s:%d%s\n", ip, port, url); printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st); charAmount = 0; for (i = 0; i < 16;i++){ if (name[i] != 0){ charAmount = charAmount + 1; } } startidx = (8-(charAmount/2)); charAmount = 0; for(i = 0; i < 16; i++){ if (i >= startidx){ if (name[charAmount] != 0){ str2[i] = name[charAmount]; } else { str2[i] = ' '; } charAmount++; } else { str2[i] = ' '; } } //zoek naar een vrije plaats in de alarm array for(j = 0; j < maxAlarms(); j++){ if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen if (oo == 1){ eenmaligAlarm(time,str2,ip,port,url,st,id,j); } else { setAlarm(time, str2, ip, port, url, st, id, j); } usedAlarms[j] = 1; j = 10; //Uit de for loop } } }else{ usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm } } for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen. if(usedAlarms[j] == 0){ deleteAlarm(j); }; } }