Giter VIP home page Giter VIP logo

packet-io's Introduction

PacketIO documentation

A PlatformIO library for framing packets sent or received over an arduino Stream, such as Serial.

The framing methods are exposed through the PacketPrint and PacketStream interfaces.

  • PacketPrint - extends Print:
    • bool end() - end a packet, returning whether it was sucessfull
    • void abort() - try as best as possible to start a new packet
  • PacketStream - extends Stream:
    • read(), peek() - return PacketStream::EOP (-2) if the packet is complete (without touching the underlying stream), otherwise behaves as normal
    • next() - stop reading from this packet, and move on

Framing Protocols

Implemented so far

  • COBS
  • SLIPS
  • COBS-R
  • Length-prefixed

Examples

Writing

// pick a protocol here - everything below doesn't need to change
COBSPrint cobs_out(Serial);

PacketPrint& out = cobs_out;

out.print("Hello ");
out.print("World");
out.end(); // mark the end of a packet

out.print("Goodbye ");
if(something_went_horribly_wrong) {
	out.abort(); // terminate this packet early and try to mark what was sent as bad
}
else {
	out.print("World");
	out.end();
}

Reading

// pick a protocol here - everything below doesn't need to change
COBSStream cobs_in(Serial);

PacketStream& in = cobs_in;

while(true) {
	char message[10];
	size_t n = 0;

	while(true) {
		// read until we get something
		int c = in.read();
		if(c == in::EOF) continue;

		// detect End Of Packet
		if(c == in::EOP) break;

		// save anything else
		message[n++] = c;
	}

	Serial.print("Got: ");
	Serial.write(message, n);
	Serial.println();

	// start reading the next message
	in.next();
}

Future work

Provide an interface to nanopb, that converts Print into pb_ostream_s and Stream into pb_istream_s, to allow protobufs to be framed.

packet-io's People

Contributors

eric-wieser avatar per1234 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

packet-io's Issues

Support for merged PacketPrint and PacketStream

Hi

It is possible to have one single object that support both the functionalities of PacketPrint and PacketStream?
It would be useful to have a single abstraction that handle both sending and receiving COBS encoded data

Thanks,
Luca

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.