MultiTimer icon indicating copy to clipboard operation
MultiTimer copied to clipboard

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉

Open askhua520 opened this issue 3 years ago • 4 comments

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉 增加排序功能很好,但是去掉了重复执行功能啊,单片机又很多都是要重复执行的定时器 增加可重复执行功能 struct MultiTimerHandle { MultiTimer* next; uint64_t deadline; MultiTimerCallback_t callback; void* userData; uint64_t interval; //记录间隔 uint8_t repeat; //重复执行标记 }; //根据repeat标志重新启动定时器 if (entry->repeat) { MultiTimerStart(entry, entry->interval, entry->callback, entry->userData, entry->repeat) }

askhua520 avatar Jan 13 '22 03:01 askhua520

我倒觉得这样才能最大限度的灵活控制。  


 QQ:  529864495  手机:  15036977063  备用QQ:   251107197  备用手机:  15038566482  说明:提供单片机软硬件开发技术指导,代做软硬件设计,调试勘误等业务,低价有偿服务。


 

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2022年1月13日(星期四) 中午11:44 收件人: @.>; 抄送: @.***>; 主题: [0x1abin/MultiTimer] 为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉 (Issue #10)

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉 增加排序功能很好,但是去掉了重复执行功能啊,单片机又很多都是要重复执行的定时器 增加可重复执行功能 struct MultiTimerHandle { MultiTimer* next; uint64_t deadline; MultiTimerCallback_t callback; void* userData; uint64_t interval; //记录间隔 uint8_t repeat; //重复执行标记 }; //根据repeat标志重新启动定时器 if (entry->repeat) { MultiTimerStart(entry, entry->interval, entry->callback, entry->userData, entry->repeat) }

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you are subscribed to this thread.Message ID: @.***>

cancundiudiu avatar Jan 13 '22 03:01 cancundiudiu

我一般这样用 #ifndef TIMER_H #define TIMER_H

#include "NewType.h"

#define tType uint16 enum TimerOut { TIMERRST = 0, TIMEROUT = 1 }; typedef struct TIMER { uint8 TimeOut; tType Value; } Timer, *pTimer;

extern void TimerStart(pTimer t, tType time); extern void TimerStop(pTimer t); extern void TimerRun(pTimer t); extern uint8 TimerOut(pTimer t);

#endif

#include "Timer.h"

//启动已经停止的定时器,如果定时器已经启动或时间到则忽略. void TimerStart(pTimer t, tType time) { if ((t->Value == 0) && (t->TimeOut == TIMERRST)) { t->Value = time; } } //重新设置定时时间并启动定时器,不管时间是否到或定时器是否启动 void TimerSet(pTimer t, tType time) { t->Value = time; t->TimeOut = TIMERRST; } //停止定时器,清除时间到标志 void TimerStop(pTimer t) { t->Value = 0; t->TimeOut = TIMERRST; } //在1ms定时器内递减,时间到时设置标志为1 void TimerRun(pTimer t) { if (t->Value > 0) { t->Value--; if (t->Value == 0) { t->TimeOut = TIMEROUT; } } } //定时器时间到,并清除时间到标志为0 uint8 TimerOut(pTimer t) { uint8 result = t->TimeOut; if (t->TimeOut == TIMEROUT) { t->TimeOut = TIMERRST; } return result; }

//使用 void Tick1ms(void){ TimerRun(&tm1); TimerRun(&tm2); TimerRun(&tm3); }

while(1){ if ( setp == 1){ if (...){ TimerStart(&tm1, 1000); } else { TimerStop(&tm1); } } ...... if (TimerOut(&tm1)) { ...... TimerStart(&tm2, 1000); } if (TimerOut(&tm2)) { ...... TimerStart(&tm3, 1000); } }

askhua520 avatar Jan 14 '22 00:01 askhua520

这个必须在回调里重启的设计对回调用匿名函数很不友好。。。

kaidegit avatar Jun 04 '24 00:06 kaidegit

可以尝试在启动定时器函数添加个重复次数的参数, 确实在回调里去重新运行启动函数很不友好,反复创建,而且也不好向多次运行的回调函数里通过传参方式去传递数据

1yangsir avatar Jul 18 '24 06:07 1yangsir