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

Make Delegators Compatible with Lambdas and std::function - issue 1337 #1361

Conversation

frankdownunder
Copy link
Contributor

Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, TimerDelegateStdFunction delegateFunction = nullptr); // Init in Milliseconds.

For the moment the others can stay.
This allows for easier comment and review.
The sample code Basic_Delegate has example calls for old style delegates,
plainOldOrdinaryFunctions, memberFunctions and lamdas.

The defines for min and max in SmingCore.h have been moved into ArduinoCompat.h
This allows the use of the header which has std::max and std::min.

Macros are nasty, especially those with the same names as standard functions. I can see those macros are needed for code that comes form the Arduino world, and they are still available by including Arduino.h

```
Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, TimerDelegateStdFunction delegateFunction = nullptr); // Init in Milliseconds.
```
For the moment the others can stay.
This allows for easier comment and review.
The sample code Basic_Delegate has example calls for old style delegates,
plainOldOrdinaryFunctions,  memberFunctions and lamdas.

The defines for min and max in SmingCore.h have been moved into ArduinoCompat.h

This allows the use of the <algorithm> header which has std::max and std::min.
@frankdownunder frankdownunder changed the title For the Timer class, I have added 2 new functions; std::function For the Timer class, I have added 2 new functions; Apr 12, 2018
@frankdownunder frankdownunder changed the title std::function For the Timer class, I have added 2 new functions; Make Delegators Compatible with Lambdas and std::function - issue 1337 Apr 12, 2018
@slaff slaff requested a review from anakod April 12, 2018 07:02
@@ -8,7 +8,7 @@
*/

#include "DateTime.h"
#include <Arduino.h>
//#include <Arduino.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, remove the commented line.

@@ -59,16 +60,31 @@ class Timer
* @param milliseconds Duration of timer in milliseconds
* @param delegateFunction Function to call when timer triggers
* @note Delegate callback method
*/
* @Deprecated Use initializeMs(xx, TimerDelegateStdFunction); instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix the indentation to this line and the one below. Use lowercase D: ex: @deprecated ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Timer.h has spaces for indentation, while Timer.cpp has tabs, - what is the standard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually only the comments in Timer.h use spaces - not very consistent!

Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, TimerDelegate delegateFunction = NULL); // Init in Milliseconds.

/** @brief Initialise microsecond timer
* @param microseconds Duration of timer in milliseconds
* @param delegateFunction Function to call when timer triggers
* @note Delegate callback method
* @Deprecated Use initializeMs(xx, TimerDelegateStdFunction); instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indentation and lower case D

@@ -142,6 +163,7 @@ class Timer
uint64_t interval = 0;
InterruptCallback callback = nullptr;
TimerDelegate delegate_func = nullptr;
TimerDelegateStdFunction delegate_stdfunc = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indentation...

@@ -9,19 +15,85 @@ public :
pinMode(ledPin, OUTPUT);
};
bool setTimer(int reqInterval) {
if (reqInterval <= 0) return false;
if (reqInterval <= 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add curly braces around the if condition.

myLed2.setTimer(500);
myLed2.blink(true);
myLed2.blinkOldDelegate(true);

Copy link
Contributor

Choose a reason for hiding this comment

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

Be consistent with the line spacing between the different groups of commands.

@@ -25,5 +25,15 @@ void yield();
}
#endif

#define abs(x) ((x)>0?(x):-(x))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for my understanding: Why you add those definitions here and remove them from WConstants.h ? Why not just leave them in WConstants.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried them there, but I had trouble getting things to compile. It would be good to get rid of those macros altogether but I understand we need to support the older style Arduino code. Happy to see them move to a better place.

@@ -9,19 +15,87 @@ public :
pinMode(ledPin, OUTPUT);
};
bool setTimer(int reqInterval) {
if (reqInterval <= 0) return false;
if (reqInterval <= 0)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be consistent with the rest of the code, please, move the opening CB at the end of the previous line.

() // No parameters to the callback
{
debugf("lamda Callback ");
if (foo > 123)
Copy link
Contributor

Choose a reason for hiding this comment

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

Braces around if.

() // No parameters to the callback
{
debugf("lamda Callback ");
if (foo > 123) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@frankdownunder In the if condition shouldn't there be an equal sign instead of bigger ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ive fixed that now.

@slaff slaff added this to the 3.5.2 milestone Apr 17, 2018
@slaff slaff removed the 0 - Backlog label Apr 17, 2018
@slaff
Copy link
Contributor

slaff commented Apr 24, 2018

@frankdownunder I like the idea of moving min/max macros to AruinoCompat.h and will do it in a separate PR. Once merged, please rebase your PR and try the following:

Simplify the example as much as possible

  • remove led blinking
  • demo the results using Serial print only.
  • don't pass a local variable by reference to a lamda that will be used also when the local variable is out of scope - better will be to pass a local variable by value.
  • improve the example with std::bind to call members that have parameters.

@slaff slaff removed this from the 3.5.2 milestone Apr 24, 2018
slaff and others added 4 commits April 24, 2018 15:31
…s. (SmingHub#1368)

The old Arduino libs can still continue to use the macros.
```
Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, TimerDelegateStdFunction delegateFunction = nullptr); // Init in Milliseconds.
```
For the moment the others can stay.
This allows for easier comment and review.
The sample code Basic_Delegate has example calls for old style delegates,
plainOldOrdinaryFunctions,  memberFunctions and lamdas.

The defines for min and max in SmingCore.h have been moved into ArduinoCompat.h

This allows the use of the <algorithm> header which has std::max and std::min.
…b.com/frankdownunder/Sming into feature/DelegatorsAndStdFunction_1337

# Conflicts:
#	samples/Basic_Delegates/app/application.cpp
@frankdownunder
Copy link
Contributor Author

@slaff I had a great deal of trouble dealing with rebase. I do not know if I did it correctly or not. Pls check carefully

@slaff
Copy link
Contributor

slaff commented May 2, 2018

I do not know if I did it correctly or not. Pls check carefully

@frankdownunder

Save somewhere your modified samples/Basic_Delegates/app/application.cpp, Timer.cpp and Timer.h. Get the latest version from develop and branch from it. In the new branch copy the modified files that you have saved earlier. Add the changes to the new branch and make a new PR.

OR

You can read "Rebase if needed" from CONTRIBUTING.md. Basically what you have to do is:

git checkout develop
git pull # make sure to get the latest develop version
git checkout feature/DelegatorsAndStdFunction_1337
git merge develop
git mergetool # if needed use your merge tool
# Once you are ready with the merge commit the changes, if that is not already the case.
git status # will tell you what you should do next.
# Now put your changes on top
git rebase develop
git mergetool # if needed use your merge tool
git status # will tell you what you should do next.
# once ready do
git push -f # to push the changes to the remote repo.

@slaff slaff mentioned this pull request May 3, 2018
24 tasks
@frankdownunder
Copy link
Contributor Author

@slaff Is there anything more I need to do on this? Did I do the rebase corectly?

@slaff
Copy link
Contributor

slaff commented May 14, 2018

https://github.com/SmingHub/Sming/pull/1361/files

@frankdownunder I don't see any rebase ( https://github.com/SmingHub/Sming/pull/1361/files ) or recent change from your side. Can you please check if you forgot to push the changes to the GitHub repository servers?

@frankdownunder
Copy link
Contributor Author

This PR has been superseded by #1378

@slaff slaff removed the 3 - Review label May 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants