init
This commit is contained in:
+107
@@ -0,0 +1,107 @@
|
||||
#include "init.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <LittleFS.h>
|
||||
#include <WiFi.h>
|
||||
#include "utils.h"
|
||||
|
||||
namespace esp32 {
|
||||
namespace init {
|
||||
|
||||
// WiFi 配置变量
|
||||
static String wifiSSID = "";
|
||||
static String wifiPassword = "";
|
||||
|
||||
// TFT 显示器引用 (在 initSystem 中设置)
|
||||
static Adafruit_ST7735* tftPtr = nullptr;
|
||||
|
||||
// 在屏幕上打印日志
|
||||
void tftLog(const char* msg, uint16_t color) {
|
||||
if (tftPtr == nullptr)
|
||||
return;
|
||||
tftPtr->setTextColor(color);
|
||||
tftPtr->println(msg);
|
||||
tftPtr->setCursor(tftPtr->getCursorX(),
|
||||
tftPtr->getCursorY() + 2); // 增加2像素行距
|
||||
}
|
||||
|
||||
// 读取并解析配置文件
|
||||
bool loadSettings() {
|
||||
if (!LittleFS.begin(true)) {
|
||||
tftLog("LittleFS mount failed!", RED);
|
||||
return false;
|
||||
}
|
||||
tftLog("LittleFS mounted", GREEN);
|
||||
|
||||
File file = LittleFS.open("/settings.json", "r");
|
||||
if (!file) {
|
||||
tftLog("Can't open settings", RED);
|
||||
return false;
|
||||
}
|
||||
tftLog("Settings file opened", GREEN);
|
||||
|
||||
// 使用 ArduinoJson 7.x 解析
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, file);
|
||||
file.close();
|
||||
|
||||
if (error) {
|
||||
tftLog("JSON parse failed!", RED);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 WiFi 配置
|
||||
wifiSSID = doc["wifi"]["ssid"].as<String>();
|
||||
wifiPassword = doc["wifi"]["password"].as<String>();
|
||||
|
||||
tftLog("Config loaded:", GREEN);
|
||||
tftPtr->print(" SSID: ");
|
||||
tftLog(wifiSSID.c_str(), WHITE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 连接 WiFi
|
||||
void connectWiFi() {
|
||||
if (wifiSSID.isEmpty()) {
|
||||
tftLog("WiFi SSID empty", RED);
|
||||
return;
|
||||
}
|
||||
|
||||
tftPtr->print("Connecting: ");
|
||||
tftLog(wifiSSID.c_str(), WHITE);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
|
||||
|
||||
int attempts = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||
delay(500);
|
||||
tftPtr->print(".");
|
||||
attempts++;
|
||||
}
|
||||
tftPtr->println();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
tftLog("WiFi connected!", GREEN);
|
||||
tftPtr->print("IP: ");
|
||||
tftLog(WiFi.localIP().toString().c_str(), WHITE);
|
||||
} else {
|
||||
tftLog("WiFi failed!", RED);
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化系统
|
||||
void initSystem(Adafruit_ST7735& tft) {
|
||||
tftPtr = &tft;
|
||||
|
||||
tftLog("=== ESP32-S3 ===", YELLOW);
|
||||
tft.println();
|
||||
|
||||
// 读取配置文件并连接 WiFi
|
||||
if (loadSettings()) {
|
||||
connectWiFi();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace init
|
||||
} // namespace esp32
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_ST7735.h>
|
||||
#include <SPI.h>
|
||||
#include "utils.h"
|
||||
#include "init.h"
|
||||
|
||||
using namespace esp32;
|
||||
|
||||
#define TFT_MOSI 16
|
||||
#define TFT_SCLK 15
|
||||
#define TFT_CS 18
|
||||
#define TFT_DC 17
|
||||
#define TFT_RST -1
|
||||
|
||||
// 屏幕尺寸为 128x160
|
||||
Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST);
|
||||
|
||||
#define function auto
|
||||
#define let auto
|
||||
function rgbTo565(uint32_t rgbColor) -> uint16_t {
|
||||
uint8_t r = (rgbColor >> 16) & 0xFF;
|
||||
uint8_t g = (rgbColor >> 8) & 0xFF;
|
||||
uint8_t b = rgbColor & 0xFF;
|
||||
|
||||
// 交换 r 和 b
|
||||
uint8_t tmp = r;
|
||||
r = b;
|
||||
b = tmp;
|
||||
|
||||
uint16_t r5 = (r * 31 / 255) << 11;
|
||||
uint16_t g6 = (g * 63 / 255) << 5;
|
||||
uint16_t b5 = (b * 31 / 255);
|
||||
|
||||
return r5 | g6 | b5;
|
||||
}
|
||||
|
||||
const int BTN_LEFT = 12; // 左按钮
|
||||
const int BTN_RIGHT = 11; // 右按钮
|
||||
const int BTN_UP = 10; // 上按钮
|
||||
|
||||
#define BLACK rgbTo565(0x000000)
|
||||
#define WHITE rgbTo565(0xFFFFFF)
|
||||
#define RED rgbTo565(0xFF0000)
|
||||
#define GREEN rgbTo565(0x00FF00)
|
||||
#define BLUE rgbTo565(0x0000FF)
|
||||
#define YELLOW rgbTo565(0xFFFF00)
|
||||
|
||||
void setup() {
|
||||
// 按钮:内部下拉
|
||||
pinMode(BTN_LEFT, INPUT_PULLDOWN);
|
||||
pinMode(BTN_RIGHT, INPUT_PULLDOWN);
|
||||
pinMode(BTN_UP, INPUT_PULLDOWN);
|
||||
|
||||
// 1. 把硬件 SPI 绑定到你现有的引脚
|
||||
SPI.begin(TFT_SCLK, -1 /*MISO 不用*/, TFT_MOSI, TFT_CS);
|
||||
SPI.setFrequency(27000000); // ST7735 通常 27MHz 左右比较稳
|
||||
|
||||
// 2. 初始化屏幕
|
||||
tft.initR(INITR_GREENTAB);
|
||||
tft.setRotation(0);
|
||||
tft.fillScreen(BLACK);
|
||||
tft.setCursor(0, 0);
|
||||
tft.setTextColor(WHITE);
|
||||
tft.setTextSize(1);
|
||||
|
||||
// 初始化系统(读取配置并连接 WiFi)
|
||||
init::initSystem(tft);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool leftPressed = digitalRead(BTN_LEFT) == HIGH; // 按下=HIGH
|
||||
bool rightPressed = digitalRead(BTN_RIGHT) == HIGH;
|
||||
bool upPressed = digitalRead(BTN_UP) == HIGH;
|
||||
|
||||
tft.fillRect(0, 30, 20, 20, leftPressed ? RED : BLACK);
|
||||
tft.fillRect(108, 30, 20, 20, rightPressed ? RED : BLACK);
|
||||
tft.fillRect(54, 0, 20, 20, upPressed ? RED : BLACK);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "utils.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
#define function auto
|
||||
#define let auto
|
||||
|
||||
namespace esp32 {
|
||||
namespace utils {
|
||||
|
||||
// 三次贝塞尔曲线计算
|
||||
function cubicBezier(float t, float p0, float p1, float p2, float p3) -> float {
|
||||
let u = 1.0f - t;
|
||||
return (u * u * u * p0) + (3.0f * u * u * t * p1) +
|
||||
(3.0f * u * t * t * p2) + (t * t * t * p3);
|
||||
}
|
||||
|
||||
function solveCubicBezierX(float xTarget,
|
||||
float x1,
|
||||
float x2,
|
||||
float epsilon) -> float {
|
||||
let t = xTarget;
|
||||
let x = cubicBezier(t, 0.0f, x1, x2, 1.0f);
|
||||
int iteration = 0;
|
||||
|
||||
while (abs(x - xTarget) > epsilon && iteration < 100) {
|
||||
let d = 3.0f * (1.0f - t) * (1.0f - t) * (x1 - 0.0f) +
|
||||
6.0f * (1.0f - t) * t * (x2 - x1) + 3.0f * t * t * (1.0f - x2);
|
||||
if (d == 0.0f)
|
||||
break;
|
||||
t -= (x - xTarget) / d;
|
||||
x = cubicBezier(t, 0.0f, x1, x2, 1.0f);
|
||||
iteration++;
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
function linear2CubicBezier(float source,
|
||||
float x1,
|
||||
float y1,
|
||||
float x2,
|
||||
float y2) -> float {
|
||||
if (source < 0.0f || source > 1.0f) {
|
||||
return source; // 返回原值而不是抛出异常
|
||||
}
|
||||
|
||||
let t = solveCubicBezierX(source, x1, x2);
|
||||
let y = cubicBezier(t, 0.0f, y1, y2, 1.0f);
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
function rgbTo565(uint32_t rgbColor) -> uint16_t {
|
||||
uint8_t r = (rgbColor >> 16) & 0xFF;
|
||||
uint8_t g = (rgbColor >> 8) & 0xFF;
|
||||
uint8_t b = rgbColor & 0xFF;
|
||||
|
||||
// 交换 r 和 b
|
||||
uint8_t tmp = r;
|
||||
r = b;
|
||||
b = tmp;
|
||||
|
||||
uint16_t r5 = (r * 31 / 255) << 11;
|
||||
uint16_t g6 = (g * 63 / 255) << 5;
|
||||
uint16_t b5 = (b * 31 / 255);
|
||||
|
||||
return r5 | g6 | b5;
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
} // namespace esp32
|
||||
Reference in New Issue
Block a user