Skip to content

Commit

Permalink
Battery support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Krasichkov authored and buglloc committed Jan 7, 2024
1 parent 798ce2c commit f262af6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions components/lilygo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ SRCS
INCLUDE_DIRS
"include"
REQUIRES
driver esp_lcd esp_lcd_touch_cst816s
driver esp_adc esp_lcd esp_lcd_touch_cst816s
lvgl
PRIV_REQUIRES
esp_timer esp_adc heap esp_hw_support
esp_timer heap esp_hw_support
)

# TODO(buglloc): fix me
Expand Down
10 changes: 3 additions & 7 deletions components/lilygo/include/lilygo/batt.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#pragma once

#include <driver/temperature_sensor.h>
#include <esp_adc/adc_oneshot.h>

#include <esp_err.h>

// fwd
struct adc_oneshot_unit_ctx_t;
typedef struct adc_oneshot_unit_ctx_t *adc_oneshot_unit_handle_t;
struct adc_cali_scheme_t;
typedef struct adc_cali_scheme_t *adc_cali_handle_t;


namespace LilyGo
{
Expand All @@ -22,6 +16,8 @@ namespace LilyGo
uint32_t BattVoltage();

private:
adc_unit_t adcUnit;
adc_channel_t adcChannel;
adc_oneshot_unit_handle_t adcHandle = nullptr;
adc_cali_handle_t caliHandle = nullptr;
bool doCalibration = false;
Expand Down
23 changes: 15 additions & 8 deletions components/lilygo/src/batt.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "lilygo/batt.h"
#include "board_config.h"

#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"
#include <driver/gpio.h>

#include <esp_adc/adc_oneshot.h>
#include <esp_adc/adc_cali.h>
#include <esp_adc/adc_cali_scheme.h>

#include <esp_err.h>
#include <esp_check.h>
#include <esp_log.h>


using namespace LilyGo;

namespace
Expand Down Expand Up @@ -67,20 +70,24 @@ namespace

esp_err_t BatteryController::Initialize()
{
esp_err_t err = adc_oneshot_io_to_channel(BATT_ADC_PIN, &adcUnit, &adcChannel);
ESP_RETURN_ON_ERROR(err, TAG, "find channel by GPIO #%d", BATT_ADC_PIN);
ESP_LOGI(TAG, "channel found: GPIO#%d = %d (unit) / %d (channel)", BATT_ADC_PIN, (int)adcUnit, (int)adcChannel);

adc_oneshot_unit_init_cfg_t initCfg = {
.unit_id = BATT_ADC_UNIT,
.unit_id = adcUnit,
};
esp_err_t err = adc_oneshot_new_unit(&initCfg, &adcHandle);
err = adc_oneshot_new_unit(&initCfg, &adcHandle);
ESP_RETURN_ON_ERROR(err, TAG, "create one shot unit");

adc_oneshot_chan_cfg_t chanCfg = {
.atten = BATT_ADC_ATTEN,
.bitwidth = BATT_ADC_WIDTH_BIT,
};
err = adc_oneshot_config_channel(adcHandle, BATT_ADC_CHANNEL, &chanCfg);
err = adc_oneshot_config_channel(adcHandle, adcChannel, &chanCfg);
ESP_RETURN_ON_ERROR(err, TAG, "configure channel");

doCalibration = iniAdcCalibration(BATT_ADC_UNIT, BATT_ADC_CHANNEL, BATT_ADC_ATTEN, &caliHandle);
doCalibration = iniAdcCalibration(adcUnit, adcChannel, BATT_ADC_ATTEN, &caliHandle);

return ESP_OK;
}
Expand All @@ -93,7 +100,7 @@ uint32_t BatteryController::BattVoltage()

int adcRaw = 0;
int voltage = 0;
esp_err_t err = adc_oneshot_read(adcHandle, BATT_ADC_CHANNEL, &adcRaw);
esp_err_t err = adc_oneshot_read(adcHandle, adcChannel, &adcRaw);
if (err != ESP_OK) {
ESP_LOGE(TAG, "adc read fail: %d", err);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions components/lilygo/src/board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ esp_err_t Board::Initialize()
ESP_LOGI(TAG, "setup temp sensor");
ESP_RETURN_ON_ERROR(tempSensor.Initialize(), TAG, "could't initialize temp sensor");

// ESP_LOGI(TAG, "setup baterry controller");
// ESP_RETURN_ON_ERROR(batteryController.Initialize(), TAG, "could't initialize baterry controller");
ESP_LOGI(TAG, "setup baterry controller");
ESP_RETURN_ON_ERROR(batteryController.Initialize(), TAG, "could't initialize baterry controller");

initialized = true;
return ESP_OK;
Expand Down
3 changes: 1 addition & 2 deletions components/lilygo/src/board_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
#define HOME_BUTTON_X 600
#define HOME_BUTTON_Y 120

#define BATT_ADC_UNIT ADC_UNIT_2
#define BATT_ADC_CHANNEL ADC_CHANNEL_0
#define BATT_ADC_PIN GPIO_NUM_4
#define BATT_ADC_ATTEN ADC_ATTEN_DB_11
#define BATT_ADC_WIDTH_BIT ADC_BITWIDTH_12
#define BATT_ADC_DEF_VREF 1100

0 comments on commit f262af6

Please sign in to comment.