#!/usr/bin/perl # # This program displays patterns of up to 8 LEDs # driven from a PC's parallel port. LEDs are hooked # to the data pins (2-9) with 470 ohm resistors and # then to a ground pin (18-25). Note that data pins # are numbered 0-7 and that my examples below are # hooked up to a 7 segment (figure 8) display. # # I've written a version of this program that # varies the speed based on CPU usage, to get # it or a program to display a given letter or # number take a look here: # # http://www.anderbergfamily.net/ant/ledcontrol/ # # Version 1.0 - 12/20/2007 # by Anthony Anderberg ant@anderbergfamily.net # # # Scott Penrose's parallel port direct-control module, # needed for direct port access. # On Win32 systems this requires inpout32.dll # use Device::ParallelPort; my $pp = Device::ParallelPort->new('auto:0'); if (!defined($pp)) { print "\nParallel port setup error: \n $! \n"; exit; } # # This lets us give sleep() non-integer values. # use Time::HiRes qw(sleep); # # Lower our process priority on operating systems # that support that kind of neighborly behavior. # We spend most of the time sleeping anyway, even # at 0.1 second per sleep this program took only # one CPU second per week on my test systems... # eval 'setpriority 0,0, (getpriority 0,0) + 4'; # # Set to a non-zero to leave a window # open and print useful info. # $debug = 0; # # This is the amount of time each LED is on. # Fractional seconds are allowed, ie: 0.5 # $hardcodesleep = 0.1; # # This is the order the LEDs are turned on in, # it will depend on what order the LEDs are # hooked to the parallel port data pins. # @displayorder=(5,3,6,4,2,0,6,7); # Clockwise Figure 8 # @displayorder=(7,6,0,2,4,6,3,5); # Counter Clockwise Figure 8 # @displayorder=(5,3,0,2,4,7); # Clockwise Circle # @displayorder=(5,7,4,2,0,3); # Counter Clockwise Circle # @displayorder=(5,6,2,6); # Back and forth # # We need to know how many steps our # chosen pattern has. # $displaycount = @displayorder; # # Most of the time the data ports are all # 'on' to begin with, we'll turn them off. # for ($step=0;$step<=8;$step++) { $pp->set_bit($step, 0); } # # Here's our main loop, it does what you'd expect. # while (1) # Who ever said infinite loops are bad? { for ($step=0;$step<$displaycount;$step++) { # # This code chooses which LEDs to turn on and off. # $led=$displayorder[$step]; if ($step == 0) { $lastled=$displayorder[$displaycount-1]; } else { $lastled=$displayorder[$step-1]; } # # This code actually changes the LEDs. # $pp->set_bit($lastled, 0); $pp->set_bit($led, 1); if ($debug) { print "Step:$step - LED:$led - LastLED:$lastled\n"; } sleep($hardcodesleep); } }