Add cpp clamp function to util.h, switched battery and signal strength percentage calcs to it #197

1.2-legacy
Professr 2020-06-22 14:06:02 -07:00
rodzic 9061b3d8c3
commit 6a857b00db
3 zmienionych plików z 11 dodań i 3 usunięć

Wyświetl plik

@ -5,6 +5,7 @@
#include "main.h"
#include "power.h"
#include "sleep.h"
#include "utils.h"
#include "target_specific.h"
bool bluetoothOn;
@ -81,8 +82,7 @@ void readPowerStatus()
} else {
// If the AXP192 returns a percentage less than 0, the feature is either not supported or there is an error
// In that case, we compute an estimate of the charge percent based on maximum and minimum voltages defined in power.h
int calculatedPercentage = ((powerStatus.batteryVoltageMv - BAT_MILLIVOLTS_EMPTY) * 1e2) / (BAT_MILLIVOLTS_FULL - BAT_MILLIVOLTS_EMPTY);
powerStatus.batteryChargePercent = (calculatedPercentage < 0) ? 0 : (calculatedPercentage > 100) ? 100 : calculatedPercentage;
powerStatus.batteryChargePercent = clamp((int)(((powerStatus.batteryVoltageMv - BAT_MILLIVOLTS_EMPTY) * 1e2) / (BAT_MILLIVOLTS_FULL - BAT_MILLIVOLTS_EMPTY)), 0, 100);
}
DEBUG_MSG("Battery %dmV %d%%\n", powerStatus.batteryVoltageMv, powerStatus.batteryChargePercent);
}

Wyświetl plik

@ -31,6 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "main.h"
#include "mesh-pb-constants.h"
#include "screen.h"
#include "utils.h"
#define FONT_HEIGHT 14 // actually 13 for "ariel 10" but want a little extra space
#define FONT_HEIGHT_16 (ArialMT_Plain_16[1] + 1)
@ -379,7 +380,7 @@ static void drawNodeInfo(OLEDDisplay *display, OLEDDisplayUiState *state, int16_
const char *username = node->has_user ? node->user.long_name : "Unknown Name";
static char signalStr[20];
snprintf(signalStr, sizeof(signalStr), "Signal: %.0f", node->snr);
snprintf(signalStr, sizeof(signalStr), "Signal: %d%%", clamp((int)((node->snr + 10) * 5), 0, 100));
uint32_t agoSecs = sinceLastSeen(node);
static char lastStr[20];

7
src/utils.h 100644
Wyświetl plik

@ -0,0 +1,7 @@
#pragma once
/// C++ v17+ clamp function, limits a given value to a range defined by lo and hi
template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi ) {
return (v < lo) ? lo : (hi < v) ? hi : v;
}