Sunday, January 11, 2009

Sending Commands to a Detached Screen Session

A quick note about sending commands to a detached screen session, good for using in start up scripts:

Example:

> screen -dmS [session name]
> screen -S [session name] -p 0 -X stuff [command^M]

where:
-d -m , means detach the session immediately, but fork off a new process, like a standard screen incarnation
-S sets a more human readable / sensible name for the screen session

-p 0 sets the targetted window within the screen session to be 0, neccessary if you haven't attached and detached from the session yet.

-X stuff sends the command to the nominated screen session. Not sure what the "stuff" part does.

Tuesday, December 30, 2008

Using Python Urllib2 to retrieve quotes from bash.org

This is a simple (read brain dead) script I wrote to pull down specified quotes from bash.org (the IRC Quote Database). The script is below and should be fairly self explanatory. The only caveat is that you may not want to replace the web specific characters if you plan on using this to put random quotes onto a web page.
'''
bashquote.py

A simple python script to retrieve quotes from the
IRC quote database, www.bash.org.
'''

import urllib2

bash_str = 'http://www.bash.org/?'

rand_str = 'http://www.bash.org/?random'
findstr = '

'
endstr = '

'


def get_quote(num):
'''
Returns the specified quote from bash.org
'''

buff = "".join(urllib2.urlopen(bash_str+str(num)).readlines())

# Check to see if the quote was rejected
if (buff.find('Quote #'+str(num)+' was rejected') != -1):

return ''

if (buff.find('Quote #'+str(num)+ ' does not exist') != -1):

return ''

st = buff.find(findstr)
en = st + buff[st:].find(endstr)

quote = buff[st+len(findstr):en]

return dodgy_char_swap(quote)

def get_random(posrating=1):

'''
Returns a random quote from bash.org

Posrating = 1: Only select from quotes with rating > 0
= 0: Select from all quotes.
'''

if posrating != 1:
buff = "".join(urllib2.urlopen(rand_str+'1').readlines())

else:
buff = "".join(urllib2.urlopen(rand_str).readlines())

st = buff.find(findstr)
en = st + buff[st:].find(endstr)

quote = buff[st+len(findstr):en]

return dodgy_char_swap(quote)

def dodgy_char_swap(quote):
'''

Swaps out some dodgy characters, using the replace
method of the string class.
'''
quote = quote.replace('<br>','') # line break

quote = quote.replace('&lt;','<') # less than

quote = quote.replace('>','>') # greater than

quote = quote.replace('\r\n','\n') # line feed

return quote

Sunday, October 26, 2008

Using a Wiimote and GlovePIE to control Neverball on Windows

This is only for Windows at the moment, I have yet to try this on Linux

You will need:
  1. A Wiimote and a bluetooth connection on your computer. I used the Toshiba BT stack with my Dell D430.
  2. Neverball (official page here, I downloaded it from here since the links on the offical page were broken for me.)
  3. GlovePIE which is glue software for connecting the Wiimote to various input devices like keyboards, mice and joypads.
  4. PPJoy, a program which allows the creation of virtual joysticks which GlovePIE can control.
The first thing to do is to pair your Wiimote with the bluetooth stack on your computer. To put your Wiimote into discovery mode, press the 1+2 buttons on the controller, until the player number LEDs all start flashing. Run your bluetooth software, and search for a new device. It should find a Human Interface Device (HID) with a name which contains "Nintendo". Pair it with this device. On my computer no pairing code was needed, but if it requests one, try "0000".

Now we have our Wiimote talking to the computer, we need to install the software to make the Wiimote talk to Neverball.

First install PPJoy, making sure to continue when Windows complains that the drivers aren't signed. PPJoy was originally designed to allow Parallel Port joysticks to be used under windows, but we are going to be using it's virtual joystick functionality to make the operating system think it has a joystick installed while we emulate the joypad with GlovePIE.

After the PPJoy has been installed run the PPJoy control panel and add a virtual joystick with 8 axes, 32 buttons and 2 POV hats (for Neverball we only need a few of these, but it will be good to make them all available to GlovePIE for later extensions). After the joystick has been created, be sure to go to "Game Controllers" in the control panel and make the new joystick your default. Neverball automatically uses the default controller (though it is possible to change through a settings file)

Now we can install and run GlovePIE. GlovePIE is a fantastic program for interfacing many different inputs to many different outputs. If you have time it is a powerful program, a lot of the basics of GlovePIE are outlined here. For now though we will just use a script which can be found here. Download and open the script in GlovePIE. Hit run and you should see the debug panel contain something like: "X: -0.13 ; Z: 0.04". These number correspond to the roll and pitch of your Wiimote. You can change the calibration numbers in the first two lines to compensate for any offsets. They debug window should read 0 for each axis when the Wiimote is place on a flat, level surface.

So now we have the Wiimote talking to the Bluetooth stack which is talking to GlovePIE which in turn is talking to the joypad through PPJoy. Now we just need to run Neverball. I have read reports of Neverball working out of the box with joypads, but in order for me to get it to recognise the joypad I had to change a line in the Neverball configuration file.

The file is found in "[Neverball Installation Directory]/data/.neverball/neverballrc". The line we want to change is:

joystick 0

which we change to

joystick 1

Now save the file, and open up Neverball.

The Wiimote should be mapped so that the axes rotate the board, while the B button is "mouse click" and the A button is ESC. The D-pad is also mapped as the arrow keys. The camera can be rotated using - and +.

Here is an example of someone (not me) using a Wiimote with Neverball.