package TeleServ; use strict; use vars qw(@ISA @EXPORT $VERSION $KEYBUF $DELAY); use Fcntl qw(:DEFAULT :flock); use Carp; use Time::HiRes qw(usleep); use Exporter; @ISA = qw(Exporter); @EXPORT = qw(tsOpen tsRead tsReadAll tsWrite tsCommand tsTemperature tsVoltage tsRpms tsWatchDog tsPwm tsVersion tsKey tsWaitKey tsPrint tsSetTerminalMode tsCursor); $VERSION = '0.01'; $KEYBUF = ''; $DELAY = 0.1; #Open device TeleServ (the controller) sub tsOpen { my $name = shift; $name = "/dev/ttyS0" unless ($name); sysopen(TELESERV, $name, O_RDWR, 0666) || croak "Can't open $name"; flock(TELESERV, LOCK_EX); } #Read string from the controller sub tsRead { my $rin; my $str = ""; vec($rin = "", fileno(TELESERV), 1) = 1; sysread(TELESERV, $str, 32) if( select($rin, undef, undef, $DELAY) > 0 ); return $str; } #Read all strings from the controller sub tsReadAll { my ($rin, $str); while (1) { vec($rin = "", fileno(TELESERV), 1) = 1; last if( select($rin, undef, undef, $DELAY) <= 0 ); sysread(TELESERV, $str, 32); } return $str; } #Write string to the controller sub tsWrite { my $str = shift; return unless ($str); syswrite(TELESERV, $str); } #Send command to the controller (valid for "terminal" mode) sub tsCommand { my $comm = shift; tsWrite(chr(0x1b)."[".$comm."\n"); } #Send some commands to the controller (parameters should be placed in array) sub tsGroupCommand { my ($comm, @in) = @_; my @out = (); foreach (@in) { tsCommand($comm.$_); push(@out, tsRead()); } return @out; } #Get temperature array sub tsTemperature { return tsGroupCommand("t", qw(0 1)); } #Get voltage array sub tsVoltage { return tsGroupCommand("v", qw(0 1 2)); } #Get fan rotation speed array (RPM) sub tsRpms { return tsGroupCommand("r", qw(0 1 2 3)); } #Get / Set value of counter of WatchDog timer sub tsWatchDog { my $watch_dog = shift; if ($watch_dog ne '') { $watch_dog = int($watch_dog / 0.3); $watch_dog = 255 if $watch_dog > 255; tsCommand("W$watch_dog"); usleep(100000); tsRead(); } tsCommand("w"); usleep(100000); my $curr_val = tsRead(); return (0.3 * $curr_val); } #Get PWM value for indicated temperature sub tsPwm { my $t = shift; tsCommand("p@".$t); return tsRead(); } #Get the controller release number sub tsVersion { tsCommand("c"); return tsRead(); } #Get key sub tsKey { my $str = $KEYBUF.tsRead(); if($str && $str =~ /([LlUuDdRr]{1})(.*)/) { $str = $1; $KEYBUF = $2; } else { $str = ''; } return $str; } #Wait until key press. Return key code sub tsWaitKey { my $key; while(($key = tsKey) eq '') { sleep 1; } return $key; } #Print string on LCD screen. Start on begin of either line 1 or 2 sub tsPrint { my ($str, $pos) = @_; $pos = 0 if (!$pos || (($pos != 0) && ($pos != 1))); tsCursor($pos, 0); usleep(50000); $str = (length($str) > 16)?substr($str, 0, 16):$str." "x(16 - length($str)); tsWrite($str."\n"); } #Close device sub tsClose { close(TELESERV); } #Switch to "terminal" mode sub tsSetTerminalMode { tsWrite("t\n"); tsReadAll; tsCommand("e"); } #Set cursor position sub tsCursor { my ($x, $y) = @_; tsCommand($x.";".$y."H"); } 1;