Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portability fixes #35

Merged
merged 25 commits into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
74e3a86
Remove the dependency to Serial. The sub class can assign this pointe…
denravonska Dec 1, 2016
7af7236
Add missing headers.
denravonska Dec 1, 2016
2afabed
Replace Arduino binary definitions with BIT(n) macro which uses bit s…
denravonska Dec 1, 2016
6c191b4
Replace byte with uint8_t.
denravonska Dec 1, 2016
69053d5
Add missing header for memcpy.
denravonska Dec 2, 2016
9140aa9
Add header for platform abstraction.
denravonska Dec 6, 2016
41bc6d7
Add N2kStream interface for reading- and writing serial data. On Ardu…
denravonska Dec 6, 2016
29faf2d
Replace byte with uint8_t.
denravonska Dec 1, 2016
4f91fbf
Add missing header for memcpy.
denravonska Dec 2, 2016
c667544
Add header for platform abstraction.
denravonska Dec 6, 2016
4963adf
Add N2kStream interface for reading- and writing serial data. On Ardu…
denravonska Dec 6, 2016
75a441e
Switch to using N2kStream and the constant data macros.
denravonska Dec 6, 2016
90b2861
Merge branch 'portability-fixes' of github.com:denravonska/NMEA2000 i…
denravonska Dec 6, 2016
6cf0e29
Fix for constant string storage on Arduinos.
denravonska Dec 6, 2016
c5f148a
Fix N2kStream for AVR targets that aren't Arduinos.
denravonska Dec 7, 2016
bd6b521
Fix N2kStream for AVR targets that aren't Arduinos.
denravonska Dec 7, 2016
5fd9923
Merge branch 'portability-fixes' of github.com:denravonska/NMEA2000 i…
denravonska Dec 7, 2016
97a80e8
Remove stray error.
denravonska Dec 7, 2016
17c5966
Stray away from itoa as it's not available on all compilers.
denravonska Dec 7, 2016
7c754d6
Remove Serial object as it should be set by the application.
denravonska Dec 7, 2016
7cb08f0
Add println for flash stored strings and convert F() calls to CSTR().
denravonska Dec 7, 2016
2728972
Fix incorrect delay signature.
denravonska Dec 7, 2016
f2ff8ee
Switch the way constant strings are handled.
denravonska Dec 7, 2016
1e56216
Add round() implementation for non-Arduino targets.
denravonska Dec 8, 2016
e6509bb
Move additional strings to flash.
denravonska Dec 8, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions ActisenseReader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
ActisenseReader.cpp

Copyright (c) 2015-2016 Timo Lappalainen, Kave Oy, www.kave.fi
Expand All @@ -20,10 +20,11 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIO
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This is class for reading Actisense format messages from given stream.
*/
#include "ActisenseReader.h"
#include <string.h>

