From 74e3a860d65199ad9fe1edadc27960acef2adff6 Mon Sep 17 00:00:00 2001 From: Marco Nilsson Date: Thu, 1 Dec 2016 09:04:37 +0100 Subject: [PATCH 01/23] Remove the dependency to Serial. The sub class can assign this pointer when needed. --- NMEA2000.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NMEA2000.cpp b/NMEA2000.cpp index ec56c0e2..0300c62a 100644 --- a/NMEA2000.cpp +++ b/NMEA2000.cpp @@ -113,7 +113,8 @@ void ClearSetCharBuf(const char *str, int MaxLen, char *buf) { } //***************************************************************************** -tNMEA2000::tNMEA2000() { +tNMEA2000::tNMEA2000() + : ForwardStream(0) { SingleFrameMessages[0]=DefSingleFrameMessages; FastPacketMessages[0]=DefFastPacketMessages; @@ -127,8 +128,7 @@ tNMEA2000::tNMEA2000() { MsgHandler=0; ISORqstHandler=0; - - ForwardStream=&Serial; + DeviceInformation[0].N2kSource=0; DeviceReady=false; AddressClaimStarted=0; From 7af7236caf68f1170a8bce115bde4edfd730eb58 Mon Sep 17 00:00:00 2001 From: Marco Nilsson Date: Thu, 1 Dec 2016 12:04:13 +0100 Subject: [PATCH 02/23] Add missing headers. --- N2kMessages.cpp | 1 + N2kMessages.h | 1 + N2kMsg.cpp | 1 + N2kMsg.h | 2 ++ 4 files changed, 5 insertions(+) diff --git a/N2kMessages.cpp b/N2kMessages.cpp index 4c5f4428..3fa5ccc2 100644 --- a/N2kMessages.cpp +++ b/N2kMessages.cpp @@ -22,6 +22,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "N2kMessages.h" +#include //***************************************************************************** // System time diff --git a/N2kMessages.h b/N2kMessages.h index 8a77e6b3..9c1a3f01 100644 --- a/N2kMessages.h +++ b/N2kMessages.h @@ -37,6 +37,7 @@ NMEA2000.h #define _N2kMessages_H_ #include "N2kMsg.h" +#include inline double RadToDeg(double v) { return v*180.0/3.1415926535897932384626433832795; } inline double DegToRad(double v) { return v/180.0*3.1415926535897932384626433832795; } diff --git a/N2kMsg.cpp b/N2kMsg.cpp index df129841..e00509fb 100644 --- a/N2kMsg.cpp +++ b/N2kMsg.cpp @@ -22,6 +22,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "N2kMsg.h" +#include //#include // For testing used memory #define Escape 0x10 diff --git a/N2kMsg.h b/N2kMsg.h index 4127d60b..749f808b 100644 --- a/N2kMsg.h +++ b/N2kMsg.h @@ -26,7 +26,9 @@ Definition for NMEA2000 message class used in my NMEA2000 libraries. #ifndef _tN2kMsg_H_ #define _tN2kMsg_H_ + #include +#include const double N2kDoubleNA=-1e9; const uint8_t N2kUInt8NA=0xff; From 2afabed738d60144c3da0d40030b0c414ab96211 Mon Sep 17 00:00:00 2001 From: Marco Nilsson Date: Thu, 1 Dec 2016 19:11:23 +0100 Subject: [PATCH 03/23] Replace Arduino binary definitions with BIT(n) macro which uses bit shifts. --- N2kMessages.cpp | 58 ++++++++++++++++++++++++------------------------- N2kMessages.h | 20 ++++++++--------- N2kMsg.h | 1 + NMEA2000.h | 11 +++++----- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/N2kMessages.cpp b/N2kMessages.cpp index 3fa5ccc2..69d40cdd 100644 --- a/N2kMessages.cpp +++ b/N2kMessages.cpp @@ -220,35 +220,35 @@ void SetN2kPGN127489(tN2kMsg &N2kMsg, unsigned char EngineInstance, double Engin N2kMsg.Add2ByteUDouble(EngineFuelPress, 1000); N2kMsg.AddByte(0xff); // reserved - int engineStatus1P1 = B00000000; - int engineStatus1P2 = B00000000; - int engineStatus2 = B00000000; - if (flagCheckEngine) engineStatus1P1 |= B00000001; - if (flagOverTemp) engineStatus1P1 |= B00000010; - if (flagLowOilPress) engineStatus1P1 |= B00000100; - if (flagLowOilLevel) engineStatus1P1 |= B00001000; - if (flagLowFuelPress) engineStatus1P1 |= B00010000; - if (flagLowSystemVoltage) engineStatus1P1 |= B00100000; - if (flagLowCoolantLevel) engineStatus1P1 |= B01000000; - if (flagWaterFlow) engineStatus1P1 |= B10000000; - - if (flagWaterInFuel) engineStatus1P2 |= B00000001; - if (flagChargeIndicator) engineStatus1P2 |= B00000010; - if (flagPreheatIndicator) engineStatus1P2 |= B00000100; - if (flagHighBoostPress) engineStatus1P2 |= B00001000; - if (flagRevLimitExceeded) engineStatus1P2 |= B00010000; - if (flagEgrSystem) engineStatus1P2 |= B00100000; - if (flagTPS) engineStatus1P2 |= B01000000; - if (flagEmergencyStopMode) engineStatus1P2 |= B10000000; - - if (flagWarning1) engineStatus2 |= B00000001; - if (flagWarning2) engineStatus2 |= B00000010; - if (flagPowerReduction) engineStatus2 |= B00000100; - if (flagMaintenanceNeeded) engineStatus2 |= B00001000; - if (flagEngineCommError) engineStatus2 |= B00010000; - if (flagSubThrottle) engineStatus2 |= B00100000; - if (flagNeutralStartProtect) engineStatus2 |= B01000000; - if (flagEngineShuttingDown) engineStatus2 |= B10000000; + int engineStatus1P1 = 0; + int engineStatus1P2 = 0; + int engineStatus2 = 0; + if (flagCheckEngine) engineStatus1P1 |= BIT(0); + if (flagOverTemp) engineStatus1P1 |= BIT(1); + if (flagLowOilPress) engineStatus1P1 |= BIT(2); + if (flagLowOilLevel) engineStatus1P1 |= BIT(3); + if (flagLowFuelPress) engineStatus1P1 |= BIT(4); + if (flagLowSystemVoltage) engineStatus1P1 |= BIT(5); + if (flagLowCoolantLevel) engineStatus1P1 |= BIT(6); + if (flagWaterFlow) engineStatus1P1 |= BIT(7); + + if (flagWaterInFuel) engineStatus1P2 |= BIT(0); + if (flagChargeIndicator) engineStatus1P2 |= BIT(1); + if (flagPreheatIndicator) engineStatus1P2 |= BIT(2); + if (flagHighBoostPress) engineStatus1P2 |= BIT(3); + if (flagRevLimitExceeded) engineStatus1P2 |= BIT(4); + if (flagEgrSystem) engineStatus1P2 |= BIT(5); + if (flagTPS) engineStatus1P2 |= BIT(6); + if (flagEmergencyStopMode) engineStatus1P2 |= BIT(7); + + if (flagWarning1) engineStatus2 |= BIT(0); + if (flagWarning2) engineStatus2 |= BIT(1); + if (flagPowerReduction) engineStatus2 |= BIT(2); + if (flagMaintenanceNeeded) engineStatus2 |= BIT(3); + if (flagEngineCommError) engineStatus2 |= BIT(4); + if (flagSubThrottle) engineStatus2 |= BIT(5); + if (flagNeutralStartProtect) engineStatus2 |= BIT(6); + if (flagEngineShuttingDown) engineStatus2 |= BIT(7); N2kMsg.Add2ByteInt(engineStatus1P2<<8 | engineStatus1P1); // Discrete Status 1 N2kMsg.Add2ByteInt(engineStatus2); // Discrete Status 1 diff --git a/N2kMessages.h b/N2kMessages.h index 9c1a3f01..68a3958b 100644 --- a/N2kMessages.h +++ b/N2kMessages.h @@ -503,11 +503,11 @@ inline void SetN2kTransmissionParameters(tN2kMsg &N2kMsg, unsigned char EngineIn bool flagSailDrive=false) { unsigned char DiscreteStatus1=0; - if (flagCheck) DiscreteStatus1 |= B00000001; - if (flagOverTemp) DiscreteStatus1 |= B00000010; - if (flagLowOilPressure) DiscreteStatus1 |= B00000100; - if (flagLowOilLevel) DiscreteStatus1 |= B00001000; - if (flagSailDrive) DiscreteStatus1 |= B00010000; + if (flagCheck) DiscreteStatus1 |= BIT(0); + if (flagOverTemp) DiscreteStatus1 |= BIT(1); + if (flagLowOilPressure) DiscreteStatus1 |= BIT(2); + if (flagLowOilLevel) DiscreteStatus1 |= BIT(3); + if (flagSailDrive) DiscreteStatus1 |= BIT(4); SetN2kPGN127493(N2kMsg, EngineInstance, TransmissionGear, OilPressure, OilTemperature,DiscreteStatus1); } @@ -525,11 +525,11 @@ inline bool ParseN2kTransmissionParameters(const tN2kMsg &N2kMsg, unsigned char unsigned char DiscreteStatus1; bool ret=ParseN2kPGN127493(N2kMsg, EngineInstance, TransmissionGear, OilPressure, OilTemperature, DiscreteStatus1); if (ret) { - flagCheck = ((DiscreteStatus1&B00000001)!=0); - flagOverTemp = ((DiscreteStatus1&B00000010)!=0); - flagLowOilPressure = ((DiscreteStatus1&B00000100)!=0); - flagLowOilLevel = ((DiscreteStatus1&B00001000)!=0); - flagSailDrive = ((DiscreteStatus1&B00010000)!=0); + flagCheck = ((DiscreteStatus1 & BIT(0))!=0); + flagOverTemp = ((DiscreteStatus1 & BIT(1))!=0); + flagLowOilPressure = ((DiscreteStatus1 & BIT(2))!=0); + flagLowOilLevel = ((DiscreteStatus1 & BIT(3))!=0); + flagSailDrive = ((DiscreteStatus1 & BIT(4))!=0); } return ret; } diff --git a/N2kMsg.h b/N2kMsg.h index 749f808b..210c8c21 100644 --- a/N2kMsg.h +++ b/N2kMsg.h @@ -37,6 +37,7 @@ const uint16_t N2kUInt16NA=0xffff; const int16_t N2kInt16NA=0x7fff; const uint32_t N2kUInt32NA=0xffffffff; const int32_t N2kInt32NA=0x7fffffff; +#define BIT(n) (1 << n) inline bool N2kIsNA(double v) { return v==N2kDoubleNA; } inline bool N2kIsNA(uint8_t v) { return v==N2kUInt8NA; } diff --git a/NMEA2000.h b/NMEA2000.h index 306a9bf3..1445b9bf 100644 --- a/NMEA2000.h +++ b/NMEA2000.h @@ -161,12 +161,11 @@ class tNMEA2000 protected: // Forward mode bit settings. - static const int FwdModeBit_EnableForward = B00000001; // If set, forward is enabled - static const int FwdModeBit_SystemMessages = B00000010; // System messages will be forwarded - static const int FwdModeBit_OnlyKnownMessages = B00000100; // Only known messages will be forwarded. System messages will be forwarded according its own bit. - static const int FwdModeBit_OwnMessages = B00001000; // Forward also all messages, what this device will send - - static const int HandleModeBit_OnlyKnownMessages = B00010000; // Only known messages will be handled. + static const int FwdModeBit_EnableForward = BIT(0); // If set, forward is enabled + static const int FwdModeBit_SystemMessages = BIT(1); // System messages will be forwarded + static const int FwdModeBit_OnlyKnownMessages = BIT(2); // Only known messages will be forwarded. System messages will be forwarded according its own bit. + static const int FwdModeBit_OwnMessages = BIT(3); // Forward also all messages, what this device will send + static const int HandleModeBit_OnlyKnownMessages = BIT(4); // Only known messages will be handled. protected: tDebugMode dbMode; // Default dm_None From 6c191b4228cdf32eeaf66898f8d2b4bcfb1e0b7c Mon Sep 17 00:00:00 2001 From: Marco Nilsson Date: Thu, 1 Dec 2016 19:22:51 +0100 Subject: [PATCH 04/23] Replace byte with uint8_t. --- ActisenseReader.cpp | 2 +- N2kMsg.cpp | 119 +++++++++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/ActisenseReader.cpp b/ActisenseReader.cpp index 4b3d14bd..ec7a6654 100644 --- a/ActisenseReader.cpp +++ b/ActisenseReader.cpp @@ -64,7 +64,7 @@ bool tActisenseReader::CheckMessage(tN2kMsg &N2kMsg) { return false; // Length does not match. Add type, length and crc } - byte CheckSum = (byte)((byteSum == 0) ? 0 : (256 - byteSum)); + uint8_t CheckSum = (uint8_t)((byteSum == 0) ? 0 : (256 - byteSum)); if ( CheckSum!=MsgBuf[MsgWritePos-1] ) { return false; // Checksum does not match } diff --git a/N2kMsg.cpp b/N2kMsg.cpp index e00509fb..cbdaecf8 100644 --- a/N2kMsg.cpp +++ b/N2kMsg.cpp @@ -1,4 +1,4 @@ -/* +/* N2kMsg.cpp Copyright (c) 2015-2016 Timo Lappalainen, Kave Oy, www.kave.fi @@ -23,6 +23,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "N2kMsg.h" #include +#include //#include // For testing used memory #define Escape 0x10 @@ -173,28 +174,28 @@ unsigned char tN2kMsg::GetByte(int &Index) const { if (Index>=8; @@ -324,7 +325,7 @@ void SetBuf8ByteDouble(double v, double precision, int &index, unsigned char *bu void SetBuf4ByteDouble(double v, double precision, int &index, unsigned char *buf) { int32_t *vi=(int32_t *)(&buf[index]); index+=4; - + (*vi)=(int32_t)round(v/precision); } @@ -332,7 +333,7 @@ void SetBuf4ByteDouble(double v, double precision, int &index, unsigned char *bu void SetBuf4ByteUDouble(double v, double precision, int &index, unsigned char *buf) { uint32_t *vi=(uint32_t *)(&buf[index]); index+=4; - + (*vi)=(uint32_t)round(v/precision); } @@ -347,7 +348,7 @@ void SetBuf3ByteDouble(double v, double precision, int &index, unsigned char *bu int16_t GetBuf2ByteInt(int &index, const unsigned char *buf) { int16_t *vi=(int16_t *)(&buf[index]); index+=2; - + return *vi; } @@ -355,7 +356,7 @@ int16_t GetBuf2ByteInt(int &index, const unsigned char *buf) { uint16_t GetBuf2ByteUInt(int &index, const unsigned char *buf) { uint16_t *vi=(uint16_t *)(&buf[index]); index+=2; - + return *vi; } @@ -363,7 +364,7 @@ uint16_t GetBuf2ByteUInt(int &index, const unsigned char *buf) { uint32_t GetBuf4ByteUInt(int &index, const unsigned char *buf) { uint32_t *vi=(uint32_t *)(&buf[index]); index+=4; - + return *vi; } @@ -371,9 +372,9 @@ uint32_t GetBuf4ByteUInt(int &index, const unsigned char *buf) { double GetBuf1ByteDouble(double precision, int &index, const unsigned char *buf, double def) { int8_t *vi=(int8_t *)(&buf[index]); index+=1; - + if (*vi==0x7f) return def; - + return ((double)(*vi))*precision; } @@ -381,9 +382,9 @@ double GetBuf1ByteDouble(double precision, int &index, const unsigned char *buf, double GetBuf1ByteUDouble(double precision, int &index, const unsigned char *buf, double def) { uint8_t *vi=(uint8_t *)(&buf[index]); index+=1; - + if (*vi==0xff) return def; - + return ((double)(*vi))*precision; } @@ -391,9 +392,9 @@ double GetBuf1ByteUDouble(double precision, int &index, const unsigned char *buf double GetBuf2ByteDouble(double precision, int &index, const unsigned char *buf, double def) { int16_t *vi=(int16_t *)(&buf[index]); index+=2; - + if (*vi==0x7fff) return def; - + return ((double)(*vi))*precision; } @@ -401,9 +402,9 @@ double GetBuf2ByteDouble(double precision, int &index, const unsigned char *buf, double GetBuf2ByteUDouble(double precision, int &index, const unsigned char *buf, double def) { uint16_t *vi=(uint16_t *)(&buf[index]); index+=2; - + if (*vi==0xffff) return def; - + return ((double)(*vi))*precision; } @@ -416,23 +417,23 @@ double GetBuf8ByteDouble(double precision, int &index, const unsigned char *buf, index+=4; long *vlhi=(long *)(&buf[index]); index+=4; - + if ( (*vlhi==0x7fff) && (*vllo==0xffff) ) return def; double v=*vlhi * 4294967296.0; - + if (v>=0) { v += *vllo; } else { v -= *vllo; } - + // Below did not work even with Due //long long vll=*vlhi; //vll<<=32; //vll|=*vllo; - - - //Serial.print(*vlhi,HEX); Serial.print(","); Serial.println(*vllo,HEX); + + + //Serial.print(*vlhi,HEX); Serial.print(","); Serial.println(*vllo,HEX); // if (((unsigned long long)(vll))==0xffffffffffffffff) return def; - + return v*precision; } @@ -445,7 +446,7 @@ double GetBuf3ByteDouble(double precision, int &index, const unsigned char *buf, // We use only 3 bytes, so set highest byte to 0 vll&=0x00ffffff; if (vll==0x007fffff) return def; - + return ((double)(vll))*precision; } @@ -455,7 +456,7 @@ double GetBuf4ByteDouble(double precision, int &index, const unsigned char *buf, index+=4; if (*vl==0x7fffffff) return def; - + return ((double)(*vl))*precision; } @@ -465,7 +466,7 @@ double GetBuf4ByteUDouble(double precision, int &index, const unsigned char *buf index+=4; if (*vl==0xffffffff) return def; - + return ((double)(*vl))*precision; } @@ -473,7 +474,7 @@ double GetBuf4ByteUDouble(double precision, int &index, const unsigned char *buf void SetBuf2ByteDouble(double v, double precision, int &index, unsigned char *buf) { int16_t *vi=(int16_t *)(&buf[index]); index+=2; - + (*vi)=(int16_t)round(v/precision); } @@ -481,7 +482,7 @@ void SetBuf2ByteDouble(double v, double precision, int &index, unsigned char *bu void SetBuf2ByteUDouble(double v, double precision, int &index, unsigned char *buf) { uint16_t *vi=(uint16_t *)(&buf[index]); index+=2; - + (*vi)=(uint16_t)round(v/precision); } @@ -489,7 +490,7 @@ void SetBuf2ByteUDouble(double v, double precision, int &index, unsigned char *b void SetBuf1ByteDouble(double v, double precision, int &index, unsigned char *buf) { int8_t *vi=(int8_t *)(&buf[index]); index+=1; - + (*vi)=(int8_t)round(v/precision); } @@ -497,7 +498,7 @@ void SetBuf1ByteDouble(double v, double precision, int &index, unsigned char *bu void SetBuf1ByteUDouble(double v, double precision, int &index, unsigned char *buf) { uint8_t *vi=(uint8_t *)(&buf[index]); index+=1; - + (*vi)=(uint8_t)round(v/precision); } @@ -524,7 +525,7 @@ void SetBuf3ByteInt(int32_t v, int &index, unsigned char *buf) { void SetBuf4ByteUInt(uint32_t v, int &index, unsigned char *buf) { uint32_t *vl=(uint32_t *)(&buf[index]); index+=4; - + (*vl)=v; } @@ -552,34 +553,36 @@ void SetBufStr(const char *str, int len, int &index, unsigned char *buf) { } //***************************************************************************** -void PrintBuf(Stream *port, unsigned char len, const unsigned char *pData, bool AddLF) { +void PrintBuf(N2kStream *port, unsigned char len, const unsigned char *pData, bool AddLF) { if (port==0) return; + char out[3]; for(int i = 0; i0) { port->print(","); }; - port->print(pData[i],HEX); + // Print bytes as hex. + port->print(itoa(pData[i], out, 16)); } - + if (AddLF) port->println(""); } //***************************************************************************** -void tN2kMsg::Print(Stream *port, bool NoData) const { +void tN2kMsg::Print(N2kStream *port, bool NoData) const { if (port==0 || !IsValid()) return; - port->print(F("Pri:")); port->print(Priority); - port->print(F(" PGN:")); port->print(PGN); - port->print(F(" Source:")); port->print(Source); - port->print(F(" Dest:")); port->print(Destination); - port->print(F(" Len:")); port->print(DataLen); + port->print(CSTR("Pri:")); port->print(Priority); + port->print(CSTR(" PGN:")); port->print(PGN); + port->print(CSTR(" Source:")); port->print(Source); + port->print(CSTR(" Dest:")); port->print(Destination); + port->print(CSTR(" Len:")); port->print(DataLen); if (!NoData) { - port->print(F(" Data:")); + port->print(CSTR(" Data:")); PrintBuf(port,DataLen,Data); } - port->print(F("\r\n")); + port->println(""); } //***************************************************************************** -void AddByteEscapedToBuf(unsigned char byteToAdd, byte &idx, unsigned char *buf, int &byteSum) +void AddByteEscapedToBuf(unsigned char byteToAdd, uint8_t &idx, unsigned char *buf, int &byteSum) { buf[idx++]=byteToAdd; byteSum+=byteToAdd; @@ -592,17 +595,17 @@ void AddByteEscapedToBuf(unsigned char byteToAdd, byte &idx, unsigned char *buf, //***************************************************************************** // Actisense Format: // <10><02><93>