#588 - Calculate TX air time

This commit is contained in:
Jm
2020-12-26 22:39:43 -08:00
parent ded2b86e55
commit e321528a6d
5 changed files with 153 additions and 60 deletions

View File

@@ -3,8 +3,13 @@
#define hoursToLog 48
// Here for convience and to avoid magic numbers.
// 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.
//
uint16_t secondsPerHour = 3600;
uint32_t lastMillis = 0;
uint32_t secSinceBoot = 0;
// Don't read out of this directly. Use the helper functions.
struct airtimeStruct {
@@ -16,65 +21,63 @@ struct airtimeStruct {
void logAirtime(reportTypes reportType, uint32_t airtime_ms)
{
currentHourIndexReset();
if (reportType == TX_LOG) {
airtimes.hourTX[currentHourIndex()] = airtimes.hourTX[currentHourIndex()] + round(airtime_ms / 1000);
airtimes.hourTX[0] = airtimes.hourTX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_LOG) {
airtimes.hourRX[currentHourIndex()] = airtimes.hourRX[currentHourIndex()] + round(airtime_ms / 1000);
airtimes.hourRX[0] = airtimes.hourRX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_ALL_LOG) {
airtimes.hourRX_ALL[currentHourIndex()] = airtimes.hourRX_ALL[currentHourIndex()] + round(airtime_ms / 1000);
airtimes.hourRX_ALL[0] = airtimes.hourRX_ALL[0] + round(airtime_ms / 1000);
} else {
// Unknown report type
}
}
// This will let us easily switch away from using millis at some point.
// todo: Don't use millis, instead maintain our own count of time since
// boot in seconds.
uint32_t secondsSinceBoot()
uint32_t getSecondsSinceBoot()
{
return millis() / 1000;
return secSinceBoot;
}
uint8_t currentHourIndex()
{
// return ((secondsSinceBoot() - (secondsSinceBoot() / (hoursToLog * secondsPerHour))) / secondsPerHour);
return ((secondsSinceBoot() / secondsPerHour) % hoursToLog);
return ((getSecondsSinceBoot() / secondsPerHour) % hoursToLog);
}
// currentHourIndexReset() should be called every time we receive a packet to log (either RX or TX)
// and every time we are asked to report on airtime usage.
void currentHourIndexReset()
void airtimeCalculator()
{
if (airtimes.lastHourIndex != currentHourIndex()) {
airtimes.hourTX[currentHourIndex()] = 0;
airtimes.hourRX[currentHourIndex()] = 0;
airtimes.hourRX_ALL[currentHourIndex()] = 0;
if (millis() - lastMillis > 1000) {
lastMillis = millis();
secSinceBoot++;
// DEBUG_MSG("------- lastHourIndex %i currentHourIndex %i\n", airtimes.lastHourIndex, currentHourIndex());
if (airtimes.lastHourIndex != currentHourIndex()) {
for (int i = hoursToLog - 2; i >= 0; --i) {
airtimes.hourTX[i + 1] = airtimes.hourTX[i];
airtimes.hourRX[i + 1] = airtimes.hourRX[i];
airtimes.hourRX_ALL[i + 1] = airtimes.hourRX_ALL[i];
}
airtimes.hourTX[0] = 0;
airtimes.lastHourIndex = currentHourIndex();
airtimes.lastHourIndex = currentHourIndex();
}
}
}
uint16_t *airtimeReport(reportTypes reportType)
{
static uint16_t array[hoursToLog];
// currentHourIndexReset();
currentHourIndexReset();
for (int i = 0; i < hoursToLog; i++) {
if (reportType == TX_LOG) {
array[i] = airtimes.hourTX[(airtimes.lastHourIndex + i) % hoursToLog];
} else if (reportType == RX_LOG) {
array[i] = airtimes.hourRX[(airtimes.lastHourIndex + i) % hoursToLog];
} else if (reportType == RX_ALL_LOG) {
array[i] = airtimes.hourRX_ALL[(airtimes.lastHourIndex + i) % hoursToLog];
} else {
// Unknown report type
return array;
}
if (reportType == TX_LOG) {
return airtimes.hourTX;
} else if (reportType == RX_LOG) {
return airtimes.hourRX;
} else if (reportType == RX_ALL_LOG) {
return airtimes.hourRX_ALL;
}
return 0;
}
return array;
uint8_t getHoursToLog()
{
return hoursToLog;
}