diff --git a/components/mapping.rst b/components/mapping.rst index cea0338be9..a68a6b5433 100644 --- a/components/mapping.rst +++ b/components/mapping.rst @@ -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 @@ -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 ################## @@ -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: @@ -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 --------