[[11_YoshidaY]]~
工事中~
*ご意見等々枠 [#hc39d1e2]
*ご意見等々枠 [#p6bbcf01]
- フォームアプリケーションでは、コピペしてもうまく行きません。WiiYourselfは本を参考にしてデバッグしてみてください。 -- [[11_YoshidaY]] &new{2012-05-18 (金) 01:38:52};

#comment
*Wiiリモコンのぺーじ [#u4e5b215]
*Wiiリモコンのぺーじ [#mce2f993]
#contents
ここではWiiリモコンのページを作って行きたいと思います。~
Wiiリモコンを扱うのはちょっと難儀します。どう使うか理解するまで苦労しました。~
ちょっと今前作ったプログラムがツンしてくるんですが、最近またやり始めようと考えてるんで、期待せずに期待して更新を待っててください。~

**環境・使用物 [#u4e5b215]
**環境・使用物 [#g7b823c0]
-PC:Lenovo PC Windows 7 32bit
-デバイス:Wii Remote (Motion Plus)
-開発環境:Visual C++ 2008, Processing
-やること
--C++(WiiYourself!)でWiiリモコンへアクセス→Processingに加速度データを送信~
--できればWii remote plusのジャイロセンサも使用したい所。4回生春の内にはできるようにします。~

**最低限必要なコード [#u4e5b215]
***WiiYourself!で加速度、ボタンの値を取得する(フォームアプリケーション) [#f655bc73]
**最低限必要なコード [#faf4cf35]
***WiiYourself!で加速度、ボタンの値を取得する(フォームアプリケーション) [#s787955f]
 #pragma once
 
 #include "stdafx.h"
 
 extern SOCKET  sock;
 char sendchar2[1024];
 char sendchar[1024];
 
 namespace WiimoteLib01cpp {
 
 	using namespace System;
 	using namespace System::ComponentModel;
 	using namespace System::Collections;
 	using namespace System::Windows::Forms;
 	using namespace System::Data;
 	using namespace System::Drawing;
 	using namespace WiimoteLib;
 
 	/// <summary>
 	/// Form1 の概要
 	///
 	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
 	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
 	///          変更する必要があります。この変更を行わないと、
 	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
 	///          正しく相互に利用できなくなります。
 	/// </summary>
 	public ref class Form1 : public System::Windows::Forms::Form
 	{
 	public: Wiimote^ wm;  //Wiimoteオブジェクトwmの宣言
 	private: System::Windows::Forms::Button^  button3;
 	private: System::Windows::Forms::Label^  label1;
 	private: System::Windows::Forms::Label^  label2;
 	private: System::Windows::Forms::Label^  label3;
 	private: System::Windows::Forms::Label^  label4;
 	private: System::Windows::Forms::Label^  label5;
 	private: System::Windows::Forms::Label^  label6;
 	private: System::Windows::Forms::Label^  label7;
 	private: System::Windows::Forms::Label^  label8;
 	private: System::Windows::Forms::Label^  label9;
 	private: System::Windows::Forms::Label^  label10;
 	private: System::Windows::Forms::Label^  label11;
 	public: 
 
 	public: 
 	public: int count;  //LED用の変数countの宣言
 	public:
 		Form1(void)
 		{
 			Control::CheckForIllegalCrossThreadCalls = false;
 			wm = gcnew Wiimote();
 			InitializeComponent();
 			//
 			//TODO: ここにコンストラクタ コードを追加します
 			//
 			wm->WiimoteChanged +=
 				gcnew System::EventHandler<WiimoteChangedEventArgs^>(
 				this, &Form1::wm_WiimoteChanged);
 			wm->SetReportType(InputReport::Buttons, true);
 			wm->SetReportType(InputReport::ButtonsAccel, true);
 			wm->Connect();
 		}
 	public:
 	void wm_WiimoteChanged(Object^
 		sender,WiimoteLib::WiimoteChangedEventArgs^ args){
 		WiimoteState^ ws;
 
 		//ここからポチコード
 		float AX, AY, AZ;
 		int Abtn, Bbtn, Plus, Minus, One, Two, Home;
 		//ボタンが何も押されていない状態ではは0(off)とする
 		AX = AY = AZ = 0.0;
 		Abtn = Bbtn = Plus = Minus = One = Two = Home =  0;
 
  		ws = args->WiimoteState;
 		
 		AX = (ws->AccelState.Values.X);
 		AY = (ws->AccelState.Values.Y);
 		AZ = (ws->AccelState.Values.Z);
 
 		//Aボタンが押されたら0→1
 		if(ws->ButtonState.A) Abtn = 1;
 		//Bボタンが押されたら0→1
 		if(ws->ButtonState.B) Bbtn = 1;
 		//Homeボタンが押されたら0→1
 		if(ws->ButtonState.Home) Home = 1;
 		//+ボタンが押されたら1を足す
 		if(ws->ButtonState.Plus) Plus++;
 		//-ボタンが押されたら1を引く
 		if(ws->ButtonState.Minus) Minus--;
 		//1ボタンが押されたら0→1
 		if(ws->ButtonState.One) One = 1;
 		//2ボタンが押されたら0→1
 		if(ws->ButtonState.Two) Two = 1;
 
 		sendchar2[0] = '\0';
 		 //文字列をsendchar(送る変数)に代入
 		sprintf_s(sendchar, "%f %f %f %d %d %d %d %d %d %d \n", AX, AY, AZ, Abtn, Bbtn, Home, Plus, Minus, One, Two);
 		strcat_s(sendchar2,sendchar);
 		send(sock, sendchar2, strlen(sendchar2), 0);
  		//ここまでポチコード
 
 		this->DrawForms(ws);
 	}
 	public:
 		void DrawForms(WiimoteState^ ws) {
 			this->label1->Text = "Button A:" + (ws->ButtonState.A);
 			this->label2->Text = "Button B:" + (ws->ButtonState.B);
 			this->label3->Text = "Button 1:" + (ws->ButtonState.One);
 			this->label4->Text = "Button 2:" + (ws->ButtonState.Two);
 			this->label5->Text = "Button ↓:" + (ws->ButtonState.Down);
 			this->label6->Text = "Button ↑:" + (ws->ButtonState.Up);
 			this->label7->Text = "Button ←:" + (ws->ButtonState.Left);
 			this->label8->Text = "Button →:" + (ws->ButtonState.Right);
 			this->label9->Text = "X軸:" + (ws->AccelState.Values.X);
 			this->label10->Text = "Y軸:" + (ws->AccelState.Values.Y);
 			this->label11->Text = "Z軸:" + (ws->AccelState.Values.Z);
 			
 	}
 
 	protected:
 		/// <summary>
 		/// 使用中のリソースをすべてクリーンアップします。
 		/// </summary>
 		~Form1()
 		{
 			if (components)
 			{
 				delete components;
 			}
 		}
 	private: System::Windows::Forms::Button^  button1;
 	private: System::Windows::Forms::Button^  button2;
 	protected: 
 
 	private:
 		/// <summary>
 		/// 必要なデザイナ変数です。
 		/// </summary>
 		System::ComponentModel::Container ^components;
 
 #pragma region Windows Form Designer generated code
 		/// <summary>
 		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
 		/// コード エディタで変更しないでください。
 		/// </summary>
 		void InitializeComponent(void)
 		{
 			this->button1 = (gcnew System::Windows::Forms::Button());
 			this->button2 = (gcnew System::Windows::Forms::Button());
 			this->button3 = (gcnew System::Windows::Forms::Button());
 			this->label1 = (gcnew System::Windows::Forms::Label());
 			this->label2 = (gcnew System::Windows::Forms::Label());
 			this->label3 = (gcnew System::Windows::Forms::Label());
 			this->label4 = (gcnew System::Windows::Forms::Label());
 			this->label5 = (gcnew System::Windows::Forms::Label());
 			this->label6 = (gcnew System::Windows::Forms::Label());
 			this->label7 = (gcnew System::Windows::Forms::Label());
 			this->label8 = (gcnew System::Windows::Forms::Label());
 			this->label9 = (gcnew System::Windows::Forms::Label());
 			this->label10 = (gcnew System::Windows::Forms::Label());
 			this->label11 = (gcnew System::Windows::Forms::Label());
 			this->SuspendLayout();
 			// 
 			// button1
 			// 
 			this->button1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(128)));
 			this->button1->Location = System::Drawing::Point(12, 312);
 			this->button1->Name = L"button1";
 			this->button1->Size = System::Drawing::Size(75, 27);
 			this->button1->TabIndex = 0;
 			this->button1->Text = L"VibON";
 			this->button1->UseVisualStyleBackColor = true;
 			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
 			// 
 			// button2
 			// 
 			this->button2->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(128)));
 			this->button2->Location = System::Drawing::Point(84, 312);
 			this->button2->Name = L"button2";
 			this->button2->Size = System::Drawing::Size(75, 27);
 			this->button2->TabIndex = 1;
 			this->button2->Text = L"VibOFF";
 			this->button2->UseVisualStyleBackColor = true;
 			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
 			// 
 			// button3
 			// 
 			this->button3->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(128)));
 			this->button3->Location = System::Drawing::Point(164, 312);
 			this->button3->Name = L"button3";
 			this->button3->Size = System::Drawing::Size(184, 27);
 			this->button3->TabIndex = 2;
 			this->button3->Text = L"LED";
 			this->button3->UseVisualStyleBackColor = true;
 			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
 			// 
 			// label1
 			// 
 			this->label1->AutoSize = true;
 			this->label1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label1->ForeColor = System::Drawing::Color::White;
 			this->label1->Location = System::Drawing::Point(9, 8);
 			this->label1->Name = L"label1";
 			this->label1->Size = System::Drawing::Size(78, 27);
 			this->label1->TabIndex = 3;
 			this->label1->Text = L"label1";
 			// 
 			// label2
 			// 
 			this->label2->AutoSize = true;
 			this->label2->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label2->ForeColor = System::Drawing::Color::White;
 			this->label2->Location = System::Drawing::Point(9, 36);
 			this->label2->Name = L"label2";
 			this->label2->Size = System::Drawing::Size(78, 27);
 			this->label2->TabIndex = 4;
 			this->label2->Text = L"label2";
 			// 
 			// label3
 			// 
 			this->label3->AutoSize = true;
 			this->label3->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label3->ForeColor = System::Drawing::Color::White;
 			this->label3->Location = System::Drawing::Point(9, 64);
 			this->label3->Name = L"label3";
 			this->label3->Size = System::Drawing::Size(78, 27);
 			this->label3->TabIndex = 5;
 			this->label3->Text = L"label3";
 			this->label3->Click += gcnew System::EventHandler(this, &Form1::label3_Click);
 			// 
 			// label4
 			// 
 			this->label4->AutoSize = true;
 			this->label4->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label4->ForeColor = System::Drawing::Color::White;
 			this->label4->Location = System::Drawing::Point(9, 91);
 			this->label4->Name = L"label4";
 			this->label4->Size = System::Drawing::Size(78, 27);
			this->label4->TabIndex = 6;
 			this->label4->Text = L"label4";
 			this->label4->Click += gcnew System::EventHandler(this, &Form1::label4_Click);
 			// 
 			// label5
 			// 
 			this->label5->AutoSize = true;
 			this->label5->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label5->ForeColor = System::Drawing::Color::White;
 			this->label5->Location = System::Drawing::Point(9, 116);
 			this->label5->Name = L"label5";
 			this->label5->Size = System::Drawing::Size(78, 27);
 			this->label5->TabIndex = 8;
 			this->label5->Text = L"label5";
 			this->label5->Click += gcnew System::EventHandler(this, &Form1::label5_Click);
 			// 
 			// label6
 			// 
 			this->label6->AutoSize = true;
 			this->label6->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label6->ForeColor = System::Drawing::Color::White;
 			this->label6->Location = System::Drawing::Point(9, 141);
 			this->label6->Name = L"label6";
 			this->label6->Size = System::Drawing::Size(78, 27);
 			this->label6->TabIndex = 7;
 			this->label6->Text = L"label6";
 			this->label6->Click += gcnew System::EventHandler(this, &Form1::label6_Click);
 			// 
 			// label7
 			// 
 			this->label7->AutoSize = true;
 			this->label7->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label7->ForeColor = System::Drawing::Color::White;
 			this->label7->Location = System::Drawing::Point(9, 165);
 			this->label7->Name = L"label7";
 			this->label7->Size = System::Drawing::Size(78, 27);
 			this->label7->TabIndex = 9;
 			this->label7->Text = L"label7";
 			this->label7->Click += gcnew System::EventHandler(this, &Form1::label7_Click);
 			// 
 			// label8
 			// 
 			this->label8->AutoSize = true;
 			this->label8->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label8->ForeColor = System::Drawing::Color::White;
 			this->label8->Location = System::Drawing::Point(9, 192);
 			this->label8->Name = L"label8";
 			this->label8->Size = System::Drawing::Size(78, 27);
 			this->label8->TabIndex = 10;
 			this->label8->Text = L"label8";
 			this->label8->Click += gcnew System::EventHandler(this, &Form1::label8_Click);
 			// 
 			// label9
 			// 
 			this->label9->AutoSize = true;
 			this->label9->BackColor = System::Drawing::Color::Maroon;
 			this->label9->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label9->ForeColor = System::Drawing::Color::White;
 			this->label9->Location = System::Drawing::Point(9, 221);
 			this->label9->Name = L"label9";
 			this->label9->Size = System::Drawing::Size(78, 27);
 			this->label9->TabIndex = 13;
 			this->label9->Text = L"label9";
			this->label9->Click += gcnew System::EventHandler(this, &Form1::label9_Click);
 			// 
 			// label10
 			// 
 			this->label10->AutoSize = true;
 			this->label10->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label10->ForeColor = System::Drawing::Color::White;
 			this->label10->Location = System::Drawing::Point(9, 251);
 			this->label10->Name = L"label10";
 			this->label10->Size = System::Drawing::Size(92, 27);
 			this->label10->TabIndex = 12;
 			this->label10->Text = L"label10";
 			this->label10->Click += gcnew System::EventHandler(this, &Form1::label10_Click);
 			// 
 			// label11
 			// 
 			this->label11->AutoSize = true;
 			this->label11->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
 			this->label11->ForeColor = System::Drawing::Color::White;
 			this->label11->Location = System::Drawing::Point(9, 278);
 			this->label11->Name = L"label11";
 			this->label11->Size = System::Drawing::Size(92, 27);
 			this->label11->TabIndex = 11;
 			this->label11->Text = L"label11";
 			this->label11->Click += gcnew System::EventHandler(this, &Form1::label11_Click);
 			// 
 			// Form1
 			// 
 			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
 			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
 			this->BackColor = System::Drawing::Color::Maroon;
 			this->ClientSize = System::Drawing::Size(353, 343);
 			this->Controls->Add(this->label9);
 			this->Controls->Add(this->label10);
 			this->Controls->Add(this->label11);
 			this->Controls->Add(this->label8);
 			this->Controls->Add(this->label7);
 			this->Controls->Add(this->label5);
 			this->Controls->Add(this->label6);
 			this->Controls->Add(this->label4);
 			this->Controls->Add(this->label3);
 			this->Controls->Add(this->label2);
 			this->Controls->Add(this->label1);
 			this->Controls->Add(this->button3);
 			this->Controls->Add(this->button2);
 			this->Controls->Add(this->button1);
 			this->Name = L"Form1";
 			this->Text = L"WiiServer";
 			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
 			this->ResumeLayout(false);
 			this->PerformLayout();
 
 		}
 #pragma endregion
 	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 				 wm->SetRumble(true);
 			 }
 	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
 				 wm->SetRumble(false);
 			 }
 	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
 				 button3->Text = "wm->SetLEDs(" + count + ")を表示中";
 				 wm->SetLEDs(count);
 				 count++;
 			 }
 	private: System::Void Form1_Load(System::Object^  sender,  System::EventArgs^  e) {
 			 }
 private: System::Void label5_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label8_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label7_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label6_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label12_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label3_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label10_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label11_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label4_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 private: System::Void label9_Click(System::Object^  sender, System::EventArgs^  e) {
 		 }
 };
 }

***Socket通信で情報を送るコード [#x6efb9b3]
***Socket通信で情報を送るコード [#td5f03b7]
 // WiimoteLib01cpp.cpp : メイン プロジェクト ファイルです。
 #include "stdafx.h"
 #include "Form1.h"
 #pragma     comment(lib,"ws2_32.lib")
 using namespace WiimoteLib01cpp;
 using namespace std;
 using namespace System;
 
 SOCKET sock;
 
 [STAThreadAttribute]
 int main(array<System::String ^> ^args)
 {
 	WSADATA	wsaData;
 	SOCKET  sock0;
 	struct  sockaddr_in addr;
 	struct  sockaddr_in client;
 	int	len;
 	
     System::Windows::Forms::MessageBox::Show("OKを押した後にClient側のプログラムを起動してください");
 
 	// winsock2の初期化
     if (WSAStartup(MAKEWORD(2,0), &wsaData))
     {   
  	   System::Windows::Forms::MessageBox::Show("winsock の初期化に失敗しました");
 	   getchar();
 	   return 1;
     }
 
 	// ソケットの作成
 	sock0= socket(AF_INET,SOCK_STREAM,0);
     if(sock0 == INVALID_SOCKET)
 	{   
 		System::Windows::Forms::MessageBox::Show("socket failed");
 		getchar();
 		return 1;
 	}
 	// ソケットの設定
 	addr.sin_family = AF_INET;
 	addr.sin_port = htons(54321);
 	addr.sin_addr.S_un.S_addr = INADDR_ANY;
 	bind(sock0, (struct sockaddr *)&addr, sizeof(addr));
 	System::Windows::Forms::MessageBox::Show("TCP クライアントからの接続要求を待っています");
 	// TCPクライアントからの接続要求を待てる状態にする
 	listen(sock0, 6);
 
 	// TCPクライアントからの接続要求を受け付ける
 	len = sizeof(client);
 	sock = accept(sock0, (struct sockaddr *)&client, &len);
 
 	if(sock > 0) {
 		// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
 		Application::EnableVisualStyles();
 		Application::SetCompatibleTextRenderingDefault(false); 
 
 		// メイン ウィンドウを作成して、実行します
 		Application::Run(gcnew Form1());
 
 	}
 	// TCPセッションの終了
 	closesocket(sock);
 	// winsock2の終了処理
 	closesocket(sock0);
 	WSACleanup();
  	return 0;
 }

**参考リンク [#u4e5b215]
**参考リンク [#l9d6d45e]
-[[Johnny Chung Lee > Projects > Wii:http://johnnylee.net/projects/wii/]]~
-[[WiiRemoteプログラミング:http://ssl.ohmsha.co.jp/cgi-bin/menu.cgi?ISBN=978-4-274-06750-1]]
-[[WiiYourself!でモーションプラスを使い角度を算出してみる(単純積分):http://www.cg-ya.net/i-media/wiiremote/wiiyourself_motionplus_basic.html]]

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS