AbleButtons V0.4.0
Lightweight button library for Arduino.
 
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1/**
2 * @file Utils.h Utility function declarations.
3 *
4 * The Utils module defines general utilities. An assert(bool-expression) that
5 * outputs to Serial if its false and print-stream << operators to make it
6 * easier to output to the Serial object using C++ style stream output
7 * operations.
8 */
9
10#include <Arduino.h>
11
12/**
13 * Output assertion to Serial when the assertion expression is false.
14 *
15 * @param func The __func__ (function) name.
16 * @param file The __FILE__ name.
17 * @param lineno The __LINE__ number.
18 * @param exp The failing assertion expression.
19 */
20extern void assertSerial(const char *func, const char *file, int lineno, const __FlashStringHelper *exp);
21
22/**
23 * Macro to assert using FlashStringHelper to reduce memory usage. If the
24 * boolean expression is false, then an assertion error is output to Serial
25 * including the function name, source file, line number and the expression
26 * itself.
27 *
28 * @param e A boolean expression evaluating to true/false.
29 */
30#define assert(e) ((e) ? (void)0 : assertSerial(__func__, __FILE__, __LINE__, F(#e)))
31
32/**
33 * Output a specific type to the Print stream.
34 *
35 * @param p The print-stream to output to.
36 * @param o The object to output.
37 *
38 * @returns The print-stream object (allows chaining of << operators - as in
39 * `Serial << x << y << z;`).
40 */
41template<typename T>
42inline Print &operator<<(Print &p, const T o) { p.print(o); return p; }
43
44/// Special type to allow `Serial << endl` to print a line and flush the stream.
45enum endl {
46 endl ///< The special value of endl triggers the Serial.println() and Serial.flush().
47};
48
49/**
50 * Special stream operator for turning Serial << endl into new line and stream flush.
51 *
52 * @param p The print-stream to output a new line and flush.
53 * @param endl The special `endl` enumeration type
54 *
55 * @returns The print-stream object (allows chaining of << operators - as in
56 * `Serial << x << y << z;`).
57 */
58inline Print &operator <<(Print &p, enum endl) { p.println(); p.flush(); return p; }
Print & operator<<(Print &p, const T o)
Output a specific type to the Print stream.
Definition: Utils.h:42
void assertSerial(const char *func, const char *file, int lineno, const __FlashStringHelper *exp)
Output assertion to Serial when the assertion expression is false.
Definition: Utils.cpp:13
endl
Special type to allow Serial << endl to print a line and flush the stream.
Definition: Utils.h:45