Skip to content

Commit b0b471e

Browse files
authored
add zhengchen_boards_wifi(征辰科技) (#635)
* add zhengchen_boards_wifi * add zhengchen_boards_wifi1
1 parent 538e07a commit b0b471e

File tree

13 files changed

+677
-0
lines changed

13 files changed

+677
-0
lines changed

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ elseif(CONFIG_BOARD_TYPE_ESP32_CGC)
159159
set(BOARD_TYPE "esp32-cgc")
160160
elseif(CONFIG_BOARD_TYPE_ESP_S3_LCD_EV_Board)
161161
set(BOARD_TYPE "esp-s3-lcd-ev-board")
162+
elseif(CONFIG_BOARD_TYPE_ZHENGCHEN_1_54TFT_WIFI)
163+
set(BOARD_TYPE "zhengchen-1.54tft-wifi")
162164
endif()
163165
file(GLOB BOARD_SOURCES
164166
${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD_TYPE}/*.cc

main/Kconfig.projbuild

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ choice BOARD_TYPE
152152
bool "元控·青春"
153153
config BOARD_TYPE_ESP_S3_LCD_EV_Board
154154
bool "乐鑫ESP S3 LCD EV Board开发板"
155+
config BOARD_TYPE_ZHENGCHEN_1_54TFT_WIFI
156+
bool "征辰科技1.54(WIFI)"
155157
endchoice
156158

157159
choice ESP_S3_LCD_EV_Board_Version_TYPE

main/boards/common/axp2101.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ int Axp2101::GetBatteryLevel() {
3030
return ReadReg(0xA4);
3131
}
3232

33+
float Axp2101::GetTemperature() {
34+
return ReadReg(0xA5);
35+
}
36+
3337
void Axp2101::PowerOff() {
3438
uint8_t value = ReadReg(0x10);
3539
value = value | 0x01;

main/boards/common/axp2101.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Axp2101 : public I2cDevice {
1010
bool IsDischarging();
1111
bool IsChargingDone();
1212
int GetBatteryLevel();
13+
float GetTemperature();
1314
void PowerOff();
1415

1516
private:

main/boards/common/board.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ bool Board::GetBatteryLevel(int &level, bool& charging, bool& discharging) {
4848
return false;
4949
}
5050

51+
bool Board::GetTemperature(float& esp32temp){
52+
return false;
53+
}
54+
5155
Display* Board::GetDisplay() {
5256
static NoDisplay display;
5357
return &display;

main/boards/common/board.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Board {
3737
virtual Backlight* GetBacklight() { return nullptr; }
3838
virtual Led* GetLed();
3939
virtual AudioCodec* GetAudioCodec() = 0;
40+
virtual bool GetTemperature(float& esp32temp);
4041
virtual Display* GetDisplay();
4142
virtual Http* CreateHttp() = 0;
4243
virtual WebSocket* CreateWebSocket() = 0;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 编译配置命令
2+
3+
**配置编译目标为 ESP32S3:**
4+
5+
```bash
6+
idf.py set-target esp32s3
7+
```
8+
9+
**打开 menuconfig:**
10+
11+
```bash
12+
idf.py menuconfig
13+
```
14+
15+
**选择板子:**
16+
17+
```
18+
Xiaozhi Assistant -> Board Type -> zhengchen-1.54tft-wifi
19+
```
20+
21+
```
22+
23+
**编译:**
24+
25+
bash
26+
idf.py build
27+
```
28+
29+
**下载:**
30+
idf.py build flash monitor
31+
32+
进行下载和显示日志
33+
34+
35+
**固件生成:**
36+
37+
```bash
38+
idf.py merge-bin
39+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#ifndef _BOARD_CONFIG_H_
3+
#define _BOARD_CONFIG_H_
4+
5+
#include <driver/gpio.h>
6+
7+
#define AUDIO_INPUT_SAMPLE_RATE 16000
8+
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
9+
#define AUDIO_I2S_MIC_GPIO_WS GPIO_NUM_4
10+
#define AUDIO_I2S_MIC_GPIO_SCK GPIO_NUM_5
11+
#define AUDIO_I2S_MIC_GPIO_DIN GPIO_NUM_6
12+
#define AUDIO_I2S_SPK_GPIO_DOUT GPIO_NUM_7
13+
#define AUDIO_I2S_SPK_GPIO_BCLK GPIO_NUM_15
14+
#define AUDIO_I2S_SPK_GPIO_LRCK GPIO_NUM_16
15+
16+
#define BOOT_BUTTON_GPIO GPIO_NUM_0
17+
#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_10
18+
#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_39
19+
20+
#define DISPLAY_SDA GPIO_NUM_41
21+
#define DISPLAY_SCL GPIO_NUM_42
22+
#define DISPLAY_RES GPIO_NUM_45
23+
#define DISPLAY_DC GPIO_NUM_40
24+
#define DISPLAY_CS GPIO_NUM_21
25+
26+
#define DISPLAY_WIDTH 240
27+
#define DISPLAY_HEIGHT 240
28+
#define DISPLAY_SWAP_XY false
29+
#define DISPLAY_MIRROR_X false
30+
#define DISPLAY_MIRROR_Y false
31+
#define BACKLIGHT_INVERT false
32+
#define DISPLAY_OFFSET_X 0
33+
#define DISPLAY_OFFSET_Y 0
34+
35+
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_20
36+
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
37+
38+
39+
#endif // _BOARD_CONFIG_H_
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"target": "esp32s3",
3+
"builds": [
4+
{
5+
"name": "zhengchen-1.54tft-wifi",
6+
"sdkconfig_append": []
7+
}
8+
]
9+
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
#pragma once
2+
#include <vector>
3+
#include <functional>
4+
#include <esp_log.h>
5+
#include <esp_timer.h>
6+
#include <driver/gpio.h>
7+
#include <esp_adc/adc_oneshot.h>
8+
#include <driver/temperature_sensor.h>
9+
#include "application.h"
10+
#include "zhengchen_lcd_display.h"
11+
12+
class PowerManager {
13+
private:
14+
// 定时器句柄
15+
esp_timer_handle_t timer_handle_;
16+
std::function<void(bool)> on_charging_status_changed_;
17+
std::function<void(bool)> on_low_battery_status_changed_;
18+
std::function<void(float)> on_temperature_changed_;
19+
20+
gpio_num_t charging_pin_ = GPIO_NUM_NC;
21+
std::vector<uint16_t> adc_values_;
22+
uint32_t battery_level_ = 0;
23+
bool is_charging_ = false;
24+
bool is_low_battery_ = false;
25+
float current_temperature_ = 0.0f;
26+
int ticks_ = 0;
27+
const int kBatteryAdcInterval = 60;
28+
const int kBatteryAdcDataCount = 3;
29+
const int kLowBatteryLevel = 20;
30+
const int kTemperatureReadInterval = 10; // 每 10 秒读取一次温度
31+
32+
adc_oneshot_unit_handle_t adc_handle_;
33+
temperature_sensor_handle_t temp_sensor_ = NULL;
34+
35+
void CheckBatteryStatus() {
36+
// Get charging status
37+
bool new_charging_status = gpio_get_level(charging_pin_) == 1;
38+
if (new_charging_status != is_charging_) {
39+
is_charging_ = new_charging_status;
40+
if (on_charging_status_changed_) {
41+
on_charging_status_changed_(is_charging_);
42+
}
43+
ReadBatteryAdcData();
44+
return;
45+
}
46+
47+
// 如果电池电量数据不足,则读取电池电量数据
48+
if (adc_values_.size() < kBatteryAdcDataCount) {
49+
ReadBatteryAdcData();
50+
return;
51+
}
52+
53+
// 如果电池电量数据充足,则每 kBatteryAdcInterval 个 tick 读取一次电池电量数据
54+
ticks_++;
55+
if (ticks_ % kBatteryAdcInterval == 0) {
56+
ReadBatteryAdcData();
57+
}
58+
59+
// 新增:周期性读取温度
60+
if (ticks_ % kTemperatureReadInterval == 0) {
61+
ReadTemperature();
62+
}
63+
}
64+
65+
void ReadBatteryAdcData() {
66+
// 读取 ADC 值
67+
int adc_value;
68+
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, ADC_CHANNEL_7, &adc_value));
69+
70+
71+
// 将 ADC 值添加到队列中
72+
adc_values_.push_back(adc_value);
73+
if (adc_values_.size() > kBatteryAdcDataCount) {
74+
adc_values_.erase(adc_values_.begin());
75+
}
76+
uint32_t average_adc = 0;
77+
for (auto value : adc_values_) {
78+
average_adc += (value + 80);
79+
}
80+
average_adc /= adc_values_.size();
81+
82+
83+
// 定义电池电量区间
84+
const struct {
85+
uint16_t adc;
86+
uint8_t level;
87+
} levels[] = {
88+
{2030, 0},
89+
{2134, 20},
90+
{2252, 40},
91+
{2370, 60},
92+
{2488, 80},
93+
{2606, 100}
94+
};
95+
// 低于最低值时
96+
if (average_adc < levels[0].adc) {
97+
battery_level_ = 0;
98+
}
99+
// 高于最高值时
100+
else if (average_adc >= levels[5].adc) {
101+
battery_level_ = 100;
102+
} else {
103+
// 线性插值计算中间值
104+
for (int i = 0; i < 5; i++) {
105+
if (average_adc >= levels[i].adc && average_adc < levels[i+1].adc) {
106+
float ratio = static_cast<float>(average_adc - levels[i].adc) / (levels[i+1].adc - levels[i].adc);
107+
battery_level_ = levels[i].level + ratio * (levels[i+1].level - levels[i].level);
108+
break;
109+
}
110+
}
111+
}
112+
// 检查是否达到低电量阈值
113+
if (adc_values_.size() >= kBatteryAdcDataCount) {
114+
bool new_low_battery_status = battery_level_ <= kLowBatteryLevel;
115+
if (new_low_battery_status != is_low_battery_) {
116+
is_low_battery_ = new_low_battery_status;
117+
if (on_low_battery_status_changed_) {
118+
on_low_battery_status_changed_(is_low_battery_);
119+
}
120+
}
121+
}
122+
123+
ESP_LOGI("PowerManager", "ADC value: %d average: %ld level: %ld", adc_value, average_adc, battery_level_);
124+
}
125+
126+
void ReadTemperature() {
127+
float temperature = 0.0f;
128+
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor_, &temperature));
129+
130+
if (abs(temperature - current_temperature_) >= 3.5f) { // 温度变化超过3.5°C才触发回调
131+
current_temperature_ = temperature;
132+
if (on_temperature_changed_) {
133+
on_temperature_changed_(current_temperature_);
134+
}
135+
ESP_LOGI("PowerManager", "Temperature updated: %.1f°C", current_temperature_);
136+
}
137+
}
138+
139+
140+
public:
141+
PowerManager(gpio_num_t pin) : charging_pin_(pin) {
142+
143+
// 初始化充电引脚
144+
gpio_config_t io_conf = {};
145+
io_conf.intr_type = GPIO_INTR_DISABLE;
146+
io_conf.mode = GPIO_MODE_INPUT;
147+
io_conf.pin_bit_mask = (1ULL << charging_pin_);
148+
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
149+
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
150+
gpio_config(&io_conf);
151+
152+
// 创建电池电量检查定时器
153+
esp_timer_create_args_t timer_args = {
154+
.callback = [](void* arg) {
155+
PowerManager* self = static_cast<PowerManager*>(arg);
156+
self->CheckBatteryStatus();
157+
},
158+
.arg = this,
159+
.dispatch_method = ESP_TIMER_TASK,
160+
.name = "battery_check_timer",
161+
.skip_unhandled_events = true,
162+
};
163+
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
164+
ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
165+
166+
// 初始化 ADC
167+
adc_oneshot_unit_init_cfg_t init_config = {
168+
.unit_id = ADC_UNIT_1,
169+
.ulp_mode = ADC_ULP_MODE_DISABLE,
170+
};
171+
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_));
172+
173+
adc_oneshot_chan_cfg_t chan_config = {
174+
.atten = ADC_ATTEN_DB_12,
175+
.bitwidth = ADC_BITWIDTH_12,
176+
};
177+
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, ADC_CHANNEL_7, &chan_config));
178+
179+
// 初始化温度传感器
180+
temperature_sensor_config_t temp_config = {
181+
.range_min = 10,
182+
.range_max = 80,
183+
.clk_src = TEMPERATURE_SENSOR_CLK_SRC_DEFAULT
184+
};
185+
ESP_ERROR_CHECK(temperature_sensor_install(&temp_config, &temp_sensor_));
186+
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor_));
187+
ESP_LOGI("PowerManager", "Temperature sensor initialized (new driver)");
188+
}
189+
190+
~PowerManager() {
191+
if (timer_handle_) {
192+
esp_timer_stop(timer_handle_);
193+
esp_timer_delete(timer_handle_);
194+
}
195+
if (adc_handle_) {
196+
adc_oneshot_del_unit(adc_handle_);
197+
}
198+
199+
if (temp_sensor_) {
200+
temperature_sensor_disable(temp_sensor_);
201+
temperature_sensor_uninstall(temp_sensor_);
202+
}
203+
204+
}
205+
206+
bool IsCharging() {
207+
// 如果电量已经满了,则不再显示充电中
208+
if (battery_level_ == 100) {
209+
return false;
210+
}
211+
return is_charging_;
212+
}
213+
214+
bool IsDischarging() {
215+
// 没有区分充电和放电,所以直接返回相反状态
216+
return !is_charging_;
217+
}
218+
219+
// 获取电池电量
220+
uint8_t GetBatteryLevel() {
221+
// 返回电池电量
222+
return battery_level_;
223+
}
224+
225+
float GetTemperature() const { return current_temperature_; } // 获取当前温度
226+
227+
void OnTemperatureChanged(std::function<void(float)> callback) {
228+
on_temperature_changed_ = callback;
229+
}
230+
231+
void OnLowBatteryStatusChanged(std::function<void(bool)> callback) {
232+
on_low_battery_status_changed_ = callback;
233+
}
234+
235+
void OnChargingStatusChanged(std::function<void(bool)> callback) {
236+
on_charging_status_changed_ = callback;
237+
}
238+
};

0 commit comments

Comments
 (0)