int main(int argc, char **argv) { int i, j; long tmp=0; long tmp_avg=0; long tmp_avg2; long offset=0; float filter_low, filter_high; float spread_percent = SPREAD / 100.0 /2.0; int b; int nsamples=N_SAMPLES; long samples[nsamples]; if (argc == 2) { offset = atol(argv[1]); } setHighPri(); setup_io(); setup_gpio(); reset_converter(); j=0; // get the dirty samples and average them for(i=0;i<nsamples;i++) { reset_converter(); samples[i] = read_cnt(0, argc); tmp_avg += samples[i]; } tmp_avg = tmp_avg / nsamples; tmp_avg2 = 0; j=0; filter_low = (float) tmp_avg * (1.0 - spread_percent); filter_high = (float) tmp_avg * (1.0 + spread_percent); // printf("%d %d\n", (int) filter_low, (int) filter_high); for(i=0;i<nsamples;i++) { if ((samples[i] < filter_high && samples[i] > filter_low) || (samples[i] > filter_high && samples[i] < filter_low) ) { tmp_avg2 += samples[i]; j++; } } if (j == 0) { printf("No data to consider\n"); exit(255); } printf("%d", (tmp_avg2 / j) - offset); // printf("average within %f percent: %d from %d samples, original: %d\n", spread_percent*100, (tmp_avg2 / j) - offset, j, tmp_avg - offset); unpull_pins(); restore_io(); }
int main(int argc, char **argv) { int lockfd; int iPin = 0; int iErr = 0; char cMode = 'V'; int iReturnCode = 0; int i, j; long tmp=0; long tmp_avg=0; long tmp_avg2; long offset=0; float filter_low, filter_high; float spread_percent = SPREAD / 100.0 /2.0; int b; int nsamples=N_SAMPLES; long samples[nsamples]; if (argc == 2) { offset = atol(argv[1]); } if (argc == 4){ clock_pin = atol(argv[1]); data_pin = atol(argv[2]); offset = atol(argv[3]); } // lockfd = open_lockfile(LOCKFILE); iErr = wiringPiSetup (); if (iErr == -1) { if (cMode == 'V') printf ("ERROR : Failed to init WiringPi %d\n", iErr); iReturnCode = -1; } setHighPri(); setup_gpio(); // reset_converter(); j=0; // get the dirty samples and average them for(i=0;i<nsamples;i++) { // reset_converter(); // reset_converter(); samples[i] = read_cnt(0, argc); tmp_avg += samples[i]; } tmp_avg = tmp_avg / nsamples; tmp_avg2 = 0; j=0; filter_low = (float) tmp_avg * (1.0 - spread_percent); filter_high = (float) tmp_avg * (1.0 + spread_percent); // printf("%d %d\n", (int) filter_low, (int) filter_high); for(i=0;i<nsamples;i++) { if ((samples[i] < filter_high && samples[i] > filter_low) || (samples[i] > filter_high && samples[i] < filter_low) ) { tmp_avg2 += samples[i]; j++; } } if (j == 0) { printf("No data to consider\n"); exit(255); } printf("%d\n", (tmp_avg2 / j) - offset); // printf("average within %f percent: %d from %d samples, original: %d\n", spread_percent*100, (tmp_avg2 / j) - offset, j, tmp_avg - offset); // unpull_pins(); // restore_io(); // power_down_hx711(); }