嵌入式代码框架编写记录(简化版)

通用代码结构记录

声明类型定义(core_types.h)

typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;
typedef char int8;
typedef volatile char vint8;
typedef unsigned char uint8;
typedef volatile unsigned char vuint8;
typedef int int16;
typedef unsigned short uint16;
typedef long int32;
typedef unsigned long uint32;
typedef uint8 u_char; /**< 8-bit value */
typedef uint8 SOCKET;
typedef uint16 u_short; /**< 16-bit value */
typedef uint16 u_int;   /**< 16-bit value */
typedef uint32 u_long;  /**< 32-bit value */
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef volatile uint32_t vu32;
typedef volatile uint16_t vu16;
typedef volatile uint8_t vu8;
typedef u8 (*CoreHandleFunc)(void *pArg);

声明核心结构体(core_struct.h)

// 核心枚举类,方便管理
typedef enum {
    Result_Ok,
    Result_Error
} Core_Result_Enum;
// 核心处理结构体
typedef struct {
    u16 handle_code;
    CoreHandleFunc handle_func;
} Core_Handle_t;
// 核心处理结构体映射表
typedef struct {
    u16 cur_size;
    u16 cap;
    Core_Handle_t *core_handle;
} Core_Handle_TB_t;

声明核心结构体的容器

#define HANDLE_TABLE_SIZE 20
Core_Handle_TB_t handleTb;

定义操作结构体容器的函数

Core_Result_Enum Handle_TB_Init(uint16_t cap) {
    if (cap == 0) {
        handleTb.cap = HANDLE_TABLE_SIZE;

    } else {
        handleTb.cap = cap;
    }
    handleTb.cur_size = 0;
    handleTb.core_handle = (Core_Handle_t *) malloc(sizeof(Core_Handle_t) * handleTb.cap);
    if (handleTb.core_handle == NULL) {
        return Result_Error;
    }
    return Result_Ok;
}
Core_Result_Enum Handle_TB_Add(Core_Handle_t *item) {
    if (handleTb.cap <= handleTb.cur_size || item == NULL || item->handle_func == NULL) {
        return Result_Error; // 容量不足
    }
    // 判断是否已经有这个功能码
    int findIdx = Handle_TB_SearchByCode(item->handle_code);
    if (findIdx != -1) {
        printf("func exist\n");
        return Result_Error;
    }
    handleTb.core_handle[handleTb.cur_size].handle_func = item->handle_func;
    handleTb.core_handle[handleTb.cur_size].handle_code = item->handle_code;
    handleTb.cur_size++;
    return Result_Ok;
}
Core_Result_Enum Handle_TB_Add2(u16 itemCode, CoreHandleFunc func) {
    if (handleTb.cap <= handleTb.cur_size || Handle_TB_SearchByCode(itemCode) != -1) {
        return Result_Error; // 容量不足
    }
    handleTb.core_handle[handleTb.cur_size].handle_func = func;
    handleTb.core_handle[handleTb.cur_size].handle_code = itemCode;
    handleTb.cur_size++;
    return Result_Ok;
}

int Handle_TB_SearchByCode(u16 itemCode) {
    if (handleTb.cur_size == 0 || handleTb.core_handle == NULL) {
        return -1;
    }
    for (int i = 0; i < handleTb.cur_size; ++i) {
        if (handleTb.core_handle[i].handle_code == itemCode) {
            return i;
        }
    }
    return -1;
}
Core_Handle_t *Handle_TB_GetHandle(u16 itemCode) {
    int idx = Handle_TB_SearchByCode(itemCode);
    if (idx == -1) {
        return NULL;
    }
    return &handleTb.core_handle[idx];

}

Core_Result_Enum Handle_TB_Remove(u16 itemCode) {
    if (handleTb.core_handle == NULL) {
        return Result_Ok;
    }
    int idx = Handle_TB_SearchByCode(itemCode);
    if (idx == -1) { // 不存在
        return Result_Error;
    }
    memcpy(handleTb.core_handle + idx, handleTb.core_handle + idx + 1,
           (handleTb.cur_size - idx) * sizeof(Core_Handle_t));
    handleTb.cur_size--;
    return Result_Ok;

}
void Handle_Free(void) {
    free(handleTb.core_handle);
    handleTb.core_handle = NULL;
}

void Handle_TB_PrintAll(void) {
    for (int i = 0; i < handleTb.cur_size; ++i) {
        printf("cur code: %#x\n", handleTb.core_handle[i].handle_code);
    }

}

测试用例

u8 FirstHandle(void *pArg) {

    printf("hello first\n");
    return 1;
}

int main() {
    Handle_TB_Init(20);
    Core_Handle_t nw;
    nw.handle_code = 0x0001;
    nw.handle_func = FirstHandle;
    Handle_TB_Add(&nw);
    nw.handle_code = 0x0002;
    nw.handle_func = FirstHandle;
    Handle_TB_Add(&nw);
    nw.handle_code = 0x0003;
    nw.handle_func = FirstHandle;
    Handle_TB_Add(&nw);
    nw.handle_code = 0x0004;
    nw.handle_func = FirstHandle;
    Handle_TB_Add(&nw);
    printf("%d;%d\n", handleTb.cap, handleTb.cur_size);

    Core_Handle_t *handle = Handle_TB_GetHandle(nw.handle_code);
    handle->handle_func(NULL);
    Handle_TB_PrintAll();
    Handle_TB_Remove(0x1);
    printf("------------------------\n");
    Handle_TB_PrintAll();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

詩不诉卿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值