-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is mostly copied and pasted from my PCRemote repository, but spruced up and renamed for Arduino library prime time
- Loading branch information
1 parent
a4fb8a9
commit 387f5bf
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <DFPrint.h> | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
String exampleValue1 = "Here is some text"; | ||
int exampleValue2 = random(100); | ||
float exampleValue3 = 3.14; | ||
|
||
// If you define the preprocessor define 'DF_DISABLE_DEBUG_PRINT', then every use of DEBUG_PRINT* will be removed during compilation | ||
DEBUG_PRINTLN("Hello world"); | ||
DEBUG_PRINTLN("Insert string ->> '{0}' <<- right there", exampleValue1); | ||
DEBUG_PRINTLN("You can have more than one value inserted: {0} * {1} = {2}", exampleValue2, exampleValue3, exampleValue2 * exampleValue3); | ||
|
||
DF::Print("Alternatively, you can call the print function directly if this is something you want to remain in your release build"); | ||
} | ||
|
||
void loop() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# === Types === | ||
|
||
# Let's colour the namespace the same as a class | ||
DF KEYWORD1 | ||
|
||
# === Functions === | ||
Print KEYWORD2 | ||
Println KEYWORD2 | ||
|
||
# Let's make the debug macro look like a function | ||
DEBUG_PRINT KEYWORD2 | ||
DEBUG_PRINTLN KEYWORD2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=DF-Print | ||
version=0.1.0 | ||
author=Daniel Neve | ||
maintainer=Daniel Neve | ||
sentence=Use C# style syntax for formatting strings. | ||
paragraph=Where `Serial.print("My value is: "); Serial.println(someVariable)` is annoyingly cumbersome. Use `DEBUG_PRINT("My value is: {0}", someVariable);` instead. | ||
category=Communication | ||
url=https://github.com/DanForever/DF-Print | ||
architectures=* | ||
includes=DFPrint.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* MIT License | ||
* Copyright (c) 2022 Daniel Neve | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
#ifndef __DF_PRINT_H__ | ||
#define __DF_PRINT_H__ | ||
|
||
#ifndef DF_DISABLE_DEBUG_PRINT | ||
# define DEBUG_PRINT(format, ...) ::DF::Print(format, ##__VA_ARGS__) | ||
# define DEBUG_PRINTLN(format, ...) ::DF::Println(format, ##__VA_ARGS__) | ||
#else | ||
# define DEBUG_PRINT(format, ...) | ||
# define DEBUG_PRINTLN(format, ...) | ||
#endif | ||
|
||
#include <cstring> | ||
|
||
#include "DFPrintInternal.h" | ||
|
||
namespace DF | ||
{ | ||
inline size_t Print(const char* text) | ||
{ | ||
return Serial.print(text); | ||
} | ||
|
||
inline size_t Println(const char* text) | ||
{ | ||
return Serial.println(text); | ||
} | ||
|
||
template <typename Arg> | ||
inline size_t Print(const char* format, Arg&& arg) | ||
{ | ||
return Internal::PrintTextWithArgument(format, arg); | ||
} | ||
|
||
template <typename Arg, typename... Args> | ||
inline size_t Print(const char* format, Arg&& arg, Args... args) | ||
{ | ||
size_t charactersPrinted = 0; | ||
|
||
charactersPrinted += Internal::PrintTextWithArgument(format, std::forward<Arg>(arg)); | ||
charactersPrinted += Print(format, std::forward<Args>(args)...); | ||
|
||
return charactersPrinted; | ||
} | ||
|
||
template <typename... Args> | ||
inline size_t Print(const char* format, Args... args) | ||
{ | ||
size_t charactersPrinted = 0; | ||
|
||
charactersPrinted += Print(format, std::forward<Args>(args)...); | ||
|
||
return charactersPrinted; | ||
} | ||
|
||
template <typename... Args> | ||
inline size_t Println(const char* format, Args... args) | ||
{ | ||
size_t charactersPrinted = 0; | ||
|
||
charactersPrinted += Print(format, std::forward<Args>(args)...); | ||
charactersPrinted += Serial.println(""); | ||
// Alternatively, could do Serial.write("\r\n"); - more efficient? Is it worth making that string be read from PROGMEM? | ||
|
||
return charactersPrinted; | ||
} | ||
} | ||
|
||
#endif // __DF_PRINT_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* MIT License | ||
* Copyright (c) 2022 Daniel Neve | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
#ifndef __DF_PRINT_INTERNAL_H__ | ||
#define __DF_PRINT_INTERNAL_H__ | ||
|
||
#include <Arduino.h> | ||
|
||
namespace DF | ||
{ | ||
namespace Internal | ||
{ | ||
// Finds the location of the first argument specifier and prints all the text | ||
// that comes before it. If no specifier is found, prints the whole string | ||
// Format is modified to remove the text that was printed | ||
inline size_t PrintTextBeforeFirstArgumentSpecifier(const char*& format) | ||
{ | ||
const char* argStart = std::strchr(format, '{'); | ||
|
||
if (!argStart) | ||
{ | ||
size_t count = Serial.print(format); | ||
format += count; | ||
return count; | ||
} | ||
|
||
size_t length = argStart - format; | ||
Serial.write(format, length); | ||
format += length; | ||
|
||
return length; | ||
} | ||
|
||
// Prints the argument and removes the argument specifier from format | ||
template <typename Arg> | ||
inline size_t PrintArgumentInPlaceOfSpecifier(const char*& format, Arg&& arg) | ||
{ | ||
const char* argEnd = std::strchr(format, '}'); | ||
|
||
if (argEnd) | ||
format = argEnd + 1; | ||
|
||
return Serial.print(std::forward<Arg>(arg)); | ||
} | ||
|
||
// This will print All the text before an argument specifier (e.g. {0}), | ||
// then the argument, and finally all the text that comes after the argument | ||
// up until the next argument specifier is detected, or the end of the string is reached | ||
// | ||
// Implementation note: | ||
// This is the main workhorse of the library, so as it's currently implemented, what | ||
// happens is that any time a string has more than one argument to be printed, there will | ||
// be no preceeding text for any argument other than the first, since the preceeding argument | ||
// also prints everything that comes afterwards. | ||
// (There may be a slightly more optimal way to implement this) | ||
template <typename Arg> | ||
inline size_t PrintTextWithArgument(const char*& format, Arg&& arg) | ||
{ | ||
size_t charactersPrinted = 0; | ||
|
||
charactersPrinted += PrintTextBeforeFirstArgumentSpecifier(format); | ||
charactersPrinted += PrintArgumentInPlaceOfSpecifier(format, std::forward<Arg>(arg)); | ||
charactersPrinted += PrintTextBeforeFirstArgumentSpecifier(format); | ||
|
||
return charactersPrinted; | ||
} | ||
} | ||
} | ||
|
||
#endif // __DF_PRINT_INTERNAL_H__ |