Skip to content

Commit

Permalink
Update to meshtastic#588 - Change "hour" to "period"
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-hamster committed Dec 27, 2020
1 parent 6e4cf22 commit 15a0b36
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 43 deletions.
69 changes: 36 additions & 33 deletions src/airtime.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,59 @@
#include "airtime.h"
#include <Arduino.h>

#define hoursToLog 48
#define periodsToLog 48

// 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 secondsPerPeriod = 3600;
uint32_t lastMillis = 0;
uint32_t secSinceBoot = 0;

// 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;
uint16_t periodTX[periodsToLog];
uint16_t periodRX[periodsToLog];
uint16_t periodRX_ALL[periodsToLog];
uint8_t lastPeriodIndex;
} airtimes;

void logAirtime(reportTypes reportType, uint32_t airtime_ms)
{

if (reportType == TX_LOG) {
airtimes.hourTX[0] = airtimes.hourTX[0] + round(airtime_ms / 1000);
airtimes.periodTX[0] = airtimes.periodTX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_LOG) {
airtimes.hourRX[0] = airtimes.hourRX[0] + round(airtime_ms / 1000);
airtimes.periodRX[0] = airtimes.periodRX[0] + round(airtime_ms / 1000);
} else if (reportType == RX_ALL_LOG) {
airtimes.hourRX_ALL[0] = airtimes.hourRX_ALL[0] + round(airtime_ms / 1000);
airtimes.periodRX_ALL[0] = airtimes.periodRX_ALL[0] + round(airtime_ms / 1000);
} else {
// Unknown report type
}
}

uint32_t getSecondsSinceBoot()
uint8_t currentPeriodIndex()
{
return secSinceBoot;
}

uint8_t currentHourIndex()
{
// return ((secondsSinceBoot() - (secondsSinceBoot() / (hoursToLog * secondsPerHour))) / secondsPerHour);
return ((getSecondsSinceBoot() / secondsPerHour) % hoursToLog);
return ((getSecondsSinceBoot() / secondsPerPeriod) % periodsToLog);
}

void airtimeCalculator()
{
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];
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.hourTX[0] = 0;
airtimes.hourRX[0] = 0;
airtimes.hourRX_ALL[0] = 0;
airtimes.periodTX[0] = 0;
airtimes.periodRX[0] = 0;
airtimes.periodRX_ALL[0] = 0;

airtimes.lastHourIndex = currentHourIndex();
airtimes.lastPeriodIndex = currentPeriodIndex();
}
}
}
Expand All @@ -70,16 +63,26 @@ uint16_t *airtimeReport(reportTypes reportType)
// currentHourIndexReset();

if (reportType == TX_LOG) {
return airtimes.hourTX;
return airtimes.periodTX;
} else if (reportType == RX_LOG) {
return airtimes.hourRX;
return airtimes.periodRX;
} else if (reportType == RX_ALL_LOG) {
return airtimes.hourRX_ALL;
return airtimes.periodRX_ALL;
}
return 0;
}

uint8_t getHoursToLog()
uint8_t getPeriodsToLog()
{
return periodsToLog;
}

uint32_t getSecondsPerPeriod()
{
return hoursToLog;
}
return secondsPerPeriod;
}

uint32_t getSecondsSinceBoot()
{
return secSinceBoot;
}
8 changes: 5 additions & 3 deletions src/airtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ void logAirtime(reportTypes reportType, uint32_t airtime_ms);

void airtimeCalculator();

uint8_t currentHourIndex();
uint8_t getHoursToLog();
uint8_t currentPeriodIndex();
uint8_t getPeriodsToLog();

uint32_t getSecondsSinceBoot();

uint16_t *airtimeReport(reportTypes reportType);
uint16_t *airtimeReport(reportTypes reportType);

uint32_t getSecondsPerPeriod();
15 changes: 8 additions & 7 deletions src/meshwifi/meshhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,11 @@ void handleReport(HTTPRequest *req, HTTPResponse *res)
res->print("\"tx_log\": [");

logArray = airtimeReport(TX_LOG);
for (int i = 0; i < getHoursToLog(); i++) {
for (int i = 0; i < getPeriodsToLog(); i++) {
uint16_t tmp;
tmp = *(logArray + i);
res->printf("%d", tmp);
if (i != getHoursToLog() - 1) {
if (i != getPeriodsToLog() - 1) {
res->print(", ");
}
}
Expand All @@ -1088,11 +1088,11 @@ void handleReport(HTTPRequest *req, HTTPResponse *res)
res->print("\"rx_log\": [");

logArray = airtimeReport(RX_LOG);
for (int i = 0; i < getHoursToLog(); i++) {
for (int i = 0; i < getPeriodsToLog(); i++) {
uint16_t tmp;
tmp = *(logArray + i);
res->printf("%d", tmp);
if (i != getHoursToLog() - 1) {
if (i != getPeriodsToLog() - 1) {
res->print(", ");
}
}
Expand All @@ -1101,18 +1101,19 @@ void handleReport(HTTPRequest *req, HTTPResponse *res)
res->print("\"rx_all_log\": [");

logArray = airtimeReport(RX_ALL_LOG);
for (int i = 0; i < getHoursToLog(); i++) {
for (int i = 0; i < getPeriodsToLog(); i++) {
uint16_t tmp;
tmp = *(logArray + i);
res->printf("%d", tmp);
if (i != getHoursToLog() - 1) {
if (i != getPeriodsToLog() - 1) {
res->print(", ");
}
}

res->println("],");
res->printf("\"seconds_since_boot\": %u,\n", getSecondsSinceBoot());
res->printf("\"hours_to_log\": %u\n", getHoursToLog());
res->printf("\"seconds_per_period\": %u,\n", getSecondsPerPeriod());
res->printf("\"periods_to_log\": %u\n", getPeriodsToLog());

res->println("},");

Expand Down

0 comments on commit 15a0b36

Please sign in to comment.