#include “esp_system.h”
#include “Game_Main.h”
#include “driver/gpio.h”
#include “esptim.h”
#include “esp_random.h”
#include “esp_mesh.h”
#include “…/Fun/videoplay.h”
#include “…/Fun/draw.h”
#include “…/Fun/RGBPanel.h”
#include “…/Fun/LanGame.h”
#include “…/Fun/GameDevice.h”
#include “…/Fun/JC24BDevice.h”
#include “…/Fun/video_control.h”
#include “…/Fun/LanWifiMesh.h”
#include “…/Fun/Button.h”
#include “…/Fun/countdown_timer.h”
#include “…/config.h”
uint8_t GMAIN_GameState;
static uint8_t GMAIN_TestColorIndex;
static TIM_timing_t GMAIN_TimeObj1;
static uint8_t GMAIN_BootClickCount = 0;
static TIM_timing_t GMAIN_BootTimeObj;
static uint8_t GMAIN_testTime = 3;
CountdownTimer game_timer; // 全局倒计时器
static TickType_t last_update = 0; // 全局显示更新时间戳
// void GMAIN_ChangeState(uint8_t state)
// {
// // 状态切换前处理当前状态
// switch (GMAIN_GameState) {
// case GAME_M_TIME:
// // 如果当前是倒计时状态,暂停计时器
// if (timer_get_state(&game_timer) == TIMER_RUNNING) {
// timer_pause(&game_timer);
// }
// break;
// }
// GMAIN_GameState = state; // 更新状态
// switch (GMAIN_GameState)
// {
// case 0:
// video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0);
// break;
// case 1:
// video_control_play(VIDEO_WIN, VIDEO_PLAY_LOOP, esp_mesh_is_root() ? 10 : 12);
// break;
// case 2:
// video_control_play(VIDEO_WIN_ASSIST, VIDEO_PLAY_LOOP, esp_mesh_is_root() ? 10 : 15);
// break;
// case 3:
// video_control_play(VIDEO_WWJ_STATE_COIN_IN, VIDEO_PLAY_SINGLE, 0);
// break;
// case 4:
// video_control_play(VIDEO_WWJ_STATE_COIN_OK, VIDEO_PLAY_LOOP, 0);
// break;
// case 5:
// video_control_play(VIDEO_WWJ_STATE_GAMMING, VIDEO_PLAY_LOOP, 0);
// break;
// case GAME_M_WWJ_STATE_STOP:
// video_control_play(VIDEO_WWJ_STATE_STOP, VIDEO_PLAY_SINGLE, 0);
// break;
// case GAME_M_TIME:
// // 初始化倒计时器(如果未初始化)
// if (timer_get_state(&game_timer) == TIMER_STOPPED) {
// timer_init(&game_timer, 10.0f); // 10秒倒计时
// }
// // 启动倒计时
// timer_start(&game_timer);
// // 初始化显示相关变量
// static TickType_t last_update = 0;
// last_update = xTaskGetTickCount(); // 重置更新时间
// break;
// case GAME_M_SELF_TEST:
// video_stop();
// GMAIN_TestColorIndex = 0;
// TIM_TIMING_SET_1MS(GMAIN_TimeObj1, 0);
// break;
// }
// }
// void GMAIN_Run(void)
// {
// switch (GMAIN_GameState)
// {
// case GAME_M_IDLE:
// break;
// case GAME_M_WIN:
// break;
// case GAME_M_WIN_ASSIST:
// break;
// case GAME_M_WWJ_STATE_COIN_IN:
// case GAME_M_WWJ_STATE_COIN_OK:
// break;
// case GAME_M_WWJ_STATE_GAMMING:
// break;
// case GAME_M_WWJ_STATE_STOP:
// break;
// case GAME_M_TIME: // 倒计时状态处理
// // 获取剩余时间
// int integer_seconds, centiseconds;
// timer_get_remaining_split(&game_timer, &integer_seconds, ¢iseconds);
// TickType_t now = xTaskGetTickCount();
// if (now - last_update >= pdMS_TO_TICKS(10)) {
// RGBPanel_FillScreen(0x0000);
// RGBPanel_ShowValue(10, 0, integer_seconds,
// RGBPanel_IMAGE_WWJ_STATE_RED_NUM0,
// RGBPanel_NO_IMAGE);
// RGBPanel_drawImageById(40, 0, RGBPanel_IMAGE_WWJ_STATE_RED_Point);
// RGBPanel_ShowValue(50, 0, centiseconds,
// RGBPanel_IMAGE_WWJ_STATE_RED_NUM0,
// RGBPanel_NO_IMAGE);
// RGBPanel_Update();
// last_update = now;
// }
// // 检查是否超时
// if (timer_is_expired(&game_timer)) {
// // 倒计时结束处理
// GMAIN_ChangeState(GAME_M_WIN); // 切换到胜利状态
// }
// break;
// case GAME_M_SELF_TEST:
// if (TIM_TIMING_RUN(GMAIN_TimeObj1))
// {
// if (GMAIN_TestColorIndex >= 8)
// {
// JC24BDevice_SendLogoPlay();
// LanWifiMesh_SendLogoPlay();
// GMAIN_ChangeState(GAME_M_IDLE);
// return;
// }
// uint8_t r = 0;
// uint8_t g = 0;
// uint8_t b = 0;
// if ((GMAIN_TestColorIndex & 0x01) > 0)
// {
// r = 0xFF;
// }
// if ((GMAIN_TestColorIndex & 0x02) > 0)
// {
// g = 0xFF;
// }
// if ((GMAIN_TestColorIndex & 0x04) > 0)
// {
// b = 0xFF;
// }
// RGBPanel_FillScreenRGB888(r, g, b);
// RGBPanel_Update();
// GMAIN_TestColorIndex++;
// TIM_TIMING_SET_100MS(GMAIN_TimeObj1, GMAIN_testTime);
// }
// break;
// }
// if (IO_KeyPressed(KEY_BUTTON_TEST))
// {
// GMAIN_ChangeState(GAME_M_WIN);
// JC24BDevice_BroadcastWin();
// LanWifiMesh_BroadcastWin();
// }
// if (TIM_TIMING_RUN(GMAIN_BootTimeObj))
// {
// TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 10000);
// GMAIN_BootClickCount = 0;
// }
// if (IO_KeyPressed(KEY_BUTTON_BOOT) && g_config.meshType == CONFIG_MESH_TYPE_ROOT)
// {
// GMAIN_BootClickCount++;
// if (GMAIN_BootClickCount >= 5)
// {
// config_set_mesh_type(CONFIG_MESH_TYPE_CHILD);
// esp_restart();
// }
// TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 2);
// }
// }
extern int g_wifi_rssi;
static void GMAIN_ShowRootFlag(void);
void GMAIN_FrameCall(uint32_t frameIndex)
{
switch (g_config.matrix_chain)
{
case 2:
{
switch (GMAIN_GameState)
{
case GAME_M_WWJ_STATE_COIN_IN:
RGBPanel_ShowValue(54, 18, g_GameDeviceData.coinInCount, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE);
RGBPanel_ShowValue(92, 18, g_GameDeviceData.coinPlayCount, RGBPanel_IMAGE_WWJ_STATE_YELLOW_NUM0, RGBPanel_NO_IMAGE);
break;
}
}
break;
case 3:
{
switch (GMAIN_GameState)
{
case GAME_M_WWJ_STATE_COIN_IN:
RGBPanel_ShowValue(102, 18, g_GameDeviceData.coinInCount, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE);
RGBPanel_ShowValue(144, 18, g_GameDeviceData.coinPlayCount, RGBPanel_IMAGE_WWJ_STATE_YELLOW_NUM0, RGBPanel_NO_IMAGE);
break;
}
}
break;
case 4:
{
switch (GMAIN_GameState)
{
case GAME_M_WWJ_STATE_COIN_IN:
case GAME_M_WWJ_STATE_COIN_OK:
RGBPanel_ShowValue(168, 18, g_GameDeviceData.coinInCount, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE);
RGBPanel_ShowValue(208, 18, g_GameDeviceData.coinPlayCount, RGBPanel_IMAGE_WWJ_STATE_YELLOW_NUM0, RGBPanel_NO_IMAGE);
break;
}
}
break;
default:
break;
}
if (esp_mesh_is_root()) { GMAIN_ShowRootFlag(); } else if (g_wifi_rssi <= -60 || g_wifi_rssi == 0) { RGBPanel_ShowWifiRssi(g_wifi_rssi); }
}
// 6*6
static const uint8_t GMAIN_mesh_root_flag[] = {
0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF,
0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF,
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
};
#define COLOR_YELLOW 0xFFE0
#define GRADIENT_STEPS (128) // 渐变的步数
static uint16_t gradientProgress = 0;
typedef struct
{
uint8_t colorR;
uint8_t colorG;
uint8_t colorB;
} color_t;
static color_t startColor = { 31, 31, 31};
static color_t endColor = { 31, 0, 0};
static uint8_t colorIndex = 0;
const uint8_t colorIndexBuff[7] = { 0x7, 0x05, 0x01, 0x03, 0x02, 0x06, 0x04};
uint16_t getGradientColor(void)
{
// 计算每个通道的渐变步长
uint16_t r = startColor.colorR + (endColor.colorR - startColor.colorR) * gradientProgress / (GRADIENT_STEPS - 1);
uint16_t g = startColor.colorG + (endColor.colorG - startColor.colorG) * gradientProgress / (GRADIENT_STEPS - 1);
uint16_t b = startColor.colorB + (endColor.colorB - startColor.colorB) * gradientProgress / (GRADIENT_STEPS - 1);
// 增加渐变进度 if (++gradientProgress == (GRADIENT_STEPS - 1)) { gradientProgress = 0; startColor = endColor; colorIndex = (colorIndex + 1) % 7; endColor.colorR = ((colorIndexBuff[colorIndex] & 0x04) > 0 ? 31 : 0); endColor.colorG = ((colorIndexBuff[colorIndex] & 0x02) > 0 ? 31 : 0); endColor.colorB = ((colorIndexBuff[colorIndex] & 0x01) > 0 ? 31 : 0); } uint16_t color = (r << 11) | (g << 5) | b; // 返回RGB565格式的颜色值 return color;
}
static void GMAIN_ShowRootFlag(void)
{
uint16_t color = getGradientColor();
int16_t startX = 58; startX += (g_config.matrix_chain - 1) * 64; for (uint8_t y = 0; y < 6; y++) { for (uint8_t x = 0; x < 6; x++) { RGBPanel_drawPixel(startX + x, y, (GMAIN_mesh_root_flag[x + y * 6] ? color : 0)); } }
}
void GMAIN_VideoEndCall(void)
{
switch (GMAIN_GameState)
{
case GAME_M_IDLE:
break;
case GAME_M_WIN:
case GAME_M_WIN_ASSIST:
case GAME_M_WWJ_STATE_COIN_IN:
case GAME_M_WWJ_STATE_STOP:
GMAIN_count_down(GAME_M_IDLE);
JC24BDevice_SendLogoPlay();
LanWifiMesh_SendLogoPlay();
break;
case GAME_M_SELF_TEST:
break;
}
}
void GMAIN_Init(void)
{
timer_init(&game_timer, 0); // 初始化为0秒
GMAIN_count_down(GAME_M_SELF_TEST);
GMAIN_BootClickCount = 0; TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 10000);
}
void GMAIN_count_down(uint8_t state)
{
// 状态切换前处理当前状态
switch (GMAIN_GameState) {
case GAME_M_TIME:
// 如果当前是倒计时状态,暂停计时器
if (timer_get_state(&game_timer) == TIMER_RUNNING) {
timer_stop(&game_timer);
}
break;
}
GMAIN_GameState = state; // 更新状态 switch (GMAIN_GameState) { case 0: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case 1: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case 2: video_stop(); // 初始化倒计时器(如果未初始化) if (timer_get_state(&game_timer) == TIMER_STOPPED) { timer_init(&game_timer, g_GameDeviceData.settingtime_value); // 10秒倒计时 } // 启动倒计时 timer_start(&game_timer); // 初始化显示相关变量 static TickType_t last_update = 0; last_update = xTaskGetTickCount(); // 重置更新时间 break; case 3: timer_pause(&game_timer); break; case 4: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case 5: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case GAME_M_SELF_TEST: video_stop(); GMAIN_TestColorIndex = 0; TIM_TIMING_SET_1MS(GMAIN_TimeObj1, 0); break; }
}
void GMAIN_count_down_Run(void)
{
switch (GMAIN_GameState)
{
case 0:
break;
case 1:
break;
case 2: // 倒计时状态处理
// 获取剩余时间
int integer_seconds, centiseconds;
timer_get_remaining_split(&game_timer, &integer_seconds, ¢iseconds);
TickType_t now = xTaskGetTickCount();
if (now - last_update >= pdMS_TO_TICKS(10)) {
RGBPanel_FillScreen(0x0000);
RGBPanel_ShowValue(0, 0, integer_seconds,
RGBPanel_IMAGE_WWJ_STATE_RED_NUM0,
RGBPanel_NO_IMAGE);
RGBPanel_drawImageById(100, 0, RGBPanel_IMAGE_WWJ_STATE_RED_Point);
RGBPanel_ShowValue(150, 0, centiseconds,
RGBPanel_IMAGE_WWJ_STATE_RED_NUM0,
RGBPanel_NO_IMAGE);
RGBPanel_Update();
last_update = now;
}
// 检查是否超时 if (timer_is_expired(&game_timer)) { // 倒计时结束处理 GMAIN_count_down(GAME_M_WIN); // 切换到胜利状态 } break; case 3: break; case 4: break; case 5: break; case GAME_M_SELF_TEST: if (TIM_TIMING_RUN(GMAIN_TimeObj1)) { if (GMAIN_TestColorIndex >= 8) { JC24BDevice_SendLogoPlay(); LanWifiMesh_SendLogoPlay(); GMAIN_count_down(GAME_M_IDLE); return; } uint8_t r = 0; uint8_t g = 0; uint8_t b = 0; if ((GMAIN_TestColorIndex & 0x01) > 0) { r = 0xFF; } if ((GMAIN_TestColorIndex & 0x02) > 0) { g = 0xFF; } if ((GMAIN_TestColorIndex & 0x04) > 0) { b = 0xFF; } RGBPanel_FillScreenRGB888(r, g, b); RGBPanel_Update(); GMAIN_TestColorIndex++; TIM_TIMING_SET_100MS(GMAIN_TimeObj1, GMAIN_testTime); } break; } // if (IO_KeyPressed(KEY_BUTTON_TEST)) // { // GMAIN_count_down(GAME_M_WIN); // JC24BDevice_BroadcastWin(); // LanWifiMesh_BroadcastWin(); // } if (TIM_TIMING_RUN(GMAIN_BootTimeObj)) { TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 10000); GMAIN_BootClickCount = 0; } if (IO_KeyPressed(KEY_BUTTON_BOOT) && g_config.meshType == CONFIG_MESH_TYPE_ROOT) { GMAIN_BootClickCount++; if (GMAIN_BootClickCount >= 5) { config_set_mesh_type(CONFIG_MESH_TYPE_CHILD); esp_restart(); } TIM_TIMING_SET_1S(GMAIN_BootTimeObj, 2); }
}
我想让显示自适应位数变化,小数点前三位数后两位数,以固定位置显示在屏幕中,缺少的自动补零,现在代码的逻辑是会自动靠拢缺少的位数空间。单个数字宽度为45