Here's some quickie off-the-cuff code that implements `stdin` reading in a non-blocking way. There's a bunch of ways to improve this code, for example, checking for `stderr` or `stdout` being an error, handling zero-length requests, using `errno`, etc. There's also a way to do this in a blocking manner. In any case, this should get you started:
Code:
#includes <exercise-for-the-reader>
// If this is in a C++ file, wrap in `extern "C"`
// https://forum.pjrc.com/threads/27827-Float-in-sscanf-on-Teensy-3-1
// This link shows how to enable float scanning.
int _read(int file, void *buf, size_t len) {
Stream *in;
if (file == stdin->_file) { // TODO: Check for output-only files and do an error
in = &Serial;
} else {
in = (Stream *)file;
}
// Non-blocking input
// Optional: blocking input, or some function call that lets you know which
int avail = in->available();
if (avail <= 0) {
return 0;
}
size_t toRead = avail;
if (toRead > len) {
toRead = len;
}
return in->readBytes((char *)buf, toRead);
}
Hopefully that gets you started.