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

Feauture/use index for widget list #297

Conversation

ShishkinDmitriy
Copy link
Collaborator

@ShishkinDmitriy ShishkinDmitriy commented Jan 18, 2025

Description

WidgetList has callback that accept self type T, but it's inconvinient to search it's index inside the callback. As solution this wiget will handle const uint8_t as parameter of callback. If value is needed then it can be obtained by the index.

How Has This Been Tested?

Checked on existing examples


Checklist

General Requirements

  • I have kept this PR in draft until all the required tasks are completed.
  • I have reviewed the contributing guidelines for this project.
  • I have tagged this PR with breaking-change if it introduces a breaking change.
  • I have checked that this PR does not introduce any breaking changes unless explicitly stated.
  • I have checked that changes generate no new warnings.
  • I have performed a self-review of my own code
  • I have built and tested ALL the examples to ensure that I haven't broken anything.

Refactor/Enhancement

  • This PR is a code refactor.
  • I have tagged this PR with enhancement.
  • I have made changes to the code that improve readability/performance/maintainability.
  • I have added documentation for the changes if necessary.
  • I have generated and reviewed the documentation locally if necessary.

Summary by CodeRabbit

Summary by CodeRabbit

  • Documentation

    • Updated documentation for ItemWidget with revised parameter types.
    • Clarified type usage for callback functions.
  • Refactor

    • Standardized parameter types across multiple examples from char* and char to uint8_t.
    • Modified WidgetList class to use uint8_t for list management and value tracking.
  • Bug Fixes

    • Improved type safety for widget and menu system parameter handling.
    • Adjusted expected output values in test sequences to reflect new numeric identifiers.

@ShishkinDmitriy ShishkinDmitriy added the enhancement Improve readability/performance/maintainability label Jan 18, 2025
Copy link
Contributor

coderabbitai bot commented Jan 18, 2025

Walkthrough

The changes primarily involve updating parameter types across multiple files and the WidgetList class from char* and char to uint8_t. This modification affects lambda function signatures in documentation and example files, particularly for color, option, and pin-related widgets. The WidgetList class in src/widget/WidgetList.h has been significantly refactored to use uint8_t as its primary type, removing the activePosition and introducing a value member to track list positions. Additionally, test files have been updated to reflect these changes in expected output values.

Changes

File Change Summary
docs/source/overview/items/item-widget.rst Updated lambda function parameter types from const char* and char to const uint8_t
examples/ButtonWithRotaryAdapter/ButtonWithRotaryAdapter.ino Changed color lambda from const char* color to const uint8_t color
examples/KeypadShield/KeypadShield.ino Modified color selection lambda parameter type from const char* to const uint8_t
examples/List/List.ino Updated color selection lambda to use const uint8_t and adjusted "Num" item functionality
examples/SimpleRotary/SimpleRotary.ino Changed color widget lambda parameter type from const char* to const uint8_t
examples/Widgets/Widgets.ino Updated multiple lambda function signatures to use const uint8_t for "Auto", "Freq", and "Pin" widgets
src/widget/WidgetList.h Refactored WidgetList class to use uint8_t, removed activePosition, added value
test/List.test.yml Modified expected output values to reflect numeric identifiers instead of string values
test/Widgets.test.yml Updated expected output values in test sequence to use numerical representations

Possibly related PRs

  • Add support for additional board types #246: The changes in the main PR regarding the parameter type updates in lambda functions for ITEM_WIDGET examples are related to the modifications in the ButtonWithRotaryAdapter.ino, KeypadShield.ino, Widgets.ino, and other examples, which also involve changing parameter types from const char* to const uint8_t in lambda functions. This indicates a consistent approach to handling parameter types across different components of the project.
  • Update WidgetBool process method to restrict value changes to edit mode #299: While this PR focuses on the WidgetBool class and its process method, it reflects a broader context of ensuring that widget interactions are handled correctly, which aligns with the changes in the main PR that modify how parameters are processed in the ITEM_WIDGET examples.

Suggested labels

feature, bugfix

Poem

🐰 A Rabbit's Ode to Type Transformation 🔢
From char to uint8, we dance and leap,
Bits and bytes in a numeric sweep,
Widgets shimmer with numeric grace,
Precision now finds its rightful place,
Code evolves, a technological treat! 🚀


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ae5039a and e4c0860.

📒 Files selected for processing (1)
  • test/List.test.yml (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: compile (STMicroelectronics:stm32:GenF1, stm32, STM32-stm32-GenF1)
  • GitHub Check: compile (esp8266:esp8266:huzzah, esp8266, esp8266-esp8266-huzzah)
  • GitHub Check: compile (esp32:esp32:esp32, esp32, esp32-esp32-esp32)
🔇 Additional comments (3)
test/List.test.yml (3)

11-15: Validate index-based approach for color changes.

Lines 11, 13, 15 log the progression of nextValue from 1 → 2 → 3. This aligns with the new index-based approach. However, ensure that the total possible values for the color widget includes these indexes and that you’re correctly handling wrap-around or boundary conditions in your production code.


23-25: Double-check reversed index transitions.

Lines 23 and 25 handle previousValue transitions from 4 → 3. Confirm that the starting index of 4 is valid for the underlying list and that no out-of-bound access is possible.


27-31: Confirm wrap-around consistency.

Lines 27, 29, and 31 detect nextValue transitions from 4 → 0 → 1, presumably indicating a wrap-around or cycling feature. Verify that this cycle is intentional and that larger lists or dynamic list sizes won’t cause issues.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🔭 Outside diff range comments (2)
examples/List/List.ino (2)

Line range hint 1-1: High-Level Feedback: Document Breaking Changes

This PR introduces a significant architectural change in how widget callbacks receive their parameters. Instead of receiving the actual values, callbacks now receive indices into their respective arrays. While this change aligns with the PR objective of making it easier to work with indices, it introduces breaking changes that need to be addressed:

  1. All widget callbacks need to be updated to handle indices instead of values
  2. Documentation should be updated to reflect this change
  3. A migration guide should be provided for users
  4. Examples should demonstrate both index and value usage patterns

Consider:

  1. Adding clear documentation about the parameter type changes
  2. Providing helper methods to convert indices to values
  3. Including examples that show both index-based and value-based usage
  4. Adding a migration guide in the PR description

Breaking Change Confirmed: Num widget callback parameter is the array index

The review comment is correct. The WIDGET_LIST implementation shows that the callback receives the index (0-4) into the nums array, not the actual value. The current callback prints indices (0,1,2,3,4) instead of the intended values (5,7,9,12,32).

The suggested fix is appropriate:

-[](const uint8_t num) { Serial.println(num); },
+// num parameter is the index (0-4) into the nums array
+[](const uint8_t num) { Serial.println(nums[num]); },
🔗 Analysis chain

Line range hint 27-29: Breaking Change: Num widget callback parameter changed

The num parameter now receives the index (0-4) into the nums array instead of the actual number. This is a breaking change that needs documentation.

Consider updating the callback to print the actual number:

-[](const uint8_t num) { Serial.println(num); },
+// num parameter is the index (0-4) into the nums array
+[](const uint8_t num) { Serial.println(nums[num]); },
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find WIDGET_LIST implementation and related files
ast-grep --pattern 'WIDGET_LIST'

# Search for documentation or comments about widget list parameters
rg -i "widget.*list.*param|widget.*list.*callback" -A 3 -B 3

# Find other examples using similar pattern
rg "WIDGET_LIST\s*\(" -A 2 -B 2

# Look for any recent changes or documentation in markdown files
fd -e md -x cat {}

Length of output: 19495


Script:

#!/bin/bash
# Find WidgetList class implementation and callback handling
ast-grep --pattern 'class WidgetList'

# Look for callback-related code in WidgetList
rg "callback" src/widget/WidgetList.h -A 5 -B 5

# Find the definition of the nums array
rg "nums\[\]|NUMS_COUNT" examples/List/List.ino -A 2 -B 2

Length of output: 2112

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5bcfdd6 and d07676d.

📒 Files selected for processing (7)
  • docs/source/overview/items/item-widget.rst (3 hunks)
  • examples/ButtonWithRotaryAdapter/ButtonWithRotaryAdapter.ino (1 hunks)
  • examples/KeypadShield/KeypadShield.ino (1 hunks)
  • examples/List/List.ino (1 hunks)
  • examples/SimpleRotary/SimpleRotary.ino (1 hunks)
  • examples/Widgets/Widgets.ino (3 hunks)
  • src/widget/WidgetList.h (3 hunks)
🧰 Additional context used
📓 Learnings (1)
src/widget/WidgetList.h (1)
Learnt from: forntoh
PR: forntoh/LcdMenu#255
File: src/widget/WidgetList.h:61-65
Timestamp: 2024-11-12T11:59:37.512Z
Learning: In `src/widget/WidgetList.h`, the `updateValue` method of `WidgetList<T>` is always called after `activePosition` has been verified by the callers, so additional bounds checking within `updateValue` is not necessary.
⏰ Context from checks skipped due to timeout of 90000ms (14)
  • GitHub Check: run-ci (examples/Widgets/Widgets.ino)
  • GitHub Check: run-ci (examples/ViewShifting/ViewShifting.ino)
  • GitHub Check: run-ci (examples/SubMenu/SubMenu.ino)
  • GitHub Check: run-ci (examples/MenuTimeout/MenuTimeout.ino)
  • GitHub Check: run-ci (examples/List/List.ino)
  • GitHub Check: run-ci (examples/ItemBack/ItemBack.ino)
  • GitHub Check: run-ci (examples/IntFloatValues/IntFloatValues.ino)
  • GitHub Check: run-ci (examples/CharsetInput/CharsetInput.ino)
  • GitHub Check: run-ci (examples/Callbacks/Callbacks.ino)
  • GitHub Check: run-ci (examples/Basic/Basic.ino)
  • GitHub Check: compile (STMicroelectronics:stm32:GenF1, stm32, STM32-stm32-GenF1)
  • GitHub Check: compile (arduino:samd:mkr1000, mkr1000, arduino-samd-mkr1000)
  • GitHub Check: compile (esp8266:esp8266:huzzah, esp8266, esp8266-esp8266-huzzah)
  • GitHub Check: compile (esp32:esp32:esp32, esp32, esp32-esp32-esp32)
🔇 Additional comments (10)
src/widget/WidgetList.h (4)

11-11: Verify the necessity of the template parameter T.

The class WidgetList still uses the template parameter T, but it now inherits from BaseWidgetValue<uint8_t>. Since T is primarily used for the values array, consider whether the template is still required or if the class can be simplified by removing the template parameter.


63-72: Ensure the new draw method handles formatting and buffer sizes correctly.

The addition of the draw method enhances functionality by allowing the widget to render into a specified buffer. Verify that the method correctly formats the output based on the format string and that it safely handles buffer sizes to prevent overflows.


74-90: Confirm boundary checks in nextValue and previousValue methods.

The nextValue and previousValue methods manage the internal value correctly by incrementing or decrementing within the valid range. Ensure that these methods maintain value within 0 and size - 1, and that cycling occurs as expected when cycle is true.


110-118: Update return type consistency in WIDGET_LIST function.

The WIDGET_LIST function now returns a pointer to WidgetList<T> instead of BaseWidgetValue<T>. Confirm that this change is consistent with the rest of the codebase and that it does not introduce compatibility issues with existing code that expects a BaseWidgetValue<T>* return type.

docs/source/overview/items/item-widget.rst (1)

Line range hint 1-1: ⚠️ Breaking Changes Alert

This PR introduces breaking changes to the callback parameter types:

  1. All callbacks now receive indices instead of actual values
  2. Existing code expecting string values will need to be updated
  3. Documentation should clearly explain the new index-based approach

Consider:

  1. Adding a migration guide
  2. Incrementing the major version number
  3. Adding deprecation warnings for the old approach
examples/ButtonWithRotaryAdapter/ButtonWithRotaryAdapter.ino (1)

32-32: ⚠️ Potential issue

Breaking Change: Color widget callback now receives index instead of value

The callback parameter type change from const char* to const uint8_t means the callback now receives the color's index (0-7) instead of the actual color name ("Red", "Green", etc.). This is a breaking change that needs documentation.

Consider adding a comment to clarify the new behavior:

 ITEM_WIDGET(
     "Color",
-    [](const uint8_t color) { Serial.println(color); },
+    // color parameter is the index (0-7) into the colors array
+    [](const uint8_t color) { Serial.println(colors[color]); },
     WIDGET_LIST(colors, COLORS_COUNT, 0, "%s")),
examples/Widgets/Widgets.ino (3)

28-29: ⚠️ Potential issue

Breaking Change: Auto widget callback parameters changed

The option parameter now receives the index (0-1) into the options array instead of the actual option value ("Buy"/"Sell"). This is a breaking change that needs documentation.

Consider updating the callback to print the actual option:

-[](const uint8_t option, bool isAuto) { Serial.println(option); Serial.println(isAuto); },
+// option parameter is the index (0-1) into the options array
+[](const uint8_t option, bool isAuto) { 
+    Serial.println(options[option]); 
+    Serial.println(isAuto); 
+},

Line range hint 54-58: Breaking Change: Pin widget parameters changed

The d1-d4 parameters now receive indices (0-14) into the pinChars array instead of the actual pin characters. This is a breaking change that needs documentation.

Consider updating the callback to print actual characters:

-[](const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4) { Serial.print(d1); Serial.print(d2); Serial.print(d3); Serial.println(d4); },
+// d1-d4 parameters are indices (0-14) into the pinChars array
+[](const uint8_t d1, const uint8_t d2, const uint8_t d3, const uint8_t d4) { 
+    Serial.print(pinChars[d1]); 
+    Serial.print(pinChars[d2]); 
+    Serial.print(pinChars[d3]); 
+    Serial.println(pinChars[d4]); 
+},

42-43: ⚠️ Potential issue

Breaking Change: Freq widget day parameter changed

The day parameter now receives the index (0-6) into the days array instead of the actual day value ("Mon"-"Sun"). This is a breaking change that needs documentation.

Consider updating the callback to print the actual day:

-[](int hour, int minute, const uint8_t day) { Serial.println(hour); Serial.println(minute); Serial.println(day); },
+// day parameter is the index (0-6) into the days array
+[](int hour, int minute, const uint8_t day) { 
+    Serial.println(hour); 
+    Serial.println(minute); 
+    Serial.println(days[day]); 
+},
examples/List/List.ino (1)

24-25: ⚠️ Potential issue

Breaking Change: Color widget callback parameter changed

The color parameter now receives the index (0-7) into the colors array instead of the actual color name. This is a breaking change that needs documentation.

Consider updating the callback to print the actual color:

-[](const uint8_t color) { Serial.println(color); },
+// color parameter is the index (0-7) into the colors array
+[](const uint8_t color) { Serial.println(colors[color]); },

src/widget/WidgetList.h Show resolved Hide resolved
examples/SimpleRotary/SimpleRotary.ino Outdated Show resolved Hide resolved
examples/KeypadShield/KeypadShield.ino Outdated Show resolved Hide resolved
docs/source/overview/items/item-widget.rst Show resolved Hide resolved
Copy link
Contributor

Memory usage change @ d07676d

Board flash % RAM for global variables %
STMicroelectronics:stm32:GenF1 💚 -176 - 0 -0.54 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:uno 💚 -210 - 0 -0.65 - 0.0 💚 -14 - 0 -0.68 - 0.0
arduino:samd:mkr1000 ❔ -168 - +24 -0.06 - +0.01 0 - 0 0.0 - 0.0
esp32:esp32:esp32 ❔ -244 - +28 -0.02 - 0.0 0 - 0 0.0 - 0.0
esp8266:esp8266:huzzah N/A N/A N/A N/A
Click for full report table
Board examples/Basic
flash
% examples/Basic
RAM for global variables
% examples/ButtonAdapter
flash
% examples/ButtonAdapter
RAM for global variables
% examples/Callbacks
flash
% examples/Callbacks
RAM for global variables
% examples/InputRotary
flash
% examples/InputRotary
RAM for global variables
% examples/IntFloatValues
flash
% examples/IntFloatValues
RAM for global variables
% examples/KeyboardAdapter
flash
% examples/KeyboardAdapter
RAM for global variables
% examples/List
flash
% examples/List
RAM for global variables
% examples/SimpleRotary
flash
% examples/SimpleRotary
RAM for global variables
% examples/SSD1803A_I2C
flash
% examples/SSD1803A_I2C
RAM for global variables
% examples/Widgets
flash
% examples/Widgets
RAM for global variables
% examples/RTOS
flash
% examples/RTOS
RAM for global variables
%
STMicroelectronics:stm32:GenF1 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -176 -0.54 0 0.0 -8 -0.02 0 0.0 0 0.0 0 0.0 -104 -0.32 0 0.0
arduino:avr:uno 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -210 -0.65 -14 -0.68 -74 -0.23 0 0.0 0 0.0 0 0.0 -154 -0.48 0 0.0 0 0.0 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -168 -0.06 0 0.0 24 0.01 0 0.0 0 0.0 0 0.0 -48 -0.02 0 0.0
esp32:esp32:esp32 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -244 -0.02 0 0.0 28 0.0 0 0.0 0 0.0 0 0.0 -104 -0.01 0 0.0 0 0.0 0 0.0
esp8266:esp8266:huzzah N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
Click for full report CSV
Board,examples/Basic<br>flash,%,examples/Basic<br>RAM for global variables,%,examples/ButtonAdapter<br>flash,%,examples/ButtonAdapter<br>RAM for global variables,%,examples/Callbacks<br>flash,%,examples/Callbacks<br>RAM for global variables,%,examples/InputRotary<br>flash,%,examples/InputRotary<br>RAM for global variables,%,examples/IntFloatValues<br>flash,%,examples/IntFloatValues<br>RAM for global variables,%,examples/KeyboardAdapter<br>flash,%,examples/KeyboardAdapter<br>RAM for global variables,%,examples/List<br>flash,%,examples/List<br>RAM for global variables,%,examples/SimpleRotary<br>flash,%,examples/SimpleRotary<br>RAM for global variables,%,examples/SSD1803A_I2C<br>flash,%,examples/SSD1803A_I2C<br>RAM for global variables,%,examples/Widgets<br>flash,%,examples/Widgets<br>RAM for global variables,%,examples/RTOS<br>flash,%,examples/RTOS<br>RAM for global variables,%
STMicroelectronics:stm32:GenF1,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-176,-0.54,0,0.0,-8,-0.02,0,0.0,0,0.0,0,0.0,-104,-0.32,0,0.0
arduino:avr:uno,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-210,-0.65,-14,-0.68,-74,-0.23,0,0.0,0,0.0,0,0.0,-154,-0.48,0,0.0,0,0.0,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-168,-0.06,0,0.0,24,0.01,0,0.0,0,0.0,0,0.0,-48,-0.02,0,0.0,,,,
esp32:esp32:esp32,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-244,-0.02,0,0.0,28,0.0,0,0.0,0,0.0,0,0.0,-104,-0.01,0,0.0,0,0.0,0,0.0
esp8266:esp8266:huzzah,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,,,,

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d07676d and 0a56c3f.

📒 Files selected for processing (7)
  • docs/source/overview/items/item-widget.rst (4 hunks)
  • examples/ButtonWithRotaryAdapter/ButtonWithRotaryAdapter.ino (2 hunks)
  • examples/KeypadShield/KeypadShield.ino (1 hunks)
  • examples/List/List.ino (2 hunks)
  • examples/SimpleRotary/SimpleRotary.ino (1 hunks)
  • examples/Widgets/Widgets.ino (3 hunks)
  • src/widget/WidgetList.h (3 hunks)
🧰 Additional context used
📓 Learnings (1)
src/widget/WidgetList.h (1)
Learnt from: forntoh
PR: forntoh/LcdMenu#255
File: src/widget/WidgetList.h:61-65
Timestamp: 2024-11-12T11:59:37.512Z
Learning: In `src/widget/WidgetList.h`, the `updateValue` method of `WidgetList<T>` is always called after `activePosition` has been verified by the callers, so additional bounds checking within `updateValue` is not necessary.
🪛 GitHub Check: clang_format
src/widget/WidgetList.h

[failure] 11-11: src/widget/WidgetList.h#L11
code should be clang-formatted [-Wclang-format-violations]


[failure] 12-12: src/widget/WidgetList.h#L12
code should be clang-formatted [-Wclang-format-violations]

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: compile (STMicroelectronics:stm32:GenF1, stm32, STM32-stm32-GenF1)
  • GitHub Check: compile (arduino:samd:mkr1000, mkr1000, arduino-samd-mkr1000)
  • GitHub Check: compile (esp8266:esp8266:huzzah, esp8266, esp8266-esp8266-huzzah)
  • GitHub Check: compile (esp32:esp32:esp32, esp32, esp32-esp32-esp32)
🔇 Additional comments (26)
src/widget/WidgetList.h (8)

9-13: Good clarification on index-based value usage.
These doc comments clearly explain that the widget’s value is now an index of type uint8_t rather than a templated T. This helps prevent confusion for callers and maintainers.

🧰 Tools
🪛 GitHub Check: clang_format

[failure] 11-11: src/widget/WidgetList.h#L11
code should be clang-formatted [-Wclang-format-violations]


[failure] 12-12: src/widget/WidgetList.h#L12
code should be clang-formatted [-Wclang-format-violations]


16-16: Confirm necessity of template when the base is fixed to <uint8_t>.
While WidgetList is templated as <typename T>, the parent class is BaseWidgetValue<uint8_t>. This design is valid if the template parameter T is only used for storing the array of possible values. Just confirm whether tying the parent to <uint8_t> is intentional and if you anticipate further refactoring to unify the design.


30-31: Potential breaking change for callback signatures.
This changes the callback from void (*callback)(const T&) to void (*callback)(const uint8_t&), affecting existing code that passes or expects <T>. Update usage references accordingly; documentation updates appear to be in place.


65-65: No additional bounds checking needed.
Per the retrieved learnings, updateValue is always preceded by verified value changes. Calling BaseWidgetValue<uint8_t>::handleChange() here is appropriate.


68-77: Confirm safe usage of snprintf in draw method.
The call to snprintf properly limits output size to ITEM_DRAW_BUFFER_SIZE - start, preventing buffer overruns. The usage of values[value] is correct as long as value < size.


79-80: Check for nextValue overflow if size > 255.
if (value + 1 < size) works well as long as size <= 255 matches value’s limit. If a widget list ever has more than 255 entries, the nextValue logic could fail or overflow.

Also applies to: 84-84


90-91: Maintain symmetry in previousValue.
Decrementing value and wrapping around works similarly well under the same constraints as nextValue. Great job mirroring the logic.

Also applies to: 95-95


108-108: Accurate doc param name.
Renaming “active position” to “value” matches the code. This avoids confusion about the meaning of the parameter.

examples/SimpleRotary/SimpleRotary.ino (1)

28-28: Ensure index-based access is in range.
Indexing colors[color] is correct, but confirm that color is always < COLORS_COUNT to avoid out-of-bounds. If your upstream logic guarantees this, you’re all set.

examples/KeypadShield/KeypadShield.ino (1)

24-24: Use index-based lookup consistently.
Switching to (const uint8_t color){ Serial.println(colors[color]); } is a clean approach to print the color name by index. Just ensure the color parameter cannot exceed COLORS_COUNT.

examples/Widgets/Widgets.ino (2)

54-54: Confirm indexes for pin digits.

Each digit parameter (d1, d2, d3, d4) is used to index the pinChars array. Ensure that these values always remain within valid limits to prevent out-of-bounds access.

Below is a script checking usage of pinChars[d*]:

✅ Verification successful

The index access of pinChars array is properly constrained

The implementation is safe because:

  • The pinChars array has 15 characters ("123456789ABCDEF")
  • WIDGET_LIST constrains selection using strlen(pinChars)
  • Parameters are uint8_t ensuring positive indices
  • The widget system is designed for controlled character selection
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
rg -A 3 'pinChars\[d'

Length of output: 543


Script:

#!/bin/bash
# Find pinChars definition and surrounding context
rg -B 3 -A 3 'pinChars\s*\[\s*\]|pinChars\s*=|char\s+pinChars'

# Get broader context of the widget setup
rg -B 5 -A 5 'WIDGET_LIST.*pinChars'

Length of output: 3510


28-28: Ensure array bounds for 'option' parameter.

When using options[option], verify that the provided option index is always within the valid array range. Otherwise, an out-of-bounds access could occur.

Below is a script that searches for all references to options[option] to confirm the approach:

✅ Verification successful

Array access is properly bounded by design

The array access is safe because:

  • The options array size (2) is explicitly passed to WIDGET_LIST using sizeof
  • WidgetList class internally tracks the array size
  • The option parameter is uint8_t, matching the size type used by WidgetList
  • The widget implementation manages value bounds as an index
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify all references to 'options[option]' for potential out-of-bounds usage
rg -A 3 'options\[option\]'

Length of output: 405


Script:

#!/bin/bash
# Find options array definition and related context
rg -B 5 -A 2 'options\[\s*\]' examples/Widgets/Widgets.ino

# Look for WIDGET_LIST macro definition
rg -B 2 -A 5 'WIDGET_LIST' --type cpp

Length of output: 1392


Script:

#!/bin/bash
# Look for WidgetList implementation to verify bounds checking
ast-grep --pattern 'template <typename T>
class WidgetList {
  $$$
}'

# Backup search with ripgrep
rg -B 2 -A 10 'class WidgetList' --type cpp

Length of output: 1408

examples/List/List.ino (3)

24-24: Check correctness of color index usage.

Accessing colors[color] is valid as long as color remains within 0..(COLORS_COUNT-1). Confirm the index is never out of range.


28-28: Verify numeric index for 'nums'.

Ensure num < NUMS_COUNT to avoid out-of-bounds issues.


47-47: No concerns with this line.

examples/ButtonWithRotaryAdapter/ButtonWithRotaryAdapter.ino (2)

32-32: Validate index for 'colors'.

When the callback uses Serial.println(colors[color]);, confirm that color is within 0..(COLORS_COUNT-1).


67-67: No substantive change at this line.

docs/source/overview/items/item-widget.rst (9)

24-24: Good overview of ItemWidget examples.

The introduction clearly indicates how ItemWidget is used, matching the new index-based approach.


32-32: Parameter type update is consistent with PR objectives.

Thank you for reflecting the transition to const uint8_t in the docs so that users understand the new callback signature.


40-41: Clear explanation of index-based approach.

The newly added note clarifies how the callback receives indices, not string values. This addresses potential confusion effectively.


43-43: Context about the 'auto' option usage looks correct.

The documentation matches the callback signature, ensuring consistency.


69-69: Correct highlight for the 'day' parameter's new type.

This line accurately reflects the shift to const uint8_t. Matches the code updates.


78-79: Index-based note ensures clarity for 'day'.

Thanks for reiterating that the callback receives day as an index. This helps users avoid confusion.


107-107: Accurate reflection of pin parameter changes.

Shifting from char to const uint8_t is properly documented and will help users adapt.


113-114: Reiterated index-based note for pin digits.

Clear statement that the callback gets indices, not characters.


119-119: Documentation reference is complete.

Linking to the example ensures readers can see the index-based usage in a practical context.

src/widget/WidgetList.h Outdated Show resolved Hide resolved
src/widget/WidgetList.h Show resolved Hide resolved
src/widget/WidgetList.h Outdated Show resolved Hide resolved
examples/Widgets/Widgets.ino Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 23, 2025
@ShishkinDmitriy ShishkinDmitriy marked this pull request as ready for review January 23, 2025 23:50
Copy link
Contributor

Memory usage change @ 43919fb

Board flash % RAM for global variables %
STMicroelectronics:stm32:GenF1 💚 -144 - 0 -0.44 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:uno 💚 -208 - 0 -0.64 - 0.0 💚 -14 - 0 -0.68 - 0.0
arduino:samd:mkr1000 💚 -136 - 0 -0.05 - 0.0 0 - 0 0.0 - 0.0
esp32:esp32:esp32 💚 -200 - 0 -0.02 - 0.0 0 - 0 0.0 - 0.0
esp8266:esp8266:huzzah N/A N/A N/A N/A
Click for full report table
Board examples/Basic
flash
% examples/Basic
RAM for global variables
% examples/ButtonAdapter
flash
% examples/ButtonAdapter
RAM for global variables
% examples/Callbacks
flash
% examples/Callbacks
RAM for global variables
% examples/InputRotary
flash
% examples/InputRotary
RAM for global variables
% examples/IntFloatValues
flash
% examples/IntFloatValues
RAM for global variables
% examples/KeyboardAdapter
flash
% examples/KeyboardAdapter
RAM for global variables
% examples/List
flash
% examples/List
RAM for global variables
% examples/SimpleRotary
flash
% examples/SimpleRotary
RAM for global variables
% examples/SSD1803A_I2C
flash
% examples/SSD1803A_I2C
RAM for global variables
% examples/Widgets
flash
% examples/Widgets
RAM for global variables
% examples/RTOS
flash
% examples/RTOS
RAM for global variables
%
STMicroelectronics:stm32:GenF1 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -144 -0.44 0 0.0 -16 -0.05 0 0.0 0 0.0 0 0.0 -88 -0.27 0 0.0
arduino:avr:uno 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -208 -0.64 -14 -0.68 -38 -0.12 0 0.0 0 0.0 0 0.0 -102 -0.32 0 0.0 0 0.0 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -136 -0.05 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -40 -0.02 0 0.0
esp32:esp32:esp32 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -200 -0.02 0 0.0 -4 -0.0 0 0.0 0 0.0 0 0.0 -116 -0.01 0 0.0 0 0.0 0 0.0
esp8266:esp8266:huzzah N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
Click for full report CSV
Board,examples/Basic<br>flash,%,examples/Basic<br>RAM for global variables,%,examples/ButtonAdapter<br>flash,%,examples/ButtonAdapter<br>RAM for global variables,%,examples/Callbacks<br>flash,%,examples/Callbacks<br>RAM for global variables,%,examples/InputRotary<br>flash,%,examples/InputRotary<br>RAM for global variables,%,examples/IntFloatValues<br>flash,%,examples/IntFloatValues<br>RAM for global variables,%,examples/KeyboardAdapter<br>flash,%,examples/KeyboardAdapter<br>RAM for global variables,%,examples/List<br>flash,%,examples/List<br>RAM for global variables,%,examples/SimpleRotary<br>flash,%,examples/SimpleRotary<br>RAM for global variables,%,examples/SSD1803A_I2C<br>flash,%,examples/SSD1803A_I2C<br>RAM for global variables,%,examples/Widgets<br>flash,%,examples/Widgets<br>RAM for global variables,%,examples/RTOS<br>flash,%,examples/RTOS<br>RAM for global variables,%
STMicroelectronics:stm32:GenF1,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-144,-0.44,0,0.0,-16,-0.05,0,0.0,0,0.0,0,0.0,-88,-0.27,0,0.0
arduino:avr:uno,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-208,-0.64,-14,-0.68,-38,-0.12,0,0.0,0,0.0,0,0.0,-102,-0.32,0,0.0,0,0.0,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-136,-0.05,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-40,-0.02,0,0.0,,,,
esp32:esp32:esp32,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-200,-0.02,0,0.0,-4,-0.0,0,0.0,0,0.0,0,0.0,-116,-0.01,0,0.0,0,0.0,0,0.0
esp8266:esp8266:huzzah,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,,,,

Copy link
Owner

@forntoh forntoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality Tests are failing for WidgetList @ShishkinDmitriy, please adjust List.test.yml and Widgets.test.yml
The tests originally are expecting the actual value instead of the index, they have to be updated to expect index.

@ShishkinDmitriy
Copy link
Collaborator Author

Functionality Tests are failing for WidgetList @ShishkinDmitriy, please adjust List.test.yml and Widgets.test.yml The tests originally are expecting the actual value instead of the index, they have to be updated to expect index.

How to run it locally?

@forntoh
Copy link
Owner

forntoh commented Jan 24, 2025

How to run it locally?

It's quite a setup @ShishkinDmitriy, you first need docker... lot's of stuff, set up 🥲
What I usually do is just run the example and perform the operations in the test file and then adjust the expected serial output with what I get from my terminal.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43919fb and ae5039a.

📒 Files selected for processing (2)
  • test/List.test.yml (1 hunks)
  • test/Widgets.test.yml (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: compile (STMicroelectronics:stm32:GenF1, stm32, STM32-stm32-GenF1)
  • GitHub Check: compile (arduino:samd:mkr1000, mkr1000, arduino-samd-mkr1000)
  • GitHub Check: compile (esp8266:esp8266:huzzah, esp8266, esp8266-esp8266-huzzah)
  • GitHub Check: compile (esp32:esp32:esp32, esp32, esp32-esp32-esp32)
🔇 Additional comments (9)
test/List.test.yml (3)

11-15: Switch to index-based output validation for nextValue

These changes correctly transition from referencing string values to using numeric indices. This aligns with the new const uint8_t parameter usage in the widget callback.


23-25: Index-based previousValue checks

Switching the test expectations to numeric indices is consistent with the new logic for backward navigation in the widget list. Ensure all references to old string-based values have been removed across the codebase.


27-31: Confirm range wrap-around behavior

The transitions from one index to another (e.g., 402) suggest wrap-around or jump behavior. Verify that the underlying widget logic intentionally allows these leaps, and the expected output for each step is correct.

Run the following commands to confirm consistency across all test files:

✅ Verification successful

Wrap-around behavior is verified and intentional

The test files consistently demonstrate wrap-around navigation and non-sequential transitions as expected behavior, with similar patterns found in both test/List.test.yml and test/Widgets.test.yml.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Examine references to WidgetList next/previous values to verify consistent indexing
rg 'WidgetList::(nextValue|previousValue)=' -A 2 test

Length of output: 4031

test/Widgets.test.yml (6)

9-15: Consistent numeric indices for toggling values

Converting from string-based values to numeric indices is logical given the new uint8_t approach. These lines appear consistent and straightforward.


116-122: Sequential increments of nextValue

You are correctly testing a series of nextValue increments from 1 through 4. This sequence demonstrates progression in the index-based approach. Confirm the final user-facing output matches your widget logic.


135-137: Confirm mid-sequence increments

The new lines verify that successive nextValue calls jump from 3 to 4. This indicates a valid numeric progression. Keep an eye on any edge cases for boundary conditions.


145-145: Index-based previousValue=3 test case

Using previousValue=3 in this context is consistent with the rollback behavior. No issues found.


149-151: Extended range values for nextValue

The updates from 11 to 12 reflect a larger range of permissible indices. Ensure these larger indices are valid and supported by your new logic.


155-157: Check wrap-around for nextValue=01

When wrapping from higher indices down to 0 and then incrementing, confirm the list boundaries are properly handled.

test/Widgets.test.yml Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 24, 2025
@forntoh forntoh marked this pull request as draft January 24, 2025 00:35
@forntoh forntoh marked this pull request as ready for review January 24, 2025 00:35
@forntoh forntoh marked this pull request as draft January 24, 2025 00:53
@forntoh forntoh marked this pull request as ready for review January 24, 2025 00:53
@ShishkinDmitriy ShishkinDmitriy merged commit 984f29d into forntoh:master Jan 24, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve readability/performance/maintainability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants