/* messageTask.cpp -- example message receive task posted from loop()
 *  In the context of the SCHIPCODE additions to HardwareSerial
 *  any char may be used as a message terminator, and two such chars
 *  can be used to frame messages if some fancy footwork is employed.
 *
 *  For the simplest usage the message terminator is set to a '\n'.
 *  A check-for-new-message is done in the main loop() which posts
 *  messageTask() when a terminator is found in the input stream.
 *  The arduino parseInt(), etal functions can be used to convert
 *  numeric values from this stream if one is careful to NOT read
 *  too far ahead -- if the parse functions don't find an end to
 *  numeric input they timeout for a default 1000ms which will
 *  booger up anything that really needs to run.
 *
 *  Just as a reminder because I always forget or get it backwards:
 *		newline or linefeed -- '\n' -- 0x0A -- 10dec -- <cntl>J
 *		carriage return     -- '\r' -- 0x0D -- 13dec -- <cntl>M
 *	 UNIX uses just the '\n' but DOgS uses both '\r\n' as does
 *	 Arduino println().
 */

/*  Example messageTask() implementation.
 *  Note that the Argweenie system is not able to treat this as an
 *  actual library function which can be replaced with use code, so
 *  this whole thing is commented out to avoid double entendre's.
 *  Thus one MUST include a messageTask() function in the app code.
 *
 * Read, convert, and echo what we got.
 *  This simple function reads a single int value and consumes whatever
 *  after to the terminator without comment.
 *
 * The parseNNT() functions skip non-numeric chars at the head of the
 *  queue and return their value after peeking but not consuming the
 *  first trailing non-number char.
 * Importantly, the terminating LF--'\n' (and any preceeding CR--'\r')
 *  is still there to be read when the last parseNNT() returns a value.
 * This means that a known number of whitespace or comma delimited decimal
 *  values can be read without timing out if we consume the '\n' at the end
 *  of the buffer. However receiving a variable number of values or sending
 *  non-numeric data instead of numbers requires some look ahead to see if
 *  we've reached the end of the line.  This is an exercise for the reader.
 *
 *  Also note that the Serial.println() method adds both CR & LF chars
 *   in that order...so our messages can be symmetric if the reader here
 *   can deal with any left over chars from the previous message
 *   (which the parseNNT() functions do).
 *
 * @param nmsg -- number of message terminators in the input queue
 */
#include "Plot.h"	// get some defines and utilities

void messageTask( unsigned int nmsg )
{
	// unsigned long mytime = millis();	// in case we want to time this

    // EXAMPLE to just see how many chars are available
	// int num = Serial.available();

/**
    // EXAMPLE to read the whole message string including '\n' terminator
    //  note that read... does NOT put a null string term in the buffer
    char buffer[21];
    int num = Serial.readBytesUntil('\n', buffer, 20);
    buffer[num] = '\0';    // string terminator
    Serial.print( "got: <" );
    Serial.print( buffer );
    Serial.println( ">" );
**/

/**/
	// EXAMPLE to just read an INT value and get on with it
	//  if one knows how many ints to expect one can call parseInt() more...
	int val = Serial.parseInt();
	Serial.print( "got: " );
	Serial.println( val );
	do
	{
		// consume anything up to the terminating '\n'
		val = Serial.read();

	} while( (val != '\n') && (val != -1) );
/**/

	// in case we want to time this
	// mytime = millis() - mytime;
	// Serial.print( "time: ");
	// Serial.println( mytime );

	return;
}
