📄 sensors.c
字号:
#include <string.h>
#include "globals.h"
#include "serial.h"
#include "options.h"
#include "error_handlers.h"
#include "sensors.h"
#include "custom_gui.h"
#define MSG_TOGGLE MSG_USER
#define MSG_UPDATE MSG_USER + 1
#define MSG_REFRESH MSG_USER + 2
#define SENSORS_PER_PAGE 9
#define NUM_OF_RETRIES 3
#define SENSORS_TO_TIME_OUT 2 //number of sensors that need to time out before the warning will be issued
#define REFRESH_RATE_PRECISION 10 // how often the samples are taken, in milliseconds
// Sensor states:
#define SENSOR_OFF 0 // OFF,
#define SENSOR_ACTIVE 1 // ACTIVE (returned value),
#define SENSOR_NA 2 // NA (displays "N/A")
typedef struct
{
void (*formula)(int raw_data, char *buf);
char label[32];
char screen_buf[64];
char pid[3];
int enabled;
int bytes; // number of data bytes expected from vehicle
} SENSOR;
static void load_sensor_states();
static void save_sensor_states();
static void fill_sensors(int page_number);
static int reset_chip_proc(int msg, DIALOG *d, int c);
static int options_proc(int msg, DIALOG *d, int c);
static int page_flipper_proc(int msg, DIALOG *d, int c);
static int sensor_proc(int msg, DIALOG *d, int c);
static int toggle_proc(int msg, DIALOG *d, int c);
static int toggle_all_proc(int msg, DIALOG *d, int c);
static int status_proc(int msg, DIALOG *d, int c);
static int page_number_proc(int msg, DIALOG *d, int c);
static int inst_refresh_rate_proc(int msg, DIALOG *d, int c);
static int avg_refresh_rate_proc(int msg, DIALOG *d, int c);
static int page_updn_handler_proc(int msg, DIALOG *d, int c);
// Sensor formulae:
static void throttle_position_formula(int data, char *buf); // Page 1
static void engine_rpm_formula(int data, char *buf);
static void vehicle_speed_formula(int data, char *buf);
static void engine_load_formula(int data, char *buf);
static void timing_advance_formula(int data, char *buf);
static void intake_pressure_formula(int data, char *buf);
static void air_flow_rate_formula(int data, char *buf);
static void fuel_system1_status_formula(int data, char *buf);
static void fuel_system2_status_formula(int data, char *buf);
static void short_term_fuel_trim_formula(int data, char *buf); // Page 2
static void long_term_fuel_trim_formula(int data, char *buf);
static void intake_air_temp_formula(int data, char *buf);
static void coolant_temp_formula(int data, char *buf);
static void fuel_pressure_formula(int data, char *buf);
static void secondary_air_status_formula(int data, char *buf);
static void pto_status_formula(int data, char *buf);
static void o2_sensor_formula(int data, char *buf);
void obd_requirements_formula(int data, char *buf);
// added 1/2/2003
static void engine_run_time_formula(int data, char *buf);
static void mil_distance_formula(int data, char *buf);
static void frp_relative_formula(int data, char *buf);
static void frp_widerange_formula(int data, char *buf);
static void o2_sensor_wrv_formula(int data, char *buf);
static void commanded_egr_formula(int data, char *buf);
static void egr_error_formula(int data, char *buf);
static void evap_pct_formula(int data, char *buf);
static void fuel_level_formula(int data, char *buf);
static void warm_ups_formula(int data, char *buf);
static void clr_distance_formula(int data, char *buf);
static void evap_vp_formula(int data, char *buf);
static void baro_pressure_formula(int data, char *buf);
static void o2_sensor_wrc_formula(int data, char *buf);
static void cat_temp_formula(int data, char *buf);
static void ecu_voltage_formula(int data, char *buf);
static void abs_load_formula(int data, char *buf);
static void eq_ratio_formula(int data, char *buf);
static void relative_tp_formula(int data, char *buf);
static void amb_air_temp_formula(int data, char *buf);
static void abs_tp_formula(int data, char *buf);
static void tac_pct_formula(int data, char *buf);
static void mil_time_formula(int data, char *buf);
static void clr_time_formula(int data, char *buf);
// variables
static int device_connected = FALSE;
static int reset_hardware = FALSE;
static int num_of_sensors = 0;
static int num_of_disabled_sensors = 0;
static int sensors_on_page = 0;
static int current_page = 0;
static float inst_refresh_rate = -1; // instantaneous refresh rate
static float avg_refresh_rate = -1; // average refresh rate
volatile int refresh_time; // time between sensor updates
static SENSOR sensors[] =
{
// formula // label //screen_buffer //pid //enabled // bytes
{ throttle_position_formula, "Absolute Throttle Position:", "", "11", 1, 1 },
{ engine_rpm_formula, "Engine RPM:", "", "0C", 1, 2 },
{ vehicle_speed_formula, "Vehicle Speed:", "", "0D", 1, 1 },
{ engine_load_formula, "Calculated Load Value:", "", "04", 1, 1 },
{ timing_advance_formula, "Timing Advance (Cyl. #1):", "", "0E", 1, 1 },
{ intake_pressure_formula, "Intake Manifold Pressure:", "", "0B", 1, 1 },
{ air_flow_rate_formula, "Air Flow Rate (MAF sensor):", "", "10", 1, 2 },
{ fuel_system1_status_formula, "Fuel System 1 Status:", "", "03", 1, 2 },
{ fuel_system2_status_formula, "Fuel System 2 Status:", "", "03", 1, 2 },
// Page 2
{ short_term_fuel_trim_formula, "Short Term Fuel Trim (Bank 1):", "", "06", 1, 2 },
{ long_term_fuel_trim_formula, "Long Term Fuel Trim (Bank 1):", "", "07", 1, 2 },
{ short_term_fuel_trim_formula, "Short Term Fuel Trim (Bank 2):", "", "08", 1, 2 },
{ long_term_fuel_trim_formula, "Long Term Fuel Trim (Bank 2):", "", "09", 1, 2 },
{ intake_air_temp_formula, "Intake Air Temperature:", "", "0F", 1, 1 },
{ coolant_temp_formula, "Coolant Temperature:", "", "05", 1, 1 },
{ fuel_pressure_formula, "Fuel Pressure (gauge):", "", "0A", 1, 1 },
{ secondary_air_status_formula, "Secondary air status:", "", "12", 1, 1 },
{ pto_status_formula, "Power Take-Off Status:", "", "1E", 1, 1 },
// Page 3
{ o2_sensor_formula, "O2 Sensor 1, Bank 1:", "", "14", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 2, Bank 1:", "", "15", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 3, Bank 1:", "", "16", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 4, Bank 1:", "", "17", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 1, Bank 2:", "", "18", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 2, Bank 2:", "", "19", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 3, Bank 2:", "", "1A", 1, 2 },
{ o2_sensor_formula, "O2 Sensor 4, Bank 2:", "", "1B", 1, 2 },
{ obd_requirements_formula, "OBD conforms to:", "", "1C", 1, 1 },
// Page 4
{ o2_sensor_wrv_formula, "O2 Sensor 1, Bank 1 (WR):", "", "24", 1, 4 }, // o2 sensors (wide range), voltage
{ o2_sensor_wrv_formula, "O2 Sensor 2, Bank 1 (WR):", "", "25", 1, 4 },
{ o2_sensor_wrv_formula, "O2 Sensor 3, Bank 1 (WR):", "", "26", 1, 4 },
{ o2_sensor_wrv_formula, "O2 Sensor 4, Bank 1 (WR):", "", "27", 1, 4 },
{ o2_sensor_wrv_formula, "O2 Sensor 1, Bank 2 (WR):", "", "28", 1, 4 },
{ o2_sensor_wrv_formula, "O2 Sensor 2, Bank 2 (WR):", "", "29", 1, 4 },
{ o2_sensor_wrv_formula, "O2 Sensor 3, Bank 2 (WR):", "", "2A", 1, 4 },
{ o2_sensor_wrv_formula, "O2 Sensor 4, Bank 2 (WR):", "", "2B", 1, 4 },
{ engine_run_time_formula, "Time Since Engine Start:", "", "1F", 1, 2 },
// Page 5
{ frp_relative_formula, "FRP rel. to manifold vacuum:", "", "22", 1, 2 }, // fuel rail pressure relative to manifold vacuum
{ frp_widerange_formula, "Fuel Pressure (gauge):", "", "23", 1, 2 }, // fuel rail pressure (gauge), wide range
{ commanded_egr_formula, "Commanded EGR:", "", "2C", 1, 1 },
{ egr_error_formula, "EGR Error:", "", "2D", 1, 1 },
{ evap_pct_formula, "Commanded Evaporative Purge:", "", "2E", 1, 1 },
{ fuel_level_formula, "Fuel Level Input:", "", "2F", 1, 1 },
{ warm_ups_formula, "Warm-ups since ECU reset:", "", "30", 1, 1 },
{ clr_distance_formula, "Distance since ECU reset:", "", "31", 1, 2 },
{ evap_vp_formula, "Evap System Vapor Pressure:", "", "32", 1, 2 },
// Page 6
{ o2_sensor_wrc_formula, "O2 Sensor 1, Bank 1 (WR):", "", "34", 1, 4 }, // o2 sensors (wide range), current
{ o2_sensor_wrc_formula, "O2 Sensor 2, Bank 1 (WR):", "", "35", 1, 4 },
{ o2_sensor_wrc_formula, "O2 Sensor 3, Bank 1 (WR):", "", "36", 1, 4 },
{ o2_sensor_wrc_formula, "O2 Sensor 4, Bank 1 (WR):", "", "37", 1, 4 },
{ o2_sensor_wrc_formula, "O2 Sensor 1, Bank 2 (WR):", "", "38", 1, 4 },
{ o2_sensor_wrc_formula, "O2 Sensor 2, Bank 2 (WR):", "", "39", 1, 4 },
{ o2_sensor_wrc_formula, "O2 Sensor 3, Bank 2 (WR):", "", "3A", 1, 4 },
{ o2_sensor_wrc_formula, "O2 Sensor 4, Bank 2 (WR):", "", "3B", 1, 4 },
{ mil_distance_formula, "Distance since MIL activated:", "", "21", 1, 2 },
// Page 7
{ baro_pressure_formula, "Barometric Pressure (absolute):", "", "33", 1, 1 },
{ cat_temp_formula, "CAT Temperature, B1S1:", "", "3C", 1, 2 },
{ cat_temp_formula, "CAT Temperature, B2S1:", "", "3D", 1, 2 },
{ cat_temp_formula, "CAT Temperature, B1S2:", "", "3E", 1, 2 },
{ cat_temp_formula, "CAT Temperature, B2S2:", "", "3F", 1, 2 },
{ ecu_voltage_formula, "ECU voltage:", "", "42", 1, 2 },
{ abs_load_formula, "Absolute Engine Load:", "", "43", 1, 2 },
{ eq_ratio_formula, "Commanded Equivalence Ratio:", "", "44", 1, 2 },
{ amb_air_temp_formula, "Ambient Air Temperature:", "", "46", 1, 1 }, // same scaling as $0F
// Page 8
{ relative_tp_formula, "Relative Throttle Position:", "", "45", 1, 1 },
{ abs_tp_formula, "Absolute Throttle Position B:", "", "47", 1, 1 },
{ abs_tp_formula, "Absolute Throttle Position C:", "", "48", 1, 1 },
{ abs_tp_formula, "Accelerator Pedal Position D:", "", "49", 1, 1 },
{ abs_tp_formula, "Accelerator Pedal Position E:", "", "4A", 1, 1 },
{ abs_tp_formula, "Accelerator Pedal Position F:", "", "4B", 1, 1 },
{ tac_pct_formula, "Comm. Throttle Actuator Cntrl:", "", "4C", 1, 1 }, // commanded TAC
{ mil_time_formula, "Engine running while MIL on:", "", "4D", 1, 2 }, // minutes run by the engine while MIL activated
{ clr_time_formula, "Time since DTCs cleared:", "", "4E", 1, 2 },
{ NULL, "", "", "", 0, 0 }
};
DIALOG sensor_dialog[] =
{
/* (proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
{ d_clear_proc, 0, 0, 0, 0, 0, C_WHITE, 0, 0, 0, 0, NULL, NULL, NULL },
{ page_updn_handler_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL },
{ d_shadow_box_proc, 40, 20, 560, 56, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ d_rtext_proc, 50, 25, 96, 20, C_BLACK, C_TRANSP, 0, 0, 0, 0, "Port Status:", NULL, NULL },
{ status_proc, 172, 25, 288, 20, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ reset_chip_proc, 464, 28, 126, 40, C_BLACK, C_GREEN, 'r', D_EXIT, 0, 0, "&Reset Interface", NULL, NULL },
{ d_rtext_proc, 50, 51, 96, 20, C_BLACK, C_TRANSP, 0, 0, 0, 0, "Refresh rate:", NULL, NULL },
{ inst_refresh_rate_proc, 154, 51, 174, 20, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ avg_refresh_rate_proc, 328, 51, 132, 20, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 87, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 91, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 0, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 94, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 123, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 127, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 1, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 130, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 1, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 159, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 163, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 2, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 166, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 2, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 195, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 199, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 3, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 202, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 3, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 231, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 235, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 4, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 238, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 4, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 267, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 271, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 5, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 274, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 5, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 303, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 307, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 6, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 310, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 6, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 339, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 343, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 7, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 346, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 7, 0, NULL, NULL, NULL },
{ d_box_proc, 40, 375, 560, 32, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ toggle_proc, 45, 379, 45, 24, C_BLACK, C_WHITE, 0, D_EXIT, 8, 0, NULL, NULL, NULL },
{ sensor_proc, 95, 382, 504, 24, C_BLACK, C_LIGHT_GRAY, 0, 0, 8, 0, NULL, NULL, NULL },
{ toggle_all_proc, 40, 420, 100, 40, C_BLACK, C_DARK_YELLOW, 'a', D_EXIT, 0, 0, "&All ON", NULL, NULL },
{ options_proc, 150, 420, 100, 40, C_BLACK, C_GREEN, 'o', D_EXIT, 0, 0, "&Options", NULL, NULL },
{ d_shadow_box_proc, 260, 420, 230, 40, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ d_button_proc, 500, 420, 100, 40, C_BLACK, C_DARK_YELLOW, 'm', D_EXIT, 0, 0, "&Main Menu", NULL, NULL },
{ st_ctext_proc, 300, 422, 38, 20, C_BLACK, C_TRANSP, 0, 0, 0, 0, "Page", NULL, NULL },
{ page_flipper_proc, 340, 425, 75, 30, C_BLACK, C_DARK_YELLOW, 'p', D_EXIT, -1, 0, "&Previous", NULL, NULL },
{ page_flipper_proc, 425, 425, 55, 30, C_BLACK, C_GREEN, 'x', D_EXIT, 1, 0, "Ne&xt", NULL, NULL },
{ page_number_proc, 300, 440, 36, 18, C_BLACK, C_LIGHT_GRAY, 0, 0, 0, 0, NULL, NULL, NULL },
{ NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
};
void inc_refresh_time(void)
{
refresh_time++;
}
END_OF_FUNCTION(inc_refresh_time);
int display_sensor_dialog(int reset)
{
int i;
int ret;
for (i = 0; sensors[i].formula; i++);
num_of_sensors = i;
current_page = 0;
if (reset)
reset_hardware = TRUE;
load_sensor_states();
fill_sensors(0);
LOCK_VARIABLE(refresh_time);
LOCK_FUNCTION(inc_refresh_time);
ret = do_dialog(sensor_dialog, -1);
save_sensor_states();
return ret;
}
static void calculate_refresh_rate(int sensor_state)
{
static int initialization_occured = FALSE;
static int num_of_sensors_off = 0;
static int reset_on_all_off_occured = FALSE;
static int sensors_on_counter = 0;
static float avg_refresh_rate_accumulator = 0;
if (!initialization_occured) // we received our first ">", initialize...
{
if (sensor_state == SENSOR_ACTIVE) // if we received HEX data
{
refresh_time = 0; // reset the time
install_int(inc_refresh_time, REFRESH_RATE_PRECISION); // install handler
initialization_occured = TRUE;
}
}
else // if this is not our first ">"
{
if ((num_of_sensors_off >= sensors_on_page) && !reset_on_all_off_occured) // if all sensors on page are OFF
{
inst_refresh_rate = -1;
avg_refresh_rate = -1;
reset_on_all_off_occured = TRUE;
broadcast_dialog_message(MSG_REFRESH, 0);
}
else // if num_of_sensors_off < sensors_on_page
{
if (sensor_state != SENSOR_OFF)
{
reset_on_all_off_occured = FALSE;
inst_refresh_rate = 1/(refresh_time*REFRESH_RATE_PRECISION*0.001);
if (sensors_on_counter < (sensors_on_page - num_of_disabled_sensors))
{
sensors_on_counter++;
avg_refresh_rate_accumulator += inst_refresh_rate;
}
else
{
avg_refresh_rate = avg_refresh_rate_accumulator/sensors_on_counter;
avg_refresh_rate_accumulator = 0;
sensors_on_counter = 0;
}
if (sensor_state == SENSOR_ACTIVE) // if we got response from ECU
refresh_time = 0; // reset time
broadcast_dialog_message(MSG_REFRESH, 0);
}
}
}
}
int reset_chip_proc(int msg, DIALOG *d, int c)
{
int ret = d_button_proc(msg, d, c);
if (ret == D_CLOSE)
{
if (comport.status == READY)
reset_hardware = TRUE;
else
alert("Port is not ready.", NULL, NULL, "OK", NULL, 0, 0);
ret = D_REDRAWME;
}
return ret;
}
int options_proc(int msg, DIALOG *d, int c)
{
int old_port;
int ret = d_button_proc(msg, d, c);
if (ret == D_CLOSE)
{
old_port = comport.number;
display_options();
if (comport.number != old_port)
reset_hardware = TRUE;
ret = D_REDRAWME;
}
return ret;
}
int page_updn_handler_proc(int msg, DIALOG *d, int c)
{
if ((msg == MSG_XCHAR) && ((c>>8) == KEY_PGUP || (c>>8) == KEY_PGDN))
{
if ((c>>8) == KEY_PGUP)
simulate_keypress('p');
if ((c>>8) == KEY_PGDN)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -