Input Card


The Input Card allows users to enter textual or numeric data. You can specify the type and precision.
Initializer
To create a input card in ESP-DASH v5, use the dash::InputCard<T, Precision>
class. The template parameters allow you to control the value type and decimal precision:
T
: Value type (e.g., ReplaceT
withdash::string
,int
,float
orconst char*
)Precision
: Number of decimal places for floating point values (default:2
)
For Text Input:
dash::InputCard<T> input(dashboard, "Enter text");
For Numericals:
dash::InputCard<T, Precision> inputInt(dashboard, "Enter int");
Callback
Input cards support a callback that is triggered when the input value changes. This allows you to handle user input dynamically. Please pay close attention to the type of the callback parameter, which is const std::optional<T>&
. This results in the callback receiving std::nullopt
when the input is cleared or invalid, very useful for handling user input gracefully.
Replace T
with the type you chose for the input card (e.g., dash::string
, int
, float
, or const char*
).
input.onChange([](const std::optional<T>& newText) {
// Handle text change
if (newText) {
Serial.printf("Text changed: %s\n", *newText);
} else {
Serial.println("Text cleared");
}
});
Methods
setValue(T value)
Set the input value. The type of value
must match the template type you chose for T
(e.g., dash::string, int, float, const char*).
input.setValue(value); // value is of type T
- Signature:
void setValue(T value)
- Parameters:
T value
— The value to set (type matches template parameterT
).
- Returns:
void
value()
Get the current input value. The return type matches your template type for T
(e.g., dash::string, int, float, const char*).
T txt = input.value();
- Signature:
T value()
- Parameters: None
- Returns:
T
— The current value (type matches template parameterT
).
Note:
T
can be one of:dash::string
,int
,float
, orconst char*
depending on how you instantiate the card.
Reference
Below is a reference code showing how to integrate this widget in a real project, including initialization and value updates usage.
// ...
// Create a input card (string, default)
dash::InputCard<dash::string> textCard(dashboard, "Input");
void setup() {
// ...
textCard.setValue("Hello");
textCard.onChange([](const std::optional<const char*>& newText) {
if (newText) {
Serial.printf("New text: %s\n", *newText);
} else {
Serial.println("Text cleared");
}
});
}
void loop() {
// ...
}