Skip to content

Commit 5a0988a

Browse files
committed
add more options to get maidenhead
- get maidenhead (ruby implementation) - get maidenhead (c implementation) with gpspipe (to get latitute and longiture) - get maidenhead (c implementation) with passing argurments (latitude and longitude)
1 parent 0344d52 commit 5a0988a

File tree

4 files changed

+221
-23
lines changed

4 files changed

+221
-23
lines changed

conky/get-grid

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,60 @@
1-
#!/usr/bin/env ruby
1+
#!/usr/bin/env bash
2+
set -e
3+
[ -n "$DEBUG" ] && set -x
24

3-
require 'gpsd_client'
4-
require 'maidenhead'
5-
require 'socket'
6-
require 'json'
5+
LAT=$1
6+
LON=$2
7+
HOST=$3
8+
PORT=$4
79

8-
ft8call_port = 2237
10+
if [ "$HOST" = "" ]; then
11+
HOST=localhost
12+
fi
13+
if [ "$PORT" = "" ]; then
14+
PORT=2947
15+
fi
916

10-
gpsd = GpsdClient::Gpsd.new()
11-
gpsd.start()
12-
apicmd = {}
17+
compile_maidenhead() {
18+
gcc $HOME/bin/conky/maidenhead.c -o $HOME/bin/conky/mh
19+
chmod +x $HOME/bin/conky/mh
20+
}
1321

14-
# get maidenhead if gps is ready
15-
if gpsd.started?
16-
pos = gpsd.get_position
17-
maid = Maidenhead.to_maidenhead(pos[:lat], pos[:lon], precision = 5)
18-
# puts "lat = #{pos[:lat]}, lon = #{pos[:lon]}, grid = #{maid}"
19-
apicmd = {:type => "STATION.SET_GRID", :value => maid}
20-
end
22+
get_maidenhead_with_ruby() {
23+
grid=$(ruby get-grid.rb)
24+
}
2125

22-
puts "#{maid}"
26+
get_maidenhead_with_gpspipe() {
27+
if ! hash gpspipe 2>/dev/null; then
28+
sudo apt install -y gpsd-clients jq
29+
fi
30+
nema=$(gpspipe -w -r $HOST:$PORT | grep -m 1 TPV)
31+
# GPS Coordinate Set
32+
lat=$(echo $nema | jq -r '"\(.lat)"')
33+
[ -z "$lat" ] && echo "Latitude error?" && exit
34+
lon=$(echo $nema | jq -r '"\(.lon)"')
35+
[ -z "$lon" ] && echo "Longitude error?" && exit
36+
grid=$($HOME/bin/conky/mh -a $lat -l $lon)
37+
}
38+
39+
get_maidenhead_with_args() {
40+
grid=$($HOME/bin/conky/mh -a $LAT -l $LON)
41+
}
42+
43+
if hash gcc 2>/dev/null; then
44+
compile_maidenhead
45+
fi
46+
47+
if [ -z "$LAT" ]; then
48+
if hash ruby 2>/dev/null; then
49+
echo Get Maidenhead with Ruby
50+
get_maidenhead_with_ruby
51+
else
52+
echo Get Maidenhead with GpsPipe
53+
get_maidenhead_with_gpspipe
54+
fi
55+
else
56+
echo Get Maidenhead with Arguments
57+
get_maidenhead_with_args
58+
fi
59+
echo $grid
2360

conky/get-grid.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'gpsd_client'
4+
require 'maidenhead'
5+
require 'socket'
6+
require 'json'
7+
8+
ft8call_port = 2237
9+
10+
options = {:host => ARGV[0] ||= ENV["HOST"], :port => ARGV[1] ||= ENV["PORT"] }
11+
12+
gpsd = GpsdClient::Gpsd.new(options)
13+
gpsd.start()
14+
apicmd = {}
15+
16+
# get maidenhead if gps is ready
17+
if gpsd.started?
18+
pos = gpsd.get_position
19+
maid = Maidenhead.to_maidenhead(pos[:lat], pos[:lon], precision = 5)
20+
# puts "lat = #{pos[:lat]}, lon = #{pos[:lon]}, grid = #{maid}"
21+
apicmd = {:type => "STATION.SET_GRID", :value => maid}
22+
end
23+
24+
puts "#{maid}"
25+

