增加断网后重连广播和温湿度广播循环切换

This commit is contained in:
2026-09-03 14:57:03 +08:00
parent 5625362916
commit 9a2fdb1709
5 changed files with 312 additions and 6 deletions
+40
View File
@@ -41,6 +41,14 @@ typedef struct {
static AppThCtx_t s_th;
/**
* 「本周期采样成功」事件(生产者=20ms 中断,消费者=500ms 任务上下文)
* ⚠ 中断里只写这三个 volatile,绝不做开关广播 / 打日志 / 写 flash
*/
static volatile uint8_t s_th_pending;
static volatile int16_t s_th_pending_t;
static volatile uint8_t s_th_pending_h;
/**
* @brief 记一次采样失败(发令 nack / 读数失败共用)
* @return 无
@@ -73,6 +81,9 @@ void app_th_init(void)
s_th.valid = 0;
s_th.fail_cnt = 0;
s_th.last_ok = 0;
s_th_pending = 0;
s_th_pending_t = 0;
s_th_pending_h = 0;
s_th.inited = 1;
log_info("th init period=%ums\n", (unsigned)BOARD_TH_PERIOD_MS);
}
@@ -119,6 +130,10 @@ void app_th_poll(uint16_t dt)
s_th.fail_cnt = 0;
s_th.valid = 1;
s_th.last_ok = 1;
/* 先填值再置 pending:消费者关中断取,生产者是中断,顺序无歧义 */
s_th_pending_t = t;
s_th_pending_h = h;
s_th_pending = 1;
log_info("th %d.%dC %d%%\n",
(int)(t / 10), (int)((t < 0) ? (-t % 10) : (t % 10)), h);
} else {
@@ -181,3 +196,28 @@ uint8_t app_th_last_sample_ok(void)
{
return s_th.last_ok;
}
/**
* @brief 取走一次「本周期采样成功」事件(消费端,任务上下文)
* @param t_x10 出参:温度 ×10
* @param h 出参:湿度 %
* @return 1=取到新采样,0=无新采样
*/
uint8_t app_th_take_sample(int16_t *t_x10, uint8_t *h)
{
uint8_t got;
local_irq_disable();
got = s_th_pending;
if (got) {
s_th_pending = 0;
if (t_x10) {
*t_x10 = s_th_pending_t;
}
if (h) {
*h = s_th_pending_h;
}
}
local_irq_enable();
return got;
}