SofaPirate / AsciiMassage

ASCII implementation of the Massage API Arduino Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AsciiMassage

AsciiMassage allows the sending and receiving of messages. More precisely, it is a microcontroller massage packer and parser for the ASCII format for any type of stream (Serial, UDP, etc). A massage message always starts with an address string and is followed by a user defined number of elements (bytes, ints, longs, floats or strings). The address string is used to route the message.

This is the ASCII implementation of the Massage API.

GITHUB PAGE

https://github.com/SofaPirate/AsciiMassage

DOCUMENTATION

The complete class documentation can be found in the "docs" folder or online here.

Receiving (parsing) a massage directly in loop()

Add the library to the top of your code and instantiate an AsciiMassageParser called "inbound":

#include <AsciiMassageParser.h>
AsciiMassageParser inbound;

Inside loop() parse the Serial stream with parse(). If parse() returns true, the massage is completed and ready.

if ( inbound.parseStream( &Serial ) ) {
 // parse completed massage elements here.
}

This example parses the elements of a massage that starts with the address "value" and that contains one long followed by one int:

// Does the massage's address match "value"?
if ( inbound.fullMatch ("value") ) {
    // Get the first long.
    long ms = inbound.nextLong();
    // Get the next int.
    int an0 = inbound.nextInt();
}

The complete block of code is as follows:

#include <AsciiMassageParser.h>
AsciiMassageParser inbound;

// [...]

void loop() {

   if ( inbound.parseStream( &Serial ) ) {
      // parse completed massage elements here.

      // Does the massage's address match "value"?
      if ( inbound.fullMatch ("value") ) {
          // Get the first long.
          long ms = inbound.nextLong();
          // Get the next int.
          int an0 = inbound.nextInt();
      }

   }

    // [...]
}

Receiving (parsing) a massage with a receive function

Add the library to the top of your code and instantiate an AsciiMassageParser called "inbound":

#include <AsciiMassageParser.h>
AsciiMassageParser inbound;

Add before loop() a function the will be called when a message is received

void readMessage() {
   // parse completed massage elements here.
}

Inside loop() parse the Serial stream with parse() and run "readMessage" when a message is received

inbound.parseStream( &Serial , readMessage );

The complete block of code is as follows:

#include <AsciiMassageParser.h>
AsciiMassageParser inbound;

// [...]

void readMessage() {
    // parse completed massage elements here.

    // Does the massage's address match "value"?
    if ( inbound.fullMatch ("value") ) {
        // Get the first long.
        long ms = inbound.nextLong();
        // Get the next int.
        int an0 = inbound.nextInt();
    }

}

void loop() {

inbound.parseStream( &Serial , readMessage );

    // [...]
}

Sending (packing) a massage example

Add the library to the top of your code and instantiate an AsciiMassagePacker called "outbound":

#include <AsciiMassagePacker.h>
AsciiMassagePacker outbound;

Pack a massage and then steam it through Serial:

outbound.beginPacket("value"); // Start a packet with the address called "value".
outbound.addLong( millis() ); // Add the milliseconds.
outbound.addInt( analogRead(0) ); // Add a reading of analog 0.
outbound.streamPacket(&Serial); // End the packet and stream it.

About

ASCII implementation of the Massage API Arduino Library

License:MIT License


Languages

Language:JavaScript 82.4%Language:Max 14.3%Language:C++ 1.9%Language:Processing 1.3%Language:C 0.1%Language:HTML 0.0%