InterruptTask.h |
---|
#ifndef INTERRUPTTASK #define INTERRUPTTASK #include "Task.h" class InterruptTask : public Task { public: int interruptState; int interruptValue; // コンストラクタ InterruptTask(); /** * タスクの割り込みに用いる。割り込み状態に入る前に * 多重割り込みが起こった場合、最初の割り込みのみが認識されます。 * 他の全ては無視されます。 * * 入力:int interruptValue: * 割り込み原因を決めるためにタスクに与えられる値 * * 戻り:boolean 割り込みが処理されたならばtrue */ virtual bool Interrupt ( int interruptValue ); /** * 割り込みの前の状態で再開する。 * これは割り込み状態からのみ呼ばれるべきであるResume state prior to */ virtual void resumeInterrupt (); /** * タスクの割り込みに用いる。割り込み状態に入る前に * 多重割り込みが起こった場合、最初の割り込みのみが認識されます。 * 他の全ては無視されます。 */ virtual bool Interrupt (); }; #endif |
InterruptTask.cpp |
---|
#include "InterruptTask.h" // コンストラクタ InterruptTask::InterruptTask() { interruptState = terminate ; interruptValue = 0 ; } bool InterruptTask::Interrupt ( int interruptValue ) { if ( state != Task::_interrupt ) { this->interruptValue = interruptValue ; interruptState = state ; state = _interrupt ; return true ; } else return false ; } void InterruptTask::resumeInterrupt () { state = interruptState ; } bool InterruptTask::Interrupt () { return Interrupt(0); } |
MultitaskingTest2.h |
---|
#include "InterruptTask.h" #define initialState 0 #define state1 1 #define state2 2 #include "common.h" class MultitaskingTest2 : public InterruptTask { // execute()の状態 public: char* name; InterruptTask* taskToInterrupt ; MultitaskingTest2 ( char* name, InterruptTask* taskToInterrupt ); void show ( char* text ); void execute (); }; |
MultitaskingTest2.cpp |
---|
#include "MultitaskingTest2.h" MultitaskingTest2::MultitaskingTest2 ( char* name, InterruptTask* taskToInterrupt ) { pc.printf("MultitaskingTest2 %s\n", name); this->name = name ; this->taskToInterrupt = taskToInterrupt ; } void MultitaskingTest2::show ( char* text ) { pc.printf( "%s: %s\n", name, text ) ; } void MultitaskingTest2::execute () { switch ( state ) { case initialState: show ( "Initial state" ) ; nextState ( state1 ) ; break ; case state1: show ( "State 1" ) ; nextState ( state2 ) ; if ( taskToInterrupt != (Task*)null ) if ( taskToInterrupt->Interrupt ()) show ( "Task interrupted" ) ; else show ( "Task already interrupted" ) ; break ; case state2: show ( "State 2" ) ; stop () ; break ; case -2: show ( "Interrupted" ) ; resumeInterrupt () ; break ; default: // 異常な状態を捕獲し停止するためにdefaultにする stop () ; break; } // switch } |
main.cpp | |
---|---|
#include "mbed.h" #include "MultitaskingTest2.h" Serial pc(USBTX, USBRX); // tx, rx int main() { pc.baud(115200); MultitaskingTest2* task1 = new MultitaskingTest2 ( "Task 1", (InterruptTask*)null ) ; new MultitaskingTest2 ( "Task 2", task1 ) ; new MultitaskingTest2 ( "Task 3", task1 ) ; Task::TaskManager () ; pc.printf( "All done\n" ) ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
MultitaskingTest2 Task 1 MultitaskingTest2 Task 2 MultitaskingTest2 Task 3 Task 3: Initial state Task 1: Initial state Task 2: Initial state Task 3: State 1 Task 3: Task interrupted Task 1: Interrupted Task 2: State 1 Task 2: Task interrupted Task 3: State 2 Task 1: Interrupted Task 2: State 2 Task 1: State 1 Task 1: State 2 All done