Archive for the ‘Python’ Category

Using PS3 BD Remote with Ubuntu Linux - Part 2

Posted on May 29th, 2009 in PS3, Python | 3 Comments »

Here it is. Part 2 of using PS3 remote with Ubuntu. In this part I will finally show you how to do something useful with remote. So, here is what we want to control with remote:

  • multimedia keys: previos/next song, up/down volume
  • cycle thru opened windows with alt-tab
  • open/close applications (music player, tv application, …)
  • whatever else you want and is controllable with keyboard or from script

Let’s get to work. I assume you have done all the “work” from part 1. If not, do it now. Next, we need library to emulate keyboard keypresses:


sudo apt-get install python-virtkey

I wrote a small wrapper around python-virtkey for easier using. Download it from github. Also, download all other scripts from there because we gonna need them in couple of minutes.

Using VirtualKey:
You’ll be using three methods from VirtualKey: press_key, hold_key, release_key, toggle_key. Hold_key is method for pressing some key and holding it pressed. Release_key is for releasing pressed key. Press_key is like you pressed some key and immediately after that released it. Small examples:


from virtualkey import VirtualKey
keys = VirtualKey()
# pressing "a" button
keys.press_key("a")
# pressing ctrl-c (holding control while pressing "c")
keys.press_key(["ctrl_left", "c"])
# holding alt and pressing tab
keys.toggle_key("alt_left")
keys.press_key("tab");
keys.press_key("tab");
keys.toggle_key("alt_left")

You can try example script (also from ps3bdremote github) which demonstrate how to use VirtualKey:


python virtualkey-test.py

Using remote:
ps3bdremote-thumbFinally, let’s map keys to remote so we can use it. On the left picture (click to zoom it) you can see how I mapped keys. You can, of course, change it as you like.

Enough talking, let’s test it!


python ps3-remote.py

You can add additional “actions”:


elif command == "stop":
   keys.press_key("xf86audiostop")

All key definitions are in virtualkey.py file.

Known issues:

  • pairing - you still need to pair with remote before using. If you know how to solve this, please help :) and leave a comment
  • alt+tab - you’ll have to use two keys for alt+tab. First, press square as this holds alt key. After that, press triangle which simulates pressing tab key. Press square again to release pressed alt. This has to be done this way because there is no information from remote what key is released, just that some key is released

So, this is it. Download scripts from github and enjoy. Again, your feedback is very appreciated. If you like or hate this script(s), leave a comment ;) Also, If you want to help and improve scripts. Thanks!

Using PS3 BD Remote with Ubuntu Linux

Posted on March 4th, 2009 in Python, Ubuntu | 14 Comments »

(There is now 2nd part of this post. Check it out.)

So, you want to use PS3 BD (bluetooth) remote with your computer which is running Ubuntu. Great. Here you can find all the information needed to accomplish this. All the scripts are written by me so your feedback is very appreciated. I’m using D-Link DBT-122 Bluetooth adapter (all bluetooth adapters should work) with already mentioned PS3 BD Remote (Model CECHZR1E) on Ubuntu 8.10 (Intrepid Ibex).

First of all, you need to install all the required applications:


sudo apt-get install bluez-utils python-bluez

Next, we need to find out bluetooth address of remote. On remote press start+enter and on computer start hcitool for scanning bluetooth devices:

hcitool scan

You will get output similar to this:


Scanning ...
	00:82:7C:B9:14:7A	BD Remote Control

Copy address of remote as we will need it in a second. If the scanning command provide no results, you should try it several times.
Next, download required scripts and extract them:


wget http://h00s.net/ps3bdremote.tar.gz
tar xvzf ps3bdremote.tar.gz
cd ps3bdremote

Now open ps3bdremote-test.py file (gedit ps3bdremote-test.py) and change address of remote (approx. 27th line) to match the address you did get with hcitool:


remote = ps3bdremote.PS3BDRemote("00:82:7C:B9:14:7A")

That’s all configuring. Let’s test it. On remote again press start+enter and start the testing script:


python ps3bdremote-test.py

Output should be similar to this:


