serial_c_iface.h File Reference
#include <Arduino.h>

Description

Provides a printf implementaton for C code running from inside an Arduino Sketch.

Author
Scott Fazackerley

IMPORTANT: This must be included after stdio.h to function properly.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1.Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3.Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file serial_c_iface.h.

Include dependency graph for serial_c_iface.h:
This graph shows which files directly or indirectly include this file:

Macros

#define ION_USE_INLINEPRINTF   0
 Setting this value to 1 will use an inline replacement for printf which reduces call stack requirements (minimal) but increase code footprint considerably for each use of printf. You decide if you want speed vs. minimal code space. More...
 
#define printf(format, ...)   serial_printf_c(format, ## __VA_ARGS__)
 

Functions

int serial_printf_c (const char *format,...)
 A version of printf that limits the number of characters printed per call. More...
 
int serial_print (const char *buffer)
 A print function wrapping Arduino's serial stream. More...
 
void serial_init (int baud_rate)
 Initializes serial port 0 for communications. More...
 
void serial_close ()
 Closes the communication port so that the pins can be used as general I/O. More...
 

Macro Definition Documentation

#define ION_USE_INLINEPRINTF   0

Setting this value to 1 will use an inline replacement for printf which reduces call stack requirements (minimal) but increase code footprint considerably for each use of printf. You decide if you want speed vs. minimal code space.

Definition at line 54 of file serial_c_iface.h.

#define printf (   format,
  ... 
)    serial_printf_c(format, ## __VA_ARGS__)

Definition at line 78 of file serial_c_iface.h.

Function Documentation

void serial_close ( )

Closes the communication port so that the pins can be used as general I/O.

Definition at line 83 of file serial_c_iface.cpp.

84  {
85  Serial.end();
86 }
void serial_init ( int  baud_rate)

Initializes serial port 0 for communications.

By default the port is set up at N-8-1.

Parameters
baud_rateThe buad rate to be used.

Definition at line 76 of file serial_c_iface.cpp.

78  {
79  Serial.begin(baud_rate);
80 }
int serial_print ( const char *  buffer)

A print function wrapping Arduino's serial stream.

Parameters
bufferPointer to the character array / buffer to print.
Returns
The number of characters outputted.

Definition at line 63 of file serial_c_iface.cpp.

65  {
66  int num;
67 
68  num = Serial.print(buffer);
69 #if DEBUG
70  Serial.flush();
71 #endif
72  return num;
73 }

Here is the caller graph for this function:

int serial_printf_c ( const char *  format,
  ... 
)

A version of printf that limits the number of characters printed per call.

When additional characters are requested for printing, the print fails.

Parameters
formatThe string that contains the text to be written to serial.
...Variable arguments of data used by the format.
Returns
The number of characters written. Returns a negative value on failure.

Definition at line 41 of file serial_c_iface.cpp.

44  {
45  va_list args;
46 
47  va_start(args, format);
48 
49  /* +1 for the null terminator \0 at the end */
50  int bufsize = vsnprintf(NULL, 0, format, args) + 1;
51  char buf[bufsize];
52 
53  va_end(args);
54 
55  va_start(args, format);
56  vsnprintf(buf, bufsize, format, args);
57  va_end(args);
58 
59  return serial_print(buf);
60 }
int serial_print(const char *buffer)
A print function wrapping Arduino&#39;s serial stream.

Here is the call graph for this function: