Browse Source

bugfix in difference, loaduino.pl can talk to a remote host

Brennen Bearnes 8 years ago
parent
commit
7efd84db31
2 changed files with 60 additions and 8 deletions
  1. 1
    1
      loaduino.pde
  2. 59
    7
      loaduino.pl

+ 1
- 1
loaduino.pde View File

@@ -6,7 +6,7 @@ Servo myservo;  // create servo object to control a servo
6 6
 
7 7
 int dial_pos = 0; // variable to store the servo position 
8 8
 int led_pin = 13;
9
-int difference;
9
+int difference = 0;
10 10
  
11 11
 unsigned char value1 = 0, value2 = 0;
12 12
 int value = 0;

+ 59
- 7
loaduino.pl View File

@@ -7,17 +7,31 @@ use 5.10.0;
7 7
 use Device::SerialPort;
8 8
 use POSIX qw(setsid);
9 9
 use File::Slurp;
10
+use Getopt::Long;
11
+use LWP::UserAgent;
12
+
13
+my $remote;
14
+my $refresh_rate = 10;
15
+my $daemon = 0;
16
+my $cpucount = 2; # default to 2 CPUs
17
+
18
+my $getopt_result = GetOptions (
19
+  "refresh=i" => \$refresh_rate, # numeric
20
+  "remote=s"  => \$remote,       # string
21
+  "daemon"    => \$daemon,       # flag
22
+  "cores=i"   => \$cpucount      # numeric
23
+);
10 24
 
11 25
 say my $device = '/dev/ttyUSB0';
12 26
 
13
-# daemonize?
14
-my $daemon = 0;
15
-if (defined($ARGV[0]) && $ARGV[0] eq '--daemon') {
16
-  $daemon = 1;
27
+if ($daemon) {
17 28
   daemonize();
18 29
 }
19 30
 
20
-say 'Found ' . countcpus() . ' CPUs' unless $daemon;
31
+# set a count explicitly - need this for remotes
32
+if ($remote && $cpucount) {
33
+  countcpus($cpucount);
34
+}
21 35
 
22 36
 # connect to the arduino
23 37
 my $port = Device::SerialPort->new($device)
@@ -36,11 +50,19 @@ while (1) {
36 50
     or die "Unable to write to $device";
37 51
 
38 52
   say "$degrees degrees, $count_out bytes sent" unless $daemon;
39
-  sleep 1;
53
+  sleep $refresh_rate;
40 54
 }
41 55
 
42 56
 sub getdegrees {
43
-  my ($load) = split / /, read_file('/proc/loadavg');
57
+  my $loadstring;
58
+  if ($remote) {
59
+    $loadstring = getremoteload($remote);
60
+  } else {
61
+    $loadstring = read_file('/proc/loadavg');
62
+  }
63
+
64
+  my ($load) = split / /, $loadstring;
65
+  say "Load: $load" unless $daemon;
44 66
   return 1 if $load >= countcpus();
45 67
   return int(($load / countcpus()) * 180);
46 68
 }
@@ -48,10 +70,40 @@ sub getdegrees {
48 70
 # sometimes i miss writing a language that's too clever for its own good
49 71
 { my $count;
50 72
 sub countcpus {
73
+  my ($setcount) = @_;
74
+  if ($setcount) {
75
+    say "CPUs explicitly set to $setcount" unless $daemon;
76
+    $count = $setcount;
77
+  }
51 78
   $count //= grep { /^processor\s+: [0-9]/ } read_file('/proc/cpuinfo');
52 79
 }
53 80
 }
54 81
 
82
+sub getremoteload {
83
+  my ($remote) = @_;
84
+  # Create a user agent object
85
+  my $ua = LWP::UserAgent->new;
86
+  $ua->agent("loaduino/0.1 ");
87
+
88
+  # Create a request
89
+  my $req = HTTP::Request->new(GET => $remote);
90
+
91
+  # Pass request to the user agent and get a response back
92
+  my $res = $ua->request($req);
93
+
94
+  # Check the outcome of the response
95
+  my $loadstring;
96
+  if ($res->is_success) {
97
+    $loadstring = $res->content;
98
+  } else {
99
+    say $res->status_line;
100
+    $loadstring = '0.0';
101
+  }
102
+ 
103
+  say "Load string: $loadstring";
104
+  return $loadstring;
105
+}
106
+
55 107
 sub daemonize {
56 108
   chdir '/';
57 109
   umask 0;