Subscribe     RSS   MOBILE   EMAIL  
Subscribe to Workshop! It's fun.
Go ahead, pick a reader:
Add to Google
Add to Yahoo
Add to Bloglines
Add to Rojo
Add to Newsgator
Add to Netvibes
RSS Feed
Feed URL:
http://workshop.nseries.com/feed
Scan this code or type this link in your browser for the mobile RSS feed:
http://workshop.nseries.com/feed
Sign up for Workshop emails!
Categories
Archives
Loading
 
04 Jan 2008
 
Tags
 

big pic

The Nokia N800 internet tablet has a feature you may not know about: a built-in FM radio tuner that you can script using Python. Here’s how it works.

First, let’s take a quick look at the radio. Go to “Application Manager,” select “Browse installable applications” and find “fmradio.” Install it. If you can’t find it, take a look in “Show installed applications” instead—you might have it installed already. Or you may need to get into developer “Red Pill” mode on your N800. See my previous article about running simple Python scripts for instructions.

Because the radio is a desktop applet, you’ll need to close or reduce all of the windows until you see the N800 desktop. Pull down the desktop menu and choose “Select applets…” Choose “FM Radio” and drag it somewhere to get started.

Playing with this application can give you a sense of what the FM tuner is capable of. Remember to plug something into your N800’s headphone jack to get a signal, or else you’re just going to get a bunch of static. Also make sure to tap the little antenna icon in the lower right corner of the main radio application window. With this, all of your local radio stations will load up automatically.

Now that you’ve seen that your N800’s FM tuner works, let’s dig into pyFMRadio.

Martin Grimme, a developer out of Germany, has written a Python module called pyFMRadio that makes it quite easy to control using Python. Go here and download the most recent version onto your computer (or download it straight to the N800). Unzip the latest version of pyFMRadio and send the unzipped files “FMradio.py” and “example.py” to your N800 via Bluetooth. Remember to keep your files on your N800 somewhat organized. I put all of my Python scripts in a folder called “Python” inside of my main Documents folder. The paths I use in this article refer to that directory. If you’re putting these files elsewhere, you’ll need to adjust paths accordingly. Regardless, both files need to be in the same folder.

Now, let’s run FMradio.py. Type this into the command line:

cd /home/user/MyDocs/.documents/Python - Change to your Python directory.
python example.py - Run the pyFMRadio example script.

When you run this script, you will see output as the script scans all frequencies for available channels. It will then go through and play each channel that it finds for a few seconds. (Remember to have something plugged into the stereo jack on your N800 or you’ll just get static.)

When you’ve had enough of this, take a closer look at example.py by opening it up on your computer. It’s fairly short, and even if you don’t know Python very well you can probably make some sense out of it.

What we are going to do is very simple. We are going to modify the “Hello World!” script I wrote about in my previous article so that it plays a radio station of your choice and displays simple details about that station on the screen. This is going to be the most featureless radio ever—you won’t even be able to change channels—but it will get us comfortable using pyFMRadio. (If you have a more extravagant idea, please share it in the comments section.)

Here’s the Hello World script again:

#!/usr/bin/env python2.5

import gtk
import hildon

window = hildon.Window()
window.connect(”destroy”, gtk.main_quit) label = gtk.Label(”Hello World!”)
window.add(label)

label.show()
window.show()

gtk.main()

All this does is display “Hello World!” in an N800-style window. If you did the previous tutorial, it’s still probably in your Python folder.

Here’s the code for a new file called “hello_radio.py”:

#!/usr/bin/env python2.5

import gtk
import hildon
from FMRadio import FMRadio, FMRadioUnavailableError

# set your variables
volume = 50
freq = 97.9

label = gtk.Label(”Playing %250.1f FM.” %25 freq)

# start the radio
try:
radio = FMRadio()
except FMRadioUnavailableError:
label = gtk.Label(”Oop! Your device has no radio.”)

radio.set_volume(volume)
radio.set_frequency(freq*1000)

window = hildon.Window()
window.connect(”destroy”, gtk.main_quit)
window.add(label)

label.show()
window.show()

gtk.main()

radio.close()

What’s going on here? It should look very similar to your “hello_world.py”, but with some new radio stuff in the mix. You’ll note that we have to import the pyFMRadio modules on line 5 (”from FMRadio import FMRadio, FMRadioUnavailableError”). We then set our volume and frequency to whatever we would like and set the label to show the station we have chosen on line 11. If you’re used to Java or PHP, you may find the syntax to be a bit odd. Basically the “%250.1f” means we’re going to display a float out just one decimal place. “%25 freq” tells Python which variable to display.

Lines 13-17 are the tricky part. Basically, these tell your N800 to *try* loading up a radio object for use. If there’s a problem (a.k.a. an exception), then it will display an error on screen instead of the FM frequency. PHP programmers don’t deal with this sort of error-catching structure very often. Java and C programmers will be more used to it.

Then, lines 19 and 20 turn the radio to the right volume and frequency—the radio will start playing automatically. And that’s it! You have a fully scriptable radio you can access via Python scripts.

The only remaining piece is to make sure to close the radio before fully exiting the script. If you don’t do this, your radio will continue to run even if you quit the application, which could be annoying. The radio.close() function on the last line of the script takes care of it.

