-
Notifications
You must be signed in to change notification settings - Fork 101
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
Change channel type in event module #40
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- CVM and TEGRA_SOC use strings instead of numbers. So the channel is NOT always convertible to int. - in wait_for_edge, add_event_detect function, std::atoi is used to convert the channel to int . If the user is using CVM orTEGRA_SOC, these functions will throw an exception. - see #39
I came up with some options for the return type of wait_for_edge function: Option 1. Use different types for each overloading function (now)// return the channel for which the edge was detected.
//If a timeout occurred, it returns 0(if the channel is int) or an empty string(if the channel is string).
std::string wait_for_edge(const std::string& channel, Edge edge, unsigned long bounce_time = 0, unsigned long timeout = 0);
int wait_for_edge(int channel, Edge edge, unsigned long bounce_time = 0, unsigned long timeout = 0);
//...
// in the user code,
if (wait_for_edge(...) == channel)
// or
// for string channel
if(!wait_for_edge(...).empty())
// for int channel
if(wait_for_edge(...))
// ... Option 2. create a GPIO::WaitResult just for it.class WaitResult
{
public:
WaitResult(const std::string& channel) : _channel(channel){}
bool is_event_detected() const { return !channel().empty(); };
operator bool() const { return is_event_detected(); }
// can get the channel info
const std::string& channel() const { return _channel; }
private:
std::string _channel;
};
// both versions return GPIO::WaitResult object
WaitResult wait_for_edge(const std::string& channel, unsigned long bounce_time = 0, unsigned long timeout = 0);
WaitResult wait_for_edge(int channel, Edge edge, unsigned long bounce_time = 0, unsigned long timeout = 0);
// ...
// in the user code,
if(wait_for_edge( .... ).is_event_detected())
// or just
if(wait_for_edge( .... ))
// ...
//...
auto result = wait_for_edge( .... );
std::string channel = result.channel(); Option 3. always return std::string// return empty string on timeout
std::string wait_for_edge(const std::string& channel, Edge edge, unsigned long bounce_time = 0, unsigned long timeout = 0);
std::string wait_for_edge(int channel, Edge edge, unsigned long bounce_time = 0, unsigned long timeout = 0);
// in the user code,
if(!wait_for_edge(...).empty())
@ShimmyShaman |
Option 2 is a nice solution. |
ShimmyShaman
approved these changes
Jan 15, 2022
Thank you for the review. |
- add GPIO::WaitResult class - change the return type of wait_for_edge to WaitResult - update README.md
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a fix for the issue #39.
(Summary: The channel type in event module should be changed because it is NOT always convertible to int. )
Tested it with
samples/test_all_apis.cpp
.@ShimmyShaman
Could you review the code please?
Major changes:
wait_for_edge
,add_event_detect
functions changedWant to discuss:
wait_for_edge
function?