The writings of Peter Stuifzand

Archive for January 2008

The MySmartUSB documentation contains a paragraph about a protocol for sending commands to the programmer. I wrote a small perl script that can send these commands.

The file: mysmartusb. It requires Device::SerialPort, which is a perl module that's available on CPAN.

Or you could get the git repository from http://code.peterstuifzand.nl/git/mysmartusb.git with the command:

git clone http://code.peterstuifzand.nl/git/mysmartusb.git/

This is my first microcontroller project video.

Yesterday was Delete your Myspace account day. I don't use any of those social networking sites and therefore I deleted all of the sites that were left. I cancelled my Facebook account a few weeks after using it for a few weeks.

Social networking sites are flawed, but maybe someday we will find out a way to make this work, but it is not today.

If people want to communicate with each other they can use email, IRC, phone, IM or any other way. It's not needed to have another system for those things. Friendships are somethingthat can't be described in a simplistic database.

friend(person1, person2)

It is a lot more complex. Still the social websites have to try something, or they could stop of course.

Maybe the direction is wrong. They try to go from social network to being useful. Maybe they should try it the other way around: from being useful to social network.

I programmed my first Atmel ATTiny2313 today. Of course this was a blinking led program.

I used the following code:

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB |= 1<<PB0; /* set PB0 to output */
    while(1) {
        PORTB &= ~(1<<PB0); /* LED on */
        _delay_ms(100);
        PORTB |= 1<<PB0; /* LED off */
        _delay_ms(900);
    }
    return 0;
}

This will blink a led if it's connected to PB0 (or pin 12 on the microcontroller).

To program the chip I used the avrdude program. I use the following commandline:

avrdude -p t2313 -c avr910 -P /dev/ttyUSB0 -e -U flash:w:blink.hex

The programmer that I used is the MySmartUSB programmer. This works on Linux, the drivers were already compiled in my kernel. When I connected the programmer, dmesg showed me that it worked.

The MySmartUSB programmer has a 10 pin avr connector for programming microcontroller. This means you need an extra board that has the pins for the chip. I created one myself. This is not that hard. The only thing you need is a pinheader (10 pins) a IC foot (I used 20 pins, as I was programming a ATTiny2313) and a few wires.

My laptop (a Toshiba Sattelite Pro L40) has working sound now! It started woking after I added the following two lines to the file /etc/modprobe.d/alsa-base

options snd-card-0 index=0 model=3stack options snd-hda-intel index=0 model=3stack

The problem was that the audio was working when I first started the laptop, but it would stop when I used the volume mixer. Changing the volume did something that made the audio stop working.

I have to say, I really like git. I like it almost so much that I switched to Linux for it on my laptop. I have two projects of mine in a git repository and it seems to work great.

I have installed Ubuntu Linux on my laptop. This is the second time that I tried installing linux on my laptop. The first time there were all kinds of problems with unsupported hardware.

When I installed Ubuntu yesterday, everything worked (except for Wireless). After a bit of searching I found a solution in ndiswrapper. I'm not that happy with this, because it is using Windows drivers. I'm waiting for the release of the new madwifi wireless drivers, which I hope will fix this problem.

At the beginning of last year I wrote a weblog entry about how to write a function that removes all space but one in Vim. It didn't work like I wanted, but now it does.

function JustOneSpace()
    " replace all whitespace around the cursor with a space
    s/\s*\%#\s*/ /e
    " search backwards for a space
    call search(' ', 'be')
    " move to the first character after the space
    normal l
endfunction

nmap <space> :call JustOneSpace()<cr>

I added the call to the search() function to move to the space that was substituted. All this time I wanted to fix this function by using regexes or special vim variables. I couldn't find these. This is quite obvious, but it didn't come to mind at first.

Update

I looks like this function doesn't work when it's used on a line with no space. It will insert a space on the spot it is supposed to, but then moves the cursor to the first space it finds when searching backwards.

This weekend I tried Gnome on Debian unstable to see how the compiz window manager and other new stuff was working. I'm currently use pekwm. After a few hours of usage, I reverted back to my current desktop.

On the way I found out a two things:

  1. You should remove the package files that are downloaded by aptitude. I collected about 7 GB of .deb packages. You can clean this up by using the command: apt-get autoclean.
  2. Gnome contains a nice Graphics Disk Usage Analyzer called baobab. It shows the amount of diskspace a file or directory uses in a graphical representation.

R is a statistics program. I can calculate statistical functions and draw nice graphs. Today I used it for showing some graphs.

I use R on my linux system, but it also works on Windows. To install it look at website of R.

Data from a file can be read with the function read.table(filename), where filename is the name of filename that you want to read. This would look like:

visitors <- read.table('visitors.dat')

The visitors.dat should contains two columns like this:

id  visitors
1    100
2    204
3    70
4    79
5    99
6    100
...  ...

You can also use the read.csv(filename) function. This will read comma separated values files.

When the file is read, the data will be put in a table named visitors, because of the variable and the <- operator. Typing visitors on the R prompt will show the contents of the table. To plot the data, type plot(visitors). It's that easy. Plot will choose a way to show the data.

After plot you can type lines(visitors) to add the lines to connect the dots.

It's also possible to add a line to show to the trend in the data:

lines(lowess(visitors), col='green')

Which will add a green line to the plot, showing the trend.

That's it. Please don't ask me questions about statistics, because I have no idea at the moment. I want to know more about it and will find some books and information.

The R program can do much, this is just the beginning of the possibilities. Of course there are many uses for statistics, like performance of your program.

This is a video podecast that I watch regularly by Bre Pettis. The Weekend projects podcast shows a project you can make over the weekend. It's very inspiring. When you haven't seen it already, you should take a look at the archive.

A few weeks ago I created an art project together with my girlfriend involving video and electronics. I created the electronics part for her.

The technical part of the project was based on a simple idea. When someone walks by it should play a short piece of video.

The sensor

I made a sensor that notices changes in the light received. The idea is that when someone walks by the light falling on the LDR changes. The resistance of the LDR changes when the light changes. The change in resistance can be found out by measuring the voltage over the LDR.

Sensor schema

I used the K8055 to measure this voltage. It's a Velleman Kit that can uses USB to communicate with a computer. One can read the voltage of the sensor with 8-bit precision using the analog port of the device.

The program

To control the K8055, I used perl and Device::Velleman::K8055 which is available on CPAN. It can only be used with Windows because it uses the Win32::API module. Also available from CPAN. There are libraries available for linux, but I didn't use those.

The perl program loops and reads one value from the AnalogChannel using ReadAnalogChannel function each time it goes trough the loop. It takes the channelnumber as an argument. It saves the last few values. This 'first' value is used to check if the value has changed enough. If is has, the movie starts playing.

The movie is played using VLC player. This player has a HTTP server, so the movie it is playing can be started and stopped by send get requests to an URL. I used LWP::UserAgent for this.

The sourcecode

# Written by Peter Stuifzand
use strict;
use warnings;

use constant PAUSE_CHANNEL_WAIT_TIME => 0.4;
use constant WAIT_TIME               => 2;
use constant DIFF_NUM => 4;
use constant KEEP_VALS => 24;

use Device::Velleman::K8055 qw(:all);
use Time::HiRes qw/sleep gettimeofday tv_interval/; 
use List::Util qw/sum/;
use LWP::UserAgent;

print "Test programma 4\n";
die "Can't open K8055 device" unless OpenDevice(0) == 0;

my $ua = LWP::UserAgent->new;

sub NextMovie {
    #$ua->post('http://localhost:13579/command.html', { wm_command => 921 });
    $ua->get('http://localhost:8080/requests/status.xml?command=pl_pause&id=4');
    return;
}

sub FlashDigital {
    ClearAllDigital();
    sleep(0.4);
    SetAllDigital();
    sleep(0.4);
    ClearAllDigital();
    sleep(0.4);
    SetAllDigital();
    sleep(0.4);
    ClearAllDigital();
}

# Start at the beginning of the movie. Sync.
$ua->get('http://localhost:8080/requests/status.xml?command=pl_seek&pos=0&id=4');

my $last_val = ReadAnalogChannel(1);
my @vals;

my $people_count = 0;

do {{
    my $val = ReadAnalogChannel(2);
    if (!@vals) {
        push @vals, $val;
    }
    # Other ways to calculate change
    #    my $avg = sum(@vals) / @vals;
    #    my $diff = abs($avg - $val);
    my $diff = abs($vals[0] - $val);
    printf "%3d %3d %3d\n", $val, $vals[0], $diff;

    push @vals, $val;
    if (@vals > KEEP_VALS) {
        shift @vals;
    }

    $last_val = $val;

    if ($diff >= DIFF_NUM) {
        $people_count++;

        my $start = [gettimeofday()];

        NextMovie();
        while ((my $diff = tv_interval($start) < 1.9)) {
        }
        NextMovie();
        my $diff = tv_interval($start);
        my $current_time = 2 * $people_count;
        my $current_time_s = $current_time % 60;
        my $current_time_m = ($current_time - $current_time_s) / 60;
        # Status information
        print "$people_count $diff s $current_time_m:$current_time_s\n";
        @vals = ();
    }
}} while (!ReadDigitalChannel(1));

CloseDevice();

Conclusion

I was inspired by the a few people to write this text. The first person is Bre Pettis who has an amazing video podcast about stuff that a person could make in the weekend. The other person was Danny O'Brien, because he talked about how we should publish more ideas, because it makes you look smart and organized.

In the last few weeks I have moved a part of my websites to my new server. This is now and actual server, not a computer hidden behind an ADSL router. I'm curious to find out how this actually works.

View archived entries