Test function for CThunk class

Dependencies:   CThunk mbed

Revision:
8:ac4f1c813c8d
Parent:
6:45ca54a72865
Child:
10:47bfaa986895
--- a/main.cpp	Wed Aug 20 13:37:08 2014 +0000
+++ b/main.cpp	Wed Aug 20 14:15:10 2014 +0000
@@ -1,50 +1,64 @@
 #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 test(void);
+    void hexdump(const void* data, int length);
+
+    Serial pc;
+    CThunk<CTest> thunk;
+
     void callback2(void* context);
-    void callback3(void* context);
-    static void callback4(uint32_t r0, uint32_t r1);
+
+private:
+    DigitalOut m_led1, m_led2;
 
-    uint32_t counter;
+    void callback1(void);
 
-    CThunk<CTest> thunk;
+    uint32_t m_counter;
 };
 
 CTest::CTest(void)
-    :thunk(this, &CTest::callback1)
+    :pc(USBTX, USBRX),
+    thunk(this, &CTest::callback1),
+    m_led1(LED1),
+    m_led2(LED2)
 {
-    counter = 0;
+    m_counter = 0;
+
+    pc.baud(115200);    
 }
 
 void CTest::callback1(void)
 {
-    pc.printf("callback1 called (this=0x%0X)\n", this);
+    pc.printf("callback1: called (this=0x%0X)\n", this);
+
+    /* turn on LED1 */
+    m_led1 = 1;
 
     /* increment member variable */
-    counter++;
+    pc.printf("callback1:   m_counter before: %i\n", m_counter);
+    m_counter++;
+    pc.printf("callback1:   m_counter after : %i\n", m_counter);
 }
 
 void CTest::callback2(void* context)
 {
-    pc.printf("Called with context value 0x%08X\n", context);
+    pc.printf("callback2: called with context value 0x%08X\n", context);
+
+    /* turn on LED2 */
+    m_led2 = 1;
 
     /* increment member variable */
-    counter+=2;
+    pc.printf("callback2:   m_counter before: %i\n", m_counter);
+    m_counter+=2;
+    pc.printf("callback2:   m_counter after : %i\n", m_counter);
 }
 
-void hexdump(const void* data, int length)
+void CTest::hexdump(const void* data, int length)
 {
     int i;
 
@@ -62,49 +76,31 @@
     pc.printf("\n");
 }
 
-static void test(void)
+int main(void)
 {
-    CThunkEntry entry;
+    /* allocate thunking test class */
     CTest test;
-
-    /* get 32 bit entry point pointer from thunk */
-    entry = test.thunk;
-    /* TEST1: */
+    /* allocate 32 bit pointer to thunk-entry */
+    CThunkEntry entry;
 
-    /* callback function has been set in the CTest constructor */
-    hexdump((const void*)entry, 16);
-    /* call entry point */
+    /* to make a point: get 32 bit entry point pointer from thunk */
+    entry = test.thunk;
 
-    pc.printf("before entry 1 (counter=%i)\n", test.counter);
-    g_led2 = 1;
+    /* TEST1: */
+    /* callback function has been set in the CTest constructor */
+    test.hexdump((const void*)entry, 20);
+    /* call entry point */
     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();
 }