Image Ulrich Habel
About me/Impressum

search

Google


Search WWW
Search rhaen daily

archives

feeds

Perl nation banner logo thing
19.10.2008

Time for Rockbox and Perl graphing

Wheeew, I was on holiday and thanks to FLOSS Weekly I decided to install Rockbox on my iPod right before I left. It's quite amazing what these guys reverse engineered and how well the support for the Apple iPod is. I own a 30gb iPod video device - nothing really fancy, however I wanted to have a look on it. No clue what I am talking about? Rockbox is an alternative firmware for your MP3 player. You'll just update your iPod to Rockbox and have a whole bunch of new features available. The most impressive part is the "I-need-no-iTunes" part. Rockbox will find all the songs on your iPod and builds a tag database out of it. Actually you can use rsync to sync your music on the iPod. I really like it.
There are also alot of small applications bundled with Rockbox. One of them is battery_bench. It's writes the current state of your battery frequently to a textfile on the iPod disk. Do you want to know how long the battery of your iPod lasts? Here is a way to draw a graph from the collected data. It's quite simple, charge the iPod until it's fully charged, play an album in an endless loop and run the battery_bench tool. After the iPod turned off due to power loss, get the datafile from the iPod and draw fancy graphs out of it.

I really had some nice feedback about the awk gnuplot thing and I wanted to introduce you to another gem in our pkgsrc collection. The gem is called p5-GDGraph. It's a great Perl module to draw graphics out of data, so let's look into the example of the iPod.

The collected data is stored in a file called battery_bench.txt and looks like this:

[...]
Battery type: 400 mAh      Buffer Entries: 1000
  Time:,  Seconds:,  Level:,  Time Left:,  Voltage[mV]:, C:, S:, U:
  02:18:34,  08314,     100%,     11:25,         4223,     -,  -,  -
  02:19:34,  08374,     100%,     11:25,         4223,     -,  -,  -
  02:20:34,  08434,     100%,     11:25,         4206,     -,  -,  -
[...]
This structure can be easily caught by Perl. I will read the file line by line, and store the data inside an array. As I wanted to draw a graph out of the time for the x-value and the Voltage for the y-axis, I will put those values into two seperated arrays. After I got all the data from the file, I will build an array of arrays out of the data, this will be passed to p5-GDGraph to draw a nice graph out of it (just like the one in the article).
1.) Get the data out of the file
my $battery_bench = "battery_bench.txt";
my @plot_data;
my @x_data;
my @y_data;

# Pulling the data out of the data file
open DATA, $battery_bench
  or die "Can't open file: $battery_bench\n";
while (<DATA>) {
        # we don't want the explanation, just the data
    if (m/^[0-9]{2}/) {
        my $dataline = $_;
        $dataline =~ s/\s//g;
        my @dataparts = split( /,/, $dataline );
        push( @x_data, $dataparts[0] );
        push( @y_data, $dataparts[4] );
    }
}
close DATA;
After we read the whole file we'll have all the data inside the two arrays, @x_data and @y_data. You already guessed it, it's the data for the X and for the Y values. The Perl module however expects the data inside an array of arrays. The construction of it is quite easy and will be shown in the next section.
2.) Pass the data to the graphing module GD::Graph::lines
# Setting up the graph
my $graph = GD::Graph::lines->new( 600, 300 );
@plot_data = ( [@x_data], [@y_data] );
$graph->set(
    x_label           => 'Time played',
    y_label           => 'Voltage (mV)',
    title             => 'Voltage graph of my iPod',
    transparent       => 0,
    x_labels_vertical => 1,
    x_label_skip      => 25,
) or die $graph->error;
my $gd = $graph->plot( \@plot_data ) or die $graph->error;
Voila, you just plotted the first graph out of it. You can use different output formats to get the actual graph as an image out of the Perl code.
# Write the graph to a file
open( IMG, '>battery_bench.png' ) or die $!;
binmode IMG;
print IMG $gd->png;
close IMG;
It's a nice way to draw some quick graphs with Perl without messing around with RRD databases. Of course this module is also an excellent way to draw graphs out of iostat, sar and other tools. If you want to draw multiple lines inside a single graph just add another array to the @plot_data. The maintainer of the module inside the pkgsrc packages collection keeps this module always updated. He's already done a great job about it, thanks for keeping it uptodate.

You can find the full example with code and graphs inside the tar.gz file inside the references. References:

[/pkgsrc] [link]

powered by NetBSD