Skip to content

Commit a5c9eab

Browse files
harinworksJamie C. Driver
authored andcommitted
diy: refactor wslcdtouch2.inc for better code clarity
1 parent c2d8986 commit a5c9eab

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

main/power/wslcdtouch2.inc

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
// Waveshare LCD Touch 2 implmentation
22
//
3-
#include "esp_err.h"
4-
#include "freertos/idf_additions.h"
5-
#include "hal/adc_types.h"
6-
#include "hal/gpio_types.h"
7-
#include "iot_button.h"
8-
#include "power.h"
9-
#include "sdkconfig.h"
10-
#include "soc/gpio_num.h"
11-
#include <button_gpio.h>
123
#include <driver/gpio.h>
134
#include <driver/ledc.h>
145
#include <esp_adc/adc_cali.h>
156
#include <esp_adc/adc_cali_scheme.h>
167
#include <esp_adc/adc_oneshot.h>
178
#include <esp_sleep.h>
9+
#include <freertos/idf_additions.h>
10+
11+
#include "button_gpio.h"
12+
#include "hal/adc_types.h"
13+
#include "hal/gpio_types.h"
14+
#include "iot_button.h"
15+
#include "power.h"
16+
#include "soc/gpio_num.h"
1817

1918
#define BATTERY_ADC_CHANNEL ADC_CHANNEL_4
20-
#define ADC_ATTEN ADC_ATTEN_DB_12
19+
#define BATTERY_ADC_ATTEN ADC_ATTEN_DB_12
20+
#define BATTERY_EMA_ALPHA 0.3 // estimated moving average smoothing factor
2121

2222
#define LCD_BL_LEDC_TIMER LEDC_TIMER_0
2323
#define LCD_BL_LEDC_MODE LEDC_LOW_SPEED_MODE
@@ -28,32 +28,9 @@
2828
#define LCD_BL_LEDC_FREQUENCY (10000)
2929

3030
static adc_oneshot_unit_handle_t adc1_handle = NULL;
31-
adc_cali_handle_t adc1_cali_chan0_handle = NULL;
31+
static adc_cali_handle_t adc1_cali_chan0_handle = NULL;
3232

33-
const float ema_alpha = 0.3; // Estimated moving average smoothing factor
34-
int ema_voltage = 0;
35-
36-
void brightness_init(void)
37-
{
38-
gpio_set_direction(CONFIG_DISPLAY_PIN_BL, GPIO_MODE_OUTPUT);
39-
gpio_set_level(CONFIG_DISPLAY_PIN_BL, 1);
40-
41-
ledc_timer_config_t ledc_timer = { .speed_mode = LCD_BL_LEDC_MODE,
42-
.timer_num = LCD_BL_LEDC_TIMER,
43-
.duty_resolution = LCD_BL_LEDC_DUTY_RES,
44-
.freq_hz = LCD_BL_LEDC_FREQUENCY, // Set output frequency at 5 kHz
45-
.clk_cfg = LEDC_AUTO_CLK };
46-
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
47-
48-
ledc_channel_config_t ledc_channel = { .speed_mode = LCD_BL_LEDC_MODE,
49-
.channel = LCD_BL_LEDC_CHANNEL,
50-
.timer_sel = LCD_BL_LEDC_TIMER,
51-
.intr_type = LEDC_INTR_DISABLE,
52-
.gpio_num = CONFIG_DISPLAY_PIN_BL,
53-
.duty = 0, // Set duty to 0%
54-
.hpoint = 0 };
55-
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
56-
}
33+
static int ema_voltage = 0;
5734

5835
static void button_shutdown(void* arg, void* ctx)
5936
{
@@ -64,7 +41,7 @@ static void button_shutdown(void* arg, void* ctx)
6441
power_shutdown();
6542
}
6643

67-
void boot_button_init(void)
44+
static esp_err_t boot_button_init(void)
6845
{
6946
const button_config_t btn_cfg = {
7047
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
@@ -78,24 +55,48 @@ void boot_button_init(void)
7855

7956
button_handle_t btn_handle = NULL;
8057
ESP_ERROR_CHECK(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn_handle));
81-
82-
JADE_ASSERT(btn_handle);
83-
8458
ESP_ERROR_CHECK(iot_button_register_cb(btn_handle, BUTTON_LONG_PRESS_HOLD, NULL, button_shutdown, NULL));
8559

8660
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
8761
gpio_hold_en(GPIO_NUM_0);
8862
gpio_deep_sleep_hold_en();
8963
esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, 0);
64+
65+
return ESP_OK;
66+
}
67+
68+
static esp_err_t brightness_init(void)
69+
{
70+
gpio_reset_pin(CONFIG_DISPLAY_PIN_BL);
71+
gpio_set_direction(CONFIG_DISPLAY_PIN_BL, GPIO_MODE_OUTPUT);
72+
gpio_set_level(CONFIG_DISPLAY_PIN_BL, 1);
73+
74+
ledc_timer_config_t ledc_timer = { .speed_mode = LCD_BL_LEDC_MODE,
75+
.timer_num = LCD_BL_LEDC_TIMER,
76+
.duty_resolution = LCD_BL_LEDC_DUTY_RES,
77+
.freq_hz = LCD_BL_LEDC_FREQUENCY, // Set output frequency at 5 kHz
78+
.clk_cfg = LEDC_AUTO_CLK };
79+
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
80+
81+
ledc_channel_config_t ledc_channel = { .speed_mode = LCD_BL_LEDC_MODE,
82+
.channel = LCD_BL_LEDC_CHANNEL,
83+
.timer_sel = LCD_BL_LEDC_TIMER,
84+
.intr_type = LEDC_INTR_DISABLE,
85+
.gpio_num = CONFIG_DISPLAY_PIN_BL,
86+
.duty = 0, // Set duty to 0%
87+
.hpoint = 0 };
88+
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
89+
90+
return ESP_OK;
9091
}
9192

