#!/usr/local/bin/perl -Tw # # $Id: $ # # Binary Clock LED Map # Copyright (C) 2007 David M. Syzdek # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # binaryclock.cgi - Displays LED map for a binary clock # # Runs best from local web server (which happens to be installed on OS X). # # Original: # http://www.syzdek.net/~syzdek/misc/binaryclock.cgi/binaryclock.cgi # # See Also: # LED Panel Widget by Zach (http://tech.no.logi.es/) # use 5.008; use strict; $|++; our $PROGRAM_NAME = 'binaryclock.cgi'; our $VERSION = '0.02'; our $DESCRIPTION = 'displays LED map for a binary clock'; our $AUTHOR = 'David Syzdek '; # I am too lazy to write error handling, so we will just # set required variables here. $ENV{'HTTP_HOST'} = $ENV{'HTTP_HOST'} ? $ENV{'HTTP_HOST'} : 'localhost'; $ENV{'SERVER_PORT'} = $ENV{'SERVER_PORT'} ? $ENV{'SERVER_PORT'} : 80; $ENV{'SCRIPT_NAME'} = $ENV{'SCRIPT_NAME'} ? $ENV{'SCRIPT_NAME'} : 'clock.cgi'; $ENV{'SCRIPT_FILENAME'} = $ENV{'SCRIPT_FILENAME'} ? $ENV{'SCRIPT_FILENAME'} : '/dev/null'; $ENV{'PATH_INFO'} = $ENV{'PATH_INFO'} ? $ENV{'PATH_INFO'} : ''; # function prototypes are your friend sub dec2bin($); # generates binary notation sub source(); # displays source code sub main(@); # main statement # generates binary notation sub dec2bin($) { my $dec = shift; my $digit; my @bin; $bin[@bin] = '0'; for $digit (32, 16, 8, 4, 2, 1) { if ($dec >= $digit) { $bin[@bin] = '1'; $dec = $dec - $digit; } else { $bin[@bin] = '0'; }; }; return(@bin); }; # displays source code sub source() { printf("Content-type: text/plain\n\n"); open(FILE, $ENV{'SCRIPT_FILENAME'}); while() { printf("%s", $_); }; close(FILE); return(0); }; # main statement sub main(@) { my @argv = @_; my $i; my @t = localtime(time()); my @hour = dec2bin($t[2]); my @min = dec2bin($t[1]); my @sec = dec2bin($t[0]); # determines whether to show source code if ($ENV{'PATH_INFO'} eq '/' . $PROGRAM_NAME) { return(source()); }; # prints header printf("Content-type: text/html\n\n"); # prints LED map depending on options printf("
    \n"); if ($ENV{'PATH_INFO'} !~ /^$/) { printf("
  • 3
  • \n"); printf("
  • 7
  • \n"); printf("
  • \n"); for($i = 0; $i < @hour; $i++) { printf("%s%s%s\n", $hour[$i], $min[$i], $sec[$i]); }; printf("
  • \n"); printf("
  • http://%s:%s%s/vertical
  • \n", $ENV{'HTTP_HOST'}, $ENV{'SERVER_PORT'}, $ENV{'SCRIPT_NAME'}); printf("
  • 1000
  • \n"); printf("
  • http://%s:%s%s
  • \n", $ENV{'HTTP_HOST'}, $ENV{'SERVER_PORT'}, $ENV{'SCRIPT_NAME'}); } else { printf("
  • 7
  • \n"); printf("
  • 3
  • \n"); printf("
  • \n"); print ( @hour, "\n"); print ( @min, "\n"); print ( @sec, "\n"); printf("
  • \n"); printf("
  • http://%s:%s%s
  • \n", $ENV{'HTTP_HOST'}, $ENV{'SERVER_PORT'}, $ENV{'SCRIPT_NAME'}); printf("
  • 1000
  • \n"); printf("
  • http://%s:%s%s/vertical
  • \n", $ENV{'HTTP_HOST'}, $ENV{'SERVER_PORT'}, $ENV{'SCRIPT_NAME'}); }; printf("
  • \n"); #printf("
  • http://www.syzdek.net/~syzdek/misc/binaryclock.cgi/binaryclock.cgi
  • \n"); printf("
\n"); printf("View source code for LED map.
\n", $ENV{'HTTP_HOST'}, $ENV{'SERVER_PORT'}, $ENV{'SCRIPT_NAME'}, $PROGRAM_NAME); # ends function return(0); }; exit(main(@ARGV)); # end of script