|
|
@ -2,10 +2,18 @@ |
|
|
|
|
|
|
|
use strict; |
|
|
|
use warnings; |
|
|
|
use 5.10.0; |
|
|
|
|
|
|
|
use Device::SerialPort; |
|
|
|
use POSIX qw(setsid); |
|
|
|
|
|
|
|
my $device = '/dev/ttyUSB0'; |
|
|
|
# daemonize |
|
|
|
if (defined($ARGV[0]) && $ARGV[0] eq '--daemon') { |
|
|
|
daemonize(); |
|
|
|
} |
|
|
|
|
|
|
|
my $device = '/dev/ttyUSB0'; |
|
|
|
my $cpucount = countcpus(); |
|
|
|
|
|
|
|
# connect to the port |
|
|
|
my $port = Device::SerialPort->new($device) |
|
|
@ -23,13 +31,27 @@ while (1) { |
|
|
|
my $val = pack('s', $load); |
|
|
|
|
|
|
|
my $count_out = $port->write($val); |
|
|
|
print "$load degrees, $count_out bytes\n"; |
|
|
|
# print "$load degrees, $count_out bytes\n"; |
|
|
|
sleep 1; |
|
|
|
} |
|
|
|
|
|
|
|
sub getdegrees { |
|
|
|
my $cpucount = 2; |
|
|
|
|
|
|
|
|
|
|
|
sub countcpus { |
|
|
|
open my $fh, '<', '/proc/cpuinfo' |
|
|
|
or die "Can't get cpu info: $!"; |
|
|
|
my @cpuinfo = <$fh>; |
|
|
|
close $fh; |
|
|
|
|
|
|
|
my $count = 0; |
|
|
|
foreach (@cpuinfo) { |
|
|
|
$count++ if /^processor\s+: [0-9]/; |
|
|
|
} |
|
|
|
|
|
|
|
return $count; |
|
|
|
} |
|
|
|
|
|
|
|
sub getdegrees { |
|
|
|
open my $fh, '<', '/proc/loadavg' |
|
|
|
or die "Can't get load: $!"; |
|
|
|
my $avg = <$fh>; |
|
|
@ -37,12 +59,25 @@ sub getdegrees { |
|
|
|
|
|
|
|
my ($load) = split / /, $avg; |
|
|
|
|
|
|
|
my $pct; |
|
|
|
my $percent; |
|
|
|
if ($load < $cpucount) { |
|
|
|
$pct = $load / 2; |
|
|
|
$percent = $load / $cpucount; |
|
|
|
} else { |
|
|
|
$pct = 1; |
|
|
|
$percent = 1; |
|
|
|
} |
|
|
|
|
|
|
|
return int($pct * 180); |
|
|
|
return int($percent * 180); |
|
|
|
} |
|
|
|
|
|
|
|
sub daemonize { |
|
|
|
chdir '/'; |
|
|
|
umask 0; |
|
|
|
open STDIN, '/dev/null'; |
|
|
|
open STDOUT, '>', '/dev/null'; |
|
|
|
open STDERR, '>', '/dev/null'; |
|
|
|
defined(my $pid = fork) |
|
|
|
or die "Can't fork: $!"; |
|
|
|
exit if $pid; |
|
|
|
setsid |
|
|
|
or die "Can't start a new sesssion: $!"; |
|
|
|
} |