I have a structure that contains several function pointers. The generic interface is made in the header file.
Header File
typedef struct
{
void (*Start)(void);
void (*ByteWrite)(uint8_t *pBuffer); // Modifies I2C buffer
uint8_t (*ByteRead)(uint8_t *pBuffer);
void (*ArrayWrite)(uint8_t *pBuffer);
uint8_t (*ArrayRead)(uint8_t *pBuffer);
bool (*Busy)(void);
} sI2C_t;
extern const sI2C_t I2C0;
extern const sI2C_t I2C1;
extern const sI2C_t I2C2;
Then in the C file each of the function-pointers are implemented to satisfy the structure interface.
C File
static void I2C0_Start(void) { ... }
static void I2C0_ByteWrite(*uint8_t) { ... }
static uint8_t I2C0_ByteRead(*uint8_t) { ... }
static void I2C0_ArrayWrite(*uint8_t) { ... }
static uint8_t I2C_ArrayRead(*uint8_t) { ... }
static bool I2C_Busy(void) { ... }
const sI2C I2C0 =
{
I2C0_Start,
I2C0_ByteWrite,
I2C0_ByteRead,
I2C0_ArrayWrite,
I2C0_ArrayRead,
I2C0_Busy
};
// Code-block repeated for I2C1, I2C2, etc. (REDUNDANT!)
This makes it relatively easy to access functions specific to the I2C interface:
bool status;
I2C0.Start();
status = I2C1.Busy();
...
Although the function-pointers are basically the same for I2C0, I2C1, and I2C2, etc., I have to write out each of them individually for every new structure interface. Since this is redundant, is there a way for me to implement these function pointers only once?