Added wrapper for PowerScheduler
This commit is contained in:
@@ -10,114 +10,41 @@
|
||||
#include "Power.h"
|
||||
static portMUX_TYPE mux;
|
||||
|
||||
Power::Power()
|
||||
{
|
||||
// TODO: Create wrappper around all this which handles single-instancing so we look like a normal arduino library from the outside.
|
||||
this->freePowerBudget = this->totalPowerBudget;
|
||||
}
|
||||
|
||||
void Power::begin()
|
||||
{
|
||||
if (powerInstance == nullptr)
|
||||
// Check if another instance of us already initialized the power scheduler,
|
||||
// if not, we will do it.
|
||||
if (powerScheduler == nullptr)
|
||||
{
|
||||
// Double check locking https://www.aristeia.com/Papers/DDJ%5FJul%5FAug%5F2004%5Frevised.pdf
|
||||
taskENTER_CRITICAL(&mux);
|
||||
if (powerInstance == nullptr)
|
||||
powerScheduler = PowerScheduler::getPowerScheduler();
|
||||
if (!(powerScheduler->tryAccquirePowerAllowance(CONSUMPTION_ESP_BASE)))
|
||||
{
|
||||
powerInstance = new Power();
|
||||
Serial.println("Alledgedly not enough power available to reserve the ESP32s base power consumption. Something is wrong.");
|
||||
return;
|
||||
}
|
||||
taskEXIT_CRITICAL(&mux);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Power.begin() was called twice!
|
||||
Serial.println("Power.begin() was called twice! No harm done, but this is a bug.");
|
||||
}
|
||||
}
|
||||
|
||||
Power *Power::getPowerManager()
|
||||
uint16_t Power::getFreePowerBudget(void)
|
||||
{
|
||||
return powerInstance;
|
||||
return powerScheduler->getFreePowerBudget();
|
||||
}
|
||||
|
||||
bool Power::tryAccquirePowerAllowance(uint16_t neededPower)
|
||||
{
|
||||
if (this->freePowerBudget >= neededPower)
|
||||
{
|
||||
this->freePowerBudget -= neededPower;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return powerScheduler->tryAccquirePowerAllowance(neededPower);
|
||||
}
|
||||
|
||||
bool Power::waitForPowerAllowance(uint16_t neededPower, TickType_t TicksToWait)
|
||||
{
|
||||
return powerScheduler->waitForPowerAllowance(neededPower, TicksToWait);
|
||||
}
|
||||
|
||||
void Power::beginPermanentDeepSleep(void)
|
||||
{
|
||||
return powerScheduler->beginPermanentDeepSleep();
|
||||
}
|
||||
|
||||
void Power::releasePower(uint16_t power)
|
||||
{
|
||||
if (this->freePowerBudget + power <= this->totalPowerBudget)
|
||||
{
|
||||
this->freePowerBudget += power;
|
||||
}
|
||||
else // TODO: Maybe we should actually throw an error here, since obviouslsy someone used us wrong.
|
||||
{
|
||||
this->freePowerBudget = this->totalPowerBudget;
|
||||
}
|
||||
// Check if there are tasks waiting for power
|
||||
checkWaitingTasks();
|
||||
}
|
||||
|
||||
bool Power::waitForPowerAllowance(uint16_t neededPower, TickType_t ticksToWait)
|
||||
{
|
||||
if (tryAccquirePowerAllowance(neededPower))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Suspend the task while waiting for power to be available
|
||||
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
|
||||
TickType_t initialTickCount = xTaskGetTickCount();
|
||||
waitingTasks.push(currentTask);
|
||||
uint32_t notificationValue;
|
||||
BaseType_t notificationStatus = xTaskNotifyWait(0, 0, ¬ificationValue, ticksToWait);
|
||||
// Code below will be executed after the task is woken up
|
||||
while (notificationStatus == pdPASS)
|
||||
{
|
||||
if (notificationValue == POWER_AVAILABLE)
|
||||
{
|
||||
// We were woken up because new power is available, check if it is enough
|
||||
if (tryAccquirePowerAllowance(neededPower))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Still not enough power available for us. Wait the remaining ticks.
|
||||
xTaskNotifyWait(0, 0, ¬ificationValue, ticksToWait - (xTaskGetTickCount() - initialTickCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (notificationStatus == pdFALSE)
|
||||
{
|
||||
// We waited long enough...
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Should be impossible to reach
|
||||
throw "Reached impossible state";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Power::checkWaitingTasks(void)
|
||||
{
|
||||
// Check if there are tasks waiting for power
|
||||
if (!waitingTasks.empty())
|
||||
{
|
||||
TaskHandle_t task = waitingTasks.front();
|
||||
waitingTasks.pop();
|
||||
xTaskNotify(task, POWER_AVAILABLE, eSetValueWithOverwrite);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user