This commit is contained in:
Lubos Petrovic
2020-12-20 17:12:38 +01:00
parent 93e1c6fd90
commit ea6261f09e
6 changed files with 109 additions and 66 deletions

View File

@@ -3,9 +3,81 @@
//#include "CarInterface.h"
#include "LiveData.h"
/**
*
*/
void CommInterface::initComm(LiveData* pLiveData, BoardInterface* pBoard) {
liveData = pLiveData;
board = pBoard;
response = "";
}
/**
* Main loop
*/
void CommInterface::mainLoop() {
// Send command from TTY to OBD2
if (Serial.available()) {
ch = Serial.read();
if (ch == '\r' || ch == '\n') {
board->customConsoleCommand(response);
response = response + ch;
Serial.println(response);
if (liveData->bleConnected) {
executeCommand(response);
}
response = "";
} else {
response = response + ch;
}
}
// Can send next command from queue to OBD
if (liveData->canSendNextAtCommand) {
liveData->canSendNextAtCommand = false;
doNextQueueCommand();
}
}
/**
Do next AT command from queue
*/
bool CommInterface::doNextQueueCommand() {
// Restart loop with AT commands
if (liveData->commandQueueIndex >= liveData->commandQueueCount) {
liveData->commandQueueIndex = liveData->commandQueueLoopFrom;
board->redrawScreen();
// log every queue loop (temp) TODO and seconds interval
liveData->params.sdcardCanNotify = true;
}
// Send AT command to obd
liveData->commandRequest = liveData->commandQueue[liveData->commandQueueIndex];
if (liveData->commandRequest.startsWith("ATSH")) {
liveData->currentAtshRequest = liveData->commandRequest;
}
Serial.print(">>> ");
Serial.println(liveData->commandRequest);
liveData->responseRowMerged = "";
executeCommand(liveData->commandRequest);
liveData->commandQueueIndex++;
}
/**
Parse result from OBD, merge frames to sigle response
*/
bool CommInterface::parseResponse() {
// 1 frame data
Serial.println(liveData->responseRow);
// Merge frames 0:xxxx 1:yyyy 2:zzzz to single response xxxxyyyyzzzz string
if (liveData->responseRow.length() >= 2 && liveData->responseRow.charAt(1) == ':') {
liveData->responseRowMerged += liveData->responseRow.substring(2);
}
}

View File

@@ -17,5 +17,9 @@ class CommInterface {
virtual void connectDevice() = 0;
virtual void disconnectDevice() = 0;
virtual void scanDevices() = 0;
virtual void mainLoop() = 0;
virtual void mainLoop();
virtual void executeCommand(String cmd) = 0;
//
bool doNextQueueCommand();
bool parseResponse();
};

View File

@@ -333,81 +333,26 @@ void CommObd2Ble4::mainLoop() {
// Serve first command (ATZ)
doNextAtCommand();
} else {
Serial.println("We have failed to connect to the server; there is nothing more we will do.");
}
}
// Send command from TTY to OBD2
if (Serial.available()) {
ch = Serial.read();
if (ch == '\r' || ch == '\n') {
board->customConsoleCommand(response);
response = response + ch;
Serial.println(response);
if (liveData->bleConnected) {
liveData->pRemoteCharacteristicWrite->writeValue(response.c_str(), response.length());
}
response = "";
} else {
response = response + ch;
}
}
// Can send next command from queue to OBD
if (liveData->canSendNextAtCommand) {
liveData->canSendNextAtCommand = false;
doNextAtCommand();
}
// Parent declaration
CommInterface::mainLoop();
if (board->scanDevices) {
board->scanDevices = false;
startBleScan();
}
}
/**
Do next AT command from queue
*/
bool CommObd2Ble4::doNextAtCommand() {
* Send command
*/
void CommObd2Ble4::executeCommand(String cmd) {
// Restart loop with AT commands
if (liveData->commandQueueIndex >= liveData->commandQueueCount) {
liveData->commandQueueIndex = liveData->commandQueueLoopFrom;
board->redrawScreen();
// log every queue loop (temp)
liveData->params.sdcardCanNotify = true;
}
// Send AT command to obd
liveData->commandRequest = liveData->commandQueue[liveData->commandQueueIndex];
if (liveData->commandRequest.startsWith("ATSH")) {
liveData->currentAtshRequest = liveData->commandRequest;
}
Serial.print(">>> ");
Serial.println(liveData->commandRequest);
String tmpStr = liveData->commandRequest + "\r";
liveData->responseRowMerged = "";
String tmpStr = cmd + "\r";
liveData->pRemoteCharacteristicWrite->writeValue(tmpStr.c_str(), tmpStr.length());
liveData->commandQueueIndex++;
return true;
}
/**
Parse result from OBD, merge frames to sigle response
*/
bool CommObd2Ble4::parseRow() {
// 1 frame data
Serial.println(liveData->responseRow);
// Merge frames 0:xxxx 1:yyyy 2:zzzz to single response xxxxyyyyzzzz string
if (liveData->responseRow.length() >= 2 && liveData->responseRow.charAt(1) == ':') {
liveData->responseRowMerged += liveData->responseRow.substring(2);
}
return true;
}

View File

@@ -13,10 +13,9 @@ class CommObd2Ble4 : public CommInterface {
void disconnectDevice() override;
void scanDevices() override;
void mainLoop() override;
void executeCommand(String cmd) override;
//
void startBleScan();
bool connectToServer(BLEAddress pAddress);
bool doNextAtCommand();
bool parseRow();
//
};

View File

@@ -46,5 +46,27 @@ void CommObd2Can::scanDevices() {
Main loop
*/
void CommObd2Can::mainLoop() {
CommInterface::mainLoop();
}
/**
* Send command to CAN bus
*/
void executeCommand(String cmd) {
/**
* Serial.println("BMS 220101");
sendPID(0x00, 0x00);
delay(delayTxRx);
byte b = receivePID(0);
if (b == 0x10) {
Serial.println("CONTINUE");
sendPID(0x01, 0x00);
delay(10);
for (byte i = 0; i < 20; i++) {
receivePID(0);
delay(10);
}
}*/
}

View File

@@ -18,6 +18,7 @@ class CommObd2Can : public CommInterface {
void disconnectDevice() override;
void scanDevices() override;
void mainLoop() override;
void executeCommand(String cmd) override;
//
/* void startBleScan();
bool connectToServer(BLEAddress pAddress);