void *UI_thread() { static int sw_ch=0,log_time,tot_rec,counter; do { while(READ_ADC == Yes); printf("\n /------------------------ LAB 4 ---------------------------------------/"); printf("\n /-------------- WELCOME!! INTEL GALILEO BASED DAS ---------------------/"); printf("\n /--------------- SELECT FROM FOLLOWING OPTIONS ------------------------/"); printf("\n 1.SET RTC TIME\n 2.SET RTC DATE\n 3.READ RTC TIME\n 4.READ ADC \n 5.EXIT APPLICATION\n\n ENTER OPTION : \t"); READ_ADC = Yes; scanf("%d",&sw_ch); switch(sw_ch) { case SET_CLK : set_rtc(); READ_ADC = No; break; case SET_DATE : set_date(); READ_ADC = No ; break; case READ_CLK : ui_disp = 1; read_rtc(); READ_ADC = No; break; case DATA_LOG : //pass signal to thread2 to update the adc value ui_disp = 1; read_rtc(); printf("\tLDR value = %d\n",adc_data); READ_ADC = No; break; case EXIT_APP : printf("\n GOODBYE !!! APPLICATION CLOSED\n "); exit_rtc(); end_prog = 1; break; default : printf("OOPS !!! WRONG OPTION "); } }while(end_prog == 0); pthread_exit(NULL); }
int RTC_Linux_Trim(void) { struct timeval now; /* Remember the slope coefficient - we won't be able to determine a good one in a few seconds when we determine the new offset! */ saved_coef_gain_rate = coef_gain_rate; if (fabs(coef_seconds_fast) > 1.0) { LOG(LOGS_INFO, LOGF_RtcLinux, "Trimming RTC, error = %.3f seconds", coef_seconds_fast); /* Do processing to set clock. Let R be the value we set the RTC to, then in 500ms the RTC ticks (R+1) (see comments in arch/i386/kernel/time.c about the behaviour of the real time clock chip). If S is the system time now, the error at the next RTC tick is given by E = (R+1) - (S+0.5). Ideally we want |E| <= 0.5, which implies R <= S <= R+1, i.e. R is just the rounded down part of S, i.e. the seconds part. */ LCL_ReadCookedTime(&now, NULL); set_rtc(now.tv_sec); /* All old samples will now look bogus under the new regime. */ n_samples = 0; operating_mode = OM_AFTERTRIM; /* Estimate the offset in case writertc is called or chronyd is terminated during rapid sampling */ coef_seconds_fast = -now.tv_usec / 1e6 + 0.5; coef_ref_time = now.tv_sec; /* And start rapid sampling, interrupts on now */ if (timeout_running) { SCH_RemoveTimeout(timeout_id); timeout_running = 0; } switch_interrupts(1); } return 1; }
void cmd_settime(void) { struct tm time; if(strlen(curchar) != 4+2+2 + 2+2+2) { printf("invalid time format (need YYYYMMDDhhmmss)\n"); } else { time.tm_sec = atoi(curchar+4+2+2+2+2); curchar[4+2+2+2+2] = 0; time.tm_min = atoi(curchar+4+2+2+2); curchar[4+2+2+2] = 0; time.tm_hour = atoi(curchar+4+2+2); curchar[4+2+2] = 0; time.tm_mday = atoi(curchar+4+2); curchar[4+2] = 0; time.tm_mon = atoi(curchar+4); curchar[4] = 0; time.tm_year = atoi(curchar); set_rtc(&time); } }
void menu_set_clock(void) { struct tm t; uint8_t p; uint8_t days; switch (rtc_state) { case RTC_INVALID: memset(&t, 0, sizeof(t)); t.tm_mday = 1; t.tm_year = 115; break; case RTC_OK: read_rtc(&t); break; case RTC_NOT_FOUND: default: return; } lcd_printf("%02d-MMM-20%2d %02d:%02d:%02d", t.tm_mday, t.tm_year - 100, t.tm_hour, t.tm_min, t.tm_sec); lcd_locate(3, 0); menu_print_month(t.tm_mon); p = SETCLK_MDAY; for (;;) { if (p < SETCLK_SET) { lcd_locate(pgm_read_byte(&(menu_setclk_pos[p])), 0); switch (p++) { case SETCLK_MDAY: t.tm_mday = menu_edit_value(t.tm_mday, 1, 31); continue; case SETCLK_MON: t.tm_mon = menu_edit_month(t.tm_mon); days = calc_number_of_days(t.tm_mon, t.tm_year); if (t.tm_mon == 1) ++days; // Could be a leap year if (t.tm_mday > days) p = SETCLK_MDAY; continue; case SETCLK_YEAR: t.tm_year = menu_edit_value(t.tm_year - 100, 15, 99) + 100; days = calc_number_of_days(t.tm_mon, t.tm_year); if (t.tm_mday > days) p = SETCLK_MDAY; continue; case SETCLK_HOUR: t.tm_hour = menu_edit_value(t.tm_hour, 0, 23); continue; case SETCLK_MIN: t.tm_min = menu_edit_value(t.tm_min, 0, 59); continue; case SETCLK_SEC: t.tm_sec = menu_edit_value(t.tm_sec, 0, 59); continue; } } else { lcd_locate(0,1); lcd_puts_P(PSTR("Write to RTC\nEdit again\nAbort")); uint8_t sel = menu_vertical(1,3); lcd_clrlines(1,3); switch (sel) { case 1: // Set time t.tm_wday = day_of_week(t.tm_year, t.tm_mon + 1, t.tm_mday); set_rtc(&t); // fall through case 3: // Abort return; default: // Edit date & time p = SETCLK_MDAY; } } } }
int main(int argc, char** argv) { int err; const char* cmd = basename(argv[0]); char *path = NULL; char *set = NULL; static const struct option opts[] = { {"set", required_argument, NULL, 's'}, {"dev", required_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {0}, }; for (int opt; (opt = getopt_long(argc, argv, "", opts, NULL)) != -1;) { switch (opt) { case 's': set = strdup(optarg); break; case 'd': path = strdup(optarg); break; case 'h': usage(cmd); err = 0; goto done; default: err = usage(cmd); goto done; } } argv += optind; argc -= optind; if (argc != 0) { err = usage(cmd); goto done; } if (!path) { path = guess_dev(); if (!path) { fprintf(stderr, "No RTC found.\n"); err = usage(cmd); goto done; } } if (set) { err = set_rtc(path, set); if (err) { printf("Set RTC failed.\n"); usage(cmd); } goto done; } err = print_rtc(path); if (err) { usage(cmd); } done: free(path); free(set); return err; }
/* The main driving routine */ int main(int argc, char *argv[]) { time_t t; /* Current time */ time_t target; /* Next wake up time */ struct tm *now; /* Pointer to current expanded time */ int delay; /* How long to sleep for */ int ret,dsv; time_t t1,stm; SYS_TIME timeinfo; TIME_FMT timefmt; if (strcmp(argv[0], "cron-parent") == 0) { return cron_parent_main(argc, argv); } create_file(CRONFILE); /* Create the crontab on /mnt/ramfs */ /* Set daylight saving flag */ t = time(NULL); TIME_FMT_ReadSystemData(&timefmt); if(timefmt.d_sav) { tzone=timefmt.t_index; dsv=daylight_saving(t); PRO_SetInt(SEC_MAN,MAN_SUMMER_CHANGED ,dsv); } for (;;) { t = time(NULL); /* Work out what time it is now */ ret=PRO_GetInt(SEC_MAN,MAN_SUMMER_CHANGED ,&dsv); if(ret != 0) dsv=0; TIME_FMT_ReadSystemData(&timefmt); // Read daylight saving flag if(timefmt.d_sav) { tzone=timefmt.t_index; if((ret=daylight_saving(t)) != dsv) { if(dsv) { AddLog(DST_SEC,SYSLOG_WARNING,DST_CHG_LEAVE,-1); time(&t1); now=localtime(&t1); stm = mktime(now); stm -= 3600; stime(&stm); set_rtc(); ret = PRO_SetInt(SEC_MAN, MAN_SUMMER_CHANGED, 0); // fl_write_conf(CONF_FILE); // summer_chg = 0 } else { AddLog(DST_SEC,SYSLOG_WARNING,DST_CHG_ENTER,1); time(&t1); now=localtime(&t1); stm = mktime(now); stm += 3600; stime(&stm); set_rtc(); ret = PRO_SetInt(SEC_MAN, MAN_SUMMER_CHANGED, 1); // fl_write_conf(CONF_FILE); // summer_chg = 1 } } } now = localtime(&t); target = t + 60 - now->tm_sec; /* Next awakening */ clean_children(); /* Clean up any child processes */ load_file(CRONFILE); /* Load the crontab if necessary */ check_runs(now); /* Run anything that needs to be run */ /* Now go to sleep for a time. * This loop will fail if it takes longer than sixty second to * execute each iteration :-) */ for (;;) { delay = target - (time(NULL)); if (delay > 0) break; target += 60; } while (delay > 0) delay = sleep(delay); } }