Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

HardwareSerial and general documentation issues #6381

Open
Labels
Component: DocumentationRelated to Arduino's documentation content feature requestA request to make an enhancement (not a bug fix)
@rkost

Description

So I just took a look into the core libraries and really wondered why there are no doxygen comments at all. That may be fine if there would be a documentation elsewhere, but as I searched for Stream::peek(), the official docs are saying "Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer." the code is virtual int read() = 0; and in the HardwareSerial I was able to find a buffer that was defined like unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];.

So why is Stream returning int at all?

It would be that helpful if there where a strict rule for documentation inside the code. Best example: https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.h
There are comments one line before a definition, there are inline comments and even comments after the function definition.

In my eyes a nice documented code would look like it follows. And yes. Of cause it will add an enormous amount of lines but it will make live much easier. You can autocreate a documentation for the whole code and you do not need to dive into implementation to look what is being returned in special cases.

/*
 Stream.h - base class for character-based streams.
 Copyright (c) 2010 David A. Mellis. All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

 parsing functions based on TextFinder library by Michael Margolis
*/
#ifndef Stream_h
#define Stream_h
#include <inttypes.h>
#include "Print.h"
// compatability macros for testing
/*
#define getInt() parseInt()
#define getInt(ignore) parseInt(ignore)
#define getFloat() parseFloat()
#define getFloat(ignore) parseFloat(ignore)
#define getString( pre_string, post_string, buffer, length)
readBytesBetween( pre_string, terminator, buffer, length)
*/
/**
 * @brief This enumeration provides the lookahead options for parseInt(), parseFloat()
 * The rules set out here are used until either the first valid character is found
 * or a time out occurs due to lack of input.
 */
enum LookaheadMode{
 /// All invalid characters are ignored.
 SKIP_ALL,
 /// Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
 SKIP_NONE,
 /// Only tabs, spaces, line feeds & carriage returns are skipped.
 SKIP_WHITESPACE
};
/// a char not found in a valid ASCII numeric field
#define NO_IGNORE_CHAR '\x01'
/**
 * @brief The Stream class.
 * For handling character streams.
 */
