Skip to content

Commit

Permalink
Add N2kStream interface for reading- and writing serial data. On Ardu…
Browse files Browse the repository at this point in the history
…ino this defaults to Stream.
  • Loading branch information
denravonska committed Dec 6, 2016
1 parent c667544 commit 4963adf
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
24 changes: 24 additions & 0 deletions N2kStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "N2kStream.h"
#include <string.h>
#include <stdlib.h>

#ifdef ARDUINO
// Arduino uses its own implementation.
#else
size_t N2kStream::print(const char *str) {
if(str == 0)
return 0;

return write(reinterpret_cast<const uint8_t*>(str), strlen(str));
}

size_t N2kStream::print(int val) {
char buf[16];
return write(itoa(buf, val, 10), strlen(buf));
}

size_t N2kStream::println(const char *str) {
return print(str) + print("\r\n");
}

#endif
61 changes: 61 additions & 0 deletions N2kStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
N2kStream.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.
I/O stream used in the NMEA2000 libraries.
*/

#ifndef _tN2kStream_H_
#define _tN2kStream_H_

#include <stdint.h>
#include <stddef.h>

#ifdef ARDUINO
// Arduino users get away with using the standard Stream class and its
// subclasses. Forward declare the Stream class here and include Arduino.h in
// the application.
#include <Arduino.h>
typedef Stream N2kStream;
#else
// Non Arduino platforms need to implement this themselves if they want to use
// functions which operate on streams.
class N2kStream {
public:
// Returns first byte if incoming data, or -1 on no available data.
virtual int read() = 0;

// Write data to stream.
virtual size_t write(const uint8_t* data, size_t size) = 0;

// Print string to stream.
size_t print(const char* str);

// Print value to stream.
size_t print(int val);

// Print string and newline to stream.
size_t println(const char *str);
};
#endif

#endif

0 comments on commit 4963adf

Please sign in to comment.