Skip to content

Commit 1d70d94

Browse files
luisan00Jamie C. Driver
authored andcommitted
diy: add support for battery level and charging status for tdisplays3
1 parent a5c9eab commit 1d70d94

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

main/Kconfig.projbuild

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ menu "Blockstream Jade"
154154
help
155155
Jade v1 (and some other devices) come with the AXP192 to manage power to ESP, display, camera
156156

157+
config HAS_BATTERY
158+
bool "Use analog input as battery level indicator and USB connection"
159+
depends on BOARD_TYPE_TTGO_TDISPLAYS3
160+
default n
161+
help
162+
Some devices can measure voltage and detect charging status through an analog input.
163+
157164
menu "SDA/SCL Pin Mapping"
158165
visible if BOARD_TYPE_CUSTOM
159166

main/power.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "power/ip5306.inc"
2828
#elif defined(CONFIG_BOARD_TYPE_WS_TOUCH_LCD2)
2929
#include "power/wslcdtouch2.inc"
30+
#elif defined(CONFIG_BOARD_TYPE_TTGO_TDISPLAYS3) && defined(CONFIG_HAS_BATTERY)
31+
// ttgo-tdisplays3 can read battery level and charging status if a battery is connected
32+
#include "power/tdisplays3.inc"
3033
#else
3134
// Stubs for other hw boards (ie. no power management)
3235
#include "power/minimal.inc"

main/power/tdisplays3.inc

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// tdisplays3 and devices with no power-management but it can measure voltage level of the battery and
2+
// detect charging status when a battery is connected through an analog input
3+
//
4+
#include <driver/gpio.h>
5+
#include <esp_adc/adc_cali.h>
6+
#include <esp_adc/adc_oneshot.h>
7+
8+
#define BATTERY_ADC_CHANNEL ADC_CHANNEL_3
9+
10+
static adc_oneshot_unit_handle_t adc1_handle = NULL;
11+
static adc_cali_handle_t adc1_cali_handle = NULL;
12+
13+
esp_err_t power_init(void)
14+
{
15+
// Initialise the ADC to measure battery level
16+
adc_oneshot_unit_init_cfg_t init_config1 = {
17+
.unit_id = ADC_UNIT_1,
18+
};
19+
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
20+
JADE_ASSERT(adc1_handle);
21+
// ADC Config
22+
adc_oneshot_chan_cfg_t config = {
23+
.atten = ADC_ATTEN_DB_12,
24+
.bitwidth = ADC_BITWIDTH_DEFAULT,
25+
};
26+
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, BATTERY_ADC_CHANNEL, &config));
27+
// Curve fitting calibration
28+
adc_cali_curve_fitting_config_t cali_config = {
29+
.unit_id = ADC_UNIT_1,
30+
.atten = ADC_ATTEN_DB_12,
31+
.bitwidth = ADC_BITWIDTH_DEFAULT,
32+
};
33+
ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_handle));
34+
return ESP_OK;
35+
}
36+
37+
esp_err_t power_shutdown(void) { return ESP_OK; }
38+
esp_err_t power_screen_on(void) { return ESP_OK; }
39+
esp_err_t power_backlight_on(const uint8_t brightness) { return ESP_OK; }
40+
esp_err_t power_backlight_off(void) { return ESP_OK; }
41+
esp_err_t power_camera_on(void) { return ESP_OK; }
42+
esp_err_t power_camera_off(void) { return ESP_OK; }
43+
44+
uint16_t power_get_vbat(void)
45+
{
46+
JADE_ASSERT(adc1_handle);
47+
int cal_vbat = 0;
48+
int raw_vbat = 0;
49+
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC_CHANNEL, &raw_vbat));
50+
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, raw_vbat, &cal_vbat));
51+
return (uint16_t)(cal_vbat * 2);
52+
}
53+
uint8_t power_get_battery_status(void)
54+
{
55+
const uint16_t vbat = power_get_vbat();
56+
57+
if (vbat > 4000) {
58+
return 5;
59+
} else if (vbat > 3800) {
60+
return 4;
61+
} else if (vbat > 3600) {
62+
return 3;
63+
} else if (vbat > 3400) {
64+
return 2;
65+
} else if (vbat > 3200) {
66+
return 1;
67+
}
68+
return 0;
69+
}
70+
71+
bool power_get_battery_charging(void)
72+
{
73+
uint16_t vbat = power_get_vbat();
74+
// If the voltage is greater than 4500 mV and less than 4750 it means its charging
75+
if (vbat > 4500 && vbat < 4750) {
76+
return true;
77+
}
78+
return false;
79+
}
80+
81+
uint16_t power_get_ibat_charge(void) { return 0; }
82+
uint16_t power_get_ibat_discharge(void) { return 0; }
83+
uint16_t power_get_vusb(void) { return 0; }
84+
uint16_t power_get_iusb(void) { return 0; }
85+
uint16_t power_get_temp(void) { return 0; }
86+
87+
bool usb_connected(void)
88+
{
89+
// If the voltage is greater than 4500 mV it means USB is connected
90+
uint16_t vbat = power_get_vbat();
91+
if (vbat > 4500) {
92+
return true;
93+
}
94+
return false;
95+
}

0 commit comments

Comments
 (0)