/** * Add the measurement for the requested attribute type with optional * arguments (enumerator over char*, gets destroyed). */ static void add_measurement(pen_type_t attr_type, imc_msg_t *msg, enumerator_t *args) { pa_tnc_attr_t *attr; enum_name_t *pa_attr_names; attr = get_measurement(attr_type, args); if (attr) { msg->add_attribute(msg, attr); return; } pa_attr_names = imcv_pa_tnc_attributes->get_names(imcv_pa_tnc_attributes, attr_type.vendor_id); if (pa_attr_names) { DBG1(DBG_IMC, "no measurement available for PA-TNC attribute type " "'%N/%N' 0x%06x/0x%08x", pen_names, attr_type.vendor_id, pa_attr_names, attr_type.type, attr_type.vendor_id, attr_type.type); } else { DBG1(DBG_IMC, "no measurement available for PA-TNC attribute type '%N' " "0x%06x/0x%08x", pen_names, attr_type.vendor_id, attr_type.vendor_id, attr_type.type); } }
boolean mica2_gps_parse_gga(char* str, gps_gga_t* gga) { //printf("%s\n", str); memset(gga, 0, sizeof(gps_gga_t)); // need to pass this as the 'unit' for the horiz. dilution. char dummy = '\0'; uint16_t value_count = 0; char* values[20]; split_commas(str, values, &value_count); // GGA message should have 14(?) fields if (value_count != 14) { //printf("not enough fields in GGA data\n"); return false; } else { gga->utc = get_timestamp(values[0]); gga->latitude = get_latlong(values[1], values[2], true); gga->longitude = get_latlong(values[3], values[4], false); gga->fix_quality = atoi(values[5]); gga->satellite_count = atoi(values[6]); gga->horiz_dilution = get_measurement(values[7], &dummy); gga->altitude = get_measurement(values[8], values[9]); gga->geoid_height = get_measurement(values[10], values[11]); gga->since_last_update = atoi(values[12]); gga->dgps_id = atoi(values[13]); return true; } }
/*************************************************************************** Grab sample temperature and calculate running mean and variance see http://www.johndcook.com/blog/standard_deviation/ ***************************************************************************/ void sampleTemp(Measurement* m, int* semaphorePtr) { double T, mLast; m->n++; if (m->n == 1) { //first measurement m->M = get_measurement(m->channel, semaphorePtr); //get_measurement first gets internal junction temp then gets TC (20ms delays) m->S = 0.0; m->max = m->M; m->min = m->M; } else { T = get_measurement_fast(m->channel, semaphorePtr); //get_measurement_fast just grabs TC value and starts next TC sampling mLast = m->M; m->M = mLast + (T - mLast)/m->n; //running mean m->S = m->S + (T - mLast) * (T - m->M); m->max = fmax(T, m->max); m->min = fmin(T, m->min); } }
int main(int argc, char* argv[]) { int ret; int i; uint8_t spi_config=0; double tval; int repeat=0; int period=1; char fname[128]; char tstring[128]; char tstring2[128]; time_t mytime; time_t desiredtime; struct timespec tstime; int not_finished=1; int elapsed=0; int showtime=0; // initialise GPIO setup_io(); INP_GPIO(LCD_RS_GPIO); // must use INP_GPIO before we can use OUT_GPIO OUT_GPIO(LCD_RS_GPIO); // parse inputs dofile=0; if (argc>2) { if (strcmp(argv[1], "msg")==0) // print to the lcd { spi_config=0; ret=spi_open(&lcd_fd, 0, spi_config); if (ret!=0) { printf("Exiting\n"); exit(1); } lcd_display_string(0,argv[2]); close(lcd_fd); exit(0); } } if (argc>1) { if (strcmp(argv[1], "-h")==0) // print help { printf("%s [sec] [filename]\n", argv[0]); printf("%s msg <message in quotes>\n", argv[0]); exit(0); } if (strcmp(argv[1], "lcdinit")==0) // initialize the LCD display { spi_config=0; ret=spi_open(&lcd_fd, 0, spi_config); if (ret!=0) { printf("Exiting\n"); exit(1); } lcd_init(); close(lcd_fd); exit(0); } if (strcmp(argv[1], "withtime")==0) { showtime=1; } else { sscanf(argv[1], "%d", &period); // period between measurements repeat=1; } if (argc>3) { if (strcmp(argv[2], "msg")==0) // print to the lcd { spi_config=0; ret=spi_open(&lcd_fd, 0, spi_config); if (ret!=0) { printf("Exiting\n"); exit(1); } lcd_init(); lcd_display_string(0,argv[3]); lcd_initialised=1; } } } if (argc>2) { strcpy(fname, argv[2]); // file name dofile=1; if (argc>4) { if (strcmp(argv[3], "msg")==0) // print to the lcd { spi_config=0; ret=spi_open(&lcd_fd, 0, spi_config); if (ret!=0) { printf("Exiting\n"); exit(1); } lcd_init(); lcd_display_string(0,argv[4]); lcd_initialised=1; } } } if (dofile) { outfile=fopen(fname, "w"); } // open SPI for ADS1118 spi_config |= SPI_CPHA; ret=spi_open(&ads_fd, 1, spi_config); if (ret!=0) // SPI error { printf("Exiting\n"); exit(1); } if (repeat==0) { // display a single measurement and exit tval=get_measurement(); if (showtime) { mytime = time(NULL); sprintf(tstring, "%d", mytime); unixtime2string(tstring, tstring2); printf("%s %#.1f\n", tstring2, tval); } else { printf("%#.1f\n", tval); } close(ads_fd); exit(0); } // open up SPI for LCD spi_config=0; ret=spi_open(&lcd_fd, 0, spi_config); if (ret!=0) { printf("Exiting\n"); exit(1); } if (lcd_initialised==0) lcd_init(); if (dofile) { // line 1 of the output file will contain the three column descriptions fprintf(outfile, "Time HH:MM:SS,Elapsed Sec,Temp C\n"); } // Align on an integer number of seconds and get current time mytime = time(NULL); tstime.tv_sec=mytime+1; tstime.tv_nsec=0; clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &tstime, NULL); mytime++; desiredtime=mytime; signal(SIGINT, sig_handler); while(not_finished) { tval=get_measurement(); for (i=1; i<10; i++) { delay_ms(10); tval=tval+get_measurement_fast(); } tval=tval/10; // print the time, elapsed counter and temperature sprintf(tstring, "%d", mytime); unixtime2string(tstring, tstring2); if (mytime==desiredtime) { printf("%s %d %#.1f\n", tstring2, elapsed, tval); if (dofile) { fprintf(outfile, "%s,%d,%#.1f\n", tstring2, elapsed, tval); fflush(outfile); } desiredtime=desiredtime+period; } sprintf(tstring, "%s %#.1f", tstring2, tval); lcd_display_string(1, tstring); // now we sleep for a certain time mytime++; tstime.tv_sec=mytime; elapsed++; clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &tstime, NULL); } close(ads_fd); // LCD requires opposite clock polarity spi_config=0; ret=spi_open(&lcd_fd, 0, spi_config); if (ret!=0) { printf("Exiting\n"); exit(1); } lcd_init(); lcd_clear(); // LCD clear lcd_display_string(0,"Hello"); // display "ADS1118" close(lcd_fd); return(0); }