diff --git a/due_obd2/due_obd2.ino b/due_obd2/due_obd2.ino index 7648df9..d362e83 100644 --- a/due_obd2/due_obd2.ino +++ b/due_obd2/due_obd2.ino @@ -141,10 +141,6 @@ void on_init_enter() Timer2.attachInterrupt(update_device_info); Timer2.start(MS(100)); - // TODO change to update when initializing bluetooth.. - // Timer1.attachInterrupt(update_percent_test); - // Timer1.start(MS(60)); - Timer3.attachInterrupt(update_ram_kb); logo_flag |= (1 << FLAG_LOGO_UPDATE); @@ -162,6 +158,10 @@ void on_init_run() display.setColor(VGA_BLACK); /*clear only initializing text*/ display.print(" ", (display.getDisplayXSize() / 2) - (INIT_TEXT_WIDTH * display.getFontXsize() / 2) - (INIT_PERCENTAGE_WIDTH / 2), display.getDisplayYSize() / 2 + 50); + if (init_percent > 90) + { + update_percent(8); + } } /* update initialization text */ if (init_flag & (1 << FLAG_INIT_UPDATE_TEXT_POS)) @@ -193,6 +193,8 @@ void on_init_run() display.setColor(COLOR_ORANGE); display.fillRect(0, initialization_y + display.getFontYsize() + 3, ((float)init_percent / 100.0) * display.getDisplayXSize(), initialization_y + display.getFontYsize() + 13); + obd2_elm327_check_connection(&elm327); + /* update bluetooth state */ display.setBackColor(VGA_FUCHSIA); display.setColor(VGA_BLACK); @@ -274,6 +276,11 @@ void on_init_run() display.print(logo_text, CENTER, logo_pos_y); } } + + if (init_percent == 100) + { + statemachine_next(); + } } void on_init_exit() @@ -297,14 +304,29 @@ void on_main_enter() #if (DEBUG == 1) Serial.println("Entering main loop"); #endif + display.setColor(VGA_AQUA); } void on_main_run() { - delay(500); -#if (DEBUG == 1) - Serial.println("main!"); -#endif + obd2_elm327_process(&elm327); + + if (elm327.value_updates & (1 << UPDATE_RPM_POS)) + { + display.print("rpm ",0,100); + display.printNumI(elm327.rpm,100,100,4,'0'); + elm327.value_updates &= ~(1 << UPDATE_RPM_POS); + #if (DEBUG == 1) + Serial.println("rpm"); + Serial.println(elm327.rpm); + #endif + } + + if (elm327.value_updates & (1 << UPDATE_THROTTLE_POS)) + { + display.printNumI(elm327.throttle_percent,100,20,3,'0'); + elm327.value_updates &= ~(1 << UPDATE_THROTTLE_POS); + } } void update_init_text() @@ -421,7 +443,7 @@ void setup() randomSeed(analogRead(0)); #if (DEBUG == 1) - Serial.begin(9600); + Serial.begin(115200); #endif /* Init display */ @@ -436,6 +458,9 @@ void setup() statemachine_register_state(&init_state, STATE_INIT); statemachine_register_state(&main_state, STATE_CAR_INFO); + + // TODO remove + //statemachine_register_state(&main_state, 0); statemachine_init(); display.clrScr(); @@ -448,7 +473,15 @@ void setup() display.setColor(COLOR_ORANGE); display.print("Waiting for ELM327", (display.getDisplayXSize() / 2) - ((18 * display.getFontXsize()) / 2), 160); - obd2_elm327_init(&elm327); + #if (DEBUG == 1) + Serial.println("checking for bt"); + #endif + if (!obd2_elm327_init(&elm327)) + { + #if (DEBUG == 1) + Serial.println("Shit man its fucked"); + #endif + } elm327.on_state_change = &bt_state_changed; #if (DEBUG == 1) Serial.println("done with elm327 init"); diff --git a/due_obd2/obd2_elm327.cpp b/due_obd2/obd2_elm327.cpp index c6854c6..9252ef8 100644 --- a/due_obd2/obd2_elm327.cpp +++ b/due_obd2/obd2_elm327.cpp @@ -1,25 +1,46 @@ #include "ELMduino.h" #include - #include "obd2_elm327.h" #define KM_IN_MILES 1.6093440 +#define RPM_MAX 7000 +#define THROTTLE_MAX 100 + +typedef enum elm327_current_value_e +{ + RPM_V, + THROTTLE_V, + COOLANT_TEMP_V, + INTAKE_AIR_TEMP_V, + AMBIENT_AIR_TEMP_V, + OIL_TEMP_V, + ENGINE_LOAD_V, + BATTERY_VOLTAGE_V, + FUEL_PRESSURE_V, + SPEED_V, + MANIFOLD_PRESSURE_V + +} elm327_current_value; ELM327 elm327_obj; +elm327_current_value current_value; -char bt_states[2][BT_STATE_LENGTH] = {"Initializing", "Connected "}; +char bt_states[2][BT_STATE_LENGTH] = {"Initializing", "Connected"}; char obd2_elm327_init(obd2_elm327_t *elm327) { +#if (DEBUG == 1) + Serial.println("initting elm327"); +#endif ELM327_SERIAL.begin(ELM327_BAUD); elm327->bt_state = BT_INITIALISING; elm327->elm327 = &elm327_obj; - if (!elm327_obj.begin(ELM327_SERIAL, false, 5000)) + if (!elm327->elm327->begin(ELM327_SERIAL, false, 2000)) { - return 0; + return 0; } return 1; } @@ -29,7 +50,7 @@ void obd2_elm327_get_state(obd2_elm327_t *elm327, char *state) strcpy(state, bt_states[elm327->bt_state]); } -void obd2_elm327_process(obd2_elm327_t *elm327) +void obd2_elm327_check_connection(obd2_elm327_t *elm327) { if (elm327->bt_state != BT_CONNECTED) { @@ -37,77 +58,146 @@ void obd2_elm327_process(obd2_elm327_t *elm327) if (bt_state != elm327->bt_state) { - // 0 for initialising, 1 for connected - elm327->bt_state = bt_state == 1 ? BT_CONNECTED : BT_INITIALISING; + // 0 for initialising, 1 for connected + elm327->bt_state = bt_state == 1 ? BT_CONNECTED : BT_INITIALISING; elm327->on_state_change(); } } +} - /* TODO maybe change to switch statement */ +void obd2_elm327_process(obd2_elm327_t *elm327) +{ - float rpm = elm327->elm327->rpm(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + switch (current_value) { - elm327->rpm = (uint16_t)rpm; + case RPM_V: + { + float rpm = elm327->elm327->rpm(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + if (rpm > 0 && rpm < RPM_MAX) + elm327->rpm = (uint16_t)rpm; + elm327->value_updates |= (1 << UPDATE_RPM_POS); + } + current_value = THROTTLE_V; + break; } - - float throttle = elm327->elm327->throttle(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case THROTTLE_V: { - elm327->throttle_percent = (uint8_t)throttle; + float throttle = elm327->elm327->throttle(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + if (throttle > 0 && throttle <= THROTTLE_MAX) + { + elm327->throttle_percent = (uint8_t)throttle; + elm327->value_updates |= (1 << UPDATE_THROTTLE_POS); + } + } + // current_value = COOLANT_TEMP_V; + //TODO remove + current_value = RPM_V; + break; } - - float coolant_temp = elm327->elm327->engineCoolantTemp(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case COOLANT_TEMP_V: { - elm327->engine_coolant_temp = (uint8_t)coolant_temp; + float coolant_temp = elm327->elm327->engineCoolantTemp(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->engine_coolant_temp = (uint8_t)coolant_temp; + elm327->value_updates |= (1 << UPDATE_COOLANT_TEMP_POS); + } + current_value = INTAKE_AIR_TEMP_V; + break; } - - float intake_air_temp = elm327->elm327->intakeAirTemp(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case INTAKE_AIR_TEMP_V: { - elm327->intake_air_temp = (uint8_t)intake_air_temp; + float intake_air_temp = elm327->elm327->intakeAirTemp(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->intake_air_temp = (uint8_t)intake_air_temp; + elm327->value_updates |= (1 << UPDATE_INTAKE_AIR_TEMP_POS); + } + current_value = AMBIENT_AIR_TEMP_V; + break; } - - float ambient_air_temp = elm327->elm327->ambientAirTemp(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case AMBIENT_AIR_TEMP_V: { - elm327->ambient_air_temp = (uint8_t) ambient_air_temp; + float ambient_air_temp = elm327->elm327->ambientAirTemp(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->ambient_air_temp = (uint8_t)ambient_air_temp; + elm327->value_updates |= (1 << UPDATE_AMBIENT_AIR_TEMP_POS); + } + current_value = OIL_TEMP_V; + break; } - - float engine_oil_temp = elm327->elm327->oilTemp(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case OIL_TEMP_V: { - elm327->engine_oil_temp = engine_oil_temp; + float engine_oil_temp = elm327->elm327->oilTemp(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->engine_oil_temp = engine_oil_temp; + elm327->value_updates |= (1 << UPDATE_OIL_TEMP_POS); + } + current_value = ENGINE_LOAD_V; + break; } - - float engine_load = elm327->elm327->engineLoad(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case ENGINE_LOAD_V: { - elm327->engine_load = (uint8_t)engine_load; + float engine_load = elm327->elm327->engineLoad(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->engine_load = (uint8_t)engine_load; + elm327->value_updates |= (1 << UPDATE_ENGINE_LOAD_POS); + } + current_value = BATTERY_VOLTAGE_V; + break; } - - float battery_voltage = elm327->elm327->batteryVoltage(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case BATTERY_VOLTAGE_V: { - elm327->battery_voltage = battery_voltage; + float battery_voltage = elm327->elm327->batteryVoltage(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->battery_voltage = battery_voltage; + elm327->value_updates |= (1 << UPDATE_BATTERY_VOLTAGE_POS); + } + current_value = FUEL_PRESSURE_V; + break; } - - float fuel_pressure = elm327->elm327->fuelPressure(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case FUEL_PRESSURE_V: { - elm327->fuel_pressure = fuel_pressure; + float fuel_pressure = elm327->elm327->fuelPressure(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->fuel_pressure = fuel_pressure; + elm327->value_updates |= (1 << UPDATE_FUEL_PRESSURE_POS); + } + current_value = SPEED_V; + break; } - - float speed = elm327->elm327->mph(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case SPEED_V: { - elm327->speed = (uint8_t)(speed * KM_IN_MILES); + float speed = elm327->elm327->mph(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->speed = (uint8_t)(speed * KM_IN_MILES); + elm327->value_updates |= (1 << UPDATE_SPEED_POS); + } + current_value = MANIFOLD_PRESSURE_V; + break; } - - float manifold_pressure = elm327->elm327->manifoldPressure(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + case MANIFOLD_PRESSURE_V: { - elm327->manifold_pressure = (uint8_t)manifold_pressure; + float manifold_pressure = elm327->elm327->manifoldPressure(); + if (elm327->elm327->nb_rx_state == ELM_SUCCESS) + { + elm327->manifold_pressure = (uint8_t)manifold_pressure; + elm327->value_updates |= (1 << UPDATE_MANIFOLD_PRESSURE_POS); + } + current_value = RPM_V; + break; + } + default: + break; } } \ No newline at end of file diff --git a/due_obd2/obd2_elm327.h b/due_obd2/obd2_elm327.h index e93f2b1..9bc768c 100644 --- a/due_obd2/obd2_elm327.h +++ b/due_obd2/obd2_elm327.h @@ -55,6 +55,18 @@ Be sure to disconnect the TX and RX pins from the BT module when programming the #define ELM327_SERIAL Serial2 +#define UPDATE_RPM_POS 0x00 +#define UPDATE_THROTTLE_POS 0x01 +#define UPDATE_COOLANT_TEMP_POS 0x02 +#define UPDATE_INTAKE_AIR_TEMP_POS 0x03 +#define UPDATE_AMBIENT_AIR_TEMP_POS 0x04 +#define UPDATE_OIL_TEMP_POS 0x05 +#define UPDATE_ENGINE_LOAD_POS 0x06 +#define UPDATE_BATTERY_VOLTAGE_POS 0x07 +#define UPDATE_FUEL_PRESSURE_POS 0x08 +#define UPDATE_SPEED_POS 0x09 +#define UPDATE_MANIFOLD_PRESSURE_POS 0x0A + enum BluetoothState { BT_INITIALISING = 0, @@ -72,6 +84,7 @@ typedef struct obt2_elm327_tag ELM327 *elm327; enum BluetoothState bt_state; void (*on_state_change)(); + uint16_t value_updates; uint16_t rpm; uint8_t throttle_percent; uint8_t engine_coolant_temp; @@ -100,6 +113,8 @@ char obd2_elm327_init(obd2_elm327_t *elm327); */ void obd2_elm327_process(obd2_elm327_t *elm327); +void obd2_elm327_check_connection(obd2_elm327_t *elm327); + /** * @brief copies the current state of the bluetooth module to the state string * diff --git a/test_elm327/test_elm327.ino b/test_elm327/test_elm327.ino index be94257..89a6d64 100644 --- a/test_elm327/test_elm327.ino +++ b/test_elm327/test_elm327.ino @@ -29,7 +29,7 @@ void setup() // Serial.println("bt not connected"); // delay(500); // } - Serial.println("bt connected"); + // Serial.println("bt connected"); Serial.println("Attempting to connect to ELM327..."); @@ -38,6 +38,7 @@ void setup() Serial.println("Couldn't connect to OBD scanner"); while (1); } + Serial.println(myELM327.connected); Serial.println("Connected to ELM327"); } @@ -46,7 +47,7 @@ void setup() void loop() { float tempRPM = myELM327.rpm(); - float t = myELM327. + // float t = myELM327. // float tempval3 = myELM327.fuelInjectTiming();