Skip to content

[mapping] Update docs #5194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions components/mapping.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Mapping Component
-----------------

The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to values. This enables e.g. mapping a string to a number or vice versa, or mapping a string such as a weather condition to an image.
The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to
values. This enables e.g. mapping a string to a number or vice versa, or mapping a string such as a weather condition to an image.

.. code-block:: yaml

Expand Down Expand Up @@ -32,6 +33,7 @@ Configuration variables:
- **to** (**Required**, string): The type of values in the map. May be one of ``string`` or ``int`` or a class specifier as discussed below.
- **entries** (**Required**, dict): A list of key-value pairs that define the mapping. The keys must be of the type specified in the ``from`` field, and the values must be of the type specified in the ``to`` field.


Mapping to a class
##################

Expand All @@ -44,7 +46,11 @@ You can also map to a class. This is useful when you want to map to a more compl
Using a mapping
###############

A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using the ``id`` function, and the value can be looked up using the ``[]`` operator as per the above example.
A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using
the ``id`` function, and the value can be looked up using the ``[]`` operator as per the above example, or the ``get`` function.
A map may be updated at run time using a lambda call, e.g. ``map.set("key", value)``.

Maps are stored in RAM, but will use PSRAM if available.

A more complex example follows:

Expand Down Expand Up @@ -84,10 +90,15 @@ A more complex example follows:
- platform: ...
# update the display drawing random text in random colors
lambda: |-
auto color = color_map[random_uint32() % 3];
auto color = color_map.get(random_uint32() % 3]); # Uses get() to index the color_map
it.printf(100, 100, id(roboto20), color, id(string_map)[random_uint32() % 3].c_str(), Color(0));


on_...:
then:
- lambda: |-
id(color_map).set(2, Color::random_color());

See Also
--------

Expand Down