2020-12-25 16:10:38 -08:00
|
|
|
#include "airtime.h"
|
|
|
|
|
#include <Arduino.h>
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
#define hoursToLog 48
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
// Here for convience and to avoid magic numbers.
|
|
|
|
|
uint16_t secondsPerHour = 3600;
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
// Don't read out of this directly. Use the helper functions.
|
|
|
|
|
struct airtimeStruct {
|
|
|
|
|
uint16_t hourTX[hoursToLog];
|
|
|
|
|
uint16_t hourRX[hoursToLog];
|
|
|
|
|
uint16_t hourRX_ALL[hoursToLog];
|
|
|
|
|
uint8_t lastHourIndex;
|
|
|
|
|
} airtimes;
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
void logAirtime(reportTypes reportType, uint32_t airtime_ms)
|
2020-12-24 22:12:59 -08:00
|
|
|
{
|
2020-12-25 16:10:38 -08:00
|
|
|
currentHourIndexReset();
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
if (reportType == TX_LOG) {
|
|
|
|
|
airtimes.hourTX[currentHourIndex()] = airtimes.hourTX[currentHourIndex()] + round(airtime_ms / 1000);
|
|
|
|
|
} else if (reportType == RX_LOG) {
|
|
|
|
|
airtimes.hourRX[currentHourIndex()] = airtimes.hourRX[currentHourIndex()] + round(airtime_ms / 1000);
|
|
|
|
|
} else if (reportType == RX_ALL_LOG) {
|
|
|
|
|
airtimes.hourRX_ALL[currentHourIndex()] = airtimes.hourRX_ALL[currentHourIndex()] + round(airtime_ms / 1000);
|
|
|
|
|
} else {
|
|
|
|
|
// Unknown report type
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
// 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()
|
|
|
|
|
{
|
|
|
|
|
return millis() / 1000;
|
|
|
|
|
}
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
uint8_t currentHourIndex()
|
|
|
|
|
{
|
|
|
|
|
// return ((secondsSinceBoot() - (secondsSinceBoot() / (hoursToLog * secondsPerHour))) / secondsPerHour);
|
|
|
|
|
return ((secondsSinceBoot() / secondsPerHour) % hoursToLog);
|
|
|
|
|
}
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
// 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()
|
|
|
|
|
{
|
|
|
|
|
if (airtimes.lastHourIndex != currentHourIndex()) {
|
|
|
|
|
airtimes.hourTX[currentHourIndex()] = 0;
|
|
|
|
|
airtimes.hourRX[currentHourIndex()] = 0;
|
|
|
|
|
airtimes.hourRX_ALL[currentHourIndex()] = 0;
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
airtimes.lastHourIndex = currentHourIndex();
|
|
|
|
|
}
|
2020-12-24 22:12:59 -08:00
|
|
|
}
|
|
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
uint16_t *airtimeReport(reportTypes reportType)
|
2020-12-24 22:12:59 -08:00
|
|
|
{
|
2020-12-25 16:10:38 -08:00
|
|
|
static uint16_t array[hoursToLog];
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-25 16:10:38 -08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|