Skip to content

Commit

Permalink
Add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 30, 2020
1 parent 48b656d commit 488fd58
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 7 deletions.
9 changes: 6 additions & 3 deletions dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/
int
lwprintf_output(int ch, lwprintf_t* lw) {
printf("%c", (char)ch);
if (ch != '\0') {
printf("%c", (char)ch);
}
return ch;
}

Expand Down Expand Up @@ -68,8 +70,9 @@ main(void) {
lwprintf_init(lwprintf_output);

/* Float tests */
for (float a = 0.0f; a < 1.0f; a += 0.01f) {
printf_run(NULL, "%10f; %10.1f; %10.0f", 1.99f + a, 1.99f + a, 1.99f + a);
for (float a = 0.0f; a < 2.0f; a += 0.01f) {
printf_run(NULL, "%10f; %10.1f; %10.0f; %+10f", 1.99f + a, 1.99f + a, 1.99f + a, 1.99 + a);
printf_run(NULL, "%10f; %10.1f; %10.0f; %+10f", -1.99f + a, -1.99f + a, -1.99f + a, -1.99 + a);
}
return 0;

Expand Down
40 changes: 40 additions & 0 deletions docs/examples_src/example_instance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "lwprintf/lwprintf.h"

/* Define application custom instance */
lwprintf_t custom_instance;

/* Define custom output function for print */
int
custom_out(int ch, lwprintf_t* p) {
/* Do whatever with this character */
if (ch == '\0') {
/* This is end of string in current formatting */
/* Maybe time to start DMA transfer? */
} else {
/* Print or send character */
}

/* Return character to proceed */
return ch;
}

/* Define output function for default instance */
int
default_out(int ch, lwprintf_t* p) {
/* Print function for default instance */

/* See custom_out function for implementation details */
}

int
main(void) {
/* Initialize default lwprintf instance with output function */
lwprintf_init(default_out);
/* Initialize custom lwprintf instance with output function */
lwprintf_init_ex(&custom_instance, custom_out);

/* Print first text over default output */
lwprintf_printf("Text: %d", 10);
/* Print text over custom instance */
lwprintf_printf_ex(&custom_instance, "Custom: %f", 3.2f);
}
28 changes: 28 additions & 0 deletions docs/examples_src/example_instance_single_func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "lwprintf/lwprintf.h"

/* Define application custom instance */
lwprintf_t custom_instance1;
lwprintf_t custom_instance2;

/* Define custom output function for print */
int
my_out(int ch, lwprintf_t* p) {
if (p == &custom_instance1) {
/* This is custom instance 1 */
} else if (p == &custom_instance2) {
/* This is custom instance 2 */
} else {
/* This is default instance */
}
return ch;
}

int
main(void) {
/* Initialize default lwprintf instance with output function */
lwprintf_init(my_out);
lwprintf_init_ex(&custom_instance1, my_out);
lwprintf_init_ex(&custom_instance2, my_out);

/* Use print functions ... */
}
4 changes: 3 additions & 1 deletion docs/examples_src/example_minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
int
lwprintf_out(int ch, lwprintf_t* lwp) {
/* May use printf to output it for test */
printf("%c", (char)ch);
if (ch != '\0') {
printf("%c", (char)ch);
}

return ch;
}
Expand Down
24 changes: 23 additions & 1 deletion docs/user-manual/how-it-works.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,29 @@
How it works
============

To be added
LwPRINTF library supports ``2`` different formatting output types:

* Write formatted data to user input array
* Directly print formatted characters by calling ``output_function`` for every formatted character in the input string

Text formatting is based on input format string followed by the data parameters.
It is mostly used to prepare numeric data types to human readable format.

.. note::
LwPRINTF is open-source implementation of regular *stdio.h* library in C language.
It implements only output functions, excluding input scanning features

Formatting functions take input *format string* followed by (optional) different data types.
Internal algorithm scans character by character to understand type of expected data user would like to have printed.

Every format specifier starts with letter ``%``, followed by optional set of flags, widths and other sets of characters.
Last part of every specifier is its type, that being type of format and data to display.

.. tip::
To print number ``1234`` in human readable format, use specifier ``%d``.
With default configuration, call ``lwprintf_printf("%d", 1234);`` and it will print ``"1234"``.

Check section :ref:`um_format_specifier` for list of all formats and data types

.. toctree::
:maxdepth: 2
2 changes: 2 additions & 0 deletions docs/user-manual/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ User manual
.. toctree::
:maxdepth: 2

how-it-works
format-specifier
instances
thread-safety
30 changes: 28 additions & 2 deletions docs/user-manual/instances.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@
LwPRINTF instances
==================

LwPRINTF architecture allows multiple instances...
LwPRINTF is very flexible and allows multiple instances for output print functions.

To be added..
.. note::
Multiple instances with LwPRINTF are useful only with direct
print functions, suchs as :c:macro:`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

.. literalinclude:: ../examples_src/example_instance.c
:language: c
:linenos:
:caption: Custom LwPRINTF instance for output

.. note::
It is perfectly valid to use single output function for all application instances.
Use check against input parameter for :c:type:`lwprintf_t` if it matches your custom LwPRINTF instance memory address

.. literalinclude:: ../examples_src/example_instance_single_func.c
:language: c
:linenos:
:caption: Single output function for all LwPRINTF instances

.. toctree::
:maxdepth: 2

0 comments on commit 488fd58

Please sign in to comment.