Scott Penrose

Teensy Console and Serial

Scott is an expert software developer with over 30 years experience, specialising in education, automation and remote data.

Teensy 3.1 console print with multiple outputs, fprintf style support for formatting and presentation of numbers in a single line and support for program memory strings.

#!C

// Examples

#define VERSION 100
void loop () {
  consolePrint("You can use a string");
  consolePrint(F("Or save memory and keep it in program space"));
  consolePrint(F("Print an integer=%d\n"), VERSION);
  consolePrint(F("The ISO8601 DateTime = %04d-%02d-%02dT%02d%:%02d:%02d\n"), 
    year(), month(), day(),
    hour(), minute(), second()
  );
}

// TODO - Support Floats (currently fprintf does not.

// ----------------------------------------------------------------------
// CONSOLE OUTPUT
void consolePrint(char* out, ...) {
	char printBuffer[BUFFER_SIZE];
	va_list argptr;
	va_start(argptr, out);
	vsnprintf(printBuffer, BUFFER_SIZE, out, argptr);
	va_end(argptr);
	if (! consolePassthrough) {
		Serial.print(printBuffer);    // Example output to two serial ports
		Serial3.print(printBuffer);
	}
}

// Convert FLASH String to String
void consolePrint(const __FlashStringHelper *out, ... ){
	char printBuffer[BUFFER_SIZE];
	va_list argptr;
	va_start(argptr, out);
	vsnprintf(printBuffer, BUFFER_SIZE, (const char *)out, argptr);
	va_end(argptr);
	consolePrint(printBuffer);
}

// Convert STRING to Char
void consolePrint(String& out, ...) {
	char strBuffer[BUFFER_SIZE];
	char printBuffer[BUFFER_SIZE];
	out.toCharArray(strBuffer, BUFFER_SIZE);

	va_list argptr;
	va_start(argptr, out);
	vsnprintf(printBuffer, BUFFER_SIZE, strBuffer, argptr);
	va_end(argptr);

	consolePrint(printBuffer, argptr);
}

  • Arduino
  • Teensy
  • Serial