Skip to content
Merged
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 content/components/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down