Int & Float Values

This example will show you how to display integer values that are bound to a given range.

Using ItemProgress, you can display integer values in a given range and define the step length between each value.

Includes

// other includes ...
#include <ItemProgress.h>
#include <LcdMenu.h> // Always comes after every item type import

Constructors

ItemProgress has multiple constructors which will enable you to customize the base functionality, using the base constructor

ItemProgress(const char* key, fptrInt callback)

will display an integer value from 0 to 1000 and with a step length of 1 i.e. each time left() or right() is called, it will increment and decrement the value by exactly 1.

To customize the range, use the following constructors which provide a method for you to map the value from 0 to 1000 to any other range (float, int, or long).

ItemProgress(const char* key, uint8_t stepLength, fptrMapping mapping, fptrInt callback)
  • key: Text to display before the value.

  • stepLength: The length of each step for incrementing or decrementing the progress value.

  • mapping: A pointer to the mapping function that maps the progress value to a custom representation (in this mapping function you can also append any text to the progress).

  • callback: function to be executed after changing the value and leaving edit mode.

The value passed to the callback function is the progress, so you also have to map it to get your desired value.

Mapping to different range example functions

The following example mapping functions shows how to map the progress value in the range 0..1000 to any other range

Map from 1..1000 to 100..200 and add a unit

char* intMapping(uint16_t progress) {
    // Map the progress value to a new range (100 to 200)
    long mapped = mapProgress(progress, 100L, 200L);

    // Buffer to store the converted string
    static char buffer[10];

    // Convert the mapped value to a string
    itoa(mapped, buffer, 10);

    // Concatenate the string with the character 'm'
    concat(buffer, 'm', buffer);

    // Return the resulting string
    return buffer;
}

As you have seen before the default step length is 1, and there are 100 values between 100 and 200, so for it to step by exactly one on the screen you need to provide a step length of 10 which is 1000/100 (max progress/total values)

Code example

// Declare the calbacks
void callback(uint16_t pos);

char* intMapping(uint16_t progress) {
    // See code in the above section
}

// Initialize the main menu items
MAIN_MENU(
    ITEM_BASIC("Con"),
    ITEM_PROGRESS("Dist", 1, intMapping, callback),
    ITEM_BASIC("Blink SOS"),
    ITEM_BASIC("Blink random")
);

// Construct the LcdMenu
LcdMenu menu(LCD_ROWS, LCD_COLS);

void setup() {
    Serial.begin(9600);
    // Initialize LcdMenu with the menu items
    menu.setupLcdWithMenu(0x27, mainMenu);
}

void loop() {
    if (!Serial.available()) return;
    char command = Serial.read();

    if (command == UP)
        menu.up();
    else if (command == DOWN)
        menu.down();
    else if (command == LEFT)
        menu.left();
    else if (command == RIGHT)
        menu.right();
    else if (command == ENTER)
        menu.enter();
    else if (command == BACK)
        menu.back();
}

void callback(uint16_t pos) {
    // do something with the progress
    Serial.println(pos);
}

Full example 👉 .../examples/IntFloatValues/IntFloatValues.ino

Last updated