can
This commit is contained in:
@@ -3,9 +3,81 @@
|
|||||||
//#include "CarInterface.h"
|
//#include "CarInterface.h"
|
||||||
#include "LiveData.h"
|
#include "LiveData.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
void CommInterface::initComm(LiveData* pLiveData, BoardInterface* pBoard) {
|
void CommInterface::initComm(LiveData* pLiveData, BoardInterface* pBoard) {
|
||||||
|
|
||||||
liveData = pLiveData;
|
liveData = pLiveData;
|
||||||
board = pBoard;
|
board = pBoard;
|
||||||
response = "";
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,5 +17,9 @@ class CommInterface {
|
|||||||
virtual void connectDevice() = 0;
|
virtual void connectDevice() = 0;
|
||||||
virtual void disconnectDevice() = 0;
|
virtual void disconnectDevice() = 0;
|
||||||
virtual void scanDevices() = 0;
|
virtual void scanDevices() = 0;
|
||||||
virtual void mainLoop() = 0;
|
virtual void mainLoop();
|
||||||
|
virtual void executeCommand(String cmd) = 0;
|
||||||
|
//
|
||||||
|
bool doNextQueueCommand();
|
||||||
|
bool parseResponse();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -333,81 +333,26 @@ void CommObd2Ble4::mainLoop() {
|
|||||||
|
|
||||||
// Serve first command (ATZ)
|
// Serve first command (ATZ)
|
||||||
doNextAtCommand();
|
doNextAtCommand();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println("We have failed to connect to the server; there is nothing more we will do.");
|
Serial.println("We have failed to connect to the server; there is nothing more we will do.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send command from TTY to OBD2
|
// Parent declaration
|
||||||
if (Serial.available()) {
|
CommInterface::mainLoop();
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (board->scanDevices) {
|
if (board->scanDevices) {
|
||||||
board->scanDevices = false;
|
board->scanDevices = false;
|
||||||
startBleScan();
|
startBleScan();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Do next AT command from queue
|
* Send command
|
||||||
*/
|
*/
|
||||||
bool CommObd2Ble4::doNextAtCommand() {
|
void CommObd2Ble4::executeCommand(String cmd) {
|
||||||
|
|
||||||
// Restart loop with AT commands
|
String tmpStr = cmd + "\r";
|
||||||
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 = "";
|
|
||||||
liveData->pRemoteCharacteristicWrite->writeValue(tmpStr.c_str(), tmpStr.length());
|
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,9 @@ class CommObd2Ble4 : public CommInterface {
|
|||||||
void disconnectDevice() override;
|
void disconnectDevice() override;
|
||||||
void scanDevices() override;
|
void scanDevices() override;
|
||||||
void mainLoop() override;
|
void mainLoop() override;
|
||||||
|
void executeCommand(String cmd) override;
|
||||||
//
|
//
|
||||||
void startBleScan();
|
void startBleScan();
|
||||||
bool connectToServer(BLEAddress pAddress);
|
bool connectToServer(BLEAddress pAddress);
|
||||||
bool doNextAtCommand();
|
|
||||||
bool parseRow();
|
|
||||||
//
|
//
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -47,4 +47,26 @@ void CommObd2Can::scanDevices() {
|
|||||||
*/
|
*/
|
||||||
void CommObd2Can::mainLoop() {
|
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);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class CommObd2Can : public CommInterface {
|
|||||||
void disconnectDevice() override;
|
void disconnectDevice() override;
|
||||||
void scanDevices() override;
|
void scanDevices() override;
|
||||||
void mainLoop() override;
|
void mainLoop() override;
|
||||||
|
void executeCommand(String cmd) override;
|
||||||
//
|
//
|
||||||
/* void startBleScan();
|
/* void startBleScan();
|
||||||
bool connectToServer(BLEAddress pAddress);
|
bool connectToServer(BLEAddress pAddress);
|
||||||
|
|||||||
Reference in New Issue
Block a user