Skip to content

Commit 4f0348b

Browse files
committed
MCU8MASS-980 Add example on how to use Serial and the Log module
1 parent d12707c commit 4f0348b

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

examples/serial/serial.ino

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @brief This file demonstrates how to use Serial for printing out messages on
3+
* the board. It also demonstrates how to use the logging module in the AVR-IoT
4+
* Cellular Library.
5+
*/
6+
#include <Arduino.h>
7+
8+
#include "log.h"
9+
10+
void setup() {
11+
12+
// As can be seen on page 7 in the hardware user guide for the AVR-IoT
13+
// Cellular Mini:
14+
// https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-IoT-Cellular-Mini-HW-UserGuide-DS50003320.pdf,
15+
// the USART/Serial connected to the debugger which is thereafter connected
16+
// to the USB port, is the Serial3. It is thus important that we use Serial3
17+
// and not Serial when printing messages.
18+
Serial3.begin(115200);
19+
Serial3.println("Hello world from Serial3");
20+
Serial3.end();
21+
22+
delay(500);
23+
24+
// The AVR-IoT Cellular Library also comes with a logging library, which
25+
// uses Serial3 under the hood and can be initialised in a similar way. It
26+
// will add a prefix to the particular logging level used (debug, info,
27+
// warning, error).
28+
Log.begin(115200);
29+
Log.info("Hello world from Log");
30+
31+
// If no prefix is wanted, we can use raw:
32+
Log.raw("This is a message without a prefix");
33+
34+
// The log level determines which messages are printed between the levels.
35+
// We can adjust the level as we want, the following will be printed under
36+
// the different levels:
37+
//
38+
// debug: All messages
39+
// info: info, warning and error messages
40+
// warn: Warning and error messages
41+
// error: Only error messages
42+
//
43+
// Note that raw messages always will be printed
44+
Log.setLogLevel(LogLevel::DEBUG);
45+
Log.debug("A debug message");
46+
Log.info("An info message");
47+
Log.warn("A warning message");
48+
Log.error("An error message");
49+
50+
Log.setLogLevel(LogLevel::INFO);
51+
Log.raw(""); // Just to add a newline
52+
Log.debug("This will not be printed now");
53+
Log.info("An info message");
54+
Log.warn("A warning message");
55+
Log.error("An error message");
56+
57+
Log.setLogLevel(LogLevel::WARN);
58+
Log.raw(""); // Just to add a newline
59+
Log.debug("This will not be printed now");
60+
Log.info("This will not be printed now");
61+
Log.warn("A warning message");
62+
Log.error("An error message");
63+
64+
Log.setLogLevel(LogLevel::ERROR);
65+
Log.raw(""); // Just to add a newline
66+
Log.debug("This will not be printed now");
67+
Log.info("This will not be printed now");
68+
Log.warn("This will not be printed now");
69+
Log.error("An error message");
70+
Log.raw(""); // Just to add a newline
71+
72+
Log.setLogLevel(LogLevel::INFO);
73+
74+
// The logging library can also format strings by using the f postfix in the
75+
// function name, so we don't have to use multiple print calls as with the
76+
// standard Arduino Serial. This makes printing more complex things far more
77+
// structured and work in the same way as the printf in the standard library
78+
// of C. Note we have to add a \r\n for a carriage return and new line when
79+
// the format based functions are used.
80+
Log.infof("This is a number: %d\r\n", 10);
81+
Log.infof("This is a string: %s\r\n", "Hello world");
82+
Log.infof("This is a hexadecimal and a string: %X - %s\r\n",
83+
31,
84+
"Hello world");
85+
}
86+
87+
void loop() {}

src/log.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,7 @@ void LogClass::debugf(const char* format, ...) {
119119
}
120120
}
121121

122-
void LogClass::raw(const char str[]) {
123-
if (log_level >= LogLevel::DEBUG) {
124-
this->print(str, DEBUG_LEVEL_FMT);
125-
}
126-
127-
this->uart->print(str);
128-
}
122+
void LogClass::raw(const char str[]) { this->print(str, ""); }
129123

130124
void LogClass::raw(const String str) { this->raw(str.c_str()); }
131125

0 commit comments

Comments
 (0)