/*
 * This method parses a incoming string (the message is expected to be complete)
 * Depending on the instruction the action is undertaken.
 */
void SerialCommunicator::setReceivedMessage(const char* newMessage)
{
	if (strcmp_P(newMessage, DUMP) == 0)
	{
		dumpAllFields();
	} else if (strncmp_P(newMessage, GET, 4) == 0)
	{
		SerialOutput.println((__FlashStringHelper *) GET);
		FieldData *fieldData = FieldData::findField(newMessage + 4);
		if (fieldData != 0) fieldData->dump();
	} else if (strncmp_P(newMessage, SET, 4) == 0)
	{
		SerialOutput.println((__FlashStringHelper *) SET);
		FieldData *fp = FieldData::findField(newMessage + 4);
		if (fp != 0)
		{
			fp->setValue(newMessage + 4 + strlen_P((const char *) fp->myClassName) + strlen_P((const char *) fp->myFieldName) + 2);
			fp->dump();
		}
	} else if (strncmp_P(newMessage, SET, 3) == 0)
	{
		SerialOutput.println((__FlashStringHelper *) SET);
		FieldData::visitAllFields(dumpWritableData, true);
	} else if (strcmp_P(newMessage, LOG_HEADER) == 0)
	{
		SerialOutput.print((__FlashStringHelper *) LOG_HEADER);
		SerialOutput.print(FIELDSEPERATOR);
		FieldData::visitAllFields(logHeaderVisitor, true);
		SerialOutput.println();
		return;
	} else if (strcmp_P(newMessage, LOG_VALUE) == 0)
	{
		logValue();
		return;
//#ifdef I_USE_RESET
//	} else if (strcmp_P(newMessage, RESET) == 0)
//	{
//		ForceHardReset();
//#endif
	} else
	{
		dumpCommands();
		if ('?' != newMessage[0])
		{
			SerialOutput.print((__FlashStringHelper *) ERROR);
			//Even though it is handy to see what has been send
			//The yun bootloader sends press ard to stop bootloader
			//echoing this means the bootloader receives ard
			//SerialOutput.println(newMessage);
		}
		return;
	}
	SerialOutput.println((__FlashStringHelper *) DONE);
}
static void dumpVisitor(FieldData& fieldData)
{
	/*
	 * If you get an error here about wdt_reset not defined and you are building for teensy
	 * add the following to .../Arduino/hardware/teensy/avr/cores/teensyXX/avr/wdt.h
	 * where XX is your teensy version
	 *
	 * #ifndef __WDT_H__
   * #define __WDT_H__
   * void wdt_reset()
   * {
   * #warning "wdt_reset is not implemented"
   * }
   * #endif
   *
	 */
	wdt_reset(); //make sure the watch dog does not trigger
	fieldData.dump();
}