1. 完善代码名称
This commit is contained in:
parent
5b1067f8d1
commit
85a7b62c89
@ -58,13 +58,13 @@ typedef struct HTimerInfo {
|
|||||||
} HTimerInfo;
|
} HTimerInfo;
|
||||||
|
|
||||||
///< 定时器注册信息
|
///< 定时器注册信息
|
||||||
typedef struct TimeRegisterInfo {
|
typedef struct HTimeRegisterInfo {
|
||||||
HTimerInfo *timers; ///< 定时器信息数组
|
HTimerInfo *timers; ///< 定时器信息数组
|
||||||
HTimerLen_t heapSize; ///< 堆当前大小
|
HTimerLen_t heapSize; ///< 堆当前大小
|
||||||
HTimerLen_t len; ///< 定时器个数
|
HTimerLen_t len; ///< 定时器个数
|
||||||
volatile uint8_t schedu; ///< 需要重新调度 (原子操作)
|
volatile uint8_t schedu; ///< 需要重新调度 (原子操作)
|
||||||
uint8_t run : 1; ///< 运行中
|
uint8_t run : 1; ///< 运行中
|
||||||
} TimeRegisterInfo;
|
} HTimeRegisterInfo;
|
||||||
|
|
||||||
///< 初始化毫秒定时器, 需要传递获取毫秒的函数
|
///< 初始化毫秒定时器, 需要传递获取毫秒的函数
|
||||||
void HTimerInitMs(uint32_t (*func)(void));
|
void HTimerInitMs(uint32_t (*func)(void));
|
||||||
@ -77,7 +77,7 @@ uint32_t HTimerGetMs();
|
|||||||
* @param info 定时器注册信息数组
|
* @param info 定时器注册信息数组
|
||||||
* @param len 定时器个数
|
* @param len 定时器个数
|
||||||
*/
|
*/
|
||||||
void HTimerInitRegister(TimeRegisterInfo *info, HTimerLen_t len);
|
void HTimerInitRegister(HTimeRegisterInfo *info, HTimerLen_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 注册定时器信息
|
* @brief 注册定时器信息
|
||||||
|
|||||||
58
src/HTimer.c
58
src/HTimer.c
@ -77,7 +77,7 @@ static uint32_t (*GetCurrentMs)(void);
|
|||||||
#define HEAP_INVALID_INDEX (HTIMER_LEN_MAX)
|
#define HEAP_INVALID_INDEX (HTIMER_LEN_MAX)
|
||||||
|
|
||||||
struct __attribute__((packed)) TimerInfo {
|
struct __attribute__((packed)) TimerInfo {
|
||||||
TimeRegisterInfo *info;
|
HTimeRegisterInfo *info;
|
||||||
uint8_t infoLen;
|
uint8_t infoLen;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -90,12 +90,12 @@ static struct TimerInfo sInfo;
|
|||||||
#define HEAP_AT(reg, heapIdx) ((reg)->timers[heapIdx].heapMem)
|
#define HEAP_AT(reg, heapIdx) ((reg)->timers[heapIdx].heapMem)
|
||||||
#define HEAP_SET(reg, heapIdx, timerIdx) ((reg)->timers[heapIdx].heapMem = (timerIdx))
|
#define HEAP_SET(reg, heapIdx, timerIdx) ((reg)->timers[heapIdx].heapMem = (timerIdx))
|
||||||
|
|
||||||
static inline HTimerInfo* getTimer(TimeRegisterInfo *reg, int heapIdx)
|
static inline HTimerInfo* GetTimer(HTimeRegisterInfo *reg, int heapIdx)
|
||||||
{
|
{
|
||||||
return ®->timers[HEAP_AT(reg, heapIdx)];
|
return ®->timers[HEAP_AT(reg, heapIdx)];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heapSwap(TimeRegisterInfo *reg, int a, int b)
|
static void HeapSwap(HTimeRegisterInfo *reg, int a, int b)
|
||||||
{
|
{
|
||||||
HTimerLen_t ta = HEAP_AT(reg, a);
|
HTimerLen_t ta = HEAP_AT(reg, a);
|
||||||
HTimerLen_t tb = HEAP_AT(reg, b);
|
HTimerLen_t tb = HEAP_AT(reg, b);
|
||||||
@ -107,13 +107,13 @@ static void heapSwap(TimeRegisterInfo *reg, int a, int b)
|
|||||||
reg->timers[tb].heapIndex = a;
|
reg->timers[tb].heapIndex = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heapUp(TimeRegisterInfo *reg, int i)
|
static void HeapUp(HTimeRegisterInfo *reg, int i)
|
||||||
{
|
{
|
||||||
while (i > 0) {
|
while (i > 0) {
|
||||||
int p = (i - 1) >> 1;
|
int p = (i - 1) >> 1;
|
||||||
|
|
||||||
HTimerInfo *ti = getTimer(reg, i);
|
HTimerInfo *ti = GetTimer(reg, i);
|
||||||
HTimerInfo *tp = getTimer(reg, p);
|
HTimerInfo *tp = GetTimer(reg, p);
|
||||||
|
|
||||||
uint32_t iTime = ti->lastTime + ti->duration;
|
uint32_t iTime = ti->lastTime + ti->duration;
|
||||||
uint32_t pTime = tp->lastTime + tp->duration;
|
uint32_t pTime = tp->lastTime + tp->duration;
|
||||||
@ -127,12 +127,12 @@ static void heapUp(TimeRegisterInfo *reg, int i)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
heapSwap(reg, i, p);
|
HeapSwap(reg, i, p);
|
||||||
i = p;
|
i = p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heapDown(TimeRegisterInfo *reg, int i)
|
static void HeapDown(HTimeRegisterInfo *reg, int i)
|
||||||
{
|
{
|
||||||
int size = reg->heapSize;
|
int size = reg->heapSize;
|
||||||
|
|
||||||
@ -142,8 +142,8 @@ static void heapDown(TimeRegisterInfo *reg, int i)
|
|||||||
int s = i;
|
int s = i;
|
||||||
|
|
||||||
if (l < size) {
|
if (l < size) {
|
||||||
HTimerInfo *tl = getTimer(reg, l);
|
HTimerInfo *tl = GetTimer(reg, l);
|
||||||
HTimerInfo *ts = getTimer(reg, s);
|
HTimerInfo *ts = GetTimer(reg, s);
|
||||||
int32_t diff = (int32_t)((tl->lastTime + tl->duration) - (ts->lastTime + ts->duration));
|
int32_t diff = (int32_t)((tl->lastTime + tl->duration) - (ts->lastTime + ts->duration));
|
||||||
if (diff < 0 || (diff == 0 && HEAP_AT(reg, l) < HEAP_AT(reg, s))) {
|
if (diff < 0 || (diff == 0 && HEAP_AT(reg, l) < HEAP_AT(reg, s))) {
|
||||||
s = l;
|
s = l;
|
||||||
@ -151,8 +151,8 @@ static void heapDown(TimeRegisterInfo *reg, int i)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (r < size) {
|
if (r < size) {
|
||||||
HTimerInfo *tr = getTimer(reg, r);
|
HTimerInfo *tr = GetTimer(reg, r);
|
||||||
HTimerInfo *ts = getTimer(reg, s);
|
HTimerInfo *ts = GetTimer(reg, s);
|
||||||
int32_t diff = (int32_t)((tr->lastTime + tr->duration) - (ts->lastTime + ts->duration));
|
int32_t diff = (int32_t)((tr->lastTime + tr->duration) - (ts->lastTime + ts->duration));
|
||||||
if (diff < 0 || (diff == 0 && HEAP_AT(reg, r) < HEAP_AT(reg, s))) {
|
if (diff < 0 || (diff == 0 && HEAP_AT(reg, r) < HEAP_AT(reg, s))) {
|
||||||
s = r;
|
s = r;
|
||||||
@ -163,12 +163,12 @@ static void heapDown(TimeRegisterInfo *reg, int i)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
heapSwap(reg, i, s);
|
HeapSwap(reg, i, s);
|
||||||
i = s;
|
i = s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void heapPush(TimeRegisterInfo *reg, HTimerLen_t timerIdx)
|
static void HeapPush(HTimeRegisterInfo *reg, HTimerLen_t timerIdx)
|
||||||
{
|
{
|
||||||
if (reg->heapSize >= reg->len) {
|
if (reg->heapSize >= reg->len) {
|
||||||
return;
|
return;
|
||||||
@ -177,10 +177,10 @@ static void heapPush(TimeRegisterInfo *reg, HTimerLen_t timerIdx)
|
|||||||
HTimerLen_t i = reg->heapSize++;
|
HTimerLen_t i = reg->heapSize++;
|
||||||
HEAP_SET(reg, i, timerIdx);
|
HEAP_SET(reg, i, timerIdx);
|
||||||
reg->timers[timerIdx].heapIndex = i;
|
reg->timers[timerIdx].heapIndex = i;
|
||||||
heapUp(reg, i);
|
HeapUp(reg, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
static HTimerLen_t heapPop(TimeRegisterInfo *reg)
|
static HTimerLen_t HeapPop(HTimeRegisterInfo *reg)
|
||||||
{
|
{
|
||||||
if (reg->heapSize == 0) {
|
if (reg->heapSize == 0) {
|
||||||
return HEAP_INVALID_INDEX;
|
return HEAP_INVALID_INDEX;
|
||||||
@ -192,7 +192,7 @@ static HTimerLen_t heapPop(TimeRegisterInfo *reg)
|
|||||||
if (--reg->heapSize > 0) {
|
if (--reg->heapSize > 0) {
|
||||||
HEAP_SET(reg, 0, HEAP_AT(reg, reg->heapSize));
|
HEAP_SET(reg, 0, HEAP_AT(reg, reg->heapSize));
|
||||||
reg->timers[HEAP_AT(reg, 0)].heapIndex = 0;
|
reg->timers[HEAP_AT(reg, 0)].heapIndex = 0;
|
||||||
heapDown(reg, 0);
|
HeapDown(reg, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -217,7 +217,7 @@ static HTimer_t AddTimerData(uint8_t id, uint32_t duration, HTimerCallType call,
|
|||||||
return HTIMER_INVALID;
|
return HTIMER_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeRegisterInfo *reg = &sInfo.info[id];
|
HTimeRegisterInfo *reg = &sInfo.info[id];
|
||||||
uint32_t now = GetCurrentMs();
|
uint32_t now = GetCurrentMs();
|
||||||
|
|
||||||
// 原子地查找并占用空闲槽位
|
// 原子地查找并占用空闲槽位
|
||||||
@ -294,7 +294,7 @@ uint32_t HTimerGetMs()
|
|||||||
return GetCurrentMs();
|
return GetCurrentMs();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTimerInitRegister(TimeRegisterInfo *info, HTimerLen_t len)
|
void HTimerInitRegister(HTimeRegisterInfo *info, HTimerLen_t len)
|
||||||
{
|
{
|
||||||
memset(info, 0, sizeof(*info) * len);
|
memset(info, 0, sizeof(*info) * len);
|
||||||
sInfo.info = info;
|
sInfo.info = info;
|
||||||
@ -315,7 +315,7 @@ uint8_t HTimerRegisterTimerInfo(uint8_t id, HTimerInfo *info, uint16_t infoLen)
|
|||||||
|
|
||||||
memset(info, 0, sizeof(*info) * infoLen);
|
memset(info, 0, sizeof(*info) * infoLen);
|
||||||
|
|
||||||
TimeRegisterInfo *reg = &sInfo.info[id];
|
HTimeRegisterInfo *reg = &sInfo.info[id];
|
||||||
reg->timers = info;
|
reg->timers = info;
|
||||||
reg->len = (HTimerLen_t)infoLen;
|
reg->len = (HTimerLen_t)infoLen;
|
||||||
reg->heapSize = 0;
|
reg->heapSize = 0;
|
||||||
@ -333,7 +333,7 @@ void HTimerRun(uint8_t id)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeRegisterInfo *reg = &sInfo.info[id];
|
HTimeRegisterInfo *reg = &sInfo.info[id];
|
||||||
if (reg->run) {
|
if (reg->run) {
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
@ -358,8 +358,8 @@ void HTimerRun(uint8_t id)
|
|||||||
t->state = kTimerUnused;
|
t->state = kTimerUnused;
|
||||||
HTIMER_BARRIER();
|
HTIMER_BARRIER();
|
||||||
if (i < reg->heapSize) {
|
if (i < reg->heapSize) {
|
||||||
heapUp(reg, i);
|
HeapUp(reg, i);
|
||||||
heapDown(reg, i);
|
HeapDown(reg, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -375,7 +375,7 @@ void HTimerRun(uint8_t id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (state == kTimerActive && t->heapIndex == HEAP_INVALID_INDEX) {
|
if (state == kTimerActive && t->heapIndex == HEAP_INVALID_INDEX) {
|
||||||
heapPush(reg, i);
|
HeapPush(reg, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -386,7 +386,7 @@ void HTimerRun(uint8_t id)
|
|||||||
HTimerInfo *t = ®->timers[tIdx];
|
HTimerInfo *t = ®->timers[tIdx];
|
||||||
|
|
||||||
if (HTIMER_ATOMIC_LOAD(&t->state) == kTimerDelete) {
|
if (HTIMER_ATOMIC_LOAD(&t->state) == kTimerDelete) {
|
||||||
heapPop(reg);
|
HeapPop(reg);
|
||||||
t->state = kTimerUnused;
|
t->state = kTimerUnused;
|
||||||
t->heapIndex = HEAP_INVALID_INDEX;
|
t->heapIndex = HEAP_INVALID_INDEX;
|
||||||
continue;
|
continue;
|
||||||
@ -397,7 +397,7 @@ void HTimerRun(uint8_t id)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
heapPop(reg);
|
HeapPop(reg);
|
||||||
|
|
||||||
t->curr = 1;
|
t->curr = 1;
|
||||||
HTIMER_BARRIER();
|
HTIMER_BARRIER();
|
||||||
@ -414,7 +414,7 @@ void HTimerRun(uint8_t id)
|
|||||||
t->lastTime = now;
|
t->lastTime = now;
|
||||||
|
|
||||||
if (t->flags == kHTimerLoop) {
|
if (t->flags == kHTimerLoop) {
|
||||||
heapPush(reg, tIdx);
|
HeapPush(reg, tIdx);
|
||||||
} else {
|
} else {
|
||||||
t->state = kTimerDelete;
|
t->state = kTimerDelete;
|
||||||
HTIMER_ATOMIC_OR(®->schedu, 1);
|
HTIMER_ATOMIC_OR(®->schedu, 1);
|
||||||
@ -448,7 +448,7 @@ void HTimerRemove(HTimer_t index)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeRegisterInfo *reg = &sInfo.info[id];
|
HTimeRegisterInfo *reg = &sInfo.info[id];
|
||||||
if (i >= reg->len) {
|
if (i >= reg->len) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -501,7 +501,7 @@ long HTimerGetCurrCallUserData(uint8_t id)
|
|||||||
if (id >= sInfo.infoLen) {
|
if (id >= sInfo.infoLen) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
TimeRegisterInfo *reg = &sInfo.info[id];
|
HTimeRegisterInfo *reg = &sInfo.info[id];
|
||||||
for (HTimerLen_t i = 0; i < reg->len; ++i) {
|
for (HTimerLen_t i = 0; i < reg->len; ++i) {
|
||||||
if (reg->timers[i].curr) {
|
if (reg->timers[i].curr) {
|
||||||
return reg->timers[i].userData;
|
return reg->timers[i].userData;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user