バランスWiiボード(Wii Fit)のボタンとLEDの使用方法です。
重さセンサを用いた重心や重さの計測は前回の記事を参照してください。
重さセンサを用いた重心や重さの計測は前回の記事を参照してください。
1.WiimoteLibの準備
Wiiリモコンと同じく、WiimoteLibの準備を行います。
2.フォームの作成
1.WiiFitLEDというフォームプロジェクトを作成し、ボタンを2つフォームに貼り付けます。
![]()
2.各ボタンをダブルクリックし、ボタンクリックイベントを登録します。
3.WiiFitLEDデモプログラム
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WiimoteLib; namespace WiiFitLED { public partial class Form1 : Form { private Wiimote wm = new Wiimote(); private Boolean isPush; private Boolean isOn; public Form1() { InitializeComponent(); //他スレッドからのコントロール呼び出し許可 Control.CheckForIllegalCrossThreadCalls = false; this.button2.Enabled = false;//切断ボタンを無効 } private void button1_Click(object sender, EventArgs e) { //接続 try { this.wm.Connect(); this.wm.WiimoteChanged += new EventHandler<WiimoteChangedEventArgs>(wm_WiimoteChanged); this.button1.Enabled = false;//接続ボタンを無効 this.button2.Enabled = true;//切断ボタンを有効 } catch { System.Windows.Forms.MessageBox.Show("接続できません"); } } //接続 void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs e) { if (this.wm.WiimoteState.ButtonState.A)//ボタンが押されているなら { if (this.isPush == false)//最初のボタンの変化時のみ処理を行う { if (this.isOn == false)//LEDがオフなら { //LEDをオンにする this.wm.SetLEDs(1); this.isOn = true; this.label1.Text = "LED:オン"; } else { //LEDをオフにする this.wm.SetLEDs(0); this.isOn = false; this.label1.Text = "LED:オフ"; } this.isPush = true; } } else { this.isPush = false; } return; } //切断 private void button2_Click(object sender, EventArgs e) { this.wm.Disconnect(); this.button1.Enabled = true;//接続ボタンを有効 this.button2.Enabled = false;//切断ボタンを有効 } } }
実行
1.WiiFitを接続してください。接続の方法はWiiリモコンと同じです。
2.F5キーを押して実行してください。
※もしエラーが発生する場合はWiiFitが正しく接続されているか確認してください。3.フォームの接続ボタンを押し、接続します。
4.WiiFitのボタンを押します。LEDが点灯し、ラベルがオンになります。
![]()
![]()
5.再びボタンを押すと、LEDが消灯し、ラベルがオフになります。
![]()
6.終了する際は、切断を押しフォームを閉じてください。
解説
ボタンをおすことで、LEDの状態が変化します。
WiimoteState.ButtonState.AWiiFitのボタンが押されているとtrue、押されていないときはfalseを返します。wm.SetLEDs(X)
最初に押したときの1回のみを処理したい場合は、サンプルプログラムのようにbooleanを使って処理します。LEDを点灯する場合は1、消灯する場合は0を引数に渡します。Wiiリモコンと同じメソッドですが、WiiFitにはLEDが1つしかないため、0か1のみとなります。

