diff --git a/content/components/mapping.md b/content/components/mapping.md index 57ab2dcc9b..6722d9f123 100644 --- a/content/components/mapping.md +++ b/content/components/mapping.md @@ -3,7 +3,8 @@ description: "Mapping Component" title: "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. ```yaml # Example configuration entry @@ -45,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,8 +89,14 @@ display: - 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