Project structure redefinition
This commit is contained in:
@@ -1,19 +1,39 @@
|
||||
// This is a personal academic project. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
|
||||
|
||||
#include "board_pins.h"
|
||||
|
||||
class BatteryController {
|
||||
public:
|
||||
BatteryController() {}
|
||||
BatteryController(const uint16_t batteryPin, const uint16_t battery_data_switch_pin) {
|
||||
_battery_pin = batteryPin;
|
||||
_battery_data_switch_pin = battery_data_switch_pin;
|
||||
}
|
||||
|
||||
void initialize() {
|
||||
pinMode(BATTERY_DATA_SWITCH_PIN, OUTPUT);
|
||||
digitalWrite(BATTERY_DATA_SWITCH_PIN, HIGH);
|
||||
pinMode(_battery_data_switch_pin, OUTPUT);
|
||||
digitalWrite(_battery_data_switch_pin, HIGH);
|
||||
analogReadResolution(12);
|
||||
analogSetWidth(12);
|
||||
adcAttachPin(_battery_pin);
|
||||
}
|
||||
|
||||
float measureVoltage() {
|
||||
return analogRead(BATTERY_DATA_PIN) * 3.3 / 4095;
|
||||
return analogRead(BATTERY_DATA_PIN) * 3.3f / 4096.f;
|
||||
}
|
||||
|
||||
int percent(int minVoltage = 7200, int maxVoltage = 8400) {
|
||||
return map(int(measureVoltage() * 1000), minVoltage, maxVoltage, 0, 100);
|
||||
int percent(const float minVoltage = 7.2f, const float maxVoltage = 8.4f,
|
||||
const float r1 = 3.3f, const float r2 = 2.f, const float k = 2.87f) {
|
||||
auto batteryVoltage = measureVoltage() * ((r1 + r2) / k);
|
||||
return round(_map(batteryVoltage, minVoltage, maxVoltage, 5.f, 100.f));
|
||||
}
|
||||
|
||||
private:
|
||||
uint16_t _battery_pin;
|
||||
uint16_t _battery_data_switch_pin;
|
||||
static constexpr const char *_tag = "BatteryController";
|
||||
|
||||
float _map(float x, float in_min, float in_max, float out_min, float out_max) {
|
||||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user