From 85a7b62c89fc39e269e52f2c9d9b7e9070b60a0c Mon Sep 17 00:00:00 2001 From: coffee Date: Thu, 4 Jun 2026 16:09:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=AE=8C=E5=96=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/HTimer.h | 6 ++--- src/HTimer.c | 58 ++++++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/include/HTimer.h b/include/HTimer.h index 78b7b07..43545fd 100644 --- a/include/HTimer.h +++ b/include/HTimer.h @@ -58,13 +58,13 @@ typedef struct HTimerInfo { } HTimerInfo; ///< 定时器注册信息 -typedef struct TimeRegisterInfo { +typedef struct HTimeRegisterInfo { HTimerInfo *timers; ///< 定时器信息数组 HTimerLen_t heapSize; ///< 堆当前大小 HTimerLen_t len; ///< 定时器个数 volatile uint8_t schedu; ///< 需要重新调度 (原子操作) uint8_t run : 1; ///< 运行中 -} TimeRegisterInfo; +} HTimeRegisterInfo; ///< 初始化毫秒定时器, 需要传递获取毫秒的函数 void HTimerInitMs(uint32_t (*func)(void)); @@ -77,7 +77,7 @@ uint32_t HTimerGetMs(); * @param info 定时器注册信息数组 * @param len 定时器个数 */ -void HTimerInitRegister(TimeRegisterInfo *info, HTimerLen_t len); +void HTimerInitRegister(HTimeRegisterInfo *info, HTimerLen_t len); /** * @brief 注册定时器信息 diff --git a/src/HTimer.c b/src/HTimer.c index 0617cc8..31862fb 100644 --- a/src/HTimer.c +++ b/src/HTimer.c @@ -77,7 +77,7 @@ static uint32_t (*GetCurrentMs)(void); #define HEAP_INVALID_INDEX (HTIMER_LEN_MAX) struct __attribute__((packed)) TimerInfo { - TimeRegisterInfo *info; + HTimeRegisterInfo *info; uint8_t infoLen; }; @@ -90,12 +90,12 @@ static struct TimerInfo sInfo; #define HEAP_AT(reg, heapIdx) ((reg)->timers[heapIdx].heapMem) #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)]; } -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 tb = HEAP_AT(reg, b); @@ -107,13 +107,13 @@ static void heapSwap(TimeRegisterInfo *reg, int a, int b) reg->timers[tb].heapIndex = a; } -static void heapUp(TimeRegisterInfo *reg, int i) +static void HeapUp(HTimeRegisterInfo *reg, int i) { while (i > 0) { int p = (i - 1) >> 1; - HTimerInfo *ti = getTimer(reg, i); - HTimerInfo *tp = getTimer(reg, p); + HTimerInfo *ti = GetTimer(reg, i); + HTimerInfo *tp = GetTimer(reg, p); uint32_t iTime = ti->lastTime + ti->duration; uint32_t pTime = tp->lastTime + tp->duration; @@ -127,12 +127,12 @@ static void heapUp(TimeRegisterInfo *reg, int i) break; } - heapSwap(reg, i, p); + HeapSwap(reg, i, p); i = p; } } -static void heapDown(TimeRegisterInfo *reg, int i) +static void HeapDown(HTimeRegisterInfo *reg, int i) { int size = reg->heapSize; @@ -142,8 +142,8 @@ static void heapDown(TimeRegisterInfo *reg, int i) int s = i; if (l < size) { - HTimerInfo *tl = getTimer(reg, l); - HTimerInfo *ts = getTimer(reg, s); + HTimerInfo *tl = GetTimer(reg, l); + HTimerInfo *ts = GetTimer(reg, s); 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))) { s = l; @@ -151,8 +151,8 @@ static void heapDown(TimeRegisterInfo *reg, int i) } if (r < size) { - HTimerInfo *tr = getTimer(reg, r); - HTimerInfo *ts = getTimer(reg, s); + HTimerInfo *tr = GetTimer(reg, r); + HTimerInfo *ts = GetTimer(reg, s); 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))) { s = r; @@ -163,12 +163,12 @@ static void heapDown(TimeRegisterInfo *reg, int i) break; } - heapSwap(reg, i, s); + HeapSwap(reg, 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) { return; @@ -177,10 +177,10 @@ static void heapPush(TimeRegisterInfo *reg, HTimerLen_t timerIdx) HTimerLen_t i = reg->heapSize++; HEAP_SET(reg, i, timerIdx); 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) { return HEAP_INVALID_INDEX; @@ -192,7 +192,7 @@ static HTimerLen_t heapPop(TimeRegisterInfo *reg) if (--reg->heapSize > 0) { HEAP_SET(reg, 0, HEAP_AT(reg, reg->heapSize)); reg->timers[HEAP_AT(reg, 0)].heapIndex = 0; - heapDown(reg, 0); + HeapDown(reg, 0); } return ret; @@ -217,7 +217,7 @@ static HTimer_t AddTimerData(uint8_t id, uint32_t duration, HTimerCallType call, return HTIMER_INVALID; } - TimeRegisterInfo *reg = &sInfo.info[id]; + HTimeRegisterInfo *reg = &sInfo.info[id]; uint32_t now = GetCurrentMs(); // 原子地查找并占用空闲槽位 @@ -294,7 +294,7 @@ uint32_t HTimerGetMs() return GetCurrentMs(); } -void HTimerInitRegister(TimeRegisterInfo *info, HTimerLen_t len) +void HTimerInitRegister(HTimeRegisterInfo *info, HTimerLen_t len) { memset(info, 0, sizeof(*info) * len); sInfo.info = info; @@ -315,7 +315,7 @@ uint8_t HTimerRegisterTimerInfo(uint8_t id, HTimerInfo *info, uint16_t infoLen) memset(info, 0, sizeof(*info) * infoLen); - TimeRegisterInfo *reg = &sInfo.info[id]; + HTimeRegisterInfo *reg = &sInfo.info[id]; reg->timers = info; reg->len = (HTimerLen_t)infoLen; reg->heapSize = 0; @@ -333,7 +333,7 @@ void HTimerRun(uint8_t id) return; } - TimeRegisterInfo *reg = &sInfo.info[id]; + HTimeRegisterInfo *reg = &sInfo.info[id]; if (reg->run) { return ; } @@ -358,8 +358,8 @@ void HTimerRun(uint8_t id) t->state = kTimerUnused; HTIMER_BARRIER(); if (i < reg->heapSize) { - heapUp(reg, i); - heapDown(reg, i); + HeapUp(reg, i); + HeapDown(reg, i); } } } @@ -375,7 +375,7 @@ void HTimerRun(uint8_t id) } 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]; if (HTIMER_ATOMIC_LOAD(&t->state) == kTimerDelete) { - heapPop(reg); + HeapPop(reg); t->state = kTimerUnused; t->heapIndex = HEAP_INVALID_INDEX; continue; @@ -397,7 +397,7 @@ void HTimerRun(uint8_t id) break; } - heapPop(reg); + HeapPop(reg); t->curr = 1; HTIMER_BARRIER(); @@ -414,7 +414,7 @@ void HTimerRun(uint8_t id) t->lastTime = now; if (t->flags == kHTimerLoop) { - heapPush(reg, tIdx); + HeapPush(reg, tIdx); } else { t->state = kTimerDelete; HTIMER_ATOMIC_OR(®->schedu, 1); @@ -448,7 +448,7 @@ void HTimerRemove(HTimer_t index) return; } - TimeRegisterInfo *reg = &sInfo.info[id]; + HTimeRegisterInfo *reg = &sInfo.info[id]; if (i >= reg->len) { return; } @@ -501,7 +501,7 @@ long HTimerGetCurrCallUserData(uint8_t id) if (id >= sInfo.infoLen) { return 0; } - TimeRegisterInfo *reg = &sInfo.info[id]; + HTimeRegisterInfo *reg = &sInfo.info[id]; for (HTimerLen_t i = 0; i < reg->len; ++i) { if (reg->timers[i].curr) { return reg->timers[i].userData;