LwPRINTF instances
LwPRINTF is very flexible and allows multiple instances for output print functions.
Note
Multiple instances with LwPRINTF are useful only with direct
print functions, suchs as lwprintf_printf
.
If application uses only format functions which write to input buffer,
it may always use default LwPRINTF instance which is
created by the library itself
Use of different instances is useful if application needs different print
configurations. Each instance has its own print_output
function,
allowing application to use multiple debug configurations (as an example)
Tip
Use functions with _ex
suffix to direcly work with custom instances.
Functions without _ex
suffix use default LwPRINTF instance
1#include "lwprintf/lwprintf.h"
2
3/* Define application custom instance */
4lwprintf_t custom_instance;
5
6/* Define custom output function for print */
7int
8custom_out(int ch, lwprintf_t* p) {
9 /* Do whatever with this character */
10 if (ch == '\0') {
11 /* This is end of string in current formatting */
12 /* Maybe time to start DMA transfer? */
13 } else {
14 /* Print or send character */
15 }
16
17 /* Return character to proceed */
18 return ch;
19}
20
21/* Define output function for default instance */
22int
23default_out(int ch, lwprintf_t* p) {
24 /* Print function for default instance */
25
26 /* See custom_out function for implementation details */
27}
28
29int
30main(void) {
31 /* Initialize default lwprintf instance with output function */
32 lwprintf_init(default_out);
33 /* Initialize custom lwprintf instance with output function */
34 lwprintf_init_ex(&custom_instance, custom_out);
35
36 /* Print first text over default output */
37 lwprintf_printf("Text: %d", 10);
38 /* Print text over custom instance */
39 lwprintf_printf_ex(&custom_instance, "Custom: %f", 3.2f);
40}
Note
It is perfectly valid to use single output function for all application instances.
Use check against input parameter for lwprintf_t
if it matches your custom LwPRINTF instance memory address
1#include "lwprintf/lwprintf.h"
2
3/* Define application custom instance */
4lwprintf_t custom_instance1;
5lwprintf_t custom_instance2;
6
7/* Define custom output function for print */
8int
9my_out(int ch, lwprintf_t* p) {
10 if (p == &custom_instance1) {
11 /* This is custom instance 1 */
12 } else if (p == &custom_instance2) {
13 /* This is custom instance 2 */
14 } else {
15 /* This is default instance */
16 }
17 return ch;
18}
19
20int
21main(void) {
22 /* Initialize default lwprintf instance with output function */
23 lwprintf_init(my_out);
24 lwprintf_init_ex(&custom_instance1, my_out);
25 lwprintf_init_ex(&custom_instance2, my_out);
26
27 /* Use print functions ... */
28}