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

Dependencies:   mbed-rtos mbed

Committer:
ryood
Date:
Mon May 30 11:06:44 2016 +0000
Revision:
2:ff926e519980
Parent:
1:747445d49c3d
RtosTImer?2?????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 0:9bbbd58541be 1 #include "mbed.h"
ryood 0:9bbbd58541be 2 #include "rtos.h"
ryood 0:9bbbd58541be 3
ryood 0:9bbbd58541be 4 class TestClass
ryood 0:9bbbd58541be 5 {
ryood 0:9bbbd58541be 6 public:
ryood 2:ff926e519980 7 TestClass(const char* _instanceName, uint32_t _updateTime) :
ryood 2:ff926e519980 8 updateTime(_updateTime),
ryood 1:747445d49c3d 9 timer(&TestClass::threadHelper, osTimerPeriodic, (void *)this)
ryood 0:9bbbd58541be 10 {
ryood 0:9bbbd58541be 11 //NOTE: The RTOS hasn't started yet, so we can't create the internal thread here
ryood 2:ff926e519980 12 strncpy(instanceName, _instanceName, 20);
ryood 0:9bbbd58541be 13 }
ryood 0:9bbbd58541be 14
ryood 0:9bbbd58541be 15 void run() {
ryood 1:747445d49c3d 16 timer.start(updateTime);
ryood 0:9bbbd58541be 17 }
ryood 0:9bbbd58541be 18
ryood 0:9bbbd58541be 19 void stop() {
ryood 1:747445d49c3d 20 timer.stop();
ryood 0:9bbbd58541be 21 }
ryood 0:9bbbd58541be 22
ryood 0:9bbbd58541be 23 private:
ryood 2:ff926e519980 24 char instanceName[20];
ryood 0:9bbbd58541be 25 uint32_t updateTime;
ryood 1:747445d49c3d 26 RtosTimer timer;
ryood 0:9bbbd58541be 27
ryood 0:9bbbd58541be 28 static void threadHelper(const void* arg) {
ryood 0:9bbbd58541be 29 printf("In threadHelper()\r\n");
ryood 0:9bbbd58541be 30
ryood 0:9bbbd58541be 31 //Cast the argument to a TestClass instance pointer
ryood 0:9bbbd58541be 32 TestClass* instance = (TestClass*)arg;
ryood 0:9bbbd58541be 33
ryood 0:9bbbd58541be 34 //Call the thread method for the TestClass instance
ryood 0:9bbbd58541be 35 instance ->threadMethod();
ryood 0:9bbbd58541be 36 }
ryood 0:9bbbd58541be 37
ryood 0:9bbbd58541be 38 void threadMethod() {
ryood 2:ff926e519980 39 printf("In threadMethod()\t%s\r\n", instanceName);
ryood 0:9bbbd58541be 40 }
ryood 0:9bbbd58541be 41 };
ryood 0:9bbbd58541be 42
ryood 0:9bbbd58541be 43 int main()
ryood 0:9bbbd58541be 44 {
ryood 0:9bbbd58541be 45 printf("\n\n\r** Using An RtosTimer inside a class test **\r\n");
ryood 0:9bbbd58541be 46
ryood 0:9bbbd58541be 47 while (true) {
ryood 0:9bbbd58541be 48 printf("In main loop\r\n");
ryood 2:ff926e519980 49 TestClass test1("test1", 1000);
ryood 2:ff926e519980 50 TestClass test2("test2", 500);
ryood 2:ff926e519980 51 test1.run();
ryood 2:ff926e519980 52 test2.run();
ryood 0:9bbbd58541be 53 Thread::wait(10000);
ryood 2:ff926e519980 54 /*
ryood 2:ff926e519980 55 test1.stop();
ryood 2:ff926e519980 56 test2.stop();
ryood 2:ff926e519980 57 */
ryood 0:9bbbd58541be 58 }
ryood 0:9bbbd58541be 59 }