#!/usr/bin/perl -w
# vim: set sw=4 ts=4 et: 
# lcddate.pl - display time and day on an external LCD display
# Copyright GPL  2005, Guido Socher
use IO::Socket;
use Fcntl;
use strict;
#
my $remote = IO::Socket::INET->new(
    Proto     => "tcp",
    PeerAddr  => "localhost",
    PeerPort  => 13666)|| die "Cannot connect to LCDd\n";
$remote->autoflush(1);
sleep 1; # give the server time to notice us...
print $remote "hello\n";
# we must read the response even if we ignore it:
my $lcdresponse = <$remote>;
# Turn off blocking mode...
fcntl($remote, F_SETFL, O_NONBLOCK);
# Set up some screen widgets...
print $remote "client_set name lcdtime\n";
print $remote "screen_add scr1\n";
print $remote "widget_add scr1 str1 string\n";
#
my $date;
my @ltime;
while(1)
{
    $lcdresponse = <$remote>;
    #$date = scalar localtime; # this is 24 char long. The following is bettter:
    @ltime = localtime;
    #return a date in yyyy-mm-dd hh:mm format
    $date = sprintf("%04d-%02d-%02d %02d:%02d",
          1900+$ltime[5],$ltime[4]+1,$ltime[3],$ltime[2],$ltime[1],$ltime[0]);
    print $remote "widget_set scr1 str1 1 1 \"$date\"\n";
    sleep 10;
}