Skip to content

Add printf to source files #111

Description

@blhough

printf doesn't work by default for some reason. You must modify the source code included in the teensy library.

follow instructions here:
http://playground.arduino.cc/Main/Printf#sourceblock1

Summary:
In directory: {installdir)/hardware/teensy/cores/teensy3/

comment out lines 87-107 in Print.cpp
image

comment out lines 98 and 99 in Print.h
paste this code below

#include <stdarg.h>
#define PRINTF_BUF 200 // define the tmp buffer size (change if desired)
int printf(const char *format, ...)
{
	char buf[PRINTF_BUF];
	va_list ap;
	va_start(ap, format);
	int  n = vsnprintf(buf, sizeof(buf), format, ap);
	for (char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines
	{
		if (*p == '\n')
			write('\r');
		write(*p);
	}
	va_end(ap);
	return n;
}
#ifdef F // check to see if F() macro is available
int printf(const __FlashStringHelper *format, ...)
{
	char buf[PRINTF_BUF];
	va_list ap;
	va_start(ap, format);
#ifdef __AVR__k
	int n = vsnprintf_P(buf, sizeof(buf), (const char *)format, ap); // progmem for AVR
#else
	int n = vsnprintf(buf, sizeof(buf), (const char *)format, ap); // for the rest of the world
#endif
	for (char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines
	{
		if (*p == '\n')
			write('\r');
		write(*p);
	}
	va_end(ap);
	return n;
}
#endif

image

I don't know an easy way to make this part of our project code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions