Vizible’s Blog

Python – Get Wiimote signal by spawning another process

Posted in Programming, Python by wearetherock on August 1, 2009

1. Connect Wiimote using C#
=====================================

using WiimoteLib;
using System.Windows.Forms;
using System;

public class WiiSignal {
	private Wiimote wm;
	public WiiSignal() {
		wm = new Wiimote();
		wm.WiimoteChanged += new WiimoteChangedEventHandler(wm_WiimoteChanged);
		try {
			wm.Connect();
			wm.SetReportType(Wiimote.InputReport.IRAccel, true);
			wm.SetLEDs(true, false, false, true);
		}catch(Exception x){
			MessageBox.Show("Please connect Wiimote");
			System.Environment.Exit(0);
		}
	}
	private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args){
		WiimoteState ws = args.WiimoteState;
		WiimoteLib.ButtonState buttonState = ws.ButtonState;
		if (buttonState.A){
			Console.WriteLine("A");
		}else if (buttonState.B){
			Console.WriteLine("B");
		}else if (buttonState.Down){
			Console.WriteLine("DOWN");
		}else if (buttonState.Up){
			Console.WriteLine("UP");
		}else if (buttonState.Home){
			Console.WriteLine("HOME");
		}else if (buttonState.Left){
			Console.WriteLine("LEFT");
		}else if (buttonState.Right){
			Console.WriteLine("RIGHT");
		}else if (buttonState.Plus){
			Console.WriteLine("PLUS");
		}else if (buttonState.Minus){
			Console.WriteLine("MINUS");
		}else if (buttonState.One){
			Console.WriteLine("ONE");
		}else if (buttonState.Two){
			Console.WriteLine("TWO");
		}else{
			Console.WriteLine("RELEASE");
		}
	}
	public void Disconnect(){
		wm.Disconnect();
	}
	public static void Main(String[] args){
		WiiSignal ws = new WiiSignal();
		while(true){}
	}
}

2. Compile (need WiimoteLib.dll from codeplex)
=====================================
> csc /r:WiimoteLib.dll WiiSignal.cs

~
3. Spawning C# process
=====================================

import time
import sys
import subprocess as subp
proc = subp.Popen('WiiSignal.exe', stdin=subp.PIPE, stdout=subp.PIPE, bufsize=0)
while True :
	#time.sleep(5)
	line = proc.stdout.readline().strip()
	print line	
	if line=="B" :
		sys.exit(0);
	if not line :
		sys.exit(0);

~

Advertisements

3 Responses

Subscribe to comments with RSS.

  1. wearetherock said, on August 2, 2009 at 7:28 am

    Simulate key press for UP,DOWN, LEFT and RIGHT button.

    import time
    import sys
    import subprocess as subp
    import win32com.client
    
    proc = subp.Popen('WiiSignal.exe', stdin=subp.PIPE, stdout=subp.PIPE, bufsize=0)
    shell = win32com.client.Dispatch("WScript.Shell")
    
    while True :
    	#time.sleep(5)
    	line = proc.stdout.readline().strip()
    	print line	
    	if line=="B" :
    		sys.exit(0);
    	elif not line :
    		sys.exit(0);
    	elif line == "UP" :
    		shell.SendKeys("{UP}")
    	elif line == "DOWN" :
    		shell.SendKeys("{DOWN}")
    	elif line == "LEFT" :
    		shell.SendKeys("{LEFT}")
    	elif line == "RIGHT" :
    		shell.SendKeys("{RIGHT}")
    
  2. wearetherock said, on August 2, 2009 at 9:23 am

    Prevent repeat key (one press release many same event)

    import time
    import sys
    import subprocess as subp
    import win32com.client
    
    proc = subp.Popen('WiiSignal.exe', stdin=subp.PIPE, stdout=subp.PIPE, bufsize=0)
    shell = win32com.client.Dispatch("WScript.Shell")
    
    previousKey = "RELEASE"
    
    def check(key):
    	global previousKey
    	
    	if previousKey == key:
    		return False
    	else:
    		previousKey = key
    		return True
    
    while True :
    	#time.sleep(5)
    	line = proc.stdout.readline().strip()
    	
    	if not check(line) :
    		continue
    		
    	print line	
    	if line=="B" :
    		sys.exit(0);
    	elif not line :
    		sys.exit(0);
    	elif line == "UP" :
    		shell.SendKeys("{UP}")
    	elif line == "DOWN" :
    		shell.SendKeys("{DOWN}")
    	elif line == "LEFT" :
    		shell.SendKeys("{LEFT}")
    	elif line == "RIGHT" :
    		shell.SendKeys("{RIGHT}")
    
    
  3. wearetherock said, on August 2, 2009 at 10:11 am

    CLRL + A

    	elif line == "A":
    		shell.SendKeys("""^a""")
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: