Browse Source

option for device, die gracefully on signals

(Well, _a_ signal.)
Brennen Bearnes 6 years ago
parent
commit
30f1ba0190
1 changed files with 20 additions and 1 deletions
  1. 20
    1
      loaduino.pl

+ 20
- 1
loaduino.pl View File

@@ -14,15 +14,17 @@ my $remote;
14 14
 my $refresh_rate = 10;
15 15
 my $daemon = 0;
16 16
 my $cpucount = 2; # default to 2 CPUs
17
+my $device = '/dev/ttyUSB0';
17 18
 
18 19
 my $getopt_result = GetOptions (
20
+  "device=s"  => \$device,       # string
19 21
   "refresh=i" => \$refresh_rate, # numeric
20 22
   "remote=s"  => \$remote,       # string
21 23
   "daemon"    => \$daemon,       # flag
22 24
   "cores=i"   => \$cpucount      # numeric
23 25
 );
24 26
 
25
-say my $device = '/dev/ttyUSB0';
27
+say "Talking to Arduino on $device";
26 28
 
27 29
 if ($daemon) {
28 30
   daemonize();
@@ -42,6 +44,9 @@ $port->baudrate(9600);
42 44
 $port->parity('none');
43 45
 $port->stopbits(1);
44 46
 
47
+# ^C
48
+$SIG{INT} = "cleanup";
49
+
45 50
 while (1) {
46 51
   my $degrees = getdegrees();
47 52
 
@@ -116,3 +121,17 @@ sub daemonize {
116 121
   setsid
117 122
     or die "Can't start a new sesssion: $!";
118 123
 }
124
+
125
+sub cleanup {
126
+  say "cleaning up";
127
+  $port->close() || warn "close failed";
128
+  undef $port;
129
+  die "murdered!";
130
+}
131
+
132
+END {
133
+  if (defined $port) {
134
+    cleanup();
135
+  }
136
+}
137
+

Loading…
Cancel
Save