Skip to content

Commit 5278c44

Browse files
committed
style: Fix indent and add functions definitions (brief)
1 parent 7204ac0 commit 5278c44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+8518
-7931
lines changed

lib/battery/battery.cpp

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,82 @@
66
* @date 2025-06
77
*/
88

9-
109
#include "battery.hpp"
1110

1211
/**
13-
* @brief Battery Class constructor
14-
*
12+
* @brief Constructs a Battery monitoring object for voltage measurement and percentage calculation.
1513
*/
1614
Battery::Battery() {}
1715

1816
/**
19-
* @brief Configure ADC Channel for battery reading
17+
* @brief Initializes the ADC channel(s) required for battery voltage measurement.
2018
*
19+
* Configures the hardware ADC based on the ESP32 chip (ADC1 or ADC2).
2120
*/
2221
void Battery::initADC()
2322
{
2423
#ifdef ADC1
25-
adc1_config_width(ADC_WIDTH_BIT_12);
26-
adc1_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
24+
adc1_config_width(ADC_WIDTH_BIT_12);
25+
adc1_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
2726
#endif
2827

2928
#ifdef ADC2
30-
adc2_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
29+
adc2_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
3130
#endif
3231
}
3332

3433
/**
35-
* @brief Set battery voltage levels
34+
* @brief Sets the maximum and minimum voltage levels for battery charge calculation.
3635
*
37-
* @param maxVoltage -> Full Charge voltage
38-
* @param minVoltage -> Min Charge voltage
36+
* @param maxVoltage Voltage considered as fully charged.
37+
* @param minVoltage Voltage considered as minimum safe level.
3938
*/
4039
void Battery::setBatteryLevels(float maxVoltage, float minVoltage)
4140
{
42-
batteryMax = maxVoltage;
43-
batteryMin = minVoltage;
41+
batteryMax = maxVoltage;
42+
batteryMin = minVoltage;
4443
}
4544

4645
/**
47-
* @brief Read battery charge and return %.
46+
* @brief Reads and computes the current battery charge as a percentage.
47+
*
48+
* Takes 100 ADC samples, averages them, compensates for the voltage divider, and calculates the charge percentage.
49+
* Returns a value between 0 and 100 (values above 160 are treated as 0).
4850
*
49-
* @return float -> % Charge
51+
* @return float Percentage of battery charge (0–100% typically).
5052
*/
5153
float Battery::readBattery()
5254
{
53-
long sum = 0; // Sum of samples taken
54-
float voltage = 0.0; // Calculated voltage
55-
float output = 0.0; // Output value
5655

57-
for (int i = 0; i < 100; i++)
58-
{
59-
#ifdef ADC1
60-
sum += static_cast<long>(adc1_get_raw(BATT_PIN));
61-
#endif
56+
long sum = 0; /**< Sum of samples taken. */
57+
float voltage = 0.0; /**< Calculated voltage. */
58+
float output = 0.0; /**< Output value. */
59+
60+
61+
for (int i = 0; i < 100; i++)
62+
{
63+
#ifdef ADC1
64+
sum += static_cast<long>(adc1_get_raw(BATT_PIN));
65+
#endif
6266

63-
#ifdef ADC2
64-
int readRaw;
65-
esp_err_t r = adc2_get_raw(BATT_PIN, ADC_WIDTH_BIT_12, &readRaw);
66-
if (r == ESP_OK)
67-
sum += static_cast<long>(readRaw);
68-
#endif
67+
#ifdef ADC2
68+
int readRaw;
69+
esp_err_t r = adc2_get_raw(BATT_PIN, ADC_WIDTH_BIT_12, &readRaw);
70+
if (r == ESP_OK)
71+
sum += static_cast<long>(readRaw);
72+
#endif
6973

70-
delayMicroseconds(150);
71-
}
74+
delayMicroseconds(150);
75+
}
7276

73-
voltage = sum / 100.0;
74-
// Custom board has a divider circuit
75-
constexpr float R1 = 100000.0; // Resistance of R1 (100K)
76-
constexpr float R2 = 100000.0; // Resistance of R2 (100K)
77-
voltage = (voltage * V_REF) / 4096.0;
78-
voltage = voltage / (R2 / (R1 + R2));
79-
voltage = roundf(voltage * 100) / 100;
77+
voltage = sum / 100.0;
78+
/**< Custom board has a divider circuit */
79+
constexpr float R1 = 100000.0; /**< Resistance of R1 (100K) */
80+
constexpr float R2 = 100000.0; /**< Resistance of R2 (100K) */
81+
voltage = (voltage * V_REF) / 4096.0;
82+
voltage = voltage / (R2 / (R1 + R2));
83+
voltage = roundf(voltage * 100) / 100;
8084

81-
output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100;
82-
return (output <= 160) ? output : 0.0f;
85+
output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100;
86+
return (output <= 160) ? output : 0.0f;
8387
}

lib/battery/battery.hpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@
1212
#include <driver/adc.h>
1313
#include <esp_adc_cal.h>
1414

15+
16+
/**
17+
* @class Battery
18+
* @brief Provides battery voltage monitoring and charge estimation.
19+
*
20+
* Handles ADC initialization, voltage range configuration, and computes battery charge percentage.
21+
*/
1522
class Battery
1623
{
1724
private:
18-
float batteryMax;
19-
float batteryMin;
20-
static constexpr float V_REF = 3.9; // ADC reference voltage
25+
float batteryMax; /**< Maximum (full charge) voltage. */
26+
float batteryMin; /**< Minimum (empty) voltage. */
27+
static constexpr float V_REF = 3.9; /**< ADC reference voltage. */
2128

2229
public:
23-
Battery();
30+
Battery();
2431

25-
void initADC();
26-
void setBatteryLevels(float maxVoltage, float minVoltage);
27-
float readBattery();
32+
void initADC();
33+
void setBatteryLevels(float maxVoltage, float minVoltage);
34+
float readBattery();
2835
};

lib/bme/bme.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212

1313
Adafruit_BME280 bme = Adafruit_BME280();
1414

15-
/**
16-
* @brief Temperature reading values
17-
*
18-
*/
19-
uint8_t tempValue = 0;
20-
uint8_t tempOld = 0;
15+
uint8_t tempValue = 0; /**< Stores the current temperature value from the BME280 sensor. */
16+
uint8_t tempOld = 0; /**< Stores the previous temperature value for comparison. */
2117

2218
/**
23-
* @brief Init BME sensor
19+
* @brief Initializes the BME280 sensor and sets up I2C communication.
2420
*
21+
* Optionally allows advanced configuration for oversampling and filtering.
2522
*/
2623
void initBME()
2724
{

lib/bme/bme.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515
#include <Adafruit_Sensor.h>
1616
#include <Adafruit_BME280.h>
1717

18-
/**
19-
* @brief BME280 Address
20-
*
21-
*/
22-
#define BME_ADDRESS 0x76
23-
24-
extern Adafruit_BME280 bme;
25-
extern uint8_t tempValue;
26-
extern uint8_t tempOld;
18+
#define BME_ADDRESS 0x76 /**< I2C address for the BME280 sensor (default: 0x76). */
19+
extern Adafruit_BME280 bme; /**< Global instance of the BME280 sensor driver. */
20+
extern uint8_t tempValue; /**< Stores the latest temperature reading from the BME280 sensor. */
21+
extern uint8_t tempOld; /**< Stores the previous temperature value to detect changes. */
2722

2823
void initBME();
2924

0 commit comments

Comments
 (0)