Minor improvements, added smart pointer for CAN class object

Added smart pointer for CAN class object to allow automatic destroying of as well as weinitialization if ::connectDevice is called more than once.
Replaced one byte variables with uint8_t.
Added const where it was suitable.
This commit is contained in:
Ján Mátik
2020-12-21 00:10:14 +01:00
parent 2379e1028d
commit 90325f86ae
2 changed files with 42 additions and 29 deletions

View File

@@ -10,7 +10,13 @@ void CommObd2Can::connectDevice() {
Serial.println("CAN connectDevice"); Serial.println("CAN connectDevice");
CAN = new MCP_CAN(pinCanCs); //CAN = new MCP_CAN(pinCanCs); // todo: remove if smart pointer is ok
CAN.reset(new MCP_CAN(pinCanCs)); // smart pointer so it's automatically cleaned when out of context and also free to re-init
if (CAN == nullptr) {
Serial.println("Error: Not enough memory to instantiate CAN class");
Serial.println("init_can() failed");
return;
}
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN->begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ) == CAN_OK) { if (CAN->begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
@@ -22,7 +28,12 @@ void CommObd2Can::connectDevice() {
return; return;
} }
CAN->setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data. if (MCP2515_OK != CAN->setMode(MCP_NORMAL)) { // Set operation mode to normal so the MCP2515 sends acks to received data.
Serial.println("Error: CAN->setMode(MCP_NORMAL) failed");
board->displayMessage(" > CAN init failed", "");
return;
}
pinMode(pinCanInt, INPUT); // Configuring pin for /INT input pinMode(pinCanInt, INPUT); // Configuring pin for /INT input
// Serve first command (ATZ) // Serve first command (ATZ)
@@ -55,11 +66,11 @@ void CommObd2Can::mainLoop() {
CommInterface::mainLoop(); CommInterface::mainLoop();
// Read data // Read data
byte b = receivePID(); const uint8_t firstByte = receivePID();
if ((b & 0xf0) == 0x10) { // First frame, request another if ((firstByte & 0xf0) == 0x10) { // First frame, request another
sendFlowControlFrame(); sendFlowControlFrame();
delay(10); delay(10);
for (byte i = 0; i < 50; i++) { for (uint8_t i = 0; i < 50; i++) {
receivePID(); receivePID();
if (rxRemaining <= 0) if (rxRemaining <= 0)
break; break;
@@ -93,15 +104,16 @@ void CommObd2Can::executeCommand(String cmd) {
/** /**
Send PID Send PID
remark: parameter cmd as const reference to aviod copying
*/ */
void CommObd2Can::sendPID(uint16_t pid, String cmd) { void CommObd2Can::sendPID(const uint16_t pid, const String& cmd) {
byte txBuf[8] = { 0 }; // init with zeroes uint8_t txBuf[8] = { 0 }; // init with zeroes
String tmpStr; String tmpStr;
txBuf[0] = cmd.length() / 2; txBuf[0] = cmd.length() / 2;
for (byte i = 0; i < 7; i++) { for (uint8_t i = 0; i < 7; i++) {
tmpStr = cmd; tmpStr = cmd;
tmpStr = tmpStr.substring(i * 2, ((i + 1) * 2)); tmpStr = tmpStr.substring(i * 2, ((i + 1) * 2));
if (tmpStr != "") { if (tmpStr != "") {
@@ -110,15 +122,15 @@ void CommObd2Can::sendPID(uint16_t pid, String cmd) {
} }
lastPid = pid; lastPid = pid;
const byte sndStat = CAN->sendMsgBuf(pid, 0, 8, txBuf); // 11 bit const uint8_t sndStat = CAN->sendMsgBuf(pid, 0, 8, txBuf); // 11 bit
// byte sndStat = CAN->sendMsgBuf(0x7e4, 1, 8, tmp); // 29 bit extended frame // uint8_t sndStat = CAN->sendMsgBuf(0x7e4, 1, 8, tmp); // 29 bit extended frame
if (sndStat == CAN_OK) { if (sndStat == CAN_OK) {
Serial.print("SENT "); Serial.print("SENT ");
} else { } else {
Serial.print("Error sending PID "); Serial.print("Error sending PID ");
} }
Serial.print(pid); Serial.print(pid);
for (byte i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; i++) {
sprintf(msgString, " 0x%.2X", txBuf[i]); sprintf(msgString, " 0x%.2X", txBuf[i]);
Serial.print(msgString); Serial.print(msgString);
} }
@@ -130,19 +142,18 @@ void CommObd2Can::sendPID(uint16_t pid, String cmd) {
*/ */
void CommObd2Can::sendFlowControlFrame() { void CommObd2Can::sendFlowControlFrame() {
byte txBuf[8] = { 0x30, 0, 0, 0, 0, 0, 0, 0 }; uint8_t txBuf[8] = { 0x30, 0, 0, 0, 0, 0, 0, 0 };
Serial.println("Flow control frame"); Serial.println("Flow control frame");
const byte sndStat = CAN->sendMsgBuf(lastPid, 0, 8, txBuf); // 11 bit const uint8_t sndStat = CAN->sendMsgBuf(lastPid, 0, 8, txBuf); // 11 bit
if (sndStat == CAN_OK) { if (sndStat == CAN_OK) {
Serial.print("Flow control frame sent "); Serial.print("Flow control frame sent ");
} else { } else {
Serial.print("Error sending flow control frame "); Serial.print("Error sending flow control frame ");
} }
Serial.print(lastPid); Serial.print(lastPid);
for (byte i = 0; i < 8; i++) { for (auto txByte : txBuf) {
//for (auto txByte : txBuf) { sprintf(msgString, " 0x%.2X", txByte);
sprintf(msgString, " 0x%.2X", txBuf[i]);
Serial.print(msgString); Serial.print(msgString);
} }
Serial.println(""); Serial.println("");
@@ -151,7 +162,7 @@ void CommObd2Can::sendFlowControlFrame() {
/** /**
Receive PID Receive PID
*/ */
byte CommObd2Can::receivePID() { uint8_t CommObd2Can::receivePID() {
if (!digitalRead(pinCanInt)) // If CAN0_INT pin is low, read receive buffer if (!digitalRead(pinCanInt)) // If CAN0_INT pin is low, read receive buffer
{ {
@@ -169,7 +180,7 @@ byte CommObd2Can::receivePID() {
sprintf(msgString, " REMOTE REQUEST FRAME"); sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString); Serial.print(msgString);
} else { } else {
for (byte i = 0; i < rxLen; i++) { for (uint8_t i = 0; i < rxLen; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]); sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString); Serial.print(msgString);
} }
@@ -191,9 +202,9 @@ byte CommObd2Can::receivePID() {
*/ */
bool CommObd2Can::processFrame() { bool CommObd2Can::processFrame() {
byte frameType = (rxBuf[0] & 0xf0) >> 4; const uint8_t frameType = (rxBuf[0] & 0xf0) >> 4;
byte start = 1; // Single and Consecutive starts with pos 1 uint8_t start = 1; // Single and Consecutive starts with pos 1
byte index = 0; // 0 - f uint8_t index = 0; // 0 - f
liveData->responseRow = ""; liveData->responseRow = "";
switch (frameType) { switch (frameType) {
@@ -221,7 +232,7 @@ bool CommObd2Can::processFrame() {
Serial.print(rxRemaining); Serial.print(rxRemaining);
Serial.print(" "); Serial.print(" ");
for (byte i = start; i < rxLen; i++) { for (uint8_t i = start; i < rxLen; i++) {
sprintf(msgString, "%.2X", rxBuf[i]); sprintf(msgString, "%.2X", rxBuf[i]);
liveData->responseRow += msgString; liveData->responseRow += msgString;
rxRemaining--; rxRemaining--;

View File

@@ -4,15 +4,17 @@
#include "CommInterface.h" #include "CommInterface.h"
#include <mcp_can.h> #include <mcp_can.h>
#include <memory>
class CommObd2Can : public CommInterface { class CommObd2Can : public CommInterface {
protected: protected:
byte pinCanInt = 15; const uint8_t pinCanInt = 15;
byte pinCanCs = 12; const uint8_t pinCanCs = 12;
MCP_CAN* CAN; std::unique_ptr <MCP_CAN> CAN;
long unsigned int rxId; long unsigned int rxId;
unsigned char rxLen = 0; unsigned char rxLen = 0;
unsigned char rxBuf[32]; uint8_t rxBuf[32];
int16_t rxRemaining; // Remaining bytes to complete message int16_t rxRemaining; // Remaining bytes to complete message
char msgString[128]; // Array to store serial string char msgString[128]; // Array to store serial string
uint16_t lastPid; uint16_t lastPid;
@@ -23,8 +25,8 @@ class CommObd2Can : public CommInterface {
void mainLoop() override; void mainLoop() override;
void executeCommand(String cmd) override; void executeCommand(String cmd) override;
// //
void sendPID(uint16_t pid, String cmd); void sendPID(const uint16_t pid, const String& cmd);
void sendFlowControlFrame(); void sendFlowControlFrame();
byte receivePID(); uint8_t receivePID();
bool processFrame(); bool processFrame();
}; };