With the following code I get an error. Any suggestions?
Code:
#include <stdio.h>
#include "Arduino.h"
FILE serial_stdout;
// Function that printf and related will use to print
int serial_putchar(char c, FILE* f)
{
if (c == '\n') serial_putchar('\r', f);
return Serial.write(c) == 1? 0 : 1;
}
/**
* DebugMsg Print debug information
*
*/
void DebugMsg(const char *fmt, ...)
{
va_list ap;
printf ("%.2X%.2X%.1X%.1X%.2X", 8, 0, MPC_UNIT_ID, MyUnitId, cmd_Debug);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
void DebugInit()
{
// Setup stdout
fdev_setup_stream(&serial_stdout, serial_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &serial_stdout;
}
Error:
Debug.cpp:In function 'void DebugInit()'
Debug.cpp:72:58: error: '_FDEV_SETUP_WRITE' was not declared in this scope
Debug.cpp:72:75: error: 'fdev_setup_stream' was not declared in this scope
Error compiling
Solved
Changed Original code to:
#include <stdio.h>
#include <Arduino.h>
extern "C" {
__attribute__((weak))
int _write(int file, char *ptr, int len)
{
// STDIN: do nothing
if (file == 0)
;
// STDOUT/STDERR: Output to Serial
else if (file == 1 || file == 2)
Serial.write((uint8_t *)ptr, len);
// Otherwise assume file is actually a pointer to a Print object and output to that.
else
((class Print *)file)->write((uint8_t *)ptr, len);
return 0;
}
}
/**
* DebugMsg Print debug information
*
*/
void DebugMsg(const char *fmt, ...)
{
va_list ap;
printf ("%.2X%.2X%.1X%.1X%.2X", 8, 0, MPC_UNIT_ID, MyUnitId, cmd_Debug);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
void DebugInit()
{
// Setup stdout
}
Code:
#include <stdio.h>
#include "Arduino.h"
FILE serial_stdout;
// Function that printf and related will use to print
int serial_putchar(char c, FILE* f)
{
if (c == '\n') serial_putchar('\r', f);
return Serial.write(c) == 1? 0 : 1;
}
/**
* DebugMsg Print debug information
*
*/
void DebugMsg(const char *fmt, ...)
{
va_list ap;
printf ("%.2X%.2X%.1X%.1X%.2X", 8, 0, MPC_UNIT_ID, MyUnitId, cmd_Debug);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
void DebugInit()
{
// Setup stdout
fdev_setup_stream(&serial_stdout, serial_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &serial_stdout;
}
Error:
Debug.cpp:In function 'void DebugInit()'
Debug.cpp:72:58: error: '_FDEV_SETUP_WRITE' was not declared in this scope
Debug.cpp:72:75: error: 'fdev_setup_stream' was not declared in this scope
Error compiling
Solved
Changed Original code to:
#include <stdio.h>
#include <Arduino.h>
extern "C" {
__attribute__((weak))
int _write(int file, char *ptr, int len)
{
// STDIN: do nothing
if (file == 0)
;
// STDOUT/STDERR: Output to Serial
else if (file == 1 || file == 2)
Serial.write((uint8_t *)ptr, len);
// Otherwise assume file is actually a pointer to a Print object and output to that.
else
((class Print *)file)->write((uint8_t *)ptr, len);
return 0;
}
}
/**
* DebugMsg Print debug information
*
*/
void DebugMsg(const char *fmt, ...)
{
va_list ap;
printf ("%.2X%.2X%.1X%.1X%.2X", 8, 0, MPC_UNIT_ID, MyUnitId, cmd_Debug);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
void DebugInit()
{
// Setup stdout
}
Last edited: