简单的函数指针的应用,正在编辑,先剧透一下,
本人学习参考过很多代码..具体哪些就不说了,各种固件库少不了的,linux的思维方式也有参考
最终的形式如下:
//file : usr_uart.h#ifndef __USR_UART_H__#define __USR_UART_H__#include "stdint.h"typedef void (*p_uart_open_fun)(void);typedef void (*p_uart_read_fun)(uint8_t data);typedef uint8_t (*p_uart_write_fun)(void);typedef void (*p_uart_close_fun)(void);typedef struct struct_uart_demands{ p_uart_open_fun open; p_uart_read_fun read; p_uart_write_fun write; p_uart_close_fun close;}T_uart_drive;extern T_uart_drive rs232;extern T_uart_drive rs485;#endif
//file:usr_uart.c#include#include #include "usr_uart.h"static void rs232_init(void);static void rs232_read(uint8_t data);static uint8_t rs232_write(void);static void rs232_disable(void);static void rs485_init(void);static void rs485_read(uint8_t data);static uint8_t rs485_write(void);static void rs485_disable(void);T_uart_drive rs232 = { rs232_init, rs232_read, rs232_write, rs232_disable };T_uart_drive rs485 = { rs485_init, rs485_read, rs485_write, rs485_disable };//////rs232 function////void rs232_init(void){}void rs232_read(uint8_t data){}uint8_t rs232_write(void){ uint8_t ret = 0; return ret;}void rs232_disable(void){}//////rs485 function////void rs485_init(void){}void rs485_read(uint8_t data){}uint8_t rs485_write(void){ uint8_t ret = 0; return ret;}void rs485_disable(void){}
//file: main.c #include#include #include "usr_uart.h"void bsp_init(void){ rs232.open(); rs485.open();}int main(void){ SystemInit(); //config the clk bsp_init(); while(1) { }}//void assert_failed(uint8_t* file, uint32_t line){ while(1) { file = file; line = line; }}
代码如上,不懂的可以问我,正在进行中,文字就先不写了