Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Examples to use Widgets #260

Merged
merged 17 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions examples/ButtonWithRotaryAdapter/ButtonWithRotaryAdapter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@

#include <Button.h>
#include <ItemInputCharset.h>
#include <ItemList.h>
#include <ItemToggle.h>
#include <ItemWidget.h>
#include <LcdMenu.h>
#include <MenuScreen.h>
#include <SimpleRotary.h>
#include <display/LiquidCrystal_I2CAdapter.h>
#include <input/ButtonAdapter.h>
#include <input/SimpleRotaryAdapter.h>
#include <renderer/CharacterDisplayRenderer.h>
#include <widget/WidgetList.h>

#define LCD_ROWS 2
#define LCD_COLS 16

// Declare the callbacks
void colorsCallback(uint8_t pos);
void toggleBacklight(bool isOn);
void inputCallback(char* value);

String colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
static const char* colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
static const uint8_t COLORS_COUNT = sizeof(colors) / sizeof(colors[0]);

// clang-format off
MENU_SCREEN(mainScreen, mainItems,
ITEM_INPUT_CHARSET("User", (const char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZ", inputCallback),
ITEM_STRING_LIST("Color", colors, 8, colorsCallback),
ITEM_WIDGET(
"Color",
[](const char* color) { Serial.println(color); },
WIDGET_LIST(colors, COLORS_COUNT, 0, "%s")),
ITEM_TOGGLE("Backlight", toggleBacklight),
ITEM_BASIC("Placeholder 1"),
ITEM_BASIC("Placeholder 2"));
Expand Down Expand Up @@ -58,10 +62,6 @@ void toggleBacklight(bool isOn) {
lcdAdapter.setBacklight(isOn);
}

void colorsCallback(uint8_t pos) {
Serial.println(colors[pos]);
}

void inputCallback(char* value) {
Serial.println(value);
}
14 changes: 10 additions & 4 deletions examples/IntFloatValues/IntFloatValues.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <ItemFloatRange.h>
#include <ItemIntRange.h>
#include <ItemWidget.h>
#include <LcdMenu.h>
#include <MenuScreen.h>
#include <display/LiquidCrystal_I2CAdapter.h>
#include <input/KeyboardAdapter.h>
#include <renderer/CharacterDisplayRenderer.h>
#include <widget/WidgetRange.h>

#define LCD_ROWS 2
#define LCD_COLS 16
Expand All @@ -17,8 +17,14 @@ void callbackFloat(float value);
// clang-format off
MENU_SCREEN(mainScreen, mainItems,
ITEM_BASIC("Con"),
ITEM_INT_RANGE("Dist", 100, 200, 100, callbackInt, "%02dm"),
ITEM_FLOAT_RANGE("Curr", -1.0f, 1.0f, -1.0f, callbackFloat, "%.2fmA", 0.01f),
ITEM_WIDGET(
"Dist",
callbackInt,
WIDGET_RANGE(100, 1, 100, 200, "%dm", 1)),
ITEM_WIDGET(
"Curr",
callbackFloat,
WIDGET_RANGE(-1.0f, 0.01f, -1.0f, 1.0f, "%.2fmA", 2)),
ITEM_BASIC("Blink SOS"),
forntoh marked this conversation as resolved.
Show resolved Hide resolved
ITEM_BASIC("Blink random"));
// clang-format on
Expand Down
40 changes: 14 additions & 26 deletions examples/List/List.ino
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
#include <ItemList.h>
#include <ItemWidget.h>
#include <LcdMenu.h>
#include <MenuScreen.h>
#include <display/LiquidCrystal_I2CAdapter.h>
#include <input/KeyboardAdapter.h>
#include <renderer/CharacterDisplayRenderer.h>
#include <widget/WidgetList.h>

#define LCD_ROWS 2
#define LCD_COLS 16

// Declare the callbacks
void colorsCallback(uint8_t pos);
void numsCallback(uint8_t pos);
static const char* colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
static const uint8_t nums[] = {5, 7, 9, 12, 32};

// Declare the array
extern String colors[];
// Initialize the array
String colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};

// Declare the array
extern String nums[];
// Initialize the array
String nums[] = {"5", "7", "9", "12", "32"};
static const uint8_t NUMS_COUNT = sizeof(nums) / sizeof(nums[0]);
static const uint8_t COLORS_COUNT = sizeof(colors) / sizeof(colors[0]);

// Initialize the main menu items
// clang-format off
MENU_SCREEN(mainScreen, mainItems,
ITEM_BASIC("List demo"),
ITEM_STRING_LIST("Col", colors, 8, colorsCallback),
ITEM_STRING_LIST("Num", nums, 5, numsCallback),
ITEM_WIDGET(
"Color",
[](const char* color) { Serial.println(color); },
WIDGET_LIST(colors, COLORS_COUNT, 0, "%s", 0, true)),
ITEM_WIDGET(
"Num",
[](const uint8_t num) { Serial.println(num); },
WIDGET_LIST(nums, NUMS_COUNT, 0, "%d", 0, true)),
ITEM_BASIC("Example"));
// clang-format on

Expand All @@ -45,15 +44,4 @@ void setup() {

void loop() {
keyboard.observe();
}

// Define the callbacks
void colorsCallback(uint8_t pos) {
// do something with the index
Serial.println(colors[pos]);
}

void numsCallback(uint8_t pos) {
// do something with the index
Serial.println(nums[pos]);
}
22 changes: 13 additions & 9 deletions examples/SimpleRotary/SimpleRotary.ino
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
#include <ItemIntRange.h>
#include <ItemList.h>
#include <ItemToggle.h>
#include <ItemWidget.h>
#include <LcdMenu.h>
#include <MenuScreen.h>
#include <SimpleRotary.h>
#include <display/LiquidCrystal_I2CAdapter.h>
#include <input/SimpleRotaryAdapter.h>
#include <renderer/CharacterDisplayRenderer.h>
#include <widget/WidgetList.h>
#include <widget/WidgetRange.h>

#define LCD_ROWS 2
#define LCD_COLS 16

// Declare the callbacks
void callback(int pos);
void colorsCallback(uint8_t pos);
void toggleBacklight(bool isOn);

String colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
static const char* colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
static const uint8_t COLORS_COUNT = sizeof(colors) / sizeof(colors[0]);

// clang-format off
MENU_SCREEN(mainScreen, mainItems,
ITEM_BASIC("Connect to WiFi"),
ITEM_STRING_LIST("Color", colors, 8, colorsCallback),
ITEM_WIDGET(
"Color",
[](const char* color) { Serial.println(color); },
WIDGET_LIST(colors, COLORS_COUNT, 0, "%s")),
ITEM_BASIC("Blink SOS"),
forntoh marked this conversation as resolved.
Show resolved Hide resolved
ITEM_INT_RANGE("Dist", 0, 50, 0, callback, "%dm"),
ITEM_WIDGET(
"Dist",
callback,
WIDGET_RANGE(0, 1, 0, 50, "%dm", 1, true)),
ITEM_TOGGLE("Backlight", toggleBacklight),
ITEM_BASIC("Blink random"));
// clang-format on
Expand Down Expand Up @@ -51,7 +59,3 @@ void toggleBacklight(bool isOn) {
void callback(int pos) {
Serial.println(pos);
}

void colorsCallback(uint8_t pos) {
Serial.println(colors[pos]);
}
3 changes: 3 additions & 0 deletions src/ItemList.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "LcdMenu.h"
#include "MenuItem.h"

// clang-format off

/**
* @class ItemList
* @brief A class representing a list of items in a menu.
Expand All @@ -29,6 +31,7 @@ class ItemList : public MenuItem {
* @param callback A pointer to the callback function to execute when
* this menu item is selected.
*/
[[deprecated("ItemList is deprecated and will be removed in future versions. Please use ITEM_WIDGET with WIDGET_LIST")]]
forntoh marked this conversation as resolved.
Show resolved Hide resolved
ItemList(const char* key, String* items, const uint8_t itemCount, fptruInt callback)
: MenuItem(key), callback(callback), items(items), itemCount(itemCount) {}

Expand Down
3 changes: 3 additions & 0 deletions src/ItemRangeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "MenuItem.h"
#include <utils/utils.h>

// clang-format off

template <typename T>
/**
* @brief Item that allows user to select a value from a range.
Expand Down Expand Up @@ -44,6 +46,7 @@ class ItemRangeBase : public MenuItem {
* @param step The step value for increment/decrement.
* @param commitOnChange If true, the callback will be called every time the value changes.
*/
[[deprecated("ItemRangeBase is deprecated and will be removed in future versions. Please use ITEM_WIDGET with WIDGET_RANGE")]]
forntoh marked this conversation as resolved.
Show resolved Hide resolved
ItemRangeBase(
const char* text,
const T min,
Expand Down
32 changes: 16 additions & 16 deletions test/IntFloatValues.test.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
name: ItemRangeBase Test
name: WidgetRange Test
version: 1
author: Thomas Forntoh
steps:
- wait-serial: "#LOG# LcdMenu::setScreen"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen::down=1"
- simulate: enterButton-press
- wait-serial: "#LOG# ItemRangeBase::enterEditMode=100"
- wait-serial: "#LOG# ItemWidget::enterEditMode=Dist"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=101"
- wait-serial: "#LOG# WidgetRange::increment=101"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=102"
- wait-serial: "#LOG# WidgetRange::increment=102"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=103"
- wait-serial: "#LOG# WidgetRange::increment=103"
- simulate: downButton-press
- wait-serial: "#LOG# ItemRangeBase::decrement=102"
- wait-serial: "#LOG# WidgetRange::decrement=102"
- simulate: downButton-press
- wait-serial: "#LOG# ItemRangeBase::decrement=101"
- wait-serial: "#LOG# WidgetRange::decrement=101"
- simulate: backButton-press
- wait-serial: "#LOG# ItemRangeBase::exitEditMode=101"
- wait-serial: "#LOG# ItemWidget::exitEditMode=Dist"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen::down=2"
- simulate: enterButton-press
- wait-serial: "#LOG# ItemRangeBase::enterEditMode=-1.00"
- wait-serial: "#LOG# ItemWidget::enterEditMode=Curr"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=-0.99"
- wait-serial: "#LOG# WidgetRange::increment=-0.99"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=-0.98"
- wait-serial: "#LOG# WidgetRange::increment=-0.98"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=-0.97"
- wait-serial: "#LOG# WidgetRange::increment=-0.97"
- simulate: upButton-press
- wait-serial: "#LOG# ItemRangeBase::increment=-0.96"
- wait-serial: "#LOG# WidgetRange::increment=-0.96"
- simulate: downButton-press
- wait-serial: "#LOG# ItemRangeBase::decrement=-0.97"
- wait-serial: "#LOG# WidgetRange::decrement=-0.97"
- simulate: downButton-press
- wait-serial: "#LOG# ItemRangeBase::decrement=-0.98"
- wait-serial: "#LOG# WidgetRange::decrement=-0.98"
- simulate: backButton-press
- wait-serial: "#LOG# ItemRangeBase::exitEditMode=-0.98"
- wait-serial: "#LOG# ItemWidget::exitEditMode=Curr"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen::down=3"
24 changes: 12 additions & 12 deletions test/List.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ steps:
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen::down=1"
- simulate: enterButton-press
- wait-serial: "#LOG# ItemList::enterEditMode=Red"
- wait-serial: "#LOG# ItemWidget::enterEditMode=Color"
- simulate: upButton-press
- wait-serial: "#LOG# ItemList::selectNext=Green"
- wait-serial: "#LOG# WidgetList::nextValue=Green"
- simulate: upButton-press
- wait-serial: "#LOG# ItemList::selectNext=Blue"
- wait-serial: "#LOG# WidgetList::nextValue=Blue"
- simulate: upButton-press
- wait-serial: "#LOG# ItemList::selectNext=Orange"
- wait-serial: "#LOG# WidgetList::nextValue=Orange"
- simulate: backButton-press
- wait-serial: "#LOG# ItemList::exitEditMode=Orange"
- wait-serial: "#LOG# ItemWidget::exitEditMode=Color"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen::down=2"
- simulate: enterButton-press
- wait-serial: "#LOG# ItemList::enterEditMode=5"
- wait-serial: "#LOG# ItemWidget::enterEditMode=Num"
- simulate: downButton-press
- wait-serial: "#LOG# ItemList::selectPrevious=32"
- wait-serial: "#LOG# WidgetList::previousValue=32"
- simulate: downButton-press
- wait-serial: "#LOG# ItemList::selectPrevious=12"
- wait-serial: "#LOG# WidgetList::previousValue=12"
- simulate: upButton-press
- wait-serial: "#LOG# ItemList::selectNext=32"
- wait-serial: "#LOG# WidgetList::nextValue=32"
- simulate: upButton-press
- wait-serial: "#LOG# ItemList::selectNext=5"
- wait-serial: "#LOG# WidgetList::nextValue=5"
- simulate: upButton-press
- wait-serial: "#LOG# ItemList::selectNext=7"
- wait-serial: "#LOG# WidgetList::nextValue=7"
- simulate: backButton-press
- wait-serial: "#LOG# ItemList::exitEditMode=7"
- wait-serial: "#LOG# ItemWidget::exitEditMode=Num"
Loading