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

2 changes: 1 addition & 1 deletion Sming/Libraries/RF24/RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

#include "Arduino.h"
#include "nRF24L01.h"
#include "RF24_config.h"
#include "RF24.h"
Expand Down
2 changes: 1 addition & 1 deletion Sming/Services/DateTime/DateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

#include <stdlib.h>
#include <stdio.h>

Expand Down
10 changes: 10 additions & 0 deletions Sming/SmingCore/ArduinoCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))



#endif /* SMINGCORE_ARDUINOCOMPAT_H_ */
7 changes: 4 additions & 3 deletions Sming/SmingCore/DataSourceStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* All files of the Sming Core are provided under the LGPL v3 license.
****/

#include <algorithm>
#include "../SmingCore/DataSourceStream.h"

int IDataSourceStream::read()
Expand Down Expand Up @@ -91,7 +92,7 @@ size_t MemoryDataStream::write(const uint8_t* data, size_t len)

uint16_t MemoryDataStream::readMemoryBlock(char* data, int bufSize)
{
int available = min(size - (pos - buf), bufSize);
int available = std::min(size - (pos - buf), bufSize);
memcpy(data, pos, available);
return available;
}
Expand Down Expand Up @@ -154,7 +155,7 @@ FileStream::~FileStream()

uint16_t FileStream::readMemoryBlock(char* data, int bufSize)
{
int len = min(bufSize, size - pos);
int len = std::min(bufSize, size - pos);
int available = fileRead(handle, data, len);
fileSeek(handle, pos, eSO_FileStart); // Don't move cursor now (waiting seek)
if(available < 0) {
Expand Down Expand Up @@ -250,7 +251,7 @@ uint16_t TemplateFileStream::readMemoryBlock(char* data, int bufSize)
debug_d("var %s not found", varName.c_str());
state = eTES_Wait;
int len = FileStream::readMemoryBlock(data, bufSize);
return min(len, skipBlockSize);
return std::min(len, skipBlockSize);
}
}
else if (state == eTES_SendingVar)
Expand Down
3 changes: 2 additions & 1 deletion Sming/SmingCore/Network/Http/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* All files of the Sming Core are provided under the LGPL v3 license.
****/

#include <algorithm>
#include "HttpRequest.h"