conky/maidenhead.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* Taken from https://opensource.com/article/19/5/how-write-good-c-main-function */
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <libgen.h>
6+
#include <errno.h>
7+
#include <string.h>
8+
#include <getopt.h>
9+
10+
#define OPTSTR "va:l:h"
11+
#define USAGE_FMT "%s [-v] [-a latitude] [-l longitude] [-h]"
12+
#define ERR_LAT "lat"
13+
#define ERR_LON "lon"
14+
#define ERR_DO_GET_GRID "do_get_grid blew up"
15+
#define DEFAULT_PROGNAME "get_grid"
16+
17+
extern int errno;
18+
extern char *optarg;
19+
extern int opterr, optind;
20+
21+
typedef struct {
22+
int verbose;
23+
char *dst;
24+
double lon;
25+
double lat;
26+
} options_t;
27+
28+
int dumb_global_variable = -11;
29+
void usage(char *progname, int opt);
30+
int do_get_grid(options_t *options);
31+
void calcLocator(char *dst, double lat, double lon);
32+
33+
int main(int argc, char *argv[]) {
34+
int opt;
35+
options_t options = { 0, 0, 0.0, 0.0 };
36+
37+
opterr = 0;
38+
39+
while ((opt = getopt(argc, argv, OPTSTR)) != EOF)
40+
switch(opt) {
41+
case 'a':
42+
if (!(options.lat = strtod(optarg, &optarg) )) {
43+
perror(ERR_LAT);
44+
exit(EXIT_FAILURE);
45+
/* NOTREACHED */
46+
}
47+
break;
48+
49+
case 'l':
50+
if (!(options.lon = strtod(optarg, &optarg) )) {
51+
perror(ERR_LON);
52+
exit(EXIT_FAILURE);
53+
/* NOTREACHED */
54+
}
55+
break;
56+
57+
case 'v':
58+
options.verbose += 1;
59+
break;
60+
61+
case 'h':
62+
default:
63+
usage(basename(argv[0]), opt);
64+
/* NOTREACHED */
65+
break;
66+
}
67+
68+
if (do_get_grid(&options) != EXIT_SUCCESS) {
69+
perror(ERR_DO_GET_GRID);
70+
exit(EXIT_FAILURE);
71+
/* NOTREACHED */
72+
}
73+
fprintf(stdout, "%s", options.dst);
74+
free(options.dst);
75+
return EXIT_SUCCESS;
76+
}
77+
78+
void usage(char *progname, int opt) {
79+
fprintf(stderr, USAGE_FMT, progname ? progname:DEFAULT_PROGNAME);
80+
exit(EXIT_FAILURE);
81+
/* NOTREACHED */
82+
}
83+
84+
int do_get_grid(options_t *options) {
85+
if (!options) {
86+
errno = EINVAL;
87+
return EXIT_FAILURE;
88+
}
89+
90+
if (!options->lon || !options->lat) {
91+
errno = ENOENT;
92+
return EXIT_FAILURE;
93+
}
94+
95+
options->dst = malloc((sizeof(char) * 3) + 1); /* + 1 allows for null string terminator. */
96+
97+
calcLocator(options->dst, options->lat, options->lon);
98+
return EXIT_SUCCESS;
99+
}
100+
101+
/* https://ham.stackexchange.com/a/5599 */
102+
void calcLocator(char *dst, double lat, double lon) {
103+
int o1, o2, o3;
104+
int a1, a2, a3;
105+
double remainder;
106+
// longitude
107+
remainder = lon + 180.0;
108+
o1 = (int)(remainder / 20.0);
109+
remainder = remainder - (double)o1 * 20.0;
110+
o2 = (int)(remainder / 2.0);
111+
remainder = remainder - 2.0 * (double)o2;
112+
o3 = (int)(12.0 * remainder);
113+
114+
// latitude
115+
remainder = lat + 90.0;
116+
a1 = (int)(remainder / 10.0);
117+
remainder = remainder - (double)a1 * 10.0;
118+
a2 = (int)(remainder);
119+
remainder = remainder - (double)a2;
120+
a3 = (int)(24.0 * remainder);
121+
dst[0] = (char)o1 + 65;
122+
dst[1] = (char)a1 + 65;
123+
dst[2] = (char)o2 + 48;
124+
dst[3] = (char)a2 + 48;
125+
dst[4] = (char)o3 + 97;
126+
dst[5] = (char)a3 + 97;
127+
/* dst[0] = (char)o1 + 'A';
128+
dst[1] = (char)a1 + 'A';
129+
dst[2] = (char)o2 + '0';
130+
dst[3] = (char)a2 + '0';
131+
dst[4] = (char)o3 + 'A';
132+
dst[5] = (char)a3 + 'A';*/
133+
dst[6] = (char)0;
134+
}

functions/additional.function

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,16 @@ sudo dpkg -i $WSPKG
117117
# CONKY
118118
##################################
119119
CONKY(){
120-
if ! hash conky 2>/dev/null; then
121-
sudo apt install -y conky
122-
fi
120+
if ! hash conky 2>/dev/null; then
121+
sudo apt install -y conky
122+
fi
123123
touch $HOME/Documents/mylog.txt #conky will fail to load if this file doesn't exist
124124
touch /var/lib/misc/dnsmasq.leases #conky will fail to load if this file doesn't exist
125-
sudo apt-get install -y ruby2.5
126-
sudo gem install gpsd_client
127-
sudo gem install maidenhead
125+
if ! hash gcc 2>/dev/null; then
126+
sudo apt-get install -y ruby2.5
127+
sudo gem install gpsd_client
128+
sudo gem install maidenhead
129+
fi
128130
cp $MYPATH/conky/.conkyrc $HOME/.conkyrc
129131
sed -i "s/N0CALL/$CALL/" $HOME/.conkyrc
130132
# Add the rest of the code here so that it can be use for installation and locally

0 commit comments

Comments
 (0)