1-Wire with Leds and Beeps oscar
1-wire-with-leds-and-beeps / software / mod_led.pl
use constant CHILD_LOG_VERBOSE => 1; # Activate verbose logging
$l_new_stdout = FileHandle->new(">> /var/log/".$fname."_child_led.log") if CHILD_LOG_VERBOSE;
$l_new_stdout->autoflush(1) if CHILD_LOG_VERBOSE;
# These values are cycles numbers: moltiply for Timer_resolution to obtain the timer duration
use constant LOG_DURATION_ON => 16;
use constant LOG_DURATION_OFF => 6;
use constant DURATION_ON => 1;
use constant DURATION_OFF => 1; # For quick flashing!
my $Timer_Resolution=250000 ; # 1'000'000 = 1 sec.
my @LED = (
GREEN_LED, # Verde
RED_LED); # Rosso
# Legal values accepted in FIFO:
my @Commands = (
"G", # - V - Start Green flashing
"R", # - R - Start Red flashing
"AG", # - AV - Alarm green
"AR" ); # - AR - Alarm red
my $which=0; # index for array led
my $alarm_status=0; # Alarm status
my $tmp_command=""; # Value from FFO: it will be immediately cleaned
my $cycles=0; # Cycles Counter
# Boolean Switch of led status (on, off)
# Denying the value 1 I get a "", so I have to work by difference: 1-Switch ...
my $lamp=0; # Stato del led: acceso o spento
my $owserver_led = OWNet->new( "localhost:4304" ) ;
my $appo_dir_list = $owserver_led->dir('/') || die ("Cannot open OWNet connection");
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Initial dir_list to chech owserver connection: $appo_dir_list\n" if LOG_DEBUG;
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Succesfully connected to owserver.\n" if LOG_DEBUG;
# FIFO startup.
# It needs a first input, else it rest blocked here.
# Conseguently I don't need to define a starting value: I'll get it from the FIFO!
unless (-p $FIFO_TO_CHILD_LED)
{
unlink $FIFO_TO_CHILD_LED;
system('mknod', $FIFO_TO_CHILD_LED, 'p') && die "can't mknod ".$FIFO_TO_CHILD_LED.": $!";
}
open (FIFO, "< ".$FIFO_TO_CHILD_LED) || die "can't read ".$FIFO_TO_CHILD_LED.": $!";
# Main !
while (1) {
if ( $tmp_command ~~ @Commands ) {
if ( $tmp_command =~ /G/ ) { # G - AG
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: 'G'reen received. \n" if CHILD_LOG_VERBOSE;
$which=0;
# Force GREEN on && RED off
$owserver_led->write( BOTH_LED , "0,1" );
} else { # R - AR
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: 'R'ed received. \n" if CHILD_LOG_VERBOSE;
$which=1;
# Force GREEN off && RED on
$owserver_led->write( BOTH_LED , "1,0" );
}
if ( $tmp_command =~ /A/ ) { # AR - AG
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: 'A'larm received. \n" if CHILD_LOG_VERBOSE;
$alarm_status=1;
$cycles=DURATION_ON;
} else { # R - G
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: 'A'larm NOT received. \n" if CHILD_LOG_VERBOSE;
$alarm_status=0;
$cycles=LOG_DURATION_ON;
}
$lamp=1;
$tmp_command="";
}
if ($cycles==0) {
if ( $lamp ) {
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: IF LAMP! Alarm: $alarm_status - Cycles: $cycles - Lamp: $lamp \n" if CHILD_LOG_VERBOSE;
if ( $alarm_status ) { $cycles=DURATION_OFF;
} else { $cycles=LOG_DURATION_OFF; }
$lamp=0;
} else {
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: ELSE LAMP! Alarm: $alarm_status - Cycles: $cycles - Lamp: $lamp \n" if CHILD_LOG_VERBOSE;
$cycles=DURATION_ON;
$lamp=1;
}
$owserver_led->write($LED[$which] , $lamp );
}
print { $l_new_stdout } (strftime "%b %e %H:%M:%S", localtime). " Child LED: Alarm: $alarm_status - Cycles: $cycles - Lamp: $lamp \n" if CHILD_LOG_VERBOSE;
$cycles--;
usleep($Timer_Resolution);
}
close(FIFO);
unlink $FIFO_TO_CHILD_LED;
use sigtrap qw(handler signal_handler normal-signals error-signals old-interface-signals );
# You got mail! Let's go to read it!
use sigtrap 'handler' => \&CambiaLampo, 'USR1';
sub CambiaLampo {
# If the FIFO contains more than one signal, the good one is the last one...
# Others are outdated, so clean all and get last!
while (<FIFO>) {
$tmp_command = $_;
}
chomp $tmp_command;
}