mirror of
https://github.com/rvtr/ctr_card_test.git
synced 2025-10-31 13:51:17 -04:00
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_card_test@1 ff8ce827-af98-4349-adb5-4c00699b5328
1702 lines
36 KiB
C++
1702 lines
36 KiB
C++
/*---------------------------------------------------------------------------*
|
|
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 <vector>
|
|
#include <map>
|
|
#include <list>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
|
|
#include <nn.h>
|
|
#include <nn/os.h>
|
|
#include <nn/fnd.h>
|
|
#include <nn/hw/ioreg.h>
|
|
#include <nn/demo.h>
|
|
//#include "HwTestLib/hwtest_SdFileManager.h"
|
|
|
|
using namespace std;
|
|
|
|
namespace nn{
|
|
namespace red{
|
|
namespace nakayama{
|
|
|
|
|
|
template <typename Datatype>
|
|
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<UIControl*> 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<typename T>
|
|
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<T*> items;
|
|
vector<string> names;
|
|
int current_item;
|
|
int cursor_position;
|
|
char cursor[2];
|
|
virtual string GetItemName(int);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Selector<T>::GetItemName
|
|
|
|
@brief アイテム名を取得します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
string Selector<T>::GetItemName(int index)
|
|
{
|
|
return names[index];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Selector<T>::Selector
|
|
|
|
@brief コンストラクタです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
Selector<T>::Selector() {
|
|
Selected = false;
|
|
Canceled = false;
|
|
current_item = 0;
|
|
cursor_position = 0;
|
|
SetCursor('>');
|
|
CursorSmoothness = 1.5;
|
|
LineHeight = 10;
|
|
FontSize = 8;
|
|
Offset = 10;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Selector<T>::Register
|
|
|
|
@brief アイテムを登録します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void Selector<T>::Register(string name, T& item)
|
|
{
|
|
names.push_back(name);
|
|
items.push_back(&item);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Selector<T>::Register
|
|
|
|
@brief 選択されているアイテムを取得します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
T* Selector<T>::GetSelectedItem()
|
|
{
|
|
if(Selected)
|
|
{
|
|
return items[current_item];
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Selector<T>::Register
|
|
|
|
@brief カーソルをセットします。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void Selector<T>::SetCursor(char cursor)
|
|
{
|
|
this->cursor[0] = cursor;
|
|
this->cursor[1] = '\0';
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Selector<T>::KeyIn
|
|
|
|
@brief キー入力を受け付けます。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void Selector<T>::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<T>::Show
|
|
|
|
@brief 表示します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void Selector<T>::Show(Display& display)
|
|
{
|
|
for(int i=0; i<items.size(); i++)
|
|
{
|
|
int x = GetX(); //Parent->X + 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<typename T>
|
|
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<T> values;
|
|
int currentIndex;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: SelectableValue<T>::SelectableValue
|
|
|
|
@brief コンストラクタです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
SelectableValue<T>::SelectableValue()
|
|
{
|
|
currentIndex = 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: SelectableValue<T>::Add
|
|
|
|
@brief 候補値を追加します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void SelectableValue<T>::Add(T value, bool isDefault)
|
|
{
|
|
if(isDefault)
|
|
{
|
|
currentIndex = values.size();
|
|
}
|
|
|
|
values.push_back(value);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: SelectableValue<T>::GetValue
|
|
|
|
@brief 選択されている値を取得します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
T SelectableValue<T>::GetValue()
|
|
{
|
|
return values[currentIndex];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: SelectableValue<T>::Next
|
|
|
|
@brief 次の候補値を選択します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void SelectableValue<T>::Next(bool loop)
|
|
{
|
|
currentIndex++;
|
|
|
|
if(currentIndex == values.size())
|
|
{
|
|
if(loop)
|
|
{
|
|
currentIndex = 0;
|
|
}
|
|
else
|
|
{
|
|
currentIndex--;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: SelectableValue<T>::Previous
|
|
|
|
@brief 前の候補値を選択します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
void SelectableValue<T>::Previous(bool loop)
|
|
{
|
|
currentIndex--;
|
|
|
|
if(currentIndex < 0)
|
|
{
|
|
if(loop)
|
|
{
|
|
currentIndex = values.size() - 1;
|
|
}
|
|
else
|
|
{
|
|
currentIndex++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: SelectableValue<T>::SelectableValue
|
|
|
|
@brief 文字列を得ます。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
string SelectableValue<T>::ToString()
|
|
{
|
|
ostringstream oss;
|
|
oss << values[currentIndex];
|
|
return oss.str();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
@brief 値選択器
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
class ValueSelector : public Selector<ISelectableValue>
|
|
{
|
|
public:
|
|
ValueSelector(){ max_name_length = 0;};
|
|
virtual ~ValueSelector(){};
|
|
virtual void Register(string, ISelectableValue&);
|
|
virtual void KeyIn(KeyPad&);
|
|
typedef Selector<ISelectableValue > selector;
|
|
|
|
private:
|
|
int max_name_length;
|
|
virtual string GetItemName(int);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
@brief 名前つき値
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
class NamedValue
|
|
{
|
|
public:
|
|
string Name;
|
|
T Value;
|
|
NamedValue(string, T);
|
|
virtual ~NamedValue(){};
|
|
NamedValue(const NamedValue<T>&);
|
|
virtual NamedValue operator=(const NamedValue<T>&);
|
|
};
|
|
|
|
template<typename T>
|
|
ostream& operator<<(ostream& stream, NamedValue<T>& value);
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: NamedValue::NamedValue
|
|
|
|
@breif コンストラクタです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
NamedValue<T>::NamedValue(string name, T value)
|
|
{
|
|
Name = name;
|
|
Value = value;
|
|
}
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: NamedValue::NamedValue
|
|
|
|
@breif コピーコンストラクタです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
NamedValue<T>::NamedValue(const NamedValue<T>& nv)
|
|
{
|
|
Name = nv.Name;
|
|
Value = nv.Value;
|
|
}
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: NamedValue::operator=
|
|
|
|
@breif =のオーバーロードです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
NamedValue<T> NamedValue<T>::operator=(const NamedValue<T>& nv)
|
|
{
|
|
Name = nv.Name;
|
|
Value = nv.Value;
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: operator<<
|
|
|
|
@breif NamedValue用<<のオーバーロードです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template<typename T>
|
|
ostream& operator<<(ostream& stream, NamedValue<T>& 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<string>* 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 <typename Datatype>
|
|
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 <typename Datatype>
|
|
RingBuffer<Datatype>::RingBuffer(int size) {
|
|
m_Size = size;
|
|
m_Buffer = new Datatype[size];
|
|
Overwriting = false;
|
|
m_Next = 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: RingBuffer::~RingBuffer
|
|
|
|
@brief デストラクタです。
|
|
|
|
@return なし
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
RingBuffer<Datatype>::~RingBuffer()
|
|
{
|
|
delete[] m_Buffer;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: RingBuffer::GetSize
|
|
|
|
@brief サイズを取得します。
|
|
|
|
@return なし
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
int RingBuffer<Datatype>::GetSize(){
|
|
if(Overwriting)
|
|
return m_Size;
|
|
else
|
|
return m_Next;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: RingBuffer::SetValue
|
|
|
|
@brief 値をセットします。
|
|
|
|
@return なし
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
void RingBuffer<Datatype>::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 <typename Datatype>
|
|
Datatype RingBuffer<Datatype>::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 <typename Datatype>
|
|
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<Datatype, int> counts;
|
|
int count;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Counter<Datatype>::Counter
|
|
|
|
@brief コンストラクタです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
Counter<Datatype>::Counter()
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Counter<Datatype>::Count
|
|
|
|
@brief カウントします。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
void Counter<Datatype>::Count(Datatype data)
|
|
{
|
|
counts[data]++;
|
|
count++;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Counter<Datatype>::GetCount
|
|
|
|
@brief すべてのカウントを取得します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
int Counter<Datatype>::GetCount()
|
|
{
|
|
return count;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Counter<Datatype>::GetCount
|
|
|
|
@brief 指定したデータのカウントを取得します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
int Counter<Datatype>::GetCount(Datatype data)
|
|
{
|
|
return counts[data];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: Counter<Datatype>::GetRate
|
|
|
|
@brief 指定したデータの割合を取得します。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
double Counter<Datatype>::GetRate(Datatype data)
|
|
{
|
|
return (double)GetCount(data) / (double)GetCount();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
@brief 直近カウンター
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
class LastCounter : public Counter<Datatype>
|
|
{
|
|
public:
|
|
LastCounter(int size = 10);
|
|
virtual void Count(Datatype data);
|
|
private:
|
|
int m_Size;
|
|
list<Datatype> dataList;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: LastCounter<Datatype>::LastCounter
|
|
|
|
@brief コンストラクタです。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
LastCounter<Datatype>::LastCounter(int size)
|
|
{
|
|
Counter<Datatype>::count = 0;
|
|
m_Size = size;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!--------------------------------------------------------------------------*
|
|
Name: LastCounter<Datatype>::Count
|
|
|
|
@brief カウントします。
|
|
|
|
@return
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
template <typename Datatype>
|
|
void LastCounter<Datatype>::Count(Datatype data)
|
|
{
|
|
dataList.push_back(data);
|
|
Counter<Datatype>::counts[data]++;
|
|
Counter<Datatype>::count++;
|
|
|
|
if(Counter<Datatype>::count > m_Size)
|
|
{
|
|
Counter<Datatype>::counts[dataList.front()]--;
|
|
Counter<Datatype>::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<double> 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<TimerHandler*> handlers;
|
|
// Timer(){};
|
|
// virtual ~Timer(){};
|
|
//};
|
|
|
|
|
|
|
|
|
|
|
|
} /* nakayama */
|
|
} /* red */
|
|
} /* nn */
|
|
|
|
#endif /* NAKAYAMA_H_ */
|