Partial work to migrate to OSThread model

This commit is contained in:
Jm
2021-01-01 12:31:46 -08:00
parent 9587729bb0
commit 925829dc58
6 changed files with 91 additions and 30 deletions

View File

@@ -3,6 +3,8 @@
#define periodsToLog 48
AirTime airTime;
// A reminder that there are 3600 seconds in an hour so I don't have
// to keep googling it.
// This can be changed to a smaller number to speed up testing.
@@ -11,6 +13,8 @@ uint32_t secondsPerPeriod = 3600;
uint32_t lastMillis = 0;
uint32_t secSinceBoot = 0;
// AirTime at;
// Don't read out of this directly. Use the helper functions.
struct airtimeStruct {
uint16_t periodTX[periodsToLog];
@@ -19,14 +23,18 @@ struct airtimeStruct {
uint8_t lastPeriodIndex;
} airtimes;
void logAirtime(reportTypes reportType, uint32_t airtime_ms)
void AirTime::logAirtime(reportTypes reportType, uint32_t airtime_ms)
{
DEBUG_MSG("Packet - logAirtime()\n");
if (reportType == TX_LOG) {
DEBUG_MSG("Packet transmitted = %u\n", (uint32_t)round(airtime_ms / 1000));
airtimes.periodTX[0] = airtimes.periodTX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_LOG) {
DEBUG_MSG("Packet received = %u\n", (uint32_t)round(airtime_ms / 1000));
airtimes.periodRX[0] = airtimes.periodRX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_ALL_LOG) {
DEBUG_MSG("Packet received (noise?) = %u\n", (uint32_t)round(airtime_ms / 1000));
airtimes.periodRX_ALL[0] = airtimes.periodRX_ALL[0] + round(airtime_ms / 1000);
} else {
// Unknown report type
@@ -38,23 +46,22 @@ uint8_t currentPeriodIndex()
return ((getSecondsSinceBoot() / secondsPerPeriod) % periodsToLog);
}
void airtimeCalculator()
void airtimeRotatePeriod()
{
if (millis() - lastMillis > 1000) {
lastMillis = millis();
secSinceBoot++;
if (airtimes.lastPeriodIndex != currentPeriodIndex()) {
for (int i = periodsToLog - 2; i >= 0; --i) {
airtimes.periodTX[i + 1] = airtimes.periodTX[i];
airtimes.periodRX[i + 1] = airtimes.periodRX[i];
airtimes.periodRX_ALL[i + 1] = airtimes.periodRX_ALL[i];
}
airtimes.periodTX[0] = 0;
airtimes.periodRX[0] = 0;
airtimes.periodRX_ALL[0] = 0;
airtimes.lastPeriodIndex = currentPeriodIndex();
if (airtimes.lastPeriodIndex != currentPeriodIndex()) {
DEBUG_MSG("Rotating airtimes to a new period = %u\n", currentPeriodIndex());
for (int i = periodsToLog - 2; i >= 0; --i) {
airtimes.periodTX[i + 1] = airtimes.periodTX[i];
airtimes.periodRX[i + 1] = airtimes.periodRX[i];
airtimes.periodRX_ALL[i + 1] = airtimes.periodRX_ALL[i];
}
airtimes.periodTX[0] = 0;
airtimes.periodRX[0] = 0;
airtimes.periodRX_ALL[0] = 0;
airtimes.lastPeriodIndex = currentPeriodIndex();
}
}
@@ -86,3 +93,15 @@ uint32_t getSecondsSinceBoot()
{
return secSinceBoot;
}
AirTime::AirTime() : concurrency::OSThread("AirTime") {}
int32_t AirTime::runOnce()
{
DEBUG_MSG("AirTime::runOnce()\n");
airtimeRotatePeriod();
secSinceBoot++;
return 1000;
}