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-26 22:39:43 -08:00
|
|
|
// 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.
|
|
|
|
|
//
|
2020-12-25 16:10:38 -08:00
|
|
|
uint16_t secondsPerHour = 3600;
|
2020-12-26 22:39:43 -08:00
|
|
|
uint32_t lastMillis = 0;
|
|
|
|
|
uint32_t secSinceBoot = 0;
|
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
|
|
|
if (reportType == TX_LOG) {
|
2020-12-26 22:39:43 -08:00
|
|
|
airtimes.hourTX[0] = airtimes.hourTX[0] + round(airtime_ms / 1000);
|
2020-12-25 16:10:38 -08:00
|
|
|
} else if (reportType == RX_LOG) {
|
2020-12-26 22:39:43 -08:00
|
|
|
airtimes.hourRX[0] = airtimes.hourRX[0] + round(airtime_ms / 1000);
|
2020-12-25 16:10:38 -08:00
|
|
|
} else if (reportType == RX_ALL_LOG) {
|
2020-12-26 22:39:43 -08:00
|
|
|
airtimes.hourRX_ALL[0] = airtimes.hourRX_ALL[0] + round(airtime_ms / 1000);
|
2020-12-25 16:10:38 -08:00
|
|
|
} else {
|
|
|
|
|
// Unknown report type
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-26 22:39:43 -08:00
|
|
|
uint32_t getSecondsSinceBoot()
|
2020-12-25 16:10:38 -08:00
|
|
|
{
|
2020-12-26 22:39:43 -08:00
|
|
|
return secSinceBoot;
|
2020-12-25 16:10:38 -08:00
|
|
|
}
|
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);
|
2020-12-26 22:39:43 -08:00
|
|
|
return ((getSecondsSinceBoot() / secondsPerHour) % hoursToLog);
|
2020-12-25 16:10:38 -08:00
|
|
|
}
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-26 22:39:43 -08:00
|
|
|
void airtimeCalculator()
|
2020-12-25 16:10:38 -08:00
|
|
|
{
|
2020-12-26 22:39:43 -08:00
|
|
|
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;
|
2020-12-26 23:37:04 -08:00
|
|
|
airtimes.hourRX[0] = 0;
|
|
|
|
|
airtimes.hourRX_ALL[0] = 0;
|
2020-12-24 22:12:59 -08:00
|
|
|
|
2020-12-26 22:39:43 -08:00
|
|
|
airtimes.lastHourIndex = currentHourIndex();
|
|
|
|
|
}
|
2020-12-25 16:10:38 -08:00
|
|
|
}
|
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-26 22:39:43 -08:00
|
|
|
// currentHourIndexReset();
|
2020-12-25 16:10:38 -08:00
|
|
|
|
2020-12-26 22:39:43 -08:00
|
|
|
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;
|
2020-12-25 16:10:38 -08:00
|
|
|
}
|
2020-12-26 22:39:43 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-12-25 16:10:38 -08:00
|
|
|
|
2020-12-26 22:39:43 -08:00
|
|
|
uint8_t getHoursToLog()
|
|
|
|
|
{
|
|
|
|
|
return hoursToLog;
|
2020-12-25 16:10:38 -08:00
|
|
|
}
|