File: track-get.pl

package info (click to toggle)
xastir 2.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,860 kB
  • sloc: ansic: 126,160; perl: 7,792; makefile: 448; sh: 228; sql: 102
file content (161 lines) | stat: -rwxr-xr-x 4,479 bytes parent folder | download | duplicates (5)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/perl

# Copyright (C) 2000-2012  Curt Mills, WE7U
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#

# This script will ask for an item designator, then create a file
# with that name + ".log" in the ~/.xastir/logs directory.  The
# file will contain APRS items created from the downloaded Garmin
# tracklog.  Reading that log file with Xastir will result in an
# object with a track being displayed on the map screen.

# You may wish to bump up MAX_TRACKS in db.h to 1024 in order to see
# the entire log, but be careful with Xastir's memory image growing
# too fast if you're connected to the 'net after making this change.

# This script uses the GPS::Garmin module which can be obtained
# from http://sourceforge.net/projects/perl-gps/


use GPS::Garmin;
use IO::File;


# Flush STDOUT
$| = 1;


# Hard-coded "from" callsign name.  This becomes the "sending"
# callsign for the APRS Items.
$from = "SAR";


# Ask for the item name.  This will turn into the 3-9 character APRS
# item name.
printf("\n\nEnter item designator: Length 1-9, all except '!' and '_' allowed, case respected:\n\n");
printf("\t123456789\n\t");
$name = <>;
chomp $name;
if (length($name) < 1) {
  die "Must be at least one character!\n";
}
if (length($name) > 9) {
  $name =~ s/(.{9}).*/\1/;  # Terminate it at 9 chars
}
if (length($name) < 3) {
  $name = $name . "   ";    # Pad the name with spaces
  $name =~ s/(.{3}).*/\1/;  # Terminate it at 3 chars
}

# We need to enforce the naming restrictions for APRS Items.  '!'
# and '_' are not allowed, anything else that's printable ASCII is
# ok.
@j = split(//, $name);
for ($i = 0; $i < length($name); $i++) {
#printf("$i  $j[$i]\n");
  if (     ($j[$i] lt ' ')
        || ($j[$i] gt '~')
        || ($j[$i] eq '!')
        || ($j[$i] eq '_') ) {
    $j[$i] = ' ';
  }
}
$name = join("",@j);

$filename = "~/.xastir/logs/$name.log";

# Get rid of spaces in the filename
$filename =~ s/\s+//g;

# Expand the tilde into the home directory
$filename =~ s{ ^ ~ ( [^/]* ) }
              { $1
                    ? (getpwnam($1))[7]
                    : ( $ENV{HOME} || $ENV{LOGDIR}
                         || (getpwuid($>))[7]
                      )
}ex;

printf("\nThe item designator will be: ($name)\n");
printf("The output filename will be: ($filename)\n\n");
printf("Connect Garmin GPS to /dev/ttyS0 (COM1), set it to Garmin/Garmin mode.\n");
printf("Press <ENTER> to proceed with download, Ctrl-C to abort\n");
<>;


# Create a file to hold the data
$output = IO::File->new("> $filename")
  or die "Couldn't open $filename for writing: $!\n";


$gps = new GPS::Garmin( 'Port' => '/dev/ttyS0',
                       'Baud' => 9600,
                      ) or die "Unable to connect to receiver: $!";


# Transfer trackpoints:
$i = 0;
$gps->prepare_transfer("trk");
while ($gps->records)
{
  ($lat,$lon,$time) = $gps->grab;
#  printf("%f %f %d\n",$lat,$lon,$time);
  if ($lon < 0) {
    $londeg = sprintf("%d",-$lon);
    $lonmin = (-$lon - $londeg) * 60;
    $lonchar = "W";
  }
  else {
    $londeg = sprintf("%d",$lon);
    $lonchar = "E";
    $lonmin = ($lon - $londeg) * 60;
  }
  if ($lat < 0) {
    $latdeg = sprintf("%d",-$lat);
    $latmin = (-$lat - $latdeg) * 60;
    $latchar = "S";
  }
  else {
    $latdeg = sprintf("%d",$lat);
    $latmin = ($lat - $latdeg) * 60;
    $latchar = "N";
  }

  # Write an APRS Item for each trackpoint
  printf($output "%s>APRS:)%s!%02d%05.2f%s/%03d%05.2f%s%s\n",
    $from,
    $name,
    $latdeg,
    $latmin,
    $latchar,
    $londeg,
    $lonmin,
    $lonchar,
    "/");

  if (! ($i % 10) ) {
    printf("$i ");
  }
  $i++;
}

printf("\n\nThe data has been saved in: ($filename)\n");
printf("Please open this logfile with Xastir to display the track\n");
printf("!!!Remember to set your GPS back to NMEA mode for APRS!!!\n");