Skip to content

Instantly share code, notes, and snippets.

@triffid
Created May 4, 2020 12:32
Show Gist options
  • Save triffid/1383256f982792b319b4925d0d2340b9 to your computer and use it in GitHub Desktop.
Save triffid/1383256f982792b319b4925d0d2340b9 to your computer and use it in GitHub Desktop.
make printf work (arduino)
// from https://forum.arduino.cc/index.php?topic=149785.msg1125620#msg1125620
#include "fix-printf.h"
#include <stdlib.h>
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Function that printf and related will use to print
static int serial_putchar(char c, FILE *f)
{
(void)(f);
Serial.write(c);
return 1;
}
// Function that scanf and related will use to read
static int serial_getchar(FILE *)
{
// Wait until character is avilable
while(Serial.available() <= 0) {}
return Serial.read();
}
extern "C" {
#include <stdio.h>
void stdio_init()
{
static FILE serial_stdinout = { .buf = NULL, .unget = 0, .flags = _FDEV_SETUP_RW, .size = 0, .len = 0, .put = serial_putchar, .get = serial_getchar, .udata = 0 };
// Set up stdout and stdin
stdout = &serial_stdinout;
stdin = &serial_stdinout;
stderr = &serial_stdinout;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment