/*---------------------------------------------------------------------------* Project: $project_name File: $name Copyright (C)$copyright_year Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: $ *---------------------------------------------------------------------------*/ #ifndef NAKAYAMA_H_ #define NAKAYAMA_H_ #include #include #include #include #include #include #include #include #include #include #include //#include "HwTestLib/hwtest_SdFileManager.h" using namespace std; namespace nn{ namespace red{ namespace nakayama{ template class RingBuffer; class KeyValuePair; /*!--------------------------------------------------------------------------* @brief ディスプレイ *---------------------------------------------------------------------------*/ class Display { public: enum Position { Upper = NN_GX_DISPLAY0, Lower = NN_GX_DISPLAY1, Both = NN_GX_DISPLAY_BOTH }; virtual void Clear(); virtual void Draw(int X, int Y, string, bool lf = true); virtual void Draw(int X, int Y, string, int Size, bool lf = true); virtual void SwapBuffer(); static Display& GetInstance(Position position); ~Display(){}; protected: Display(Position); Position position; nn::demo::DrawFrameworkEx* GetTextDrawFramework(); }; /*!--------------------------------------------------------------------------* @brief デバッガ出力 *---------------------------------------------------------------------------*/ class DebugDisplay : public Display { public: virtual void Clear(); virtual void Draw(int X, int Y, string, bool lf = true); virtual void Draw(int X, int Y, string, int Size, bool lf = true); virtual void SwapBuffer(); static Display& GetInstance(Display::Position pos); private: DebugDisplay(Display::Position pos) : Display(pos){}; ~DebugDisplay(){}; }; /*!--------------------------------------------------------------------------* @brief キーパッド *---------------------------------------------------------------------------*/ class KeyPad { public: enum KeyMask { A = REG_PAD_KEYINPUT_A_MASK, B = REG_PAD_KEYINPUT_B_MASK, X = REG_PAD_KEYINPUT_X_MASK, Y = REG_PAD_KEYINPUT_Y_MASK, SELECT = REG_PAD_KEYINPUT_SEL_MASK, START = REG_PAD_KEYINPUT_START_MASK, L = REG_PAD_KEYINPUT_L_MASK, R = REG_PAD_KEYINPUT_R_MASK, UP = REG_PAD_KEYINPUT_UP_MASK, DOWN = REG_PAD_KEYINPUT_DOWN_MASK, LEFT = REG_PAD_KEYINPUT_LEFT_MASK, RIGHT = REG_PAD_KEYINPUT_RIGHT_MASK, ALL = A|B|X|Y|SELECT|START|L|R|UP|DOWN|LEFT|RIGHT }; static KeyPad& GetInstance(); void WaitForChange(KeyMask = ALL); void ReadKey(); bool IsTrigger(KeyMask); bool IsPress(KeyMask); nn::fnd::TimeSpan PressDetectionDelay; nn::fnd::TimeSpan PressDetectionInterval; private: KeyPad(); KeyPad(const KeyPad& kyepad); virtual ~KeyPad(){}; u16 m_KeyCurrent; u16 m_KeyPrevious; nn::fnd::TimeSpan m_PressTime; nn::fnd::TimeSpan m_DetectionTime; }; /*!--------------------------------------------------------------------------* @brief ユーザインタフェース *---------------------------------------------------------------------------*/ class UIControl { public: UIControl(){ X=0; Y=0; Parent = NULL;} virtual ~UIControl(){} int X; int Y; UIControl* Parent; virtual void Show(Display&){}; virtual void KeyIn(KeyPad&){}; virtual int GetX() { if(Parent == NULL){ return X; } return (Parent->GetX() + X); }; virtual int GetY() { if(Parent == NULL){ return Y; } return (Parent->GetY() + Y); }; }; /*!--------------------------------------------------------------------------* @brief ページ *---------------------------------------------------------------------------*/ class Page : public UIControl { public: virtual void Show(Display&); virtual void Add(UIControl&); private: list controls; }; /*!--------------------------------------------------------------------------* @brief ラベル *---------------------------------------------------------------------------*/ class Label : public UIControl { public: string Text; virtual void Show(Display&); }; /*!--------------------------------------------------------------------------* @brief 時計 *---------------------------------------------------------------------------*/ class Clock : public UIControl { private: nn::fnd::TimeSpan start; public: void Reset() { start = nn::os::Tick::GetSystemCurrent().ToTimeSpan(); }; virtual void Show(Display&); }; /*!--------------------------------------------------------------------------* @brief 選択器 *---------------------------------------------------------------------------*/ template class Selector : public UIControl{ public: Selector(); virtual ~Selector(){}; virtual void Register(string,T&); virtual T* GetSelectedItem(); bool Selected; bool Canceled; int Offset; int LineHeight; int FontSize; virtual void SetCursor(char); double CursorSmoothness; virtual void Show(Display&); virtual void KeyIn(KeyPad&); protected: vector items; vector names; int current_item; int cursor_position; char cursor[2]; virtual string GetItemName(int); }; /*!--------------------------------------------------------------------------* Name: Selector::GetItemName @brief アイテム名を取得します。 @return *---------------------------------------------------------------------------*/ template string Selector::GetItemName(int index) { return names[index]; } /*!--------------------------------------------------------------------------* Name: Selector::Selector @brief コンストラクタです。 @return *---------------------------------------------------------------------------*/ template Selector::Selector() { Selected = false; Canceled = false; current_item = 0; cursor_position = 0; SetCursor('>'); CursorSmoothness = 1.5; LineHeight = 10; FontSize = 8; Offset = 10; } /*!--------------------------------------------------------------------------* Name: Selector::Register @brief アイテムを登録します。 @return *---------------------------------------------------------------------------*/ template void Selector::Register(string name, T& item) { names.push_back(name); items.push_back(&item); } /*!--------------------------------------------------------------------------* Name: Selector::Register @brief 選択されているアイテムを取得します。 @return *---------------------------------------------------------------------------*/ template T* Selector::GetSelectedItem() { if(Selected) { return items[current_item]; } return NULL; } /*!--------------------------------------------------------------------------* Name: Selector::Register @brief カーソルをセットします。 @return *---------------------------------------------------------------------------*/ template void Selector::SetCursor(char cursor) { this->cursor[0] = cursor; this->cursor[1] = '\0'; } /*!--------------------------------------------------------------------------* Name: Selector::KeyIn @brief キー入力を受け付けます。 @return *---------------------------------------------------------------------------*/ template void Selector::KeyIn(KeyPad& key) { key.ReadKey(); key.PressDetectionDelay = nn::fnd::TimeSpan::FromMilliSeconds(500); key.PressDetectionInterval = nn::fnd::TimeSpan::FromMilliSeconds(100); if(key.IsTrigger(KeyPad::A)) { Selected = true; } else if(key.IsTrigger(KeyPad::B)) { Canceled = true; } else if(key.IsTrigger(KeyPad::UP)) { if(current_item == 0) { current_item = items.size() -1; } else { current_item--; } } else if(key.IsPress(KeyPad::UP)) { if(current_item == 0) { } else { current_item--; } } else if(key.IsTrigger(KeyPad::DOWN)) { if(current_item == items.size() - 1) { current_item = 0; } else { current_item++; } } else if(key.IsPress(KeyPad::DOWN)) { if(current_item == items.size() - 1) { } else { current_item++; } } } /*!--------------------------------------------------------------------------* Name: Selector::Show @brief 表示します。 @return *---------------------------------------------------------------------------*/ template void Selector::Show(Display& display) { for(int i=0; iX + X; int y = GetY(); //Parent->Y + Y; if(i == current_item) { double difference = abs(current_item * LineHeight - cursor_position); int sign = current_item * LineHeight < cursor_position ? -1 : 1; cursor_position += sign * ceil(difference / CursorSmoothness); //display.Draw(x, y + cursor_position, cursor, FontSize); display.Draw(x, y + cursor_position, cursor, FontSize, false); // とりあえず、 } display.Draw(x + Offset, y + (i * LineHeight), GetItemName(i) ); } } /*!--------------------------------------------------------------------------* @brief 選択可能値のインタフェース *---------------------------------------------------------------------------*/ class ISelectableValue { public: //SelectableValue(){}; //virtual ~ISelectableValue(){}; //virtual void Add(void* , bool isDefault = false) = 0; //virtual void GetValue() = 0; virtual void Next(bool loop=false) = 0; virtual void Previous(bool loop=false) = 0; virtual string ToString() = 0; }; ostream& operator<<(ostream& stream, ISelectableValue* svalue); /*!--------------------------------------------------------------------------* @brief 汎用な選択可能値 *---------------------------------------------------------------------------*/ template class SelectableValue : public ISelectableValue { public: SelectableValue(); virtual ~SelectableValue(){}; virtual void Add(T, bool isDefault = false); virtual T GetValue(); virtual void Next(bool loop=false); virtual void Previous(bool loop=false); virtual string ToString(); private: vector values; int currentIndex; }; /*!--------------------------------------------------------------------------* Name: SelectableValue::SelectableValue @brief コンストラクタです。 @return *---------------------------------------------------------------------------*/ template SelectableValue::SelectableValue() { currentIndex = 0; } /*!--------------------------------------------------------------------------* Name: SelectableValue::Add @brief 候補値を追加します。 @return *---------------------------------------------------------------------------*/ template void SelectableValue::Add(T value, bool isDefault) { if(isDefault) { currentIndex = values.size(); } values.push_back(value); } /*!--------------------------------------------------------------------------* Name: SelectableValue::GetValue @brief 選択されている値を取得します。 @return *---------------------------------------------------------------------------*/ template T SelectableValue::GetValue() { return values[currentIndex]; } /*!--------------------------------------------------------------------------* Name: SelectableValue::Next @brief 次の候補値を選択します。 @return *---------------------------------------------------------------------------*/ template void SelectableValue::Next(bool loop) { currentIndex++; if(currentIndex == values.size()) { if(loop) { currentIndex = 0; } else { currentIndex--; } } } /*!--------------------------------------------------------------------------* Name: SelectableValue::Previous @brief 前の候補値を選択します。 @return *---------------------------------------------------------------------------*/ template void SelectableValue::Previous(bool loop) { currentIndex--; if(currentIndex < 0) { if(loop) { currentIndex = values.size() - 1; } else { currentIndex++; } } } /*!--------------------------------------------------------------------------* Name: SelectableValue::SelectableValue @brief 文字列を得ます。 @return *---------------------------------------------------------------------------*/ template string SelectableValue::ToString() { ostringstream oss; oss << values[currentIndex]; return oss.str(); } /*!--------------------------------------------------------------------------* @brief 値選択器 *---------------------------------------------------------------------------*/ class ValueSelector : public Selector { public: ValueSelector(){ max_name_length = 0;}; virtual ~ValueSelector(){}; virtual void Register(string, ISelectableValue&); virtual void KeyIn(KeyPad&); typedef Selector selector; private: int max_name_length; virtual string GetItemName(int); }; /*!--------------------------------------------------------------------------* @brief 名前つき値 *---------------------------------------------------------------------------*/ template class NamedValue { public: string Name; T Value; NamedValue(string, T); virtual ~NamedValue(){}; NamedValue(const NamedValue&); virtual NamedValue operator=(const NamedValue&); }; template ostream& operator<<(ostream& stream, NamedValue& value); /*!--------------------------------------------------------------------------* Name: NamedValue::NamedValue @breif コンストラクタです。 @return *---------------------------------------------------------------------------*/ template NamedValue::NamedValue(string name, T value) { Name = name; Value = value; } /*!--------------------------------------------------------------------------* Name: NamedValue::NamedValue @breif コピーコンストラクタです。 @return *---------------------------------------------------------------------------*/ template NamedValue::NamedValue(const NamedValue& nv) { Name = nv.Name; Value = nv.Value; } /*!--------------------------------------------------------------------------* Name: NamedValue::operator= @breif =のオーバーロードです。 @return *---------------------------------------------------------------------------*/ template NamedValue NamedValue::operator=(const NamedValue& nv) { Name = nv.Name; Value = nv.Value; return *this; } /*!--------------------------------------------------------------------------* Name: operator<< @breif NamedValue用<<のオーバーロードです。 @return *---------------------------------------------------------------------------*/ template ostream& operator<<(ostream& stream, NamedValue& nv) { stream << nv.Name; return stream; } /*!--------------------------------------------------------------------------* @brief テキスト書き込み器 *---------------------------------------------------------------------------*/ class TextWriter { public: TextWriter(){}; virtual ~TextWriter(){}; //! 文字列を書き込みます virtual void Write(string){}; //! 文字列を行として書き込みます virtual void WriteLine(string){}; private: }; ///*!--------------------------------------------------------------------------* // @brief スレッドを使わないSDカードへ書き込み器 // // *---------------------------------------------------------------------------*/ //class SimpleSdCardWriter : public TextWriter{ //private: // string filePath; // string* buffer; // string buffer0; // string buffer1; // nn::os::CriticalSection cs; // nn::os::CriticalSection cs2; // nn::os::Thread thread; // //public: // // コンストラクタ // SimpleSdCardWriter(string filePath, bool append = true) // { // nn::hwtest::SdFileManager::Initialize(1); // cs.Initialize(); // cs2.Initialize(); // this->filePath = filePath; // // if(!append) // { // nn::hwtest::SdFileManager::WriteFile(filePath, ""); // } // // buffer = &buffer0; // }; // // // デストラクタ // virtual ~SimpleSdCardWriter() // { // cs.Finalize(); // cs2.Finalize(); // nn::hwtest::SdFileManager::Finalize(); // }; // // // 書き込みます // virtual void Write(string text) // { // cs.Enter(); // (*buffer)+=text; // cs.Leave(); // // if(cs2.TryEnter()) // { // string* writing; // if(buffer == &buffer0) // { // buffer = &buffer1; // writing = &buffer0; // } // else if(buffer == &buffer1) // { // buffer = &buffer0; // writing = &buffer1; // } // nn::hwtest::SdFileManager::AppendFile(filePath, writing->c_str()); // (*writing) = ""; // cs2.Leave(); // } // }; // // // 改行つきで書き込みます // virtual void WriteLine(string text) // { // Write(text + "\n"); // }; // // virtual void Flush() // { // cs2.Enter(); // nn::hwtest::SdFileManager::AppendFile(filePath, buffer->c_str()); // (*buffer) = ""; // cs2.Leave(); // } //}; // // // ///*!--------------------------------------------------------------------------* // @brief SDカードへ書き込み器 // // *---------------------------------------------------------------------------*/ //class SdCardWriter : public TextWriter //{ //private: // string filePath; // string* buffer; // string buffer0; // string buffer1; // bool append; // bool end; // nn::os::CriticalSection cs; // nn::os::Thread writeThread; // // // 書き込みスレッド関数 // static void WriteThreadFunction(SdCardWriter* sd) // { // static int numOfUser = 0; // static nn::os::CriticalSection writeSection; // // // 初期処理 // numOfUser++; // if(numOfUser == 1) // { // writeSection.Initialize(); // writeSection.Enter(); // nn::hwtest::SdFileManager::Initialize(1); // writeSection.Leave(); // } // else // { // writeSection.Enter(); // writeSection.Leave(); // } // // // ファイルをクリアする // // ToDo: ちゃんとクリアされるようにする。 // if(sd->append == false) // { // writeSection.Enter(); // nn::hwtest::SdFileManager::WriteFile(sd->filePath, ""); // writeSection.Leave(); // } // // // 書き込み処理 // string* writing; // while(!(sd->end)) // { // if(sd->buffer->size() > 0) // { // // バッファを入れ替える // if(sd->buffer == &(sd->buffer0)) // { // sd->buffer = &(sd->buffer1); // writing = &(sd->buffer0); // } // else if(sd->buffer == &(sd->buffer1)) // { // sd->buffer = &(sd->buffer0); // writing = &(sd->buffer1); // } // writeSection.Enter(); // nn::hwtest::SdFileManager::AppendFile(sd->filePath, writing->c_str()); // writeSection.Leave(); // (*writing) = ""; // } // // // 兄弟スレッドの処理を進めるために少しスリープする // nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(100)); // } // // // 終了処理 // if(numOfUser == 1) // { // writeSection.Enter(); // nn::hwtest::SdFileManager::Finalize(); // writeSection.Leave(); // writeSection.Finalize(); // } // numOfUser--; // }; // //public: // // コンストラクタ // SdCardWriter(string filePath, bool append = true) // { // this->filePath = filePath; // this->append = append; // // cs.Initialize(); // // buffer = &buffer0; // // end = false; // writeThread.StartUsingAutoStack(WriteThreadFunction, this, 4096); // }; // // // デストラクタ // virtual ~SdCardWriter() // { // cs.Finalize(); // end = true; // writeThread.Join(); // }; // // // 書き込みます // virtual void Write(string text) // { // cs.Enter(); // (*buffer)+=text; // cs.Leave(); // }; // // // 改行つきで書き込みます // virtual void WriteLine(string text) // { // Write(text + "\n"); // }; //}; /*!--------------------------------------------------------------------------* @brief デバッグ出力器 *---------------------------------------------------------------------------*/ class DebugWriter : public TextWriter{ public: DebugWriter(){}; virtual void Write(string); virtual void WriteLine(string); }; /*!--------------------------------------------------------------------------* @brief 名前出力器 *---------------------------------------------------------------------------*/ class NameWriter : public TextWriter{ public: NameWriter(){}; NameWriter(string name, TextWriter* writer); virtual void Write(string); virtual void WriteLine(string); private: TextWriter* m_Writer; string m_Name; }; /*!--------------------------------------------------------------------------* @brief コンソール *---------------------------------------------------------------------------*/ class Console : public UIControl, public TextWriter { public: Console(); virtual ~Console(); virtual void Write(string); virtual void WriteLine(string); void Clear(); virtual void Show(Display&); int Width; //void SetPosition(Display::Position position); //Display::Position GetPosition(); private: string m_Line; //void Display(); //Display::Position position; RingBuffer* m_Buffer; }; /*!--------------------------------------------------------------------------* @brief テキスト読み取り器 *---------------------------------------------------------------------------*/ class TextReader { public: TextReader(){}; virtual ~TextReader(){}; // ファイルの最後まで読み取ります virtual char* Read(){ return NULL; }; // 一行だけ読み取ります virtual char* ReadLine(){ return NULL; }; private: }; /*!--------------------------------------------------------------------------* @brief テスト用テキスト読み取り器 *---------------------------------------------------------------------------*/ class TestReader : public TextReader { public: TestReader(); virtual ~TestReader(); virtual char* Read(); virtual char* ReadLine(); bool EndOfFile(); private: int current_line; int lineSize; char** line; bool eof; }; /*!--------------------------------------------------------------------------* @brief パーサ *---------------------------------------------------------------------------*/ class Parser { public: Parser(TextReader* reader); virtual ~Parser(); int GetValue(); int GetValue(char* key); void MoveSet(); void MoveSet(char* key); private: char* m_CurrentLine; void ReadNextLine(); void ParseValues(); bool IsValueLine(); bool IsSetLine(); TextReader* m_Reader; KeyValuePair *m_Values[100]; int m_CurrentIndex; int m_MaxIndex; }; /*!--------------------------------------------------------------------------* @brief キーと値のペア *---------------------------------------------------------------------------*/ class KeyValuePair { public: KeyValuePair(); KeyValuePair(char* key,int value); ~KeyValuePair(); const char* GetKey(); void SetKey(char* key); int GetValue(); void SetValue(int value); private: char* m_Key; int m_Value; }; /*!--------------------------------------------------------------------------* @brief リングバッファ *---------------------------------------------------------------------------*/ template class RingBuffer { public: RingBuffer(int size); virtual ~RingBuffer(); int GetSize(); void SetValue(Datatype data); Datatype operator[](int i); private: Datatype* m_Buffer; int m_Size; int m_Next; bool Overwriting; }; /*!--------------------------------------------------------------------------* Name: RingBuffer::RingBuffer @brief コンストラクタです。 @return なし *---------------------------------------------------------------------------*/ template RingBuffer::RingBuffer(int size) { m_Size = size; m_Buffer = new Datatype[size]; Overwriting = false; m_Next = 0; } /*!--------------------------------------------------------------------------* Name: RingBuffer::~RingBuffer @brief デストラクタです。 @return なし *---------------------------------------------------------------------------*/ template RingBuffer::~RingBuffer() { delete[] m_Buffer; } /*!--------------------------------------------------------------------------* Name: RingBuffer::GetSize @brief サイズを取得します。 @return なし *---------------------------------------------------------------------------*/ template int RingBuffer::GetSize(){ if(Overwriting) return m_Size; else return m_Next; } /*!--------------------------------------------------------------------------* Name: RingBuffer::SetValue @brief 値をセットします。 @return なし *---------------------------------------------------------------------------*/ template void RingBuffer::SetValue(Datatype data) { m_Buffer[m_Next] = data; m_Next = (m_Next + 1) % m_Size; if(m_Next == 0) { Overwriting = true; } } /*!--------------------------------------------------------------------------* Name: RingBuffer::operator[] @brief operator[] @return *---------------------------------------------------------------------------*/ template Datatype RingBuffer::operator[](int i) { int start = 0; if(Overwriting) { start = m_Next; } return m_Buffer[(start + i) % m_Size]; } /*!--------------------------------------------------------------------------* @brief 抽象プログラム *---------------------------------------------------------------------------*/ class Program { public: Program(){}; virtual ~Program(){}; virtual void Execute(){}; void WaitButtonB() { KeyPad &key = KeyPad::GetInstance(); while(1) { key.ReadKey(); if(key.IsPress(KeyPad::B)) { break; } nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromNanoSeconds(1)); } } }; /*!--------------------------------------------------------------------------* @brief カウンター *---------------------------------------------------------------------------*/ template class Counter { public: Counter(); virtual ~Counter(){}; virtual void Count(Datatype data); virtual int GetCount(); virtual int GetCount(Datatype data); virtual double GetRate(Datatype data); protected: map counts; int count; }; /*!--------------------------------------------------------------------------* Name: Counter::Counter @brief コンストラクタです。 @return *---------------------------------------------------------------------------*/ template Counter::Counter() { count = 0; } /*!--------------------------------------------------------------------------* Name: Counter::Count @brief カウントします。 @return *---------------------------------------------------------------------------*/ template void Counter::Count(Datatype data) { counts[data]++; count++; } /*!--------------------------------------------------------------------------* Name: Counter::GetCount @brief すべてのカウントを取得します。 @return *---------------------------------------------------------------------------*/ template int Counter::GetCount() { return count; } /*!--------------------------------------------------------------------------* Name: Counter::GetCount @brief 指定したデータのカウントを取得します。 @return *---------------------------------------------------------------------------*/ template int Counter::GetCount(Datatype data) { return counts[data]; } /*!--------------------------------------------------------------------------* Name: Counter::GetRate @brief 指定したデータの割合を取得します。 @return *---------------------------------------------------------------------------*/ template double Counter::GetRate(Datatype data) { return (double)GetCount(data) / (double)GetCount(); } /*!--------------------------------------------------------------------------* @brief 直近カウンター *---------------------------------------------------------------------------*/ template class LastCounter : public Counter { public: LastCounter(int size = 10); virtual void Count(Datatype data); private: int m_Size; list dataList; }; /*!--------------------------------------------------------------------------* Name: LastCounter::LastCounter @brief コンストラクタです。 @return *---------------------------------------------------------------------------*/ template LastCounter::LastCounter(int size) { Counter::count = 0; m_Size = size; } /*!--------------------------------------------------------------------------* Name: LastCounter::Count @brief カウントします。 @return *---------------------------------------------------------------------------*/ template void LastCounter::Count(Datatype data) { dataList.push_back(data); Counter::counts[data]++; Counter::count++; if(Counter::count > m_Size) { Counter::counts[dataList.front()]--; Counter::count--; dataList.pop_front(); } } /*!--------------------------------------------------------------------------* @brief 平均器 *---------------------------------------------------------------------------*/ class Average { public: Average(); virtual ~Average(){}; virtual void Add(double); virtual double GetValue(); protected: double total; int count; }; /*!--------------------------------------------------------------------------* @brief 直近平均器 *---------------------------------------------------------------------------*/ class LastAverage : public Average { public: LastAverage(int size = 10); virtual void Add(double); protected: int size; list lastValues; }; ///*!--------------------------------------------------------------------------* // @brief タイマーハンドラー // // タイマに登録したい処理をExecuteに記述します。 // // *---------------------------------------------------------------------------*/ //class TimerHandler //{ //public: //// TimerHandler(){}; //// virtual ~TimerHandler(){}; // virtual void Execute() = 0; // nn::fnd::TimeSpan Interval; // nn::fnd::TimeSpan NextTime; //}; /*!--------------------------------------------------------------------------* @brief タイマーハンドラー2 タイマに登録したい処理をExecuteに記述します。 *---------------------------------------------------------------------------*/ class TimerHandler2 { private: // nn::os::Timer timer; bool stop; nn::os::Thread thread; nn::fnd::TimeSpan stopTime; static void control(TimerHandler2* handler) { while(!handler->stop) { //handler->timer.Wait(); handler->Execute(); if(handler->stopTime != 0 && nn::os::Tick::GetSystemCurrent().ToTimeSpan() > handler->stopTime) { break; } nn::os::Thread::Sleep(handler->Interval); } }; public: TimerHandler2() { // timer.Initialize(false); }; virtual ~TimerHandler2() { // timer.Finalize(); }; virtual void Start() { this->stopTime = 0; stop = false; //timer.StartPeriodic( 0, Interval ); thread.StartUsingAutoStack(TimerHandler2::control, this, 4096, 0); }; virtual void Start(nn::fnd::TimeSpan time) { this->stopTime = nn::os::Tick::GetSystemCurrent().ToTimeSpan() + time; stop = false; //timer.StartPeriodic( 0, Interval ); thread.StartUsingAutoStack(TimerHandler2::control, this, 4096, 0); }; virtual void Stop() { // timer.Stop(); stop = true; thread.Join(); thread.Finalize(); }; virtual void Execute() = 0; nn::fnd::TimeSpan Interval; }; ///*!--------------------------------------------------------------------------* // @brief タイマーハンドラー2 // // タイマに登録したい処理をExecuteに記述します。 // // *---------------------------------------------------------------------------*/ //class TimerHandler2 //{ //private: // nn::os::Alarm alarm; // static void invoke(TimerHandler2* handler, bool canceled) // { // handler->Execute(); // }; // static bool initialized; //public: // TimerHandler2() // { // if(TimerHandler2::initialized == false) // { // nn::os::InitializeAlarmSystem(); // TimerHandler2::initialized = true; // } // alarm.Initialize(); // }; // virtual ~TimerHandler2() // { // alarm.Finalize(); // }; // // virtual void Start() // { // alarm.SetPeriodic( Interval, invoke, this); // }; // // virtual void Stop() // { // alarm.Cancel(); // }; // // virtual void Execute() = 0; // // nn::fnd::TimeSpan Interval; //}; ///*!--------------------------------------------------------------------------* // @brief タイマー // // *---------------------------------------------------------------------------*/ //class Timer //{ //public: // static void Start(); // static void Start(nn::fnd::TimeSpan); // static void Stop(); // static void RegisterHandler(TimerHandler&); // static void UnregisterHandler(TimerHandler&); // //private: // static bool stop; // static nn::fnd::TimeSpan stopTime; // static void ThreadFunc(uptr param); // static nn::os::Thread thread; // static vector handlers; // Timer(){}; // virtual ~Timer(){}; //}; } /* nakayama */ } /* red */ } /* nn */ #endif /* NAKAYAMA_H_ */