Compare commits
No commits in common. "3d9e7e7b2ff7372114f0022470b1d9e66506584e" and "531ac11c68a856537fc7e34891b24844d5cf493a" have entirely different histories.
3d9e7e7b2f
...
531ac11c68
@ -9,10 +9,10 @@ namespace ad7606 {
|
|||||||
|
|
||||||
// 数据/控制引脚
|
// 数据/控制引脚
|
||||||
static const int PIN_AD_MISO = 8; // DOUTA -> ESP32 MISO
|
static const int PIN_AD_MISO = 8; // DOUTA -> ESP32 MISO
|
||||||
static const int PIN_AD_CS = 39; // CS_N
|
static const int PIN_AD_CS = 12; // CS_N
|
||||||
static const int PIN_AD_CONVST = 13; // CONVST(CO-A/CO-B 共用)
|
static const int PIN_AD_CONVST = 13; // CONVST(CO-A/CO-B 共用)
|
||||||
static const int PIN_AD_RESET = 40; // RESET
|
static const int PIN_AD_RESET = 5; // RESET
|
||||||
static const int PIN_AD_BUSY = 38; // BUSY
|
static const int PIN_AD_BUSY = 14; // BUSY
|
||||||
|
|
||||||
// 可选:过采样和模式引脚(-1 表示不配置/已硬件固定)
|
// 可选:过采样和模式引脚(-1 表示不配置/已硬件固定)
|
||||||
static const int PIN_AD_OS0 = -1;
|
static const int PIN_AD_OS0 = -1;
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
#define INIT_H
|
#define INIT_H
|
||||||
|
|
||||||
#include <Adafruit_ST7735.h>
|
#include <Adafruit_ST7735.h>
|
||||||
#include <U8g2_for_Adafruit_GFX.h>
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
namespace esp32 {
|
namespace esp32 {
|
||||||
@ -18,7 +17,7 @@ bool loadSettings();
|
|||||||
void connectWiFi();
|
void connectWiFi();
|
||||||
|
|
||||||
// 初始化系统
|
// 初始化系统
|
||||||
void initSystem(Adafruit_ST7735& tft, U8G2_FOR_ADAFRUIT_GFX& u8g2);
|
void initSystem(Adafruit_ST7735& tft);
|
||||||
|
|
||||||
// 获取后端接口 URL(来自 settings.json)
|
// 获取后端接口 URL(来自 settings.json)
|
||||||
const String& getBackendUrl();
|
const String& getBackendUrl();
|
||||||
|
|||||||
52
src/init.cpp
52
src/init.cpp
@ -14,19 +14,15 @@ static String backendUrl = "";
|
|||||||
|
|
||||||
// TFT 显示器引用 (在 initSystem 中设置)
|
// TFT 显示器引用 (在 initSystem 中设置)
|
||||||
static Adafruit_ST7735* tftPtr = nullptr;
|
static Adafruit_ST7735* tftPtr = nullptr;
|
||||||
static U8G2_FOR_ADAFRUIT_GFX* u8g2Ptr = nullptr;
|
|
||||||
|
|
||||||
// 在屏幕上打印日志
|
// 在屏幕上打印日志
|
||||||
void tftLog(const char* msg, uint16_t color) {
|
void tftLog(const char* msg, uint16_t color) {
|
||||||
if (tftPtr == nullptr || u8g2Ptr == nullptr)
|
if (tftPtr == nullptr)
|
||||||
return;
|
return;
|
||||||
u8g2Ptr->setForegroundColor(color);
|
tftPtr->setTextColor(color);
|
||||||
int16_t x = tftPtr->getCursorX();
|
tftPtr->println(msg);
|
||||||
int16_t y = tftPtr->getCursorY();
|
tftPtr->setCursor(tftPtr->getCursorX(),
|
||||||
u8g2Ptr->setCursor(x, y);
|
tftPtr->getCursorY() + 2); // 增加2像素行距
|
||||||
u8g2Ptr->print(msg);
|
|
||||||
// 更新光标位置,准备下一行(中文字体高度约14像素)
|
|
||||||
tftPtr->setCursor(0, y + 14);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取并解析配置文件
|
// 读取并解析配置文件
|
||||||
@ -59,7 +55,7 @@ bool loadSettings() {
|
|||||||
wifiPassword = doc["wifi"]["password"].as<String>();
|
wifiPassword = doc["wifi"]["password"].as<String>();
|
||||||
|
|
||||||
// 读取后端接口(可选)
|
// 读取后端接口(可选)
|
||||||
if (doc["backend"].is<JsonObject>() && doc["backend"]["url"].is<const char*>()) {
|
if (doc.containsKey("backend")) {
|
||||||
backendUrl = doc["backend"]["url"].as<String>();
|
backendUrl = doc["backend"]["url"].as<String>();
|
||||||
}
|
}
|
||||||
if (backendUrl.isEmpty()) {
|
if (backendUrl.isEmpty()) {
|
||||||
@ -67,8 +63,9 @@ bool loadSettings() {
|
|||||||
backendUrl = "http://nf.xn--876a.net/api/data";
|
backendUrl = "http://nf.xn--876a.net/api/data";
|
||||||
}
|
}
|
||||||
|
|
||||||
tftLog("配置已加载:", GREEN);
|
tftLog("Config loaded:", GREEN);
|
||||||
tftLog((" SSID: " + wifiSSID).c_str(), WHITE);
|
tftPtr->print(" SSID: ");
|
||||||
|
tftLog(wifiSSID.c_str(), WHITE);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -80,40 +77,35 @@ void connectWiFi() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tftLog(("连接 WiFi: " + wifiSSID).c_str(), WHITE);
|
tftPtr->print("Connecting: ");
|
||||||
|
tftLog(wifiSSID.c_str(), WHITE);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
|
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
|
||||||
|
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
int16_t x = tftPtr->getCursorX();
|
|
||||||
int16_t y = tftPtr->getCursorY();
|
|
||||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||||
delay(500);
|
delay(500);
|
||||||
u8g2Ptr->setCursor(x + attempts * 6, y);
|
tftPtr->print(".");
|
||||||
u8g2Ptr->print(".");
|
|
||||||
attempts++;
|
attempts++;
|
||||||
}
|
}
|
||||||
tftPtr->setCursor(0, y + 14);
|
tftPtr->println();
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
tftLog("WiFi 已连接!", GREEN);
|
tftLog("WiFi connected!", GREEN);
|
||||||
tftLog(("IP: " + WiFi.localIP().toString()).c_str(), WHITE);
|
tftPtr->print("IP: ");
|
||||||
|
tftLog(WiFi.localIP().toString().c_str(), WHITE);
|
||||||
} else {
|
} else {
|
||||||
tftLog("WiFi 连接失败!", RED);
|
tftLog("WiFi failed!", RED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化系统
|
// 初始化系统
|
||||||
void initSystem(Adafruit_ST7735& tft, U8G2_FOR_ADAFRUIT_GFX& u8g2) {
|
void initSystem(Adafruit_ST7735& tft) {
|
||||||
tftPtr = &tft;
|
tftPtr = &tft;
|
||||||
u8g2Ptr = &u8g2;
|
|
||||||
tft.setCursor(0, tft.getCursorY() + 14);
|
|
||||||
|
|
||||||
tftLog("=== 欢迎使用 ESP32 ===", YELLOW);
|
tftLog("=== ESP32-S3 ===", YELLOW);
|
||||||
tftLog("微弱压力数据采集系统", YELLOW);
|
tft.println();
|
||||||
|
|
||||||
tft.setCursor(0, tft.getCursorY() + 14);
|
|
||||||
|
|
||||||
// 读取配置文件并连接 WiFi
|
// 读取配置文件并连接 WiFi
|
||||||
if (loadSettings()) {
|
if (loadSettings()) {
|
||||||
@ -122,9 +114,7 @@ void initSystem(Adafruit_ST7735& tft, U8G2_FOR_ADAFRUIT_GFX& u8g2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 提供后端 URL 给外部使用
|
// 提供后端 URL 给外部使用
|
||||||
const String& getBackendUrl() {
|
const String& getBackendUrl() { return backendUrl; }
|
||||||
return backendUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace init
|
} // namespace init
|
||||||
} // namespace esp32
|
} // namespace esp32
|
||||||
|
|||||||
43
src/main.cpp
43
src/main.cpp
@ -69,9 +69,6 @@ struct AppState {
|
|||||||
char statusMsg[64];
|
char statusMsg[64];
|
||||||
uint32_t statusUntilMs = 0;
|
uint32_t statusUntilMs = 0;
|
||||||
uint16_t statusColor = WHITE;
|
uint16_t statusColor = WHITE;
|
||||||
|
|
||||||
// 帮助界面
|
|
||||||
bool helpMode = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static UI appUI;
|
static UI appUI;
|
||||||
@ -214,29 +211,6 @@ static void buildScene(void* ctx, DisplayList& out) {
|
|||||||
// 背景整屏清除
|
// 背景整屏清除
|
||||||
out.addFillRect(1, 0, 0, SCREEN_W, SCREEN_H, BLACK);
|
out.addFillRect(1, 0, 0, SCREEN_W, SCREEN_H, BLACK);
|
||||||
|
|
||||||
// 帮助界面
|
|
||||||
if (st->helpMode) {
|
|
||||||
out.addText(2, SCREEN_W / 2, 14, YELLOW, "帮助 / 操作说明", true);
|
|
||||||
|
|
||||||
uint16_t normal = WHITE;
|
|
||||||
uint16_t hint = esp32::utils::rgbTo565(0x80E1FF);
|
|
||||||
int y = 34;
|
|
||||||
out.addText(10, SCREEN_W / 2, y, normal, "左键: 录制开始/停止", true); y += 16;
|
|
||||||
out.addText(11, SCREEN_W / 2, y, normal, "停止后自动上传", true); y += 16;
|
|
||||||
out.addText(12, SCREEN_W / 2, y, normal, "右键: 标定并切换档位", true); y += 16;
|
|
||||||
out.addText(13, SCREEN_W / 2, y, normal, "≥2点后显示 mN 拟合", true); y += 16;
|
|
||||||
out.addText(14, SCREEN_W / 2, y, normal, "上键: 打开/关闭帮助", true); y += 22;
|
|
||||||
out.addText(15, SCREEN_W / 2, y, hint, "按 上键 返回主界面", true);
|
|
||||||
|
|
||||||
// 底部上传状态提示(临时显示)
|
|
||||||
uint32_t nowMs = millis();
|
|
||||||
if (st->statusUntilMs > nowMs && st->statusMsg[0] != '\0') {
|
|
||||||
out.addText(400, SCREEN_W / 2, SCREEN_H - 10, st->statusColor,
|
|
||||||
st->statusMsg, true);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 第一行:显示原始码 并在拟合可用时附加 mN
|
// 第一行:显示原始码 并在拟合可用时附加 mN
|
||||||
char line[64];
|
char line[64];
|
||||||
if (st->fitReady) {
|
if (st->fitReady) {
|
||||||
@ -449,13 +423,13 @@ void setup() {
|
|||||||
// u8g2_font_wqy12_t_chinese1: 12像素,常用汉字(约2500字)
|
// u8g2_font_wqy12_t_chinese1: 12像素,常用汉字(约2500字)
|
||||||
// u8g2_font_wqy12_t_chinese2: 12像素,更多汉字(约6000字)
|
// u8g2_font_wqy12_t_chinese2: 12像素,更多汉字(约6000字)
|
||||||
// u8g2_font_wqy12_t_chinese3: 12像素,完整汉字(约9000字)
|
// u8g2_font_wqy12_t_chinese3: 12像素,完整汉字(约9000字)
|
||||||
u8g2.setFont(u8g2_font_wqy12_t_gb2312); // 12像素字体,比 unifont 小
|
u8g2.setFont(u8g2_font_wqy12_t_chinese3); // 12像素字体,比 unifont 小
|
||||||
|
|
||||||
tft.setTextColor(WHITE);
|
tft.setTextColor(WHITE);
|
||||||
tft.setTextSize(1);
|
tft.setTextSize(1);
|
||||||
|
|
||||||
// 初始化系统(读取配置并连接 WiFi)
|
// 初始化系统(读取配置并连接 WiFi)
|
||||||
init::initSystem(tft, u8g2);
|
init::initSystem(tft);
|
||||||
|
|
||||||
// UI 初始化,传入 u8g2 以支持中文
|
// UI 初始化,传入 u8g2 以支持中文
|
||||||
appUI.begin(&tft, &u8g2);
|
appUI.begin(&tft, &u8g2);
|
||||||
@ -519,8 +493,8 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BTN_RIGHT:记录当前电压到当前档位,并循环到下一档(帮助界面下无效)
|
// BTN_RIGHT:记录当前电压到当前档位,并循环到下一档
|
||||||
if (!app.helpMode && rightPressed && !prevRight) {
|
if (rightPressed && !prevRight) {
|
||||||
float c = (float)app.lastRaw;
|
float c = (float)app.lastRaw;
|
||||||
int idx = app.presetIdx;
|
int idx = app.presetIdx;
|
||||||
app.capturedCode[idx] = c;
|
app.capturedCode[idx] = c;
|
||||||
@ -556,8 +530,8 @@ void loop() {
|
|||||||
app.presetIdx = (app.presetIdx + 1) % 4;
|
app.presetIdx = (app.presetIdx + 1) % 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 左键:开始/停止录制,停止后上传(帮助界面下无效)
|
// 左键:开始/停止录制,停止后上传
|
||||||
if (!app.helpMode && leftPressed && !prevLeft) {
|
if (leftPressed && !prevLeft) {
|
||||||
if (!app.recActive) {
|
if (!app.recActive) {
|
||||||
app.recActive = true;
|
app.recActive = true;
|
||||||
app.recLen = 0;
|
app.recLen = 0;
|
||||||
@ -569,11 +543,6 @@ void loop() {
|
|||||||
postRecording(app);
|
postRecording(app);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上键:打开/关闭帮助界面(任意界面有效)
|
|
||||||
if (upPressed && !prevUp) {
|
|
||||||
app.helpMode = !app.helpMode;
|
|
||||||
}
|
|
||||||
// printf("l:%s, r:%s, u:%s\n", leftPressed ? "Y" : "N", rightPressed ? "Y"
|
// printf("l:%s, r:%s, u:%s\n", leftPressed ? "Y" : "N", rightPressed ? "Y"
|
||||||
// : "N", upPressed ? "Y" : "N"); 驱动 UI 帧刷新
|
// : "N", upPressed ? "Y" : "N"); 驱动 UI 帧刷新
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user