HttpRequest::HttpRequest(const URL& uri) {
Expand Down Expand Up @@ -159,7 +160,7 @@ String HttpRequest::getBody()
char buf[1024];
while(stream->available() > 0) {
int available = memory->readMemoryBlock(buf, 1024);
memory->seek(max(available, 0));
memory->seek(std::max(available, 0));
ret += String(buf, available);
if(available < 1024) {
break;
Expand Down
3 changes: 2 additions & 1 deletion Sming/SmingCore/Network/Http/Stream/HttpChunkedStream.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "HttpChunkedStream.h"

HttpChunkedStream::HttpChunkedStream(ReadWriteStream *stream)
Expand Down Expand Up @@ -43,7 +44,7 @@ uint16_t HttpChunkedStream::readMemoryBlock(char* data, int bufSize)
int len = readSize;
char buffer[len];
len = stream->readMemoryBlock(buffer, len);
stream->seek(max(len, 0));
stream->seek(std::max(len, 0));
if(len < 1) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Network/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ err_t MqttClient::onReceive(pbuf *buf)
continue;
}

int available = min(waitingSize, buf->tot_len - received);
int available = std::min(waitingSize, buf->tot_len - received);
waitingSize -= available;
if (current != NULL)
{
Expand Down
5 changes: 3 additions & 2 deletions Sming/SmingCore/Network/TcpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* All files of the Sming Core are provided under the LGPL v3 license.
****/

#include <algorithm>
#include "TcpConnection.h"

#include "../../SmingCore/DataSourceStream.h"
Expand Down Expand Up @@ -265,7 +266,7 @@ int TcpConnection::write(IDataSourceStream* stream)
do
{
pushCount++;
int read = min(NETWORK_SEND_BUFFER_SIZE, getAvailableWriteSize());
int read = std::min((uint16_t)NETWORK_SEND_BUFFER_SIZE, getAvailableWriteSize());
if (read > 0)
available = stream->readMemoryBlock(buffer, read);
else
Expand All @@ -275,7 +276,7 @@ int TcpConnection::write(IDataSourceStream* stream)
{
int written = write(buffer, available, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
total += written;
stream->seek(max(written, 0));
stream->seek(std::max(written, 0));
debug_d("Written: %d, Available: %d, isFinished: %d, PushCount: %d [TcpBuf: %d]", written, available, (stream->isFinished()?1:0), pushCount, tcp_sndbuf(tcp));
repeat = written == available && !stream->isFinished() && pushCount < 25;
}
Expand Down
4 changes: 2 additions & 2 deletions Sming/SmingCore/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void SPIClass::transfer(uint8 *buffer, size_t numberBytes) {
while (blocks--) {

// get full BLOCKSIZE or number of remaining bytes
bufLenght = min(numberBytes-bufIndx, (unsigned int)BLOCKSIZE);
bufLenght = std::min(numberBytes-bufIndx, (unsigned int)BLOCKSIZE);

#ifdef SPI_DEBUG
debugf("Write/Read Block %d total %d bytes", total-blocks, bufLenght);
Expand Down Expand Up @@ -453,7 +453,7 @@ void SPIClass::setFrequency(int freq) {
return;
}

freq = min(freq, _CPU_freq/2);
freq = std::min(freq, _CPU_freq/2);
_SPISettings._speed = freq;


Expand Down
3 changes: 0 additions & 3 deletions Sming/SmingCore/SmingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

#include <functional>

#define min(A, B) std::min(A, B)
#define max(A, B) std::max(A, B)

#include "../Wiring/WiringFrameworkIncludes.h"

#include "Delegate.h"
Expand Down
32 changes: 32 additions & 0 deletions Sming/SmingCore/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ Timer& Timer::initializeUs(uint32_t microseconds, TimerDelegate delegateFunction
return *this;
}

Timer& Timer::initializeMs(uint32_t milliseconds, TimerDelegateStdFunction delegateFunction)
{
setCallback(delegateFunction);
setIntervalMs(milliseconds);
return *this;
}

Timer& Timer::initializeUs(uint32_t microseconds, TimerDelegateStdFunction delegateFunction)
{
setCallback(delegateFunction);
setIntervalUs(microseconds);
return *this;
}

void Timer::start(bool repeating/* = true*/)
{
this->repeating = repeating;
Expand Down Expand Up @@ -135,6 +149,7 @@ void Timer::setCallback(InterruptCallback interrupt/* = NULL*/)
ETS_INTR_LOCK();
callback = interrupt;
delegate_func = nullptr;
delegate_stdfunc = nullptr;
ETS_INTR_UNLOCK();

if (!interrupt)
Expand All @@ -146,6 +161,19 @@ void Timer::setCallback(TimerDelegate delegateFunction)
ETS_INTR_LOCK();
callback = nullptr;
delegate_func = delegateFunction;
delegate_stdfunc = nullptr;
ETS_INTR_UNLOCK();

if (!delegateFunction)
stop();
}

void Timer::setCallback(const TimerDelegateStdFunction& delegateFunction)
{
ETS_INTR_LOCK();
callback = nullptr;
delegate_func = nullptr;
delegate_stdfunc = delegateFunction;
ETS_INTR_UNLOCK();

if (!delegateFunction)
Expand Down Expand Up @@ -197,6 +225,10 @@ void Timer::tick()
{
delegate_func();
}
else if (delegate_stdfunc)
{
delegate_stdfunc();
}
else{
stop();
}
Expand Down
36 changes: 29 additions & 7 deletions Sming/SmingCore/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef _SMING_CORE_Timer_H_
#define _SMING_CORE_Timer_H_


#include <functional>
#include "../SmingCore/Interrupts.h"
#include "../SmingCore/Delegate.h"
#include "../Wiring/WiringFrameworkDependencies.h"
Expand All @@ -25,6 +25,7 @@
#define MAX_OS_TIMER_INTERVAL_US 268435000

typedef Delegate<void()> TimerDelegate;
typedef std::function<void()> TimerDelegateStdFunction;

class Timer
{
Expand Down Expand Up @@ -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

*/
Timer& IRAM_ATTR initializeUs(uint32_t microseconds, TimerDelegate delegateFunction = NULL); // Init in Microseconds.

/** @brief Initialise millisecond timer
* @param milliseconds Duration of timer in milliseconds
* @param delegateFunction Function to call when timer triggers
* @note Delegate callback method
*/
Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, TimerDelegateStdFunction delegateFunction = nullptr); // 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
*/
Timer& IRAM_ATTR initializeUs(uint32_t microseconds, TimerDelegateStdFunction delegateFunction = nullptr); // Init in Microseconds.
/** @brief Start timer running
* @param repeating Set to true for repeating timer. Set to false for one-shot.
*/
Expand Down Expand Up @@ -120,11 +136,16 @@ class Timer
*/
void IRAM_ATTR setCallback(InterruptCallback interrupt = NULL);

/** @brief Set timer trigger function
* @param delegateFunction Function to be called on timer trigger
* @note Delegate callback method
*/
void IRAM_ATTR setCallback(TimerDelegate delegateFunction);
/** @brief Set timer trigger function
* @param delegateFunction Function to be called on timer trigger
* @note Delegate callback method
*/
void IRAM_ATTR setCallback(TimerDelegate delegateFunction);
/** @brief Set timer trigger function
* @param delegateFunction Function to be called on timer trigger
* @note Delegate callback method
*/
void IRAM_ATTR setCallback(const TimerDelegateStdFunction& delegateFunction);


protected:
Expand All @@ -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...

bool repeating = false;
bool started = false;

Expand Down
12 changes: 6 additions & 6 deletions Sming/Wiring/WConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@

#define sq(x) ((x)*(x))
//#define abs(x) ((x)>0?(x):-(x))
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
//#ifndef min
//#define min(a,b) ((a)<(b)?(a):(b))
//#endif
//#ifndef max
//#define max(a,b) ((a)>(b)?(a):(b))
//#endif
//#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
Expand Down
Loading