Milosch Meriac
/
cthunk_example
Test function for CThunk class
main.cpp
- Committer:
- meriac
- Date:
- 2014-08-20
- Revision:
- 5:64411d876ad4
- Parent:
- 4:97c886c2d89b
- Child:
- 6:45ca54a72865
File content as of revision 5:64411d876ad4:
#include <mbed.h> DigitalOut g_led1(LED1); DigitalOut g_led2(LED2); DigitalOut g_led3(LED3); DigitalOut g_led4(LED4); Serial pc(USBTX, USBRX); #include <CThunk.h> class CTest { public: CTest(void); void callback1(void); void callback2(void* context); void callback3(void* context); static void callback4(uint32_t r0, uint32_t r1); uint32_t counter; CThunk<CTest> thunk; }; CTest::CTest(void) :thunk(this) { counter = 0; } void CTest::callback1(void) { pc.printf("callback1 called (this=0x%0X)\n", this); /* increment member variable */ counter++; } void CTest::callback2(void* context) { pc.printf("Called with context value 0x%08X\n", context); /* increment member variable */ counter+=2; } void hexdump(const void* data, int length) { int i; pc.printf("Dump %u bytes from 0x%08X\n", length, data); for(i=0; i<length; i++) { if((i%16) == 0) pc.printf("\n"); else if((i%8) == 0) pc.printf(" - "); pc.printf("0x%02X ", ((uint8_t*)data)[i]); } pc.printf("\n"); } static void test(void) { CThunkEntry entry; CTest test; /* get 32 bit entry point pointer from thunk */ entry = test.thunk; /* TEST1: */ /* assign callback1 to thunk - no context needed */ //test.thunk.callback(&CTest::callback1); hexdump((const void*)entry, 16); /* call entry point */ pc.printf("before entry 1 (counter=%i)\n", test.counter); g_led2 = 1; test.thunk.callback(&CTest::callback1); entry(); pc.printf("after entry 1 (counter=%i)\n", test.counter); /* TEST2: */ /* assign a context ... */ test.thunk.context(0xDEADBEEF); /* and switch callback to callback2 */ test.thunk.callback(&CTest::callback2); /* call entry point */ pc.printf("before entry 2 (counter=%i)\n", test.counter); g_led3 = 1; entry(); pc.printf("after entry 2 (counter=%i)\n", test.counter); } int main(void) { pc.baud(115200); printf("Test 1\n"); /* run tests */ g_led1 = 1; test(); /* turn both LED's on */ g_led4 = 1; while(1) __WFI(); }