Skip to content

Commit 18efcce

Browse files
committed
New formatting style
Clang formatter, Linux brace style,
1 parent 201f436 commit 18efcce

File tree

7 files changed

+306
-292
lines changed

7 files changed

+306
-292
lines changed

.clang-format

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
BasedOnStyle: LLVM
3+
IndentWidth: 8
4+
UseTab: Never
5+
TabWidth: 8
6+
BreakBeforeBraces: Linux
7+
AllowShortIfStatementsOnASingleLine: false
8+
ColumnLimit: 120
9+
---

board/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

board/.vscode/settings.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"files.associations": {
33
"cjson.h": "c"
44
},
5-
"editor.defaultFormatter": "ms-vscode.cpptools",
6-
"editor.formatOnSave": true
5+
"editor.formatOnSave": true,
6+
"editor.defaultFormatter": "xaver.clang-format",
7+
"[c]": {
8+
"editor.defaultFormatter": "xaver.clang-format"
9+
},
710
}

board/src/battery.c

+89-84
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "esp_adc/adc_oneshot.h"
21
#include "esp_adc/adc_cali.h"
32
#include "esp_adc/adc_cali_scheme.h"
3+
#include "esp_adc/adc_oneshot.h"
44
#include "esp_log.h"
55

66
#define BATT_VOLTAGE_ADC_CHANNEL ADC_CHANNEL_3
@@ -9,112 +9,117 @@
99
#define ADC_UNIT ADC_UNIT_1
1010
#define ADC_VREF 1100
1111

12-
// Calibrate the battery voltage measurement based on the resistor voltage divider
12+
// Calibrate the battery voltage measurement
13+
// based on the resistor voltage divider
1314
#define VDIV_RATIO 5.02
1415

1516
static const char *TAG = "Battery";
1617

1718
adc_oneshot_unit_handle_t batt_voltage_adc_handle;
1819
adc_cali_handle_t batt_voltage_adc_cali_handle = NULL;
1920

20-
static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_bitwidth_t bitwidth, adc_cali_handle_t *out_handle)
21+
static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_bitwidth_t bitwidth,
22+
adc_cali_handle_t *out_handle)
2123
{
22-
adc_cali_handle_t handle = NULL;
23-
esp_err_t ret = ESP_FAIL;
24-
bool calibrated = false;
25-
26-
if (!calibrated) {
27-
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
28-
adc_cali_curve_fitting_config_t cali_config = {
29-
.unit_id = unit,
30-
.chan = channel,
31-
.atten = atten,
32-
.bitwidth = bitwidth,
33-
};
34-
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
35-
36-
37-
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
38-
adc_cali_line_fitting_config_t cali_config = {
39-
.unit_id = unit,
40-
.atten = atten,
41-
.bitwidth = bitwidth,
42-
};
43-
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
44-
#endif
24+
adc_cali_handle_t handle = NULL;
25+
esp_err_t ret = ESP_FAIL;
26+
bool calibrated = false;
27+
28+
if (!calibrated) {
29+
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
30+
adc_cali_curve_fitting_config_t cali_config = {
31+
.unit_id = unit,
32+
.chan = channel,
33+
.atten = atten,
34+
.bitwidth = bitwidth,
35+
};
36+
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
37+
38+
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
39+
adc_cali_line_fitting_config_t cali_config = {
40+
.unit_id = unit,
41+
.atten = atten,
42+
.bitwidth = bitwidth,
43+
};
44+
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
45+
#endif
46+
47+
if (ret == ESP_OK) {
48+
calibrated = true;
49+
}
50+
}
4551

52+
*out_handle = handle;
4653
if (ret == ESP_OK) {
47-
calibrated = true;
54+
ESP_LOGI(TAG, "Calibration Success");
55+
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
56+
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
57+
} else {
58+
ESP_LOGE(TAG, "Invalid arg or no memory");
4859
}
49-
}
50-
51-
*out_handle = handle;
52-
if (ret == ESP_OK) {
53-
ESP_LOGI(TAG, "Calibration Success");
54-
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
55-
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
56-
} else {
57-
ESP_LOGE(TAG, "Invalid arg or no memory");
58-
}
59-
60-
return calibrated;
60+
61+
return calibrated;
6162
}
6263

63-
// Configures the battery ADC
64+
// Configures the battery ADC
6465
static void config_batt_adc()
6566
{
66-
ESP_LOGI(TAG, "Configuring ADC characteristics");
67-
68-
// ADC1 Init
69-
adc_oneshot_unit_init_cfg_t init_config1 = {
70-
.unit_id = ADC_UNIT,
71-
.ulp_mode = ADC_ULP_MODE_DISABLE,
72-
};
73-
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &batt_voltage_adc_handle));
74-
75-
// ADC1 Config
76-
adc_oneshot_chan_cfg_t config = {
77-
.bitwidth = BATT_VOLTAGE_ADC_BITWIDTH,
78-
.atten = BATT_VOLTAGE_ADC_ATTEN,
79-
};
80-
ESP_ERROR_CHECK(adc_oneshot_config_channel(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &config));
67+
ESP_LOGI(TAG, "Configuring ADC characteristics");
68+
69+
// ADC1 Init
70+
adc_oneshot_unit_init_cfg_t init_config1 = {
71+
.unit_id = ADC_UNIT,
72+
.ulp_mode = ADC_ULP_MODE_DISABLE,
73+
};
74+
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &batt_voltage_adc_handle));
75+
76+
// ADC1 Config
77+
adc_oneshot_chan_cfg_t config = {
78+
.bitwidth = BATT_VOLTAGE_ADC_BITWIDTH,
79+
.atten = BATT_VOLTAGE_ADC_ATTEN,
80+
};
81+
ESP_ERROR_CHECK(adc_oneshot_config_channel(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &config));
8182
}
8283

8384
// Deinitialize calibration of an ADC handle
8485
static void adc_calibration_deinit(adc_cali_handle_t handle)
8586
{
86-
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
87-
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
87+
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
88+
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
8889

89-
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
90-
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
91-
#endif
90+
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
91+
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
92+
#endif
9293
}
9394

94-
// Read ADC voltage from battery and return battery voltage calculated from the ADC and voltage divider
95+
// Read ADC voltage from battery and return battery
96+
// voltage calculated from the ADC and voltage divider
9597
float get_battery_voltage()
9698
{
97-
int adc_raw[1][10];
98-
int voltage[1][10];
99-
100-
config_batt_adc();
101-
102-
// ADC1 Read
103-
ESP_ERROR_CHECK(adc_oneshot_read(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &adc_raw[0][0]));
104-
ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, adc_raw[0][0]);
105-
106-
// ADC1 Calibration
107-
bool do_calibration_batt_voltage = adc_calibration_init(ADC_UNIT, BATT_VOLTAGE_ADC_CHANNEL, BATT_VOLTAGE_ADC_ATTEN, BATT_VOLTAGE_ADC_BITWIDTH, &batt_voltage_adc_cali_handle);
108-
109-
if (do_calibration_batt_voltage) {
110-
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(batt_voltage_adc_cali_handle, adc_raw[0][0], &voltage[0][0]));
111-
ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, voltage[0][0]);
112-
}
113-
114-
// ADC1 Teardown
115-
ESP_ERROR_CHECK(adc_oneshot_del_unit(batt_voltage_adc_handle));
116-
if(do_calibration_batt_voltage)
117-
adc_calibration_deinit(batt_voltage_adc_cali_handle);
118-
119-
return (voltage[0][0] / 1000.f) * VDIV_RATIO;
99+
int adc_raw[1][10];
100+
int voltage[1][10];
101+
102+
config_batt_adc();
103+
104+
// ADC1 Read
105+
ESP_ERROR_CHECK(adc_oneshot_read(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &adc_raw[0][0]));
106+
ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, adc_raw[0][0]);
107+
108+
// ADC1 Calibration
109+
bool do_calibration_batt_voltage =
110+
adc_calibration_init(ADC_UNIT, BATT_VOLTAGE_ADC_CHANNEL, BATT_VOLTAGE_ADC_ATTEN, BATT_VOLTAGE_ADC_BITWIDTH,
111+
&batt_voltage_adc_cali_handle);
112+
113+
if (do_calibration_batt_voltage) {
114+
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(batt_voltage_adc_cali_handle, adc_raw[0][0], &voltage[0][0]));
115+
ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL,
116+
voltage[0][0]);
117+
}
118+
119+
// ADC1 Teardown
120+
ESP_ERROR_CHECK(adc_oneshot_del_unit(batt_voltage_adc_handle));
121+
if (do_calibration_batt_voltage)
122+
adc_calibration_deinit(batt_voltage_adc_cali_handle);
123+
124+
return (voltage[0][0] / 1000.f) * VDIV_RATIO;
120125
}

0 commit comments

Comments
 (0)