//*****************************************************************************
tActisenseReader::tActisenseReader() {
Expand All @@ -43,10 +44,10 @@ void tActisenseReader::ClearBuffer() {
//*****************************************************************************
bool tActisenseReader::AddByteToBuffer(char NewByte) {
if (MsgWritePos>=MAX_STREAM_MSG_BUF_LEN) return false;

MsgBuf[MsgWritePos]=NewByte;
MsgWritePos++;
if ( MsgBuf[1]+3!=MsgWritePos ) byteSum+=NewByte; // !Do not add CRC to byteSum
if ( MsgBuf[1]+3!=MsgWritePos ) byteSum+=NewByte; // !Do not add CRC to byteSum
return true;
}

Expand All @@ -57,14 +58,14 @@ bool tActisenseReader::AddByteToBuffer(char NewByte) {

//*****************************************************************************
bool tActisenseReader::CheckMessage(tN2kMsg &N2kMsg) {

N2kMsg.Clear();

if (MsgWritePos!=MsgBuf[1]+3) {
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
}
Expand All @@ -80,24 +81,25 @@ bool tActisenseReader::CheckMessage(tN2kMsg &N2kMsg) {
memcpy(&(N2kMsg.MsgTime), &(MsgBuf[8]), 4);
N2kMsg.DataLen=MsgBuf[12];
for (int i=13, j=0; i<MsgWritePos-1; i++, j++) N2kMsg.Data[j]=MsgBuf[i];

return true;
}

//*****************************************************************************
// Read Actisense formatted NMEA2000 message from stream
// Read Actisense formatted NMEA2000 message from stream
// Actisense Format:
// <10><02><93><length (1)><priority (1)><PGN (3)><destination (1)><source (1)><time (4)><len (1)><data (len)><CRC (1)><10><03>
bool tActisenseReader::GetMessageFromStream(tN2kMsg &N2kMsg) {
bool result=false;

if (ReadStream==0) return false;

while (ReadStream->available() > 0 && !result) {
int NewByte=ReadStream->read();
if (ReadStream==0)
return false;

int NewByte;
while ((NewByte = ReadStream->read()) != -1 && !result) {
// Serial.println((char)NewByte,HEX);
if (MsgIsComing) {
if (EscapeReceived) {
if (EscapeReceived) {
switch (NewByte) {
case Escape: // Escaped Escape
EscapeReceived=false;
Expand All @@ -120,7 +122,7 @@ bool tActisenseReader::GetMessageFromStream(tN2kMsg &N2kMsg) {
default: // Error
ClearBuffer();
}
} else {
} else {
if (NewByte==Escape) {
EscapeReceived=true;
} else {
Expand All @@ -146,14 +148,14 @@ bool tActisenseReader::GetMessageFromStream(tN2kMsg &N2kMsg) {
EscapeReceived=(NewByte==Escape);
}
}

return result;
}

//*****************************************************************************
void tActisenseReader::ParseMessages() {
tN2kMsg N2kMsg;

while (GetMessageFromStream(N2kMsg)) {
if (MsgHandler!=0) MsgHandler(N2kMsg);
}
Expand Down
20 changes: 11 additions & 9 deletions ActisenseReader.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
ActisenseReader.h

Copyright (c) 2015-2016 Timo Lappalainen, Kave Oy, www.kave.fi
Expand All @@ -20,7 +20,7 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIO
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This is class for reading Actisense format messages from given stream.

There is unresolved problem to use programming port with reading data.
Expand All @@ -29,7 +29,9 @@ USB port there is no problem.
*/
#ifndef _ACTISENSE_READER_H_
#define _ACTISENSE_READER_H_

#include "N2kMsg.h"
#include "N2kStream.h"

class tActisenseReader
{
Expand All @@ -42,26 +44,26 @@ class tActisenseReader
// Buffer for incoming messages from stream
unsigned char MsgBuf[MAX_STREAM_MSG_BUF_LEN];
int MsgWritePos;

protected:
Stream* ReadStream;
N2kStream* ReadStream;
// Handler callback
void (*MsgHandler)(const tN2kMsg &N2kMsg);

protected:
bool AddByteToBuffer(char NewByte);
void ClearBuffer();
bool CheckMessage(tN2kMsg &N2kMsg);

public:
tActisenseReader();
// Set stream, which would be used for reading messages. You have to
// open stream first, so e.g. for SerialUSB call begin first.
void SetReadStream(Stream* _stream) { ReadStream=_stream; }
void SetReadStream(N2kStream* _stream) { ReadStream=_stream; }

// You can either call this or ParseMessages periodically.
bool GetMessageFromStream(tN2kMsg &N2kMsg);

// Set message handler with SetMsgHandler and then call this periodically or use GetMessageFromStream
void ParseMessages();
// Set message handler to be used in ParseMessages, when message has been received.
Expand Down
61 changes: 61 additions & 0 deletions N2kDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
N2kDef.h

Copyright (c) 2015-2016 Timo Lappalainen, Kave Oy, www.kave.fi

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Type definitions and utility macros used in the NMEA2000 libraries.

*/

#ifndef _tN2kDef_H_

#include <stdint.h>

extern "C" {
// Application execution delay. Must be implemented by application.
extern void delay(uint32_t ms);

// Current uptime in milliseconds. Must be implemented by application.
extern uint32_t millis();
}

// Declare PROGMEM macros to nothing on non-AVR targets.
#if !defined(__AVR__)
#define PROGMEM
#define pgm_read_byte(var) *var
#define pgm_read_word(var) *var
#define pgm_read_dword(var) *var
#endif

// Definition for the F(str) macro. On Arduinos use what the framework
// provides to utilize the Stream class. On standard AVR8 we declare
// our own helper class which is handled by the N2kStream. On anything
// else we resort to char strings.
#if defined(ARDUINO)
#include <WString.h>
#elif defined(__AVR__)
#include <avr/pgmspace.h>
class __FlashStringHelper;
#define F(str) (reinterpret_cast<const __FlashStringHelper*>(PSTR(str)))
#else
#define F(str) str
#endif

#endif
59 changes: 30 additions & 29 deletions N2kMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "N2kMessages.h"
#include <string.h>

//*****************************************************************************
// System time
Expand Down Expand Up @@ -219,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

Expand Down
21 changes: 11 additions & 10 deletions N2kMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ NMEA2000.h
#define _N2kMessages_H_

#include "N2kMsg.h"
#include <stdint.h>

inline double RadToDeg(double v) { return v*180.0/3.1415926535897932384626433832795; }
inline double DegToRad(double v) { return v/180.0*3.1415926535897932384626433832795; }
Expand Down Expand Up @@ -502,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);
}

Expand All @@ -524,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;
}
Expand Down
10 changes: 5 additions & 5 deletions N2kMessagesEnumToStr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
N2kMessagesEnumToStr.h

Copyright (c) 2015-2016 Timo Lappalainen, Kave Oy, www.kave.fi
Expand All @@ -19,12 +19,12 @@ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIG
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This is collection of functions for handling NMEA2000 bus messages.

This is collection of functions for handling NMEA2000 bus messages.
Library contains functions to convert enums in N2kMessages.h to
const char *

If you do not need enums in clear text, you do not need this library.
If you do not need enums in clear text, you do not need this library.
*/

#ifndef _N2kMessagesEnumToStr_H_
Expand All @@ -39,7 +39,7 @@ template<typename T> void PrintN2kEnumType(T a, Stream *OutputStream, bool addLF
if (str[0] != '\0') {
if (addLF) { OutputStream->println(str); } else { OutputStream->print(str); }
} else {
OutputStream->print("unknown ("); OutputStream->print(a); OutputStream->println(")");
OutputStream->print(F("unknown (")); OutputStream->print(a); OutputStream->println(F(")"));
}
}

Expand Down
Loading