WiringPi-Perl example
Tags perl, raspberry pi, wiringpi
This is a Raspberry Pi project I did a few months ago and meant to post somewhere. There is a Perl interface to the WiringPi module. It's available from https://github.com/WiringPi/WiringPi-Perl. Unfortunately it doesn't come with any examples, but the WiringPi module comes with some nice examples. Unfortunately they are all written in C and it can take some trial and error to translate and I never found any translations of the examples online, or any WiringPi-Perl specific examples.
Here is my version of test1.c translated to Perl (flashes a series of 8 LEDs back and forth like Knight Rider's grill or a 1970's Cylon eyeball, a button allows you to speed up the action):
Unfortunately, you have to run this as root, which seems to common to all WiringPi versions. After compiling the C code, you can suid the program and just run it directly as a normal user, so you can compile it and then let your kids run it, but working in Perl is fun.
I'll attach a link to a schematic in the next couple of days, since I felt like the original script could have used one (the schematic is the same for both test1.c and test1.pl). I'm using a Pi Cobbler from Adafruit to connect the Pi to my breadboard, which has made test wiring much easier for me.
Here is my version of test1.c translated to Perl (flashes a series of 8 LEDs back and forth like Knight Rider's grill or a 1970's Cylon eyeball, a button allows you to speed up the action):
Code:
#!/usr/bin/perl #test1.pl use wiringpi; use constant { OUTPUT=>1, INPUT=>0, HIGH=>1, LOW=>0 }; $SIG{'INT'}=\&safe_exit; $SIG{'QUIT'}=\&safe_exit; $SIG{__DIE__}=\&safe_exit; print("Raspberry Pi wiringPi test program\n"); if( wiringpi::wiringPiSetup()==-1 ){ die 1; } for( my $pin=0;$pin<8;++$pin ){ wiringpi::pinMode($pin,OUTPUT); } wiringpi::pinMode(8,INPUT); @data=( [0,1,1], [1,1,1], [0,0,0],[2,1,1], [1,0,0],[3,1,1], [2,0,0],[4,1,1], [3,0,0],[5,1,1], [4,0,0],[6,1,1], [5,0,0],[7,1,1], [6,0,1], [7,0,1], [7,1,1], [6,1,1], [7,0,0],[5,1,1], [6,0,0],[4,1,1], [5,0,0],[3,1,1], [4,0,0],[2,1,1], [3,0,0],[1,1,1], [2,0,0],[0,1,1], [1,0,1], [0,0,1], [9,9,9] ); my $inc=0; while( 1==1 ){ my( $pin,$state,$duration )=@{$data[$inc]}; if( $pin+$state+$duration==27 ){ $inc=0; next; } wiringpi::digitalWrite($pin,$state); if( wiringpi::digitalRead(8)==LOW ){ wiringpi::delay($duration*10); }else{ wiringpi::delay( $duration*100 ); } ++$inc; } # this just turns off all the LEDs when you exit the script sub safe_exit{ for( my $pin=0;$pin<8;++$pin ){ wiringpi::digitalWrite($pin,LOW); } exit 1; }
I'll attach a link to a schematic in the next couple of days, since I felt like the original script could have used one (the schematic is the same for both test1.c and test1.pl). I'm using a Pi Cobbler from Adafruit to connect the Pi to my breadboard, which has made test wiring much easier for me.
Total Comments 0