"多功能网关的通信模式切换"。
在这个例子中,网关需要支持多种通信方式(如 Wi-Fi、LoRa、NB-IoT)。我们不希望在主任务里写一堆 if-else 或 switch-case。
场景设计:通用通信引擎 (Communication Engine)
1. I:Interface (接口协议层)
定义所有通信方式必须遵守的"合同"。在 C 语言中,这是一个结构体。
c
// comm_interface.h
typedef struct {
const char* type_name;
int (*connect)(void); // 连接
int (*send)(uint8_t *data, uint16_t len); // 发送
void (*sleep)(void); // 休眠(低功耗处理)
} CommInterface;
2. T:Transformation (具体的零件/驱动层)
这里我们实现两个"零件":Wi-Fi 和 LoRa。
c
// wifi_driver.c (零件 T1)
#include "comm_interface.h"
static int wifi_conn() { /* 执行 AT 指令连 Wi-Fi */ return 0; }
static int wifi_send(uint8_t *d, uint16_t l) { /* 通过 TCP 发送 */ return 0; }
CommInterface wifi_t = {
.type_name = "ESP8266_WIFI",
.connect = wifi_conn,
.send = wifi_send
};
// lora_driver.c (零件 T2)
#include "comm_interface.h"
static int lora_conn() { /* 设置扩频因子和频率 */ return 0; }
static int lora_send(uint8_t *d, uint16_t l) { /* 通过射频芯片发送 */ return 0; }
CommInterface lora_t = {
.type_name = "SX1278_LORA",
.connect = lora_conn,
.send = lora_send
};
3. E:Engine (FreeRTOS 任务引擎)
这是核心控制逻辑,它只管调用接口 I,不关心当前用的是哪个 T。
c
// comm_engine.c
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
// 引擎持有的接口指针
static CommInterface *current_comm = NULL;
extern QueueHandle_t xDataQueue; // 假设数据来自其他任务
void vCommEngineTask(void *pvParameters) {
// 动态选择一个 T (也可以通过宏定义或配置选择)
current_comm = (CommInterface *)pvParameters;
printf("[Engine] 启动通信引擎,当前模式: %s\n", current_comm->type_name);
// 1. 调用 I 层接口进行连接
if (current_comm->connect() == 0) {
uint8_t buffer[64];
for (;;) {
// 2. 等待队列数据(典型的 FreeRTOS 阻塞模式)
if (xQueueReceive(xDataQueue, buffer, portMAX_DELAY) == pdPASS) {
// 3. 调用 I 层接口发送,引擎不关心具体是发往 Wi-Fi 还是 LoRa
current_comm->send(buffer, 64);
printf("[Engine] 数据已通过 %s 成功发送\n", current_comm->type_name);
}
}
}
}
4. 为什么这对 FreeRTOS 开发至关重要?
内存隔离与静态分配
在嵌入式中,你可以为不同的 T 零件分配不同的静态配置。通过将 CommInterface 指针作为 xTaskCreate 的参数传入,你可以用同一个 Engine 函数创建出两个功能不同的 Task。
易于测试 (Mocking)
如果你现在手头没有 LoRa 硬件,你可以快速写一个 sim_lora_t (模拟零件),只做 printf 输出。Engine 代码不需要任何改动就能跑通业务逻辑测试。
处理中断 (ISR) 的解耦
你可以把 I 接口扩展,加入回调函数。当硬件中断触发时,底层驱动 (T) 调用 I 中的回调,由 E 来决定是否唤醒某个 FreeRTOS 任务。