You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/mapping.rst
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,8 @@
1
1
Mapping Component
2
2
-----------------
3
3
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.
5
6
6
7
.. code-block:: yaml
7
8
@@ -32,6 +33,7 @@ Configuration variables:
32
33
- **to** (**Required**, string): The type of values in the map. May be one of ``string`` or ``int`` or a class specifier as discussed below.
33
34
- **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.
34
35
36
+
35
37
Mapping to a class
36
38
##################
37
39
@@ -44,7 +46,11 @@ You can also map to a class. This is useful when you want to map to a more compl
44
46
Using a mapping
45
47
###############
46
48
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.
48
54
49
55
A more complex example follows:
50
56
@@ -73,6 +79,8 @@ A more complex example follows:
73
79
hex: 00FF00
74
80
- id: blue
75
81
hex: 0000FF
82
+
- id: magenta
83
+
hex: FFFF00
76
84
77
85
font:
78
86
- file: gfonts://Roboto
@@ -84,10 +92,15 @@ A more complex example follows:
84
92
- platform: ...
85
93
# update the display drawing random text in random colors
86
94
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
0 commit comments