Timer & play video

本文介绍了一种利用Android中的Timer来限制视频播放时长的方法。通过设置定时任务,在指定时间到达后停止视频播放,同时提供了视频未播放完时重新开始播放的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近在给工厂写test program,写的几个都与Timer有关,下面就将设置定时功能的方法分享给大家

 

eg 1: 播放一段时间的video

 

public class Test extends Activity{

        private String path = "/sdcard/ok.mp4";                  //设置video文件的路径
        private VideoView mVideoView;

        private Timer mTimer=null;
        private TimerTask mTimerTask=null;
        private static final int TIMER_LIMIT=50*1000;       //设定播放时间50s

 

       private Handler mHandler=new Handler(){            //时间到的响应
            public void handleMessage(Message msg){
                mVideoView.stopPlayback();
            }
        };

 

       @Override

       public void onCreate (Bundle saveInstanceState){

             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);

             mVideoView = (VideoView) findViewById(R.id.videoview);
             
            mVideoView.setVideoPath(path);
            mVideoView.start();       //开始播放
            startTimer();                  //开始计时
    
            mVideoView.setOnCompletionListener(new OnCompletionListener() {    //设置让video在时间未到时重播

               @Override
                public void onCompletion(MediaPlayer mp) {
                    mVideoView.start();
                } 
            });

    }

 

           private void startTimer() {
               mTimer=new Timer();
                if (mTimerTask == null){
                   mTimerTask=new TimerTask(){
                        public void run(){
                              mTimer.cancel();
                              mHandler.sendMessage(mHandler.obtainMessage());   //时间到时发出message
                              mTimer=null;
                              mTimerTask=null;
                      }
                };
                mTimer.schedule(mTimerTask, TIMER_LIMIT);
            }
    }

}

 

有点跳跃,不知道大家能否看懂,不懂留言问吧

另一个例子在我的下一篇文章中

