Milosch Meriac / Mbed 2 deprecated cthunk_example

Dependencies:   CThunk mbed

Revision:
0:a6725b4373c6
Child:
1:2e8f85f1ed3e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Aug 13 07:44:05 2014 +0000
@@ -0,0 +1,87 @@
+#include <mbed.h>
+#include <CThunk.h>
+
+class CTest
+{
+public:
+    CTest(void);
+    void callback1(void);
+    void callback2(void* context);
+    void callback3(void* context);
+
+    uint32_t counter;
+
+    CThunk<CTest> thunk;
+
+private:
+    DigitalOut m_led;
+};
+
+CTest::CTest(void)
+    :m_led(LED1)
+{
+    counter = 0;
+}
+
+void CTest::callback1(void)
+{
+    /* increment member variable */
+    counter++;
+}
+
+void CTest::callback2(void* context)
+{
+    printf("Called with context value 0x%08X\n", context);
+
+    /* increment member variable */
+    counter = (uint32_t)context;
+}
+
+void CTest::callback3(void* context)
+{
+    Ticker* sender = static_cast<Ticker*>(context);
+
+    printf("Called by ticker object 0x%08X: \n", sender);
+
+    m_led = !m_led;
+}
+
+
+int main(void)
+{
+    Ticker tick;
+    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 = &CTest::callback1;
+    /* call entry point */
+    entry();
+
+
+    /* TEST2: */
+
+    /* assign a context ... */
+    test.thunk = 0xDEADBEEF;
+    /* and switch callback to callback2 */
+    test.thunk = &CTest::callback2;
+    /* call entry point */
+    entry();
+
+
+    /* TEST3: */
+
+    /* try ticker */
+    tick.attach(test.thunk, 1.0);
+    /* assign a context ... */
+    test.thunk = (void*)&tick;
+    /* change the callback function */
+    test.thunk = &CTest::callback3;
+    /* call entry point */
+    entry();
+}
\ No newline at end of file