As you see, scripting the FM tuner on your N800 opens up a lot of possibilities. By adding a few widgets—maybe a slider to choose the station and one to adjust the volume—you could make your own full-featured FM radio. You could even use the audio recording tools to make a radio TiVo or something cool like that. If you do, share your ideas and developments in the comments section.

 
share          
email   share        print   print       
Comments (39)
v
12:00 AM
01.25.08



"thanks this looks interesting, i would love to hear cristian nold"


hollandy
12:00 AM
01.25.08



this is great the sport feed service i have installed charges a monthly charge with this i can set up my own results feed for free nice1


Somto
12:00 AM
01.29.08



This has opened my eyes. i must get mobispine. hola


marlonj
12:00 AM
01.30.08



"excelent tutorial !!! i waiting for the next one"


DejanCencelj
12:00 AM
02.02.08



"Hi! The code above is in correct! import mjs.processing.mobile.mlocation.*; PFont font; double[] coords = new double[2]; int latitude = (int) (coords[0] * 10000); int longitude = (int) (coords[1] *"


melanie
12:00 AM
02.02.08



"my friends and my officemate are using N70 units, now almost a week that we are encountering a sending messages problem.. long long or we cannot send"


Neil
12:00 AM
02.05.08



Don't think my last post worked! I get the same error as night-hawk1 - I copied & pasted your code exactly...


Neil
12:00 AM
02.05.08



"I'm getting the same error as night-hawk1. I ran the first test script that prints ""HI"" and that worked fine. Then tried the code above and got same error message when trying to export midlet."


Josh Knowles
12:00 AM
02.05.08



"Hey, I wrote this article. night-hawk1, would you mind telling me more about the bug? Were you copying in any of DejanCencelj's code? Did your complier give a line number? I'd be happy to help!"


night-hawk1
12:00 AM
02.05.08



"getting this error expecting RPAREN, found 'coords' Syntax, Error , maybe missing a right parenthesis ?"


Marc Ong
12:00 AM
02.05.08



This is nice!


Neil
12:00 AM
02.06.08



"Can't get this to work either. I get an error with this line ""int start = xml.indexOf(""instance"">""10);"" <--unexpected token: int"


steven jackson
12:00 AM
02.07.08



"these devices are awesome! with the extinction of public payphones it would be great to have devices like these available around the city to use when your phone battery is empty, and they are beautif"


Alex
12:00 AM
02.07.08



Great post - I'm going to see this thig at eyebeam


pdavila
12:00 AM
02.10.08



Thanks for a great and easy start on integrating the N95 with marlonj's great work. Is there a way to not have the OS ask you every time if you will allow the app to access the GPS?


Masud
12:00 AM
02.10.08



It better any other software


adams
12:00 AM
02.10.08



how can i get the new housing n centre body model N95?


carola
12:00 AM
02.11.08



those laser cut gears are quite beautiful..


spark
12:00 AM
02.18.08



what are good mobile phones


Keith J. Schultz
12:00 AM
02.18.08



"Have N958Gb downloaded and installed plugin and connect software for Mac. Have PBG4 , OSX10.4.11. The N95Gb showed up but ISync says that it is not supported. What do I do with the plugin?"


ashiq
12:00 AM
02.22.08



good


ashiq
12:00 AM
02.22.08



good


Khalid Salim Ahmed
12:00 AM
02.26.08



"I can use GPRS connection with post paid card but connection not available with pre-paid card, notice that this prepaid card work properly in any other mobile except all N95 8GB"


m.timmins
12:00 AM
02.28.08



download


maximus
12:00 AM
03.01.08



lets see how its works


Romy
12:00 AM
03.03.08



i want easiest way ro transfer files from your pc to nokia nseries phone


jhon
12:00 AM
03.04.08



i love music and my n95


Snamporgetti Saudi Arabia LTD.
12:00 AM
03.10.08



"N95 is a fabulous mobile , but the liqued crystal screan is horrible , it was broken without any resoan , 2 times"


hitesh
12:00 AM
03.18.08



I want to download maps


vishesh jeswani
12:00 AM
03.18.08



"i have purchased Nokia N 81 three days back, but unable to load movie+music+video from the CD ( activation code no - F 4 K 5 L P ) to mobile set ,supplied with the phone. Kindly suggest the steps."


sadek
12:00 AM
03.29.08



map pf N73 music edition


SHERIF MOSALLAM
12:00 AM
04.08.08



VERY GOOD


Andrian
12:00 AM
04.12.08



mi servi maps per n 95 8gb grazie!!! :-)


Alex
12:00 AM
05.27.08



Does this works on the n82 because im really considering on buy one just for this development tool


ciol nouimi
12:00 AM
06.14.08



mkhgv


ciol nouimi
12:00 AM
06.14.08



no comemy


ezzat
12:00 AM
06.29.08



web/GPR


Vinay
12:00 AM
06.30.08



-


cetin koprulu
11:37 PM
09.19.08



mukemmel


 
 
 
Enter comment
Name


Email (not displayed publicly)
 
Related Posts
feature mini
How to upgrade your Internet Tablet to OS2008
feature mini
Internet Tablet Video Converter
feature mini
Python for S60 1.4.3 released
 
Workshop Mobile Code
 
Location:         Language: English