mwx::stream

input-output stream

Upper-level class that handles input/output streams.

  • Interfaces to several classes (Serial, Wire, SPI, smplbuf) by polymorphism using CRTP (Curiously Recurring Template Pattern) method.

    • In CRTP, lower classes are defined as template class Derived : public stream<Derived>; and methods of lower classes are referenced from upper classes.

  • This class defines common processing such as print method, << operator, etc., and calls write() method, etc. implemented in lower classes, which is similar to using virtual functions.

Interfaces (implemented in lower classes)

Lower classes implement the functions listed below.

available()

int available()

// example
while(Serial.available()) {
  int c = Serial.read();
  // ... any
}

Returns 1 if the input exists, 0 if it does not.

The return value of this implementation is not the buffer length.

flush()

void flush()

// example
Serial.println("long long word .... ");
Serial.flush();

Flush output (wait for output to complete).

read()

int read()

// example
int c;
while (-1 != (c = read())) {
    // any
}

Inputs 1-byte data from the stream. If the data does not exist, return -1.

write()

size_t write(int c)

// example
Serial.write(0x30);

Outputs 1 byte to the stream.

vOutput()

static void vOutput(char out, void* vp)

This is a static function that produces a single byte output. Since this is not a class method, member variables and other information are not available. Instead, a pointer to the class instance is passed to vp, which is passed as a parameter.

This static function is used internally and the function pointer is passed as a one-byte output function of fctprintf(). This is used to implement the print method, etc.

Interface

putchar()

void mwx::stream::putchar(char c)

// example
Serial.putchar('A');
// result -> A

Output a single byte.

size_t print(T val, int base = DEC) // T: 整数型
size_t print(double val, int place = 2)
size_t print(const char*str)
size_t print(std::initializer_list<int>)

// example
Serial.print("the value is ");
Serial.print(123, DEC);
Serial.println(".");
// result -> the value is 123.

Serial.print(123.456, 1);
// result -> 123.5

Serial.print({ 0x12, 0x34, 0xab, 0xcd });
// will output 4byte of 0x12 0x34 0xab 0xcd in binary.

Performs various types of formatted output.

printfmt()

size_t printfmt(const char* format, ...);

// example 
Serial.printfmt("the value is %d.", 123);
// result -> the value is 123.

Prints output in printf format.

TWESDK/TWENET/current/src/printf/README.md 参照

operator <<

// examples
Serial << "this value is" // const char*
       << int(123)
       << '.';
       << mwx::crlf;
// result -> this value is 123.

Serial << fromat("this value is %d.", 123) << twe::crlf;
// result -> this value is 123.

Serial << mwx::flush; // flush here

Serial << bigendian(0x1234abcd);
// will output 4byte of 0x12 0x34 0xab 0xcd in binary.

Serial << int(0x30) // output 0x30=48, "48"
       << '/'
       << uint8_t(0x31); // output '1', not "48"
// result -> 48/1

smplbuf<char,16> buf = { 0x12, 0x34, 0xab, 0xcd };
Serail << but.to_stream();
// will output 4byte of 0x12 0x34 0xab 0xcd in binary.

Seiral << make_pair(buf.begin(), buf.end());
// will output 4byte of 0x12 0x34 0xab 0xcd in binary.

Serial << bytelist({ 0x12, 0x34, 0xab, 0xcd });
// will output 4byte of 0x12 0x34 0xab 0xcd in binary.

When outputting as a byte string, cast to uint8_t, uint16_t, uint32_t type. When outputting numerical values as strings, explicitly cast to int type.

Single-byte types are handled differently depending on the type name. Usually, use the size-conscious uint8_t[S] type.

set_timeout(), get_error_status(), clear_error_status()

uint8_t get_error_status()
void clear_error_status()
void set_timeout(uint8_t centisec)

// example
Serial.set_timeout(100); // 1000msのタイムアウトを設定
uint8_t c;
Serial >> c;

Manages input timeouts and errors using the >> operator.

The timeout period is specified by set_timeout(), and input is processed using the >> operator. If no input is received within the given timeout period, the error value can be read out with get_error_status(). The error status is cleared by clear_error_status().

Error Value

operator >>

inline D& operator >> (uint8_t& v)
inline D& operator >> (char_t& v)
template <int S> inline D& operator >> (uint8_t(&v)[S])
inline D& operator >> (uint16_t& v)
inline D& operator >> (uint32_t& v)
inline D& operator >> (mwx::null_stream&& p)

//// Example
uint8_t c;

the_twelite.stop_watchdog(); // stop watchdog
Serial.set_timeout(0xFF); // no timeout

// read out 1 byte
Serial >> c;
Serial << crlf << "char #1: [" << c << ']';

// skipping
Serial >> null_stream(3); // Read away 3 bytes
Serial << crlf << "char #2-4: skipped";

// Read 4 bytes (limited to fixed-length arrays of type uint8_t)
uint8_t buff[4];
Serial >> buff;
Serial << crlf << "char #5-8: [" << buff << "]";

Input processing.

  • Cannot be executed within setup().

  • Because it waits for polling, depending on the timeout time setting (e.g. no timeout), the watchdog timer may be triggered and reset.

Normally, the following readout is performed during loop().

void loop() {
  uint8_t c;
  while(Serial.available()) {
    Serial >> c;
    // Or c = Serial.read();
    
    switch(c) { ... }  // Branch processing according to the value of c
  }
}

The following is a list of types that can be read and stored.

最終更新