diff --git a/src/main.cpp b/src/main.cpp index 6fa14d3..7bc7fbf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,8 +9,9 @@ CAN_message_t canMsgTx; unsigned long previousCanICLMillis = 0; -bool engineIsRunning = true; +bool engineIsRunning = false; int previousACStatus = LOW; +int acSignalRequest = 0; void setup() { initialize(); @@ -35,26 +36,29 @@ void initialize() { pinMode(EXTERNAL_TEMPERATURE_SENSOR, INPUT_ANALOG); + pinMode(PC13, OUTPUT); + Can1.begin(); Can1.setBaudRate(500000); } void processCan(long currentMillis) { - // if (Can1.read(canMsgRx)) { - // switch (canMsgRx.id) { - // case DME2: - // processCanReadDME2(canMsgRx); - // break; - // } - // } + if (Can1.read(canMsgRx)) { + switch (canMsgRx.id) { + case DME2: + processCanReadDME2(canMsgRx); + break; + } + } processCanWriteICL(currentMillis); } void processCanReadDME2(CAN_message_t msg) { byte byte3 = msg.buf[3]; - engineIsRunning = bitRead(byte3, 3) == 1; - Serial.printf("Engine is running %s\n", engineIsRunning); + engineIsRunning = bitRead(byte3, 3) == HIGH; + + digitalWrite(PC13, engineIsRunning ? HIGH : LOW); } void processCanWriteICL(long currentMillis) { @@ -89,37 +93,61 @@ void processCanWriteICL3() { canMsgTx.id = CAN_ICL3; canMsgTx.len = 8; - if (engineIsRunning) { - if (acStatus != previousACStatus) { - // E46 sends a signal of ac requesting before sending the actual request for compressor activation - // this is valid for both states on/off - canMsgTx.buf[0] = 0x80; - canMsgTx.buf[1] = calculateFanStage(previousACStatus); - } else { - switch (acStatus) { - case HIGH: - canMsgTx.buf[0] = 0xD9; // E36 AC Compressor does not have variable control - break; - default: // OFF - canMsgTx.buf[0] = 0x00; - break; - } - - canMsgTx.buf[1] = calculateFanStage(acStatus); - } - - previousACStatus = acStatus; - } else { - canMsgTx.buf[0] = 0x00; - canMsgTx.buf[1] = 0x00; - } - canMsgTx.buf[2] = 0x00; canMsgTx.buf[3] = readTemperatureSensor(); canMsgTx.buf[4] = 0x00; canMsgTx.buf[5] = 0x00; canMsgTx.buf[6] = 0x00; canMsgTx.buf[7] = 0x00; + + if (!engineIsRunning) { + canMsgTx.buf[0] = 0x00; + canMsgTx.buf[1] = 0x00; + Can1.write(canMsgTx); + return; + } + + if (acStatus != previousACStatus) { + // E46 sends a signal of ac requesting before sending the actual request for compressor state change + // this is valid for both states on/off + canMsgTx.buf[0] = 0x80; + + switch (acStatus) { + case HIGH: + if (acSignalRequest >= 1) { + acSignalRequest = 0; + previousACStatus = acStatus; + } + canMsgTx.buf[1] = calculateFanStage(LOW); + break; + default: // OFF + if (acSignalRequest > 1) { + acSignalRequest = 0; + previousACStatus = acStatus; + canMsgTx.buf[1] = calculateFanStage(LOW); + } else if (acSignalRequest == 1) { + canMsgTx.buf[1] = calculateFanStage(LOW); + } else { + canMsgTx.buf[1] = calculateFanStage(HIGH); + } + break; + } + + acSignalRequest++; + Can1.write(canMsgTx); + return; + } + + switch (acStatus) { + case HIGH: + canMsgTx.buf[0] = 0xD2; + break; + default: // OFF + canMsgTx.buf[0] = 0x00; + break; + } + + canMsgTx.buf[1] = calculateFanStage(acStatus); Can1.write(canMsgTx); }