定时器类的封装

/**
 * 在Scene里添加  this.addChild(new TimeCount())
 * 使用美术字的倒计时   需要 fnt文件预加载
 * 美术字的名字注意修改
 * 使用的时候通过
 * cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_SET_TIME, 5) 设置倒计时时间
 * cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_START) 使得倒计时开启
 * cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_STOP)  使得倒计时暂停
 *
 * 在需要得到倒计时结束的类里去
 * cc.eventManager.addCustomListener(USER_TIME_COUNT_FINISH, function(event){
        cc.log('app 获取了定时器传回的结束')
    })
 */
var USER_TIME_COUNT_SET_TIME = "USER_TIME_COUNT_SET_TIME"   //设置倒计时时间
var USER_TIME_COUNT_START = "USER_TIME_COUNT_START"         //开始倒计时
var USER_TIME_COUNT_PAUSE = "USER_TIME_COUNT_PAUSE"           //停止倒计时
var USER_TIME_COUNT_FINISH = "USER_TIME_COUNT_FINISH"       //倒计时结束     由定时器抛出
var TimeCount = cc.Layer.extend({
    EventArr:[USER_TIME_COUNT_SET_TIME, USER_TIME_COUNT_START, USER_TIME_COUNT_PAUSE],
    Label_time:null,
    Label_Position:null,
    Count_Time:0,
    ScheduleRun:false,
    Start_DateTime:0,
    ctor:function(){
        this._super()
        this.init()
    },
    init:function(){
        this.Label_Position = cc.p(cc.winSize.width>>1,cc.winSize.height*0.95);       //调整倒计时坐标
        this.Label_time = new cc.LabelBMFont("TimeCount", res.bmFont);
       /* if(this.Label_time.width <= 0) cc.error('字体文件没有预加载 或 TimeCount模块没有设置正确的BmFont')*/
        this.Label_time.setPosition(this.Label_Position)
        this.addChild(this.Label_time)
        this.scheduleUpdate()

        for(var index in this.EventArr){
            var event = this.EventArr[index]
            cc.eventManager.addCustomListener(event, this.onGetCustom.bind(this))
        }
    },
    onGetCustom:function(event){
        var data = event.getUserData();
        switch (event.getEventName()){
            case USER_TIME_COUNT_SET_TIME:
                cc.log('设置了倒计时时间:',data)
                this.Count_Time = data
                this.Label_time.setString(this.Count_Time)
                break
            case USER_TIME_COUNT_START:
                cc.log('启动了倒计时:')
                this.Start_DateTime = Date.now()
                this.ScheduleRun = true
                break
            case USER_TIME_COUNT_PAUSE:
                cc.log('停止了倒计时')
                this.ScheduleRun = false
                break
        }
    },
    update:function(){
        if(this.ScheduleRun){
            if(Date.now() - this.Start_DateTime>=1000){
                this.Count_Time --;
                this.Label_time.setString(this.Count_Time)
                this.Start_DateTime += 1000
                if(0 == this.Count_Time){
                    this.timeStop();
                    cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_FINISH)
                }
            }
        }
    },
    timeStop:function(){
        cc.log('倒计时结束了:')
        this.ScheduleRun = false
    },
    onExit:function(){
        this.unscheduleUpdate()
        this.removeAllChildren(true)
        for(var index in this.EventArr){
            var event = this.EventArr[index]
            cc.eventManager.removeCustomListeners(event)
        }
    }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 STM32 中,我们可以使用定时器来产生一定的时间间隔,从而实现一些定时的功能。软件定时器则是在程序中通过编写代码来模拟一个计时器,从而达到定时的效果。下面是一个简单的软件定时器封装示例: ```c #include "stm32f4xx.h" #define TIMER_NUM 10 // 定时器最大数量 typedef struct { uint32_t count; // 定时器计数值 uint32_t timeout; // 定时器超时值 uint8_t used; // 定时器是否在使用 } timer_t; static timer_t timers[TIMER_NUM]; void TIM2_IRQHandler(void) { for (int i = 0; i < TIMER_NUM; i++) { if (timers[i].used) { timers[i].count++; if (timers[i].count >= timers[i].timeout) { timers[i].count = 0; timers[i].used = 0; } } } TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } void timer_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitStruct.TIM_Prescaler = 84 - 1; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1; TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct); NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_Cmd(TIM2, ENABLE); } int timer_create(uint32_t timeout) { for (int i = 0; i < TIMER_NUM; i++) { if (!timers[i].used) { timers[i].count = 0; timers[i].timeout = timeout; timers[i].used = 1; return i; } } return -1; } void timer_delete(int id) { if (id >= 0 && id < TIMER_NUM) { timers[id].used = 0; } } int timer_is_timeout(int id) { if (id >= 0 && id < TIMER_NUM) { return timers[id].count == 0 && timers[id].used; } return 0; } ``` 在上面的代码中,我们定义了一个 `timer_t` 结构体来表示一个定时器,其中包括了计数值、超时值和是否在使用等信息。我们还定义了一个 `TIM2_IRQHandler` 中断处理函数,用于更新定时器的计数值,并判断是否超时。最后,我们实现了三个函数来创建定时器、删除定时器和判断定时器是否超时。在程序中,我们可以使用这些函数来实现我们需要的定时功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值