Skip to content

Commit 7e751e6

Browse files
committed
Added support for custom device monitor filters
1 parent 2f0abf6 commit 7e751e6

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

core/userguide/device/cmd_monitor.rst

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ You can also specify which environments should be processed by default using
175175
Filters
176176
-------
177177

178-
A list of filters that can be applied for monitor output using :option:`pio device monitor --filter` or :ref:`projectconf` and :ref:`projectconf_monitor_filters` options.
179-
option.
178+
PlatformIO allows you to apply multiple filters to the device monitor INPUT & OUTPUT
179+
streams using :option:`pio device monitor --filter` command or
180+
:ref:`projectconf_monitor_filters` option.
181+
182+
Built-in Filters
183+
~~~~~~~~~~~~~~~~
180184

181185
.. list-table::
182186
:header-rows: 1
@@ -208,8 +212,46 @@ option.
208212
* - ``esp8266_exception_decoder``
209213
- Custom filter for :ref:`platform_espressif8266` which decodes crash exception
210214

215+
.. _cmd_device_monitor_custom_filters:
216+
217+
Custom Filters
218+
~~~~~~~~~~~~~~
219+
220+
:ref:`piocore` provides an API to extend device monitor with custom filters declared
221+
in :ref:`projectconf_pio_monitor_dir` or in "monitor" folder of :ref:`platforms`.
222+
Each filter is a Python-based file and its name should have the ``filter_`` prefix.
223+
In a Python code, you need to extend ``DeviceMonitorFilter`` class to get access to
224+
the ``rx()`` and ``tx()`` methods/callbacks. See the base API below:
225+
226+
**filter_demo.py**
227+
228+
.. code-block:: python
229+
230+
from platformio.commands.device import DeviceMonitorFilter
231+
232+
233+
class Demo(DeviceMonitorFilter):
234+
NAME = "demo"
235+
236+
def __init__(self, *args, **kwargs):
237+
super(Demo, self).__init__(*args, **kwargs)
238+
print("Demo filter is loaded")
239+
240+
def rx(self, text):
241+
return f"Received: {text}\n"
242+
243+
def tx(self, text):
244+
print(f"Sent: {text}\n")
245+
return text
246+
247+
**Examples**
248+
249+
- https://github.com/platformio/platformio-core/tree/develop/platformio/commands/device/filters
250+
- https://github.com/platformio/platform-espressif32/tree/develop/monitor
251+
- https://github.com/platformio/platform-espressif8266/tree/develop/monitor
252+
211253
Capture output to a file
212-
------------------------
254+
~~~~~~~~~~~~~~~~~~~~~~~~
213255

214256
You need to use a ``log2file`` filter from :ref:`cmd_device_monitor_filters`:
215257

@@ -227,16 +269,6 @@ or using :ref:`projectconf` and :ref:`projectconf_monitor_filters`
227269
platform = ...
228270
monitor_filters = log2file, default
229271
230-
231-
Device Monitor Filter API
232-
-------------------------
233-
234-
:ref:`piocore` provides an API to extend device monitor with a custom filter declared
235-
in "monitor" folder of :ref:`platforms`. See examples:
236-
237-
- https://github.com/platformio/platform-espressif32/tree/develop/monitor
238-
- https://github.com/platformio/platform-espressif8266/tree/develop/monitor
239-
240272
Examples
241273
--------
242274

envvars.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Allows one to override :ref:`projectconf` option :ref:`projectconf_pio_test_dir`
144144

145145
Allows one to override :ref:`projectconf` option :ref:`projectconf_pio_boards_dir`.
146146

147+
.. envvar:: PLATFORMIO_MONITOR_DIR
148+
149+
Allows one to override :ref:`projectconf` option :ref:`projectconf_pio_monitor_dir`.
150+
147151
.. envvar:: PLATFORMIO_SHARED_DIR
148152

149153
Allows one to override :ref:`projectconf` option :ref:`projectconf_pio_shared_dir`.

projectconf/section_platformio.rst

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ a different configuration (new build flags, etc):
340340
``workspace_dir``
341341
^^^^^^^^^^^^^^^^^
342342

343-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``.pio``"
343+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``.pio``"
344344

345345
The path to a project workspace directory where PlatformIO keeps by default
346346
compiled objects, static libraries, firmwares, and external library
@@ -402,7 +402,7 @@ This option can also be configured by the global environment variable
402402
``include_dir``
403403
^^^^^^^^^^^^^^^
404404

405-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``include``"
405+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``include``"
406406

407407
The path to project's default header files. PlatformIO uses it for the
408408
:ref:`cmd_run` command. The default value is ``include`` meaning an
@@ -420,7 +420,7 @@ This option can also be configured by the global environment variable
420420
``src_dir``
421421
^^^^^^^^^^^
422422

423-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``src``"
423+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``src``"
424424

425425
The path to the project's directory with source code. PlatformIO uses
426426
it for the :ref:`cmd_run` command. The default value is ``src``
@@ -439,7 +439,7 @@ This option can also be configured by the global environment variable
439439
``lib_dir``
440440
^^^^^^^^^^^
441441

442-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``lib``"
442+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``lib``"
443443

444444
You can put your own/private libraries here. The source code of each library
445445
should be placed in separate directory, like
@@ -488,7 +488,7 @@ preprocessor's include paths and build them.
488488
``data_dir``
489489
^^^^^^^^^^^^
490490

491-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``data``"
491+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``data``"
492492

493493
Data directory to store contents and :ref:`platform_espressif_uploadfs`.
494494
The default value is ``data`` that means that folder is located in the root of
@@ -502,7 +502,7 @@ This option can also be configured by the global environment variable
502502
``test_dir``
503503
^^^^^^^^^^^^
504504

505-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``test``"
505+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``test``"
506506

507507
The directory where :ref:`unit_testing` engine will look for the
508508
tests. The default value is ``test``, meaning a ``test`` directory
@@ -516,7 +516,7 @@ This option can also be configured by the global environment variable
516516
``boards_dir``
517517
^^^^^^^^^^^^^^
518518

519-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``boards``"
519+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``boards``"
520520

521521
The location of project-specific board definitions. Each project may
522522
choose a suitable directory name. The default value is ``boards``,
@@ -531,12 +531,26 @@ By default, PlatformIO looks for boards in this order:
531531
This option can also be configured by the global environment variable
532532
:envvar:`PLATFORMIO_BOARDS_DIR`.
533533

534+
.. _projectconf_pio_monitor_dir:
535+
536+
``monitor_dir``
537+
^^^^^^^^^^^^^^^
538+
539+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``monitor``"
540+
541+
The directory where :ref:`cmd_device_monitor` will look for the
542+
:ref:`cmd_device_monitor_custom_filters`. The default value is ``monitor``,
543+
meaning a ``monitor`` directory located in the root of the project.
544+
545+
This option can also be configured by the global environment variable
546+
:envvar:`PLATFORMIO_MONITOR_DIR`.
547+
534548
.. _projectconf_pio_shared_dir:
535549

536550
``shared_dir``
537551
^^^^^^^^^^^^^^
538552

539-
Type: ``DirPath`` | Multiple: ``No`` | Default: "Project/``shared``"
553+
Type: ``DirPath`` | Multiple: ``No`` | Default: "<Project>/``shared``"
540554

541555
:ref:`pioremote` uses this folder to synchronize extra files between remote
542556
machine. For example, you can share :ref:`projectconf_extra_scripts`.

0 commit comments

Comments
 (0)