improved bus wait with raii class that updates last access
This commit is contained in:
37
lib/RS485/busdelay.h
Normal file
37
lib/RS485/busdelay.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
class BusDelay
|
||||
{
|
||||
public:
|
||||
BusDelay(uint32_t &lastAccess, const uint32_t minDelay, const char *title) : m_lastAccess(lastAccess)
|
||||
{
|
||||
const uint32_t now = millis();
|
||||
const uint32_t wait = now - lastAccess;
|
||||
if (wait < minDelay)
|
||||
{
|
||||
LOG_WARN(title, "delay", wait);
|
||||
delay(wait);
|
||||
}
|
||||
}
|
||||
|
||||
BusDelay(BusDelay &) = delete;
|
||||
BusDelay operator=(BusDelay &) = delete;
|
||||
|
||||
~BusDelay()
|
||||
{
|
||||
m_lastAccess = millis();
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t &m_lastAccess;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user