class Stream : public Print
{
public:
 /**
 * @brief Determines whether there is any character inside the stream's buffer.
 * @return The current size of the buffer.
 */
 virtual int available() = 0;
 /**
 * @brief Reads the oldest int from the buffer and deleting it afterwards.
 * @return The oldest int from this stream.
 */
 virtual int read() = 0;
 /**
 * @brief Reads the oldest int from the buffer WITHOUT deleting it.
 * TODO: This should be const, shouldn't it? It should not make any changes to the stream.
 * @return The oldest int from this stream.
 */
 virtual int peek() = 0;
 /**
 * @brief Default constructor
 * Setting timeout to 1000ms by default.
 */
 Stream() {_timeout=1000;}
// parsing methods
 /**
 * @brief sets maximum milliseconds to wait for stream data
 * @param timeout The time to wait for stream data.
 */
 void setTimeout(unsigned long timeout);
 /**
 * @brief gets the maximum milliseconds to wait for stream data
 * @return The time to wait for stream data.
 */
 unsigned long getTimeout(void) { return _timeout; }
 /**
 * @brief reads data from the stream until the target string is found.
 * TODO: Does this make any changes to the stream's buffer?
 * @param target The string to search for.
 * @return true if target string is found, false if timed out (see setTimeout)
 */
 bool find(char *target);
 /**
 * @brief reads data from the stream until the target string is found.
 * TODO: Does this make any changes to the stream's buffer?
 * @param target The string to search for.
 * @return true if target string is found, false if timed out (see setTimeout)
 */
 bool find(uint8_t *target) { return find ((char *)target); }
 /**
 * @brief reads data from the stream until the target string of given length is found.
 * @param target The string to search for.
 * @param length The length of the given target. Target will be cut off if the string is longer than 'length'
 * @return true if target string is found, false if timed out
 */
 bool find(char *target, size_t length);
 /**
 * @brief reads data from the stream until the target string of given length is found.
 * @param target The string to search for.
 * @param length The length of the given target. Target will be cut off if the string is longer than 'length'
 * @return true if target string is found, false if timed out
 */
 bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
 /**
 * @brief reads data from the stream until the target character is found.
 * @param target The character to search for.
 * @return true if the target character was found, false if timed out.
 */
 bool find(char target) { return find (&target, 1); }
 /**
 * @brief reads data from the stream until the target string was found.
 * Search will be terminated if the given terminator was found.
 * @param target The string to search for
 * @param terminator The string that causes the search to stop if found.
 * @return true if the target was found, false if the terminator was found first or if timed out.
 */
 bool findUntil(char *target, char *terminator);
 /**
 * @brief reads data from the stream until the target string was found.
 * Search will be terminated if the given terminator was found.
 * @param target The string to search for
 * @param terminator The string that causes the search to stop if found.
 * @return true if the target was found, false if the terminator was found first or if timed out.
 */
 bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
 /**
 * @brief reads data from the stream until the target string of the given targetLen length was found.
 * Search will be terminated if the given terminator of the given termLen length was found.
 * @param target The string to search for
 * @param targetLen The length of the given target. Target will be cut off if the string is longer than 'targetLen'
 * @param terminator The string that causes the search to stop if found.
 * @param termLen The length of the given terminator string.
 * @return true if the target was found, false if the terminator was found first or if timed out.
 */
 bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen);
 /**
 * @brief reads data from the stream until the target string of the given targetLen length was found.
 * Search will be terminated if the given terminator of the given termLen length was found.
 * @param target The string to search for
 * @param targetLen The length of the given target. Target will be cut off if the string is longer than 'targetLen'
 * @param terminator The string that causes the search to stop if found.
 * @param termLen The length of the given terminator string.
 * @return true if the target was found, false if the terminator was found first or if timed out.
 */
 bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
 /**
 * @brief Something useful here
 * Lookahead is terminated by the first character that is not a valid part of an integer.
 * See LookaheadMode enumeration at the top of the file.
 * Once parsing commences, 'ignore' will be skipped in the stream.
 * @param lookahead determines how parseInt looks ahead in the stream.
 * @param ignore Something useful here.
 * @return the first valid (long) integer value from the current position.
 */
 long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
 /**
 * @brief Something useful here
 * Lookahead is terminated by the first character that is not a valid part of an float.
 * See LookaheadMode enumeration at the top of the file.
 * Once parsing commences, 'ignore' will be skipped in the stream.
 * @param lookahead determines how parseInt looks ahead in the stream.
 * @param ignore Something useful here.
 * @return the first valid float value from the current position.
 */
 float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
 /**
 * @brief read chars from this stream into the given buffer
 * This will timeout if not enough characters are stored inside this stream.
 * @param buffer The buffer to write the characters to.
 * @param length The amount of characters to read.
 * @return The amount of characters that were actually read. 0 if no data was found.
 */
 size_t readBytes( char *buffer, size_t length);
 /**
 * @brief read chars from this stream into the given buffer
 * This will timeout if not enough characters are stored inside this stream.
 * @param buffer The buffer to write the characters to.
 * @param length The amount of characters to read.
 * @return The amount of characters that were actually read. 0 if no data was found.
 */
 size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
 /**
 * @brief reads chars from this stream into the given buffer until the terminator character was found.
 * Note that the terminator character will not be transferred to the given buffer.
 * @param terminator The character on which to terminate this action
 * @param buffer The buffer to write the characters to.
 * @param length The amount of characters to read if the terminator character was not found before.
 * @return The amount of characters that were actually read. 0 if no data was found or the terminator was found first.
 */
 size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
 size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
 /**
 * @brief Reads the complete stream.
 * @return the buffer as String object.
 */
 String readString();
 /**
 * @brief Reads the stream until terminator was found
 * TODO timeout or what?
 * @param terminator The character to terminate the action on.
 * @return This stream as string object until terminator.
 */
 String readStringUntil(char terminator);
protected:
 /// number of milliseconds to wait for the next char before aborting timed read
 unsigned long _timeout;
 /// used for timeout measurement
 unsigned long _startMillis;
 /**
 * @brief Starts reading this stream until a timeout occurs.
 * @return Something useful here.
 */
 int timedRead();
 /**
 * @brief Starts reading this stream without deleting the read content until a timeout occurs.
 * @return Something useful here.
 * @return 
 */
 int timedPeek();
 /**
 * @brief returns the next numeric digit in the stream or -1 if timeout
 * @param lookahead Something useful here
 * @param detectDecimal Something useful here
 * @return Something useful here
 */
 int peekNextDigit(LookaheadMode lookahead, bool detectDecimal);
 /**
 * @brief Something useful here.
 * These overload exists for compatibility with any class that has derived
 * Stream and used parseFloat/Int with a custom ignore character. To keep
 * the public API simple, these overload remains protected.
 * @param ignore The characters to skip.
 * @return Something useful here.
 */
 long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
 /**
 * @brief Something useful here.
 * These overload exists for compatibility with any class that has derived
 * Stream and used parseFloat/Int with a custom ignore character. To keep
 * the public API simple, these overload remains protected.
 * @param ignore The characters to skip.
 * @return Something useful here.
 */
 float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
 /**
 * @brief Something useful here.
 */
 struct MultiTarget {
 /// string you're searching for
 const char *str;
 /// length of string you're searching for
 size_t len; 
 /// index used by the search routine.
 size_t index; 
 };
 
 /**
 * @brief This allows you to search for an arbitrary number of strings.
 * @param targets Something useful here
 * @param tCount Something useful here
 * @return index of the target that is found first or -1 if timeout occurs.
 */
 int findMulti(struct MultiTarget *targets, int tCount);
};
#undef NO_IGNORE_CHAR
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: DocumentationRelated to Arduino's documentation content feature requestA request to make an enhancement (not a bug fix)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /