RtosTimerをクラス内で使うテスト

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
ryood
Date:
2016-05-30
Revision:
0:9bbbd58541be
Child:
1:747445d49c3d

File content as of revision 0:9bbbd58541be:

#include "mbed.h"
#include "rtos.h"

class TestClass
{
public:
    TestClass(uint32_t _updateTime) :  updateTime(_updateTime)
    {
        //NOTE: The RTOS hasn't started yet, so we can't create the internal thread here
    }

    void run() {
        timer = new RtosTimer(&TestClass::threadHelper, osTimerPeriodic, (void *)this) ;
        timer->start(updateTime);
    }

    void stop() {
        timer->stop();
        delete timer;
    }

private:
    uint32_t updateTime;
    RtosTimer* timer;

    static void threadHelper(const void* arg) {
        printf("In threadHelper()\r\n");

        //Cast the argument to a TestClass instance pointer
        TestClass* instance = (TestClass*)arg;

        //Call the thread method for the TestClass instance
        instance ->threadMethod();
    }

    void threadMethod() {
        printf("In threadMethod()\r\n");
    }
};

int main()
{
    printf("\n\n\r** Using An RtosTimer inside a class test **\r\n");
    
    while (true) {
        printf("In main loop\r\n");
        TestClass test(1000);
        test.run();
        Thread::wait(10000);
        test.stop();
    }
}