Trying to connect to PS3 BD Remote...
Trying to connect to PS3 BD Remote...
Trying to connect to PS3 BD Remote...
Connected

If you have problems connecting to remote, terminate script. Press start+enter and immediatly after that start testing script. Eventually, it will connect with remote. After that press buttons on your remote and it will display pressed keys:


audio
releasedbutton
play
releasedbutton
eject
releasedbutton

That’s it. Now it would be cool if you could use these buttons to play/pause player on your computer, start tvtime etc. Well, you can. I will explain how in next post. To be continued :)

Scripts: ps3bdremote.tar.gz

Python + sqlite3 bug

Posted on July 14th, 2007 in Python, sqlite | No Comments »

Nakon 1h čupanja kose i provjere gdje je greška u najobičnijem INSERT queryu, čini se da sam naletio na bug u Pythonu i dbapi2-u za sqlite3. Problem je kod unosa podataka u tablicu (INSERT INTO). Naime, nakon inserta a prije zatvaranja connectiona potrebno je pozvati commit() metodu. Nešto kao ovo:
connection = sqlite.connect("baza.db")
cursor = connection.cursor()
...
cursor.execute("INSERT INTO tablica (stupac1) VALUES (?), "podatak")
...
neki_kod
...
connection.commit()
connection.close()

Da, ovo bi trebalo radit, ali ne radi! Problem je u tome što je commit() potrebno pozvati odmah nakon inserta. Aaaaaa. Dakle ovo:
connection = sqlite.connect("baza.db")
cursor = connection.cursor()
...
cursor.execute("INSERT INTO tablica (stupac1) VALUES (?), "podatak")
connection.commit()
...
neki_kod
...
connection.close()

Testirati ću to još detaljnije kad se naspavam :)

Python skripta za ažuriranje playliste na PSP-u (2)

Posted on June 19th, 2007 in PSP, Python | 2 Comments »

Kako je eBook reader glup odnosno nema opciju za nasumičan odabir pjesme, pojavila se potreba za novom verzijom skripte koja će raditi upravo to :) . Znači, potrebno je izmjeniti skriptu tako da učita popis pjesama u listu, razmješa istu i zapiše na PSP.

Da bi to radilo kako treba, trebalo je napraviti mali algoritam za randomiziranje liste. Algoritam se temelji na slijedećem: uzme se prva stavka i zamjeni sa nasumično odabranom stavkom. Nakon toga uzme se druga stavka i također zamjeni sa nasumičnom stavkom itd.

for i in range(len(lista)):
  rnd = random.randint(0, len(lista)-1)
  tmp = lista[i]
  lista[i] = lista[rnd]
  lista[rnd] = tmp

Nakon toga sam shvatio da u random klasi postoji i metoda shuffle koja radi upravo to :) tako da se isti učinak postiže pomoću:
random.shuffle(lista)

Python rulez.

Skripta: psp-ver2.zip

Python skripta za ažuriranje playliste na PSP-u

Posted on June 3rd, 2007 in PSP, Python | No Comments »

Jedini program na PSP-u za čitanje e-knjiga a da istovremeno podržava i slušanje mp3-a je eReader. No, program ne zna pretraživati karticu i pronaći gdje mu se nalaze mp3 datoteke već ima posebnu datoteku odnosno playlistu gdje je zapisano gdje se nalaze.

I zbog toga, nakon svake promjene (brisanje ili dodavanje datoteka) listu je potrebno ažurirati. To i nije toliki problem ali dosadi otprilike nakon drugog puta. Kako ja često brišem/dodajem mp3-e, ručno ažuriranje liste nakon svake promjene je nedopustivo :) te sam stoga napravio Python skriptu koja radi upravo to.

Skripta prvo pretraži datoteke koje se nalazi na PSP-u, napravi listu (iz nekog razloga, eReader zahtjeva da svaka stavka u listi mora biti 2x napisana) te na kraju prebaci na PSP. Dakle, nakon dodavanja pjesama na PSP potrebno je samo pokrenuti skriptu i to je to.

Skripta: psp.tar