Skip to content

examples/buttons: Fix poll mode for button press detection.#1

Open
Nishant-IFX wants to merge 1 commit intomasterfrom
dev/infineon/btn-app-poll-fix
Open

examples/buttons: Fix poll mode for button press detection.#1
Nishant-IFX wants to merge 1 commit intomasterfrom
dev/infineon/btn-app-poll-fix

Conversation

@Nishant-IFX
Copy link

@Nishant-IFX Nishant-IFX commented Dec 11, 2025

Summary

  • Fix the buttons example application to properly support both poll and signal modes for button press detection.
  • The existing implementation had issues with poll mode not correctly handling button press/release events and error conditions.
  • Refactored button state processing into a dedicated function and fixed error handling in both modes.
  • This change affects the buttons example application in nuttx-apps.
  • Related NuttX Apps : [BUG] Button daemon: Button status doesn't get detected when Polling is enabled #2

Impact

  • Is new feature added? Is existing feature changed? YES - Existing buttons example is fixed and refactored to work properly in both poll and signal modes.
  • Impact on user (will user need to adapt to change)? NO - Users can continue using the example as before, but it will now work correctly.
  • Impact on build (will build process change)? NO.
  • Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO - Only the example application is changed.
  • Impact on documentation (is update required / provided)? NO - The example usage remains the same.
  • Impact on security (any sort of implications)? NO.
  • Impact on compatibility (backward/forward/interoperability)? NO - Fully backward compatible.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): WSL (Ubuntu 24.04), x86_64, GCC 13.3.0
  • Target(s): tricore (tc397:buttons), arm (STM32F4Discovery:buttons)
  • Testing: Verified button press and release detection in both CONFIG_EXAMPLES_BUTTONS_POLL and CONFIG_EXAMPLES_BUTTONS_SIGNAL modes on both target boards

Copy link
Member

@parthitce parthitce left a comment

Choose a reason for hiding this comment

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

Thanks for your efforts @Nishant-IFX

#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
Copy link
Member

Choose a reason for hiding this comment

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

nit, alpha sort

static void process_sample(btn_buttonset_t sample,
btn_buttonset_t *oldsample)
{
int i;
Copy link
Member

Choose a reason for hiding this comment

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

move under CONFIG_EXAMPLES_BUTTONS_NAMES

/* Initialize the set to empty, then add the configured button
* signal.
*/

Copy link
Member

Choose a reason for hiding this comment

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

nit, remove empty lines. applies everywhere

@Nishant-IFX
Copy link
Author

Addressed all review comments and updated accordingly. Please review and let me know if any further changes are required.

}

sample = (btn_buttonset_t)value.si_value.sival_int;
process_sample(sample, &oldsample);
Copy link

Choose a reason for hiding this comment

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

CONFIG_EXAMPLES_BUTTONS_NAMES is optional config. If this config is not enabled, then you will run into compilation issues. Ensure, process_sample is called, only when the config is enabled, else print the sample value (as in original code).

}

/* Ignore the default signal action */
{
Copy link

Choose a reason for hiding this comment

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

Below section of code related to signal set looks like a redundant one. Is this really needed?

goto errout_with_fd;
}

if (!(fds[0].revents & POLLIN))
Copy link

Choose a reason for hiding this comment

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

Is this to handle spurious wakeup? If so, comment shall be added before continue as /* Spurious wakeup? */

#ifdef CONFIG_EXAMPLES_BUTTONS_NAMES
process_sample(sample, &oldsample);
#else
printf("Sample = %jd\n", (intmax_t)sample);
Copy link

Choose a reason for hiding this comment

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

Don't replicate this section of code as this is already part of process_sample. Your first commit is correct and I believe you misunderstood Parthiban's comment. His comment is to move variable declaration "int i" inside CONFIG_EXAMPLES_BUTTONS_NAMES and not to move the entire process_sample inside this macro. Fix this.

Copy link

@Siva-IFX Siva-IFX left a comment

Choose a reason for hiding this comment

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

@parthitce - Compliance check has passed and only CI has failed. Hope this is fine.

@Nishant-IFX
Copy link
Author

Added a small follow-up fix: sample values were printing repeatedly in loop. Now printing the sample value only when it changes to avoid redundant output.

@Nishant-IFX
Copy link
Author

@parthitce - please check the latest updates. If everything looks good, could you kindly proceed with the approval?

@parthitce
Copy link
Member

@parthitce - please check the latest updates. If everything looks good, could you kindly proceed with the approval?

Could you please squash the commits? Otherwise good

@Nishant-IFX Nishant-IFX force-pushed the dev/infineon/btn-app-poll-fix branch from d229c96 to b628c19 Compare January 21, 2026 09:22
@Nishant-IFX
Copy link
Author

@parthitce - Squashed all 5 commits into one and updated the PR. Please have a look.

- Refactor button handling to properly support poll mode and ensure
  reliable press/release detection using process_sample()
- Rework button state processing structure based on review feedback and
  align code with NuttX coding standards (include order, formatting, and
  whitespace cleanup)
- Move the loop index variable declaration inside the
  CONFIG_EXAMPLES_BUTTON_NAMES conditional to ensure variables used by
  the directive remain properly scoped and avoid configuration-related
  build warnings
- Fix logic that caused sample values to be printed repeatedly in a loop
  and update output to print only when values change
- Final cleanup of sample print logic and minor refactoring for
  consistency and clarity

Tested on: Infineon’s AURIX™ TC397 Evaluation Board: KIT_A2G_TC397_5V_TFT and STM32F4Discovery

Signed-off-by: Nishant-IFX <Nishant.Kumar@infineon.com>
@Nishant-IFX Nishant-IFX force-pushed the dev/infineon/btn-app-poll-fix branch from b628c19 to f4bbdea Compare January 21, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants