This repository was archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArduino_AVRSTL.cpp
80 lines (70 loc) · 2.25 KB
/
Arduino_AVRSTL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Arduino_AVRSTL.h"
#include <Arduino.h>
//
// Configuration Help
//
// If you're using a serial port that's statically declared somewhere in
// Arduino (e.g. Serial1 on Leonardo)
// 1. Set ARDUINOSTL_SERIAL_DEVICE to your device
// 2. Uncomment the ARDUINOSTL_DEFAULT_CIN_COUT flag.
//
// If you're using a sofware serial port:
// 1. Set ARDUINOSTL_DEFAULT_SERIAL to NULL
// 2. Comment out ARDUINOSTL_DEFAULT_CIN_COUT
// Your sketch must contain delarations of cin and cout, and a call to
// ArduinoSTL_serial.connect().
//
#define ARDUINOSTL_DEFAULT_SERIAL Serial
#define ARDUINOSTL_DEFAULT_CIN_COUT
using namespace std;
#ifdef ARDUINOSTL_DEFAULT_CIN_COUT
// Create cout and cin.. there doesn't seem to be a way
// to control what serial device at runtime. Grr.
namespace std
{
ohserialstream cout(ARDUINOSTL_DEFAULT_SERIAL);
ihserialstream cin(ARDUINOSTL_DEFAULT_SERIAL);
}
#endif // ARDUINOSTL_DEFAULT_CIN_COUT
/*
* Implementation of printf() is highly libc dependent.
*
* This implementation is tested on:
*
* ARDUINO_ARCH_AVR (Classic Arduinos) - Working
* TEENSYDUINO (ARM-based Teensy) - cin/cout work, printf doesn't
* ARDUINO_ARCH_* - ARMs are probably the same as above.
*/
#if defined(ARDUINO_ARCH_AVR)
ArduinoSTL_STDIO ArduinoSTL_Serial(ARDUINOSTL_DEFAULT_SERIAL);
// arduino_putchar(char, FILE*)
// Output a single character to the serial port.
// returns: 0 on success, 1 on error
// note:
// To maintain serial port compatibility this function
// automatically addes a \r when it sees a \n
//
static int arduino_putchar(char c, FILE* f) {
Stream *uart = ArduinoSTL_Serial.getUart();
if (c == '\n') uart->write('\r');
return uart->write(c) == 1? 0 : 1;
}
// arduino_getchar(FILE*)
// Take a character from the serial port. This function
// must block until a character is ready.
// returns: The character or -1 on a read error
//
static int arduino_getchar(FILE *f) {
Stream *uart = ArduinoSTL_Serial.getUart();
while (! uart->available()) { /* wait */ }
return uart->read();
}
void ArduinoSTL_STDIO::connect(Stream *u) {
if (file != NULL)
free (file);
uart = u;
file = fdevopen(arduino_putchar, arduino_getchar);
}
#else
#warning "printf() will not be functional on this platform."
#endif