Browse Source

added README, shorter loaduino.pl

Brennen Bearnes 9 years ago
parent
commit
54a0014097
2 changed files with 46 additions and 38 deletions
  1. 25
    0
      README
  2. 21
    38
      loaduino.pl

+ 25
- 0
README View File

@@ -0,0 +1,25 @@
1
+Hacked this together for a demo at the Boulder Linux Users Group. Had
2
+some help from Chris Rojas, Nate Seidle, Ryan Owens, Dave Stillman,
3
+and Google.
4
+
5
+This is the first thing I've built with an Arduino besides blinking a single
6
+LED. It is stupid-simple and does not have very many good reasons to exist.
7
+
8
+Bill of materials:
9
+
10
+  Arduino Duemilanove
11
+  Servo motor of some sort
12
+  SparkFun medium-sized shipping box (red)
13
+  1 blinky yellow LED
14
+  Random wires
15
+  Red electrical tape
16
+  Chunk of manila folder
17
+  Orange masking tape
18
+  Tiny ziptie
19
+  USB cable
20
+
21
+loaduino.pde needs to be compiled and loaded to the Arduino. loaduino.pl
22
+will run and spit some debugging info if invoked with no options. You may
23
+also specify --daemon to run it as a background process.
24
+
25
+All of this is likely to be a bit flaky.

+ 21
- 38
loaduino.pl View File

@@ -6,16 +6,20 @@ use 5.10.0;
6 6
 
7 7
 use Device::SerialPort;
8 8
 use POSIX qw(setsid);
9
+use File::Slurp;
9 10
 
10
-# daemonize
11
+say my $device = '/dev/ttyUSB0';
12
+
13
+# daemonize?
14
+my $daemon = 0;
11 15
 if (defined($ARGV[0]) && $ARGV[0] eq '--daemon') {
16
+  $daemon = 1;
12 17
   daemonize();
13 18
 }
14 19
 
15
-my $device   = '/dev/ttyUSB0';
16
-my $cpucount = countcpus();
20
+say 'Found ' . countcpus() . ' CPUs' unless $daemon;
17 21
 
18
-# connect to the port
22
+# connect to the arduino
19 23
 my $port = Device::SerialPort->new($device)
20 24
   or die "Couldn't connect to $device";
21 25
 
@@ -25,48 +29,27 @@ $port->parity('none');
25 29
 $port->stopbits(1);
26 30
 
27 31
 while (1) {
28
-  my $load = getdegrees();
32
+  my $degrees = getdegrees();
29 33
 
30
-  # pack into a 16 bit int, I think
31
-  my $val = pack('s', $load);
34
+  # pack into a 16 bit int (i think) and send to the arduino
35
+  my $count_out = $port->write(pack 's', $degrees)
36
+    or die "Unable to write to $device";
32 37
 
33
-  my $count_out = $port->write($val);
34
-  # print "$load degrees, $count_out bytes\n";
38
+  say "$degrees degrees, $count_out bytes sent" unless $daemon;
35 39
   sleep 1;
36 40
 }
37 41
 
42
+sub getdegrees {
43
+  my ($load) = split / /, read_file('/proc/loadavg');
44
+  return 1 if $load >= countcpus();
45
+  return int(($load / countcpus()) * 180);
46
+}
38 47
 
39
-
48
+# sometimes i miss writing a language that's too clever for its own good
49
+{ my $count;
40 50
 sub countcpus {
41
-  open my $fh, '<', '/proc/cpuinfo'
42
-    or die "Can't get cpu info: $!";
43
-  my @cpuinfo = <$fh>;
44
-  close $fh;
45
-
46
-  my $count = 0;
47
-  foreach (@cpuinfo) {
48
-    $count++ if /^processor\s+: [0-9]/;
49
-  }
50
-
51
-  return $count;
51
+  $count //= grep { /^processor\s+: [0-9]/ } read_file('/proc/cpuinfo');
52 52
 }
53
-
54
-sub getdegrees {
55
-  open my $fh, '<', '/proc/loadavg'
56
-    or die "Can't get load: $!";
57
-  my $avg = <$fh>;
58
-  close $fh;
59
-
60
-  my ($load) = split / /, $avg;
61
-
62
-  my $percent;
63
-  if ($load < $cpucount) {
64
-    $percent = $load / $cpucount;
65
-  } else {
66
-    $percent = 1;
67
-  }
68
-
69
-  return int($percent * 180);
70 53
 }
71 54
 
72 55
 sub daemonize {

Loading…
Cancel
Save