Created
November 4, 2017 00:04
-
-
Save C47D/f80cf64d3513aa44a25ac5f47fc44727 to your computer and use it in GitHub Desktop.
test for using function pointer on the nrf24 component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vtable | |
struct nrf_spi_operations { | |
uint8_t (*spi_read)(void); | |
void (*spi_write)(uint8_t data); | |
} | |
// microcontroller A | |
uint8_t microA_spi_read(void); | |
void microA_spi_write(uint8_t data); | |
// microcontroller B | |
uint32_t microB_spi_read(void); | |
void microB_spi_write(uint8_t data); | |
enum { | |
PSOC4, | |
PSOC5, | |
PSOC6, | |
}; | |
// generic function pointer | |
typedef void void_f(void); | |
uint32_t spi_write_comp(uint8_t type_psoc, void_f *p, uint8_t data) | |
{ | |
switch(type_psoc) { | |
case PSOC4: { | |
psoc4_spi_write * spi_write = (psoc4_spi_write *)p; | |
return psoc4_spi_write(data); | |
} | |
case PSOC5: { | |
} | |
case PSOC6: { | |
} | |
} | |
} | |
int main() | |
{ | |
struct nrf_spi_operations myMicroA; | |
myMicroA.spi_read = microA_spi_read; | |
myMicroA.spi_write = microA_spi_write; | |
struct nrf_spi_operations myMicroB; | |
myMicroB.spi_read = microB_spi_read; | |
myMicroB.spi_write = microB_spi_write; | |
// using the functions | |
// in microcontroller apps we never return from main | |
while(1) { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment