This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

get_stream_helper()

Helper object for using mwx::stream
    Use operators and methods provided by mwx::stream via the stream_helper that refers to a uint8_t smplbuf array.
    smplbuf_u8<32> b;
    auto&& bs = b.get_stream_helper(); // helper object
    
    // Generate data sequence
    uint8_t FOURCHARS[]={'A', 'B', 'C', 'D'};
    bs << FOURCHARS;
    bs << ';';
    bs << uint32_t(0x30313233); // "0123"
    bs << format(";%d", 99);
    
    Serial << b << crlf; // output to Serial via smplbuf_u8<32> class
    
    // Result: ABCD;0123;99
    

    Since the type name of the helper object can be long, it is resolved using auto&&. You can use interfaces defined by mwx::stream, such as the << operator, with this object.

    The created helper object bs starts reading and writing from the head of the base array b. If at the end of the array, data is appended using append(). The position advances with each read/write operation.

    The helper function supports the >> operator for reading.

    // ...continued from the above example
    // ABCD;0123;99 <- stored in b
    
    // Variables to store read data
    uint8_t FOURCHARS_READ[4];
    uint32_t u32val_read;
    uint8_t c_read[2];
    
    // Read using >> operator
    bs.rewind();                // rewind position to the start
    bs >> FOURCHARS_READ;      // 4 characters
    bs >> mwx::null_stream(1); // skip 1 character
    bs >> u32val_read;         // 32-bit data
    bs >> mwx::null_stream(1); // skip 1 character
    bs >> c_read;              // 2 characters
    
    // Display results
    Serial << crlf << "4chars=" << FOURCHARS_READ;
    Serial << crlf << format("32bit val=0x%08x", u32val_read);
    Serial << crlf << "2chars=" << c_read;
    
    // 4chars=ABCD
    // 32bit val=0x30313233
    // 2chars=99