void GMAIN_count_down(uint8_t state) { // 状态切换前处理当前状态 switch (GMAIN_GameState) { case 2: // 如果当前是倒计时状态,暂停计时器 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_COUNTDOWN_READY, 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); } // 启动倒计时 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_WIN , VIDEO_PLAY_LOOP, 0); break; case 5: video_control_play(VIDEO_LOSE , 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_elapsed_split(&game_timer, &integer_seconds, &centiseconds); TickType_t now = xTaskGetTickCount(); if (now - last_update >= pdMS_TO_TICKS(10)) { RGBPanel_FillScreen(0x0000); char int_str[4]; char frac_str[3]; format_fixed_digits(integer_seconds, 3, int_str); format_fixed_digits(centiseconds, 2, frac_str); int current_x = START_X; int current_y = 5; for (int i = 0; i < 3; i++) { uint8_t digit = int_str[i] - '0'; RGBPanel_drawImageById(current_x, current_y, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0 + digit); current_x += DIGIT_WIDTH; } RGBPanel_drawImageById(current_x, current_y+13, RGBPanel_IMAGE_WWJ_STATE_RED_Point); current_x += POINT_WIDTH; for (int i = 0; i < 2; i++) { uint8_t digit = frac_str[i] - '0'; RGBPanel_drawImageById(current_x, current_y, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0 + digit); current_x += DIGIT_WIDTH; } } last_update = now; // 超时判断 if (timer_is_expired(&game_timer)) { timer_pause(&game_timer); break; } RGBPanel_Update(); 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; }为什么他数到10的时候没有办法停留在10这个数,而是重置
最新发布
07-10
void GMAIN_ChangeState(uint8_t state) { GMAIN_GameState = 7; switch (GMAIN_GameState) { case GAME_M_IDLE: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case GAME_M_WIN: video_control_play(VIDEO_WIN, VIDEO_PLAY_LOOP, esp_mesh_is_root() ? 10 : 12); break; case GAME_M_WIN_ASSIST: video_control_play(VIDEO_WIN_ASSIST, VIDEO_PLAY_LOOP, esp_mesh_is_root() ? 10 : 15); break; case GAME_M_WWJ_STATE_COIN_IN: video_control_play(VIDEO_WWJ_STATE_COIN_IN, VIDEO_PLAY_SINGLE, 0); break; case GAME_M_WWJ_STATE_COIN_OK: video_control_play(VIDEO_WWJ_STATE_COIN_OK, VIDEO_PLAY_LOOP, 0); break; case GAME_M_WWJ_STATE_GAMMING: 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: static TickType_t last_update = 0; const TickType_t update_interval = pdMS_TO_TICKS(200); // 200ms更新间隔 // 状态进入初始化(只执行一次) if(Time_flag == 0) { // 安全初始化 esp_err_t ret = countdown_timer_init(10.0f); // 显示初始值 RGBPanel_FillScreen(0x0000); RGBPanel_ShowValue(0, 0, 10, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0, RGBPanel_NO_IMAGE); RGBPanel_Update(); countdown_timer_start(); Time_flag = 1; last_update = xTaskGetTickCount(); } // 状态持续处理 else { int integer, decimal; printf("1=%d", Time_flag); // 获取剩余时间(带错误检查) if(!countdown_timer_get_remaining(&integer, &decimal)) { Time_flag = 0; countdown_timer_deinit(); printf("2=%d", Time_flag); break; } TickType_t now = xTaskGetTickCount(); if(now - last_update >= update_interval) { printf("3=%d", Time_flag); RGBPanel_FillScreen(0x0000); RGBPanel_ShowValue(0, 0, integer, RGBPanel_IMAGE_WWJ_STATE_WHITE_NUM0, RGBPanel_NO_IMAGE); RGBPanel_Update(); last_update = now; } } break; case GAME_M_SELF_TEST: video_stop(); GMAIN_TestColorIndex = 0; TIM_TIMING_SET_1MS(GMAIN_TimeObj1, 0); break; } }差不多像这样,在状态值为7的时候进入一个显示倒计时任务,该倒计时可被随时暂停
07-08
#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; // 全局显示更新时间戳 // 显示区域定义 #define DISPLAY_WIDTH 256 // 屏幕宽度 #define DIGIT_WIDTH 45 // 单数字宽度 #define POINT_WIDTH 22 // 小数点宽度 // 总宽度计算:3整数位 + 小数点 + 2小数位 #define TOTAL_WIDTH (3DIGIT_WIDTH + POINT_WIDTH + 2DIGIT_WIDTH) #define START_X ((DISPLAY_WIDTH - TOTAL_WIDTH) / 2) // 居中显示 // 格式化数字为固定位数字符串(自动补零) void format_fixed_digits(int value, int digits, char* output) { for (int i = digits - 1; i >= 0; i–) { output[i] = (value % 10) + ‘0’; // 取最后一位 value /= 10; } output[digits] = ‘\0’; // 字符串终止符 } extern int g_wifi_rssi; static void GMAIN_ShowRootFlag(void); void GMAIN_FrameCall(uint32_t frameIndex) { 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_READY: case GAME_M_TIME: case GAME_M_PAUSE: case GAME_M_WIN: case GAME_M_LOSE: 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 GAME_M_IDLE: video_control_play(VIDEO_LOGO, VIDEO_PLAY_LOOP, 0); break; case GAME_M_READY: video_control_play(VIDEO_COUNTDOWN_READY, VIDEO_PLAY_LOOP, 0); break; case GAME_M_TIME: video_stop(); // 初始化倒计时器(如果未初始化) if (timer_get_state(&game_timer) == TIMER_STOPPED) { timer_init(&game_timer, g_GameDeviceData.settingtime_value); } // 启动倒计时 timer_start(&game_timer); // 初始化显示相关变量 static TickType_t last_update = 0; last_update = xTaskGetTickCount(); // 重置更新时间 break; case GAME_M_PAUSE: timer_pause(&game_timer); break; case GAME_M_WIN: video_control_play(VIDEO_WIN , VIDEO_PLAY_LOOP, 0); break; case GAME_M_LOSE: video_control_play(VIDEO_LOSE , 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 GAME_M_IDLE: break; case GAME_M_READY: break; case GAME_M_TIME: // 正计时状态处理 // 获取已耗时 int integer_seconds, centiseconds; timer_get_elapsed_split(&game_timer, &integer_seconds, &centiseconds); TickType_t now = xTaskGetTickCount(); if (now - last_update >= pdMS_TO_TICKS(10)) { RGBPanel_FillScreen(0x0000); char int_str[4]; char frac_str[3]; format_fixed_digits(integer_seconds, 3, int_str); format_fixed_digits(centiseconds, 2, frac_str); int current_x = START_X; int current_y = 5; for (int i = 0; i < 3; i++) { uint8_t digit = int_str[i] - ‘0’; RGBPanel_drawImageById(current_x, current_y, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0 + digit); current_x += DIGIT_WIDTH; } RGBPanel_drawImageById(current_x, current_y+13, RGBPanel_IMAGE_WWJ_STATE_RED_Point); current_x += POINT_WIDTH; for (int i = 0; i < 2; i++) { uint8_t digit = frac_str[i] - ‘0’; RGBPanel_drawImageById(current_x, current_y, RGBPanel_IMAGE_WWJ_STATE_RED_NUM0 + digit); current_x += DIGIT_WIDTH; } } last_update = now; RGBPanel_Update(); break; case GAME_M_PAUSE: break; case GAME_M_WIN: break; case GAME_M_LOSE: 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); } } #include “countdown_timer.h” #include “esp_log.h” #include “esp_timer.h” // 全局校准后的tick周期(单位:毫秒) float calibrated_tick_period_ms = 1.0f; // 默认值 /** @brief 校准系统tick周期 @note 应在系统初始化时调用 */ void timer_calibrate(void) { TickType_t start_ticks = xTaskGetTickCount(); uint64_t start_us = esp_timer_get_time(); vTaskDelay(pdMS_TO_TICKS(1000)); // 精确延迟1秒 uint64_t real_duration_us = esp_timer_get_time() - start_us; TickType_t elapsed_ticks = xTaskGetTickCount() - start_ticks; calibrated_tick_period_ms = (real_duration_us / 1000.0f) / elapsed_ticks; ESP_LOGI(“TIMER_CAL”, “Calibrated tick period: %.6f ms”, (double)calibrated_tick_period_ms); } // 初始化计时器 void timer_init(CountdownTimer *timer, float seconds) { timer->total_ms = (uint64_t)(seconds * 1000); // 秒转毫秒 timer->elapsed_ms = 0; timer->state = TIMER_STOPPED; ESP_LOGI(“TIMER”, “Initialized: %.1f seconds (%llu ms)”, seconds, timer->total_ms); } // 启动或恢复计时 void timer_start(CountdownTimer *timer) { if (timer->state == TIMER_STOPPED) { timer->elapsed_ms = 0; // 重置已耗时 timer->start_tick = xTaskGetTickCount(); timer->state = TIMER_RUNNING; ESP_LOGI(“TIMER”, “Started: %llu ms target”, timer->total_ms); } else if (timer->state == TIMER_PAUSED) { // 恢复时补偿暂停时间 TickType_t now = xTaskGetTickCount(); uint64_t paused_duration = (uint64_t)((now - timer->pause_tick) * calibrated_tick_period_ms); timer->start_tick += pdMS_TO_TICKS(paused_duration); timer->state = TIMER_RUNNING; ESP_LOGI(“TIMER”, “Resumed: %llu ms elapsed”, timer->elapsed_ms); } } // 暂停计时 void timer_pause(CountdownTimer *timer) { if (timer->state == TIMER_RUNNING) { timer->state = TIMER_PAUSED; timer->pause_tick = xTaskGetTickCount(); // 更新已耗时 TickType_t now = xTaskGetTickCount(); timer->elapsed_ms = (uint64_t)((now - timer->start_tick) * calibrated_tick_period_ms); ESP_LOGI(“TIMER”, “Paused: %llu ms elapsed”, timer->elapsed_ms); } } // 停止并重置计时 void timer_stop(CountdownTimer *timer) { timer->state = TIMER_STOPPED; timer->elapsed_ms = 0; ESP_LOGI(“TIMER”, “Stopped and reset”); } // 获取已耗时(毫秒) uint64_t timer_get_elapsed_ms(CountdownTimer *timer) { if (timer->state == TIMER_RUNNING) { TickType_t now = xTaskGetTickCount(); return (uint64_t)((now - timer->start_tick) * calibrated_tick_period_ms); } return timer->elapsed_ms; } // 获取已耗时分段显示(秒和百分秒) void timer_get_elapsed_split(CountdownTimer *timer, int *seconds, int *centiseconds) { uint64_t elapsed_ms = timer_get_elapsed_ms(timer); // 限制不超过设定值 if (elapsed_ms > timer->total_ms) { elapsed_ms = timer->total_ms; } // 计算总秒数 *seconds = (int)(elapsed_ms / 1000); // 计算百分秒(四舍五入) uint64_t remainder_ms = elapsed_ms % 1000; *centiseconds = (int)((remainder_ms + 5) / 10); // +5实现四舍五入 // 处理进位 if (*centiseconds >= 100) { *seconds += 1; *centiseconds = 0; } // 调试输出 ESP_LOGD(“TIMER_SPLIT”, “Elapsed: %llu ms → %d.%02d s”, elapsed_ms, *seconds, *centiseconds); } // 检查是否超时 bool timer_is_expired(CountdownTimer *timer) { if (timer->state != TIMER_RUNNING) return false; uint64_t elapsed_ms = timer_get_elapsed_ms(timer); return (elapsed_ms > timer->total_ms); } // 获取当前状态 TimerState timer_get_state(CountdownTimer *timer) { return timer->state; } 我需要一个功能,暂停可以暂停在我设定的值,暂停时屏幕显示的是这个值,设定值与原先的计时开始设定值获取方式是一样的
07-10
#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, &centiseconds); // 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, &centiseconds); 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
07-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值