9293
esp_err_t power_init(void)
9394
{
9495
// Use the BOOT button as a sleep/wakeup button
95-
boot_button_init();
96+
ESP_ERROR_CHECK(boot_button_init());
9697

9798
// Initialise backlight brightness
98-
brightness_init();
99+
ESP_ERROR_CHECK(brightness_init());
99100

100101
// Initialise the ADC to measure battery level
101102
adc_oneshot_unit_init_cfg_t init_config1 = {
@@ -106,14 +107,14 @@ esp_err_t power_init(void)
106107

107108
adc_oneshot_chan_cfg_t config = {
108109
.bitwidth = ADC_BITWIDTH_DEFAULT,
109-
.atten = ADC_ATTEN_DB_12,
110+
.atten = BATTERY_ADC_ATTEN,
110111
};
111112
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, BATTERY_ADC_CHANNEL, &config));
112113

113114
// Use voltage calibration
114115
adc_cali_curve_fitting_config_t cali_config = {
115116
.unit_id = ADC_UNIT_1,
116-
.atten = ADC_ATTEN,
117+
.atten = BATTERY_ADC_ATTEN,
117118
.bitwidth = ADC_BITWIDTH_DEFAULT,
118119
};
119120
if (adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_chan0_handle) != ESP_OK) {
@@ -129,14 +130,15 @@ esp_err_t power_shutdown(void) { esp_deep_sleep_start(); }
129130
esp_err_t power_screen_on(void) { return ESP_OK; }
130131
esp_err_t power_screen_off(void) { return ESP_OK; }
131132

132-
esp_err_t power_backlight_on(const uint8_t brightness)
133+
esp_err_t power_backlight_on(uint8_t brightness)
133134
{
134-
uint32_t new_brightness = brightness * 20;
135-
if (new_brightness > 100) {
136-
return ESP_OK;
135+
if (brightness < BACKLIGHT_MIN) {
136+
brightness = BACKLIGHT_MIN;
137+
} else if (brightness > BACKLIGHT_MAX) {
138+
brightness = BACKLIGHT_MAX;
137139
}
138140

139-
uint32_t duty = (new_brightness * (LCD_BL_LEDC_DUTY - 1)) / 100;
141+
uint32_t duty = (brightness * (LCD_BL_LEDC_DUTY - 1)) / 5;
140142
ESP_ERROR_CHECK(ledc_set_duty(LCD_BL_LEDC_MODE, LCD_BL_LEDC_CHANNEL, duty));
141143
ESP_ERROR_CHECK(ledc_update_duty(LCD_BL_LEDC_MODE, LCD_BL_LEDC_CHANNEL));
142144

@@ -157,8 +159,8 @@ esp_err_t power_camera_off(void) { return ESP_OK; }
157159
uint16_t power_get_vbat(void)
158160
{
159161
JADE_ASSERT(adc1_handle);
160-
int raw_adc = 0;
161-
int voltage;
162+
163+
int raw_adc, voltage;
162164
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC_CHANNEL, &raw_adc));
163165

164166
if (adc1_cali_chan0_handle) {
@@ -168,26 +170,26 @@ uint16_t power_get_vbat(void)
168170
}
169171

170172
// apply Estimated Moving Average since the ADC seems to have a lot of noise
171-
ema_voltage = ema_alpha * voltage + (1 - ema_alpha) * (ema_voltage == 0 ? voltage : ema_voltage);
173+
ema_voltage = BATTERY_EMA_ALPHA * voltage + (1 - BATTERY_EMA_ALPHA) * (ema_voltage == 0 ? voltage : ema_voltage);
172174
/* JADE_LOGI("Raw: %.2dmV, Smoothed: %.2d mV", voltage, ema_voltage); */
173175
return (uint16_t)ema_voltage;
174176
}
175177

176178
uint8_t power_get_battery_status(void)
177179
{
178-
int vbat = power_get_vbat() * 3; // apply voltage divider
179-
if (vbat < 3000)
180-
return 0;
181-
else if (vbat < 3200)
182-
return 1;
183-
else if (vbat < 3400)
184-
return 2;
185-
else if (vbat < 3600)
186-
return 3;
187-
else if (vbat < 3800)
188-
return 4;
189-
else
180+
const uint16_t vbat = power_get_vbat() * 3; // apply voltage divider
181+
if (vbat > 3800) {
190182
return 5;
183+
} else if (vbat > 3600) {
184+
return 4;
185+
} else if (vbat > 3400) {
186+
return 3;
187+
} else if (vbat > 3200) {
188+
return 2;
189+
} else if (vbat > 3000) {
190+
return 1;
191+
}
192+
return 0;
191193
}
192194

193195
// The STAT pin on the charging IC isn't exposed to a GPIO so we cannot read it

0 commit comments

Comments
 (0)