firmware/src/concurrency/InterruptableDelay.h

42 lines
967 B
C
Raw Normal View History

2020-10-10 01:57:57 +00:00
#pragma once
#include "../freertosinc.h"
2020-10-11 01:18:47 +00:00
#ifdef HAS_FREE_RTOS
#include "concurrency/BinarySemaphoreFreeRTOS.h"
#define BinarySemaphore BinarySemaphoreFreeRTOS
#else
#include "concurrency/BinarySemaphorePosix.h"
#define BinarySemaphore BinarySemaphorePosix
#endif
2020-10-10 01:57:57 +00:00
namespace concurrency
{
/**
* An object that provides delay(msec) like functionality, but can be interrupted by calling interrupt().
*
* Useful for they top level loop() delay call to keep the CPU powered down until our next scheduled event or some external event.
*
* This is implmented for FreeRTOS but should be easy to port to other operating systems.
*/
class InterruptableDelay
{
2020-10-11 01:18:47 +00:00
BinarySemaphore semaphore;
2020-10-10 01:57:57 +00:00
public:
InterruptableDelay();
~InterruptableDelay();
/**
* Returns false if we were interrupted
*/
bool delay(uint32_t msec);
void interrupt();
void interruptFromISR(BaseType_t *pxHigherPriorityTaskWoken);
};
} // namespace concurrency