Skip to content

Commit fb0b139

Browse files
committed
[mapping] Update docs
1 parent f16f003 commit fb0b139

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

components/mapping.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Mapping Component
22
-----------------
33

4-
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.
4+
The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to
5+
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.
56

67
.. code-block:: yaml
78
@@ -32,6 +33,7 @@ Configuration variables:
3233
- **to** (**Required**, string): The type of values in the map. May be one of ``string`` or ``int`` or a class specifier as discussed below.
3334
- **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.
3435

36+
3537
Mapping to a class
3638
##################
3739

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

47-
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.
49+
A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using
50+
the ``id`` function, and the value can be looked up using the ``[]`` operator as per the above example, or the ``get`` function.
51+
A map may be updated at run time using a lambda call, e.g. ``map["key"] = value`` or ``map.set("key", value)``.
52+
53+
Maps are stored in RAM, but will use PSRAM if available.
4854

4955
A more complex example follows:
5056

@@ -73,6 +79,8 @@ A more complex example follows:
7379
hex: 00FF00
7480
- id: blue
7581
hex: 0000FF
82+
- id: magenta
83+
hex: FFFF00
7684
7785
font:
7886
- file: gfonts://Roboto
@@ -84,10 +92,15 @@ A more complex example follows:
8492
- platform: ...
8593
# update the display drawing random text in random colors
8694
lambda: |-
87-
auto color = color_map[random_uint32() % 3];
95+
auto color = color_map.get(random_uint32() % 3]); # Uses get() to index the color_map
8896
it.printf(100, 100, id(roboto20), color, id(string_map)[random_uint32() % 3].c_str(), Color(0));
8997
9098
99+
on_...:
100+
then:
101+
- lambda: |-
102+
id(color_map)[2] = Color::random_color();
103+
91104
See Also
92105
--------
93106

0 commit comments

Comments
 (0)