CでOOP

Switchクラス

/* 状態名 */
typedef enum
{
	eOFF,
	eON
} ESW_STATE;

/* Switchクラス */
typedef struct
{
	ESW_STATE state;
	Lamp *target;
} SWITCH;

/* 生成 */
SWITCH *SwitchCreate(void)
{
	SWITCH *this;
	this = (SWITCH *)malloc(sizeof(SWITCH));
	
	this->target = 0;/* 属性の初期化 */
	this->state = eOFF;
	return this;
}

/* オン */
void SwitchOn(SWITCH *this)
{
	if(this->state == eON)	{ return; }
	
	LampOn(this->target);
	this->state = eON;
}

/* オフ */
void SwitchOff(SWITCH *this)
{
	if(this->state == eOFF)	{ return; }
	
	LampOff(this->target);
	this->state = eOFF;
}

/* 関連の結合 */
void SwitchSetTarget(SWITCH *this, Lamp* obj)
{
	this->target = obj;
}


Lamp* SwitchGetTarget(SWITCH *this)
{
	return (this->target);
}

とりあえずこんな感じ。
最近、シリアルで4Hzごとデータを受信し、計測時間を設定してデータの平均値をLCDに出力するファームを作ったのだけど、そいつを全部OOPで書き直そうと考えている。

  • ハードウェアラッピング
  • UML(っぽい)関連付け
  • C++に向けたキレイなOOPコーディング

この辺を上達させるつもり!