-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkm.pl
More file actions
executable file
·78 lines (62 loc) · 1.61 KB
/
km.pl
File metadata and controls
executable file
·78 lines (62 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
#
# This script can be used to retrieve the odometer wheel
# turns and convert them into kilometers/miles the bike
# has traveled.
#
# Edit the default values for the serial port and front
# wheel size you want to use.
#
use strict;
use warnings;
use Math::Trig;
use Getopt::Std;
use Device::SerialPort;
my %args = ();
# command line arguments
getopts("hmp:d:", \%args);
# default values
$args{'d'} = 26 unless defined $args{'d'};
$args{'p'} = '/dev/tty.usbserial-A403JXK2' unless defined $args{'p'};
# print help
if(defined $args{'h'} or not $args{'d'} =~ /^\d+\.?\d*$/) {
printf("-h print help\n");
printf("-m output distance in miles (default kilometers)\n");
printf("-p <port> serial device port to use\n");
printf("-d <diameter> bicycle front wheel diameter in inches\n");
exit 0;
}
# use SerialPort to configure
my $dev = Device::SerialPort->new($args{'p'});
$dev or die "can't open $args{'p'}\n";
# config
$dev->baudrate(9600);
$dev->databits(8);
$dev->parity("none");
$dev->stopbits(1);
$dev->handshake("none");
# flush
$dev->purge_rx();
# get filehandle
open FH, '+<', $dev->{'NAME'};
# send print
print FH "p\r";
# dump echo
<FH>;
# get reply
my $rotations = <FH>;
chomp $rotations;
# calculate distance
if($args{'m'}) {
my $circumference = $args{'d'} * pi;
my $distance = ($rotations * $circumference) / 63360;
printf("%.1f mi\n", $distance);
} else {
my $circumference = $args{'d'} * 0.0254 * pi;
my $distance = ($rotations * $circumference) / 1000;
printf("%.1f km\n", $distance);
}
# finish
close FH;
$dev->close();
exit 0;