Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- meriac
- Date:
- 2014-08-20
- Revision:
- 6:45ca54a72865
- Parent:
- 5:64411d876ad4
- Child:
- 8:ac4f1c813c8d
File content as of revision 6:45ca54a72865:
#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, &CTest::callback1)
{
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: */
/* callback function has been set in the CTest constructor */
hexdump((const void*)entry, 16);
/* call entry point */
pc.printf("before entry 1 (counter=%i)\n", test.counter);
g_led2 = 1;
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();
}