初始提交卡包 USB 工程

纳入固件源码、屏幕组件、nand_xfer 工具和文档。忽略本机构建产物、sdkconfig,以及含本机路径的依赖锁。

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
zhengxin
2026-10-13 21:54:50 +08:00
co-authored by Cursor
commit 851e347cf5
260 changed files with 269349 additions and 0 deletions
+332
View File
@@ -0,0 +1,332 @@
#include "UI_DISPATCH/almanac_service.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "cjson.h"
#include "esp_log.h"
static almanac_provider_t s_provider = {0};
static const char *TAG = "almanac_service";
/* main/CMakeLists.txt: EMBED_TXTFILES "UI_DISPATCH/yiji_data_2026_2030.json" */
extern const char yiji_json_start[] asm("_binary_yiji_data_2026_2030_json_start");
extern const char yiji_json_end[] asm("_binary_yiji_data_2026_2030_json_end");
static cJSON *s_yiji_root = NULL;
static bool s_yiji_load_attempted = false;
/* 1900~2099 农历数据,bit 编码规则沿用常见农历算法实现 */
static const uint32_t LUNAR_INFO[] = {
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x05ac0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,
0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06aa0, 0x1a6c4, 0x0aae0,
0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4,
0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0,
0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160,
0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252,
};
typedef struct {
int year;
int month;
int day;
bool is_leap_month;
} lunar_date_t;
static int64_t days_from_civil(int y, unsigned m, unsigned d)
{
y -= (m <= 2);
{
const int era = (y >= 0 ? y : y - 399) / 400;
const unsigned yoe = (unsigned)(y - era * 400);
const unsigned doy = (153 * (m + (m > 2 ? (unsigned)-3 : 9)) + 2) / 5 + d - 1;
const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
return era * 146097 + (int64_t)doe - 719468;
}
}
static int lunar_leap_month(int year)
{
return (int)(LUNAR_INFO[year - 1900] & 0x0f);
}
static int lunar_leap_days(int year)
{
const int lm = lunar_leap_month(year);
if (lm == 0) {
return 0;
}
return (LUNAR_INFO[year - 1900] & 0x10000) ? 30 : 29;
}
static int lunar_month_days(int year, int month)
{
return (LUNAR_INFO[year - 1900] & (0x10000 >> month)) ? 30 : 29;
}
static int lunar_year_days(int year)
{
int sum = 348;
uint32_t mask = 0;
for (mask = 0x8000; mask > 0x8; mask >>= 1) {
if (LUNAR_INFO[year - 1900] & mask) {
sum += 1;
}
}
return sum + lunar_leap_days(year);
}
static bool solar_to_lunar(int year, int month, int day, lunar_date_t *out)
{
int64_t offset = 0;
int lunar_year = 0;
int year_days = 0;
int leap = 0;
bool in_leap = false;
int lunar_month = 0;
int month_days = 0;
if (out == NULL) {
return false;
}
if (year < 1900 || year > 2099 || month < 1 || month > 12 || day < 1 || day > 31) {
return false;
}
offset = days_from_civil(year, (unsigned)month, (unsigned)day) - days_from_civil(1900, 1, 31);
if (offset < 0) {
return false;
}
lunar_year = 1900;
year_days = lunar_year_days(lunar_year);
while (lunar_year < 2100 && offset >= year_days) {
offset -= year_days;
lunar_year++;
if (lunar_year <= 2099) {
year_days = lunar_year_days(lunar_year);
}
}
if (lunar_year > 2099) {
return false;
}
leap = lunar_leap_month(lunar_year);
lunar_month = 1;
while (lunar_month <= 12) {
if (leap > 0 && lunar_month == (leap + 1) && !in_leap) {
lunar_month -= 1;
in_leap = true;
month_days = lunar_leap_days(lunar_year);
} else {
month_days = lunar_month_days(lunar_year, lunar_month);
}
if (offset < month_days) {
break;
}
offset -= month_days;
if (in_leap && lunar_month == leap) {
in_leap = false;
}
lunar_month++;
}
out->year = lunar_year;
out->month = lunar_month;
out->day = (int)offset + 1;
out->is_leap_month = in_leap;
return true;
}
static const char *lunar_month_to_text(int month)
{
static const char *month_text[] = {
"", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊",
};
if (month < 1 || month > 12) {
return "";
}
return month_text[month];
}
static const char *lunar_day_to_text(int day)
{
static const char *day_text[] = {
"", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十",
};
if (day < 1 || day > 30) {
return "";
}
return day_text[day];
}
static bool almanac_builtin_get(uint16_t year, uint8_t month, uint8_t day, almanac_entry_t *out)
{
lunar_date_t lunar = {0};
cJSON *item = NULL;
cJSON *node_year = NULL;
cJSON *node_month = NULL;
cJSON *node_day = NULL;
cJSON *yi = NULL;
cJSON *ji = NULL;
int n = 0;
int i = 0;
size_t used = 0;
const char *word = NULL;
if (out == NULL) {
return false;
}
memset(out, 0, sizeof(*out));
if (solar_to_lunar((int)year, (int)month, (int)day, &lunar)) {
snprintf(out->lunar, sizeof(out->lunar), "%s%s月%s",
lunar.is_leap_month ? "闰" : "",
lunar_month_to_text(lunar.month),
lunar_day_to_text(lunar.day));
}
/* 内置 JSON 懒加载:只解析一次,失败后不重复尝试 */
if (!s_yiji_load_attempted) {
const char *json_start = yiji_json_start;
const char *json_end = yiji_json_end;
s_yiji_load_attempted = true;
if (json_end > json_start) {
const size_t json_len = (size_t)(json_end - json_start);
char *json_buf = (char *)malloc(json_len + 1);
if (json_buf != NULL) {
memcpy(json_buf, json_start, json_len);
json_buf[json_len] = '\0';
s_yiji_root = cJSON_Parse(json_buf);
free(json_buf);
if (s_yiji_root == NULL) {
ESP_LOGW(TAG, "parse yiji_data json failed");
}
} else {
ESP_LOGW(TAG, "malloc yiji_data buffer failed");
}
}
}
if (s_yiji_root != NULL && cJSON_IsArray(s_yiji_root)) {
n = cJSON_GetArraySize(s_yiji_root);
for (i = 0; i < n; i++) {
item = cJSON_GetArrayItem(s_yiji_root, i);
if (!cJSON_IsObject(item)) {
continue;
}
node_year = cJSON_GetObjectItem(item, "lunar_year");
node_month = cJSON_GetObjectItem(item, "lunar_month");
node_day = cJSON_GetObjectItem(item, "lunar_day");
if (!cJSON_IsNumber(node_year) || !cJSON_IsNumber(node_month) || !cJSON_IsNumber(node_day)) {
continue;
}
/*
* 约定:JSON 中 lunar_year/lunar_month/lunar_day 作为“查表日期键”使用,
* 这里按传入公历日期(year/month/day)直接匹配。
* 这样修改 JSON 的 2026-09-17 会直接影响界面显示。
*/
if (node_year->valueint != (int)year ||
node_month->valueint != (int)month ||
node_day->valueint != (int)day) {
continue;
}
yi = cJSON_GetObjectItem(item, "yi");
ji = cJSON_GetObjectItem(item, "ji");
used = 0;
if (cJSON_IsArray(yi)) {
const int yi_n = cJSON_GetArraySize(yi) < 4 ? cJSON_GetArraySize(yi) : 4;
for (int k = 0; k < yi_n; k++) {
cJSON *w = cJSON_GetArrayItem(yi, k);
if (!cJSON_IsString(w) || w->valuestring == NULL) {
continue;
}
word = w->valuestring;
if (used > 0 && used < sizeof(out->dos) - 1) {
out->dos[used++] = ' ';
out->dos[used] = '\0';
}
if (used < sizeof(out->dos) - 1) {
used += (size_t)snprintf(out->dos + used, sizeof(out->dos) - used, "%s", word);
if (used >= sizeof(out->dos)) {
used = sizeof(out->dos) - 1;
}
}
}
}
used = 0;
if (cJSON_IsArray(ji)) {
const int ji_n = cJSON_GetArraySize(ji) < 4 ? cJSON_GetArraySize(ji) : 4;
for (int k = 0; k < ji_n; k++) {
cJSON *w = cJSON_GetArrayItem(ji, k);
if (!cJSON_IsString(w) || w->valuestring == NULL) {
continue;
}
word = w->valuestring;
if (used > 0 && used < sizeof(out->donts) - 1) {
out->donts[used++] = ' ';
out->donts[used] = '\0';
}
if (used < sizeof(out->donts) - 1) {
used += (size_t)snprintf(out->donts + used, sizeof(out->donts) - used, "%s", word);
if (used >= sizeof(out->donts)) {
used = sizeof(out->donts) - 1;
}
}
}
}
break;
}
}
if (out->dos[0] == '\0') {
snprintf(out->dos, sizeof(out->dos), "-- -- -- --");
}
if (out->donts[0] == '\0') {
snprintf(out->donts, sizeof(out->donts), "-- -- -- --");
}
return true;
}
void almanac_service_set_provider(const almanac_provider_t *provider)
{
if (provider == NULL || provider->get == NULL) {
s_provider.get = NULL;
s_provider.ctx = NULL;
return;
}
s_provider = *provider;
}
bool almanac_service_get(uint16_t year, uint8_t month, uint8_t day, almanac_entry_t *out)
{
if (out == NULL) {
return false;
}
if (s_provider.get != NULL && s_provider.get(year, month, day, out, s_provider.ctx)) {
return true;
}
return almanac_builtin_get(year, month, day, out);
}