float adc_to_temperature(int sensor, char *calibration_file, uint16_t adc) { int R2K2 = get_calibration_value(sensor, calibration_file); float aT = -167.123; float bT = 0.275501; float cT = -0.000102316; float dT = 1.92025e-08; float volt = (2.5 / 1023) * adc; float R = R2K2 * adc / (2048.0 - adc); return aT + R * (bT + R * (cT + R * dT)); }
static int do_calibration(void) { int i, x, y; int dx[6], dy[6]; int tx[6], ty[6]; int delta, delta_x[6], delta_y[6]; int status=0; /* calculate the expected point */ x = info.xres / 4; y = info.yres / 4; dx[0] = x; dy[0] = info.yres / 2; dx[1] = info.xres / 2; dy[1] = y; dx[2] = info.xres - x; dy[2] = info.yres - y; dx[3] = 400; dy[3] = 360; dx[4] = 200; dy[4] = 360; dx[5] = 600; dy[5] = 240; retry: for (i = 0; i < 3; i ++) { draw_cross(dx[i], dy[i], 0); get_input(&tx[i], &ty[i]); log_write("get event: %d,%d -> %d,%d\n", tx[i], ty[i], dx[i], dy[i]); draw_cross(dx[i], dy[i], 1); } if( (dx[0] == 0) || (dx[1] == 0) || (dx[2] == 0) || (dy[0] == 0) || (dy[1] == 0) || (dy[2] == 0)){ log_write("Calibration Got ZERO Value\n"); goto retry; } if( (dy[0] < dy[1]) && (dy[1] - dy[0] < 10)){ log_write("Point 0 and Point 1 Get too Closed\n"); goto retry; }else if( dy[0] > dx[1]){ goto retry; } if( (dy[1] < dy[2]) && (dy[2] - dy[1] < 10)) goto retry; else if( dy[1] > dy[2]) goto retry; /* check ok, calulate the result */ delta = (tx[0] - tx[2]) * (ty[1] - ty[2]) - (tx[1] - tx[2]) * (ty[0] - ty[2]); delta_x[0] = (dx[0] - dx[2]) * (ty[1] - ty[2]) - (dx[1] - dx[2]) * (ty[0] - ty[2]); delta_x[1] = (tx[0] - tx[2]) * (dx[1] - dx[2]) - (tx[1] - tx[2]) * (dx[0] - dx[2]); delta_x[2] = dx[0] * (tx[1] * ty[2] - tx[2] * ty[1]) - dx[1] * (tx[0] * ty[2] - tx[2] * ty[0]) + dx[2] * (tx[0] * ty[1] - tx[1] * ty[0]); delta_y[0] = (dy[0] - dy[2]) * (ty[1] - ty[2]) - (dy[1] - dy[2]) * (ty[0] - ty[2]); delta_y[1] = (tx[0] - tx[2]) * (dy[1] - dy[2]) - (tx[1] - tx[2]) * (dy[0] - dy[2]); delta_y[2] = dy[0] * (tx[1] * ty[2] - tx[2] * ty[1]) - dy[1] * (tx[0] * ty[2] - tx[2] * ty[0]) + dy[2] * (tx[0] * ty[1] - tx[1] * ty[0]); cal_val[0] = delta_x[0]; cal_val[1] = delta_x[1]; cal_val[2] = delta_x[2]; cal_val[3] = delta_y[0]; cal_val[4] = delta_y[1]; cal_val[5] = delta_y[2]; cal_val[6] = delta; for (i = 3; i < 6; i ++) { draw_cross(dx[i], dy[i], 0); get_input(&tx[i], &ty[i]); get_calibration_value(&tx[i],&ty[i]); if(tx[i] >= dx[i] ) status = (tx[i] - dx[i] > status) ? tx[i] - dx[i]: status; else if(dx[i] > tx[i] ){ status = (dx[i] - tx[i] > status) ? dx[i] - tx[i]: status; } if(ty[i] >= dy[i] ) status = (ty[i] - dy[i] > status) ? ty[i] - dy[i]: status; else if(dy[i] > ty[i] ){ status = (dy[i] - ty[i] > status) ? dy[i] - ty[i]: status; } log_write("get event: %d,%d -> %d,%d status=%d\n", tx[i], ty[i], dx[i], dy[i],status); draw_cross(dx[i], dy[i], 1); } if(status > DIFF_VALUE){ return status; } save_conf(cal_val); write_conf(cal_val); return 0; }