-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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)
- Loading branch information
Showing
4 changed files
with
221 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,60 @@ | ||
#!/usr/bin/env ruby | ||
#!/usr/bin/env bash | ||
set -e | ||
[ -n "$DEBUG" ] && set -x | ||
|
||
require 'gpsd_client' | ||
require 'maidenhead' | ||
require 'socket' | ||
require 'json' | ||
LAT=$1 | ||
LON=$2 | ||
HOST=$3 | ||
PORT=$4 | ||
|
||
ft8call_port = 2237 | ||
if [ "$HOST" = "" ]; then | ||
HOST=localhost | ||
fi | ||
if [ "$PORT" = "" ]; then | ||
PORT=2947 | ||
fi | ||
|
||
gpsd = GpsdClient::Gpsd.new() | ||
gpsd.start() | ||
apicmd = {} | ||
compile_maidenhead() { | ||
gcc $HOME/bin/conky/maidenhead.c -o $HOME/bin/conky/mh | ||
chmod +x $HOME/bin/conky/mh | ||
} | ||
|
||
# get maidenhead if gps is ready | ||
if gpsd.started? | ||
pos = gpsd.get_position | ||
maid = Maidenhead.to_maidenhead(pos[:lat], pos[:lon], precision = 5) | ||
# puts "lat = #{pos[:lat]}, lon = #{pos[:lon]}, grid = #{maid}" | ||
apicmd = {:type => "STATION.SET_GRID", :value => maid} | ||
end | ||
get_maidenhead_with_ruby() { | ||
grid=$(ruby get-grid.rb) | ||
} | ||
|
||
puts "#{maid}" | ||
get_maidenhead_with_gpspipe() { | ||
if ! hash gpspipe 2>/dev/null; then | ||
sudo apt install -y gpsd-clients jq | ||
fi | ||
nema=$(gpspipe -w -r $HOST:$PORT | grep -m 1 TPV) | ||
# GPS Coordinate Set | ||
lat=$(echo $nema | jq -r '"\(.lat)"') | ||
[ -z "$lat" ] && echo "Latitude error?" && exit | ||
lon=$(echo $nema | jq -r '"\(.lon)"') | ||
[ -z "$lon" ] && echo "Longitude error?" && exit | ||
grid=$($HOME/bin/conky/mh -a $lat -l $lon) | ||
} | ||
|
||
get_maidenhead_with_args() { | ||
grid=$($HOME/bin/conky/mh -a $LAT -l $LON) | ||
} | ||
|
||
if hash gcc 2>/dev/null; then | ||
compile_maidenhead | ||
fi | ||
|
||
if [ -z "$LAT" ]; then | ||
if hash ruby 2>/dev/null; then | ||
echo Get Maidenhead with Ruby | ||
get_maidenhead_with_ruby | ||
else | ||
echo Get Maidenhead with GpsPipe | ||
get_maidenhead_with_gpspipe | ||
fi | ||
else | ||
echo Get Maidenhead with Arguments | ||
get_maidenhead_with_args | ||
fi | ||
echo $grid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'gpsd_client' | ||
require 'maidenhead' | ||
require 'socket' | ||
require 'json' | ||
|
||
ft8call_port = 2237 | ||
|
||
options = {:host => ARGV[0] ||= ENV["HOST"], :port => ARGV[1] ||= ENV["PORT"] } | ||
|
||
gpsd = GpsdClient::Gpsd.new(options) | ||
gpsd.start() | ||
apicmd = {} | ||
|
||
# get maidenhead if gps is ready | ||
if gpsd.started? | ||
pos = gpsd.get_position | ||
maid = Maidenhead.to_maidenhead(pos[:lat], pos[:lon], precision = 5) | ||
# puts "lat = #{pos[:lat]}, lon = #{pos[:lon]}, grid = #{maid}" | ||
apicmd = {:type => "STATION.SET_GRID", :value => maid} | ||
end | ||
|
||
puts "#{maid}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* Taken from https://opensource.com/article/19/5/how-write-good-c-main-function */ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <libgen.h> | ||
#include <errno.h> | ||
#include <string.h> | ||
#include <getopt.h> | ||
|
||
#define OPTSTR "va:l:h" | ||
#define USAGE_FMT "%s [-v] [-a latitude] [-l longitude] [-h]" | ||
#define ERR_LAT "lat" | ||
#define ERR_LON "lon" | ||
#define ERR_DO_GET_GRID "do_get_grid blew up" | ||
#define DEFAULT_PROGNAME "get_grid" | ||
|
||
extern int errno; | ||
extern char *optarg; | ||
extern int opterr, optind; | ||
|
||
typedef struct { | ||
int verbose; | ||
char *dst; | ||
double lon; | ||
double lat; | ||
} options_t; | ||
|
||
int dumb_global_variable = -11; | ||
void usage(char *progname, int opt); | ||
int do_get_grid(options_t *options); | ||
void calcLocator(char *dst, double lat, double lon); | ||
|
||
int main(int argc, char *argv[]) { | ||
int opt; | ||
options_t options = { 0, 0, 0.0, 0.0 }; | ||
|
||
opterr = 0; | ||
|
||
while ((opt = getopt(argc, argv, OPTSTR)) != EOF) | ||
switch(opt) { | ||
case 'a': | ||
if (!(options.lat = strtod(optarg, &optarg) )) { | ||
perror(ERR_LAT); | ||
exit(EXIT_FAILURE); | ||
/* NOTREACHED */ | ||
} | ||
break; | ||
|
||
case 'l': | ||
if (!(options.lon = strtod(optarg, &optarg) )) { | ||
perror(ERR_LON); | ||
exit(EXIT_FAILURE); | ||
/* NOTREACHED */ | ||
} | ||
break; | ||
|
||
case 'v': | ||
options.verbose += 1; | ||
break; | ||
|
||
case 'h': | ||
default: | ||
usage(basename(argv[0]), opt); | ||
/* NOTREACHED */ | ||
break; | ||
} | ||
|
||
if (do_get_grid(&options) != EXIT_SUCCESS) { | ||
perror(ERR_DO_GET_GRID); | ||
exit(EXIT_FAILURE); | ||
/* NOTREACHED */ | ||
} | ||
fprintf(stdout, "%s", options.dst); | ||
free(options.dst); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
void usage(char *progname, int opt) { | ||
fprintf(stderr, USAGE_FMT, progname ? progname:DEFAULT_PROGNAME); | ||
exit(EXIT_FAILURE); | ||
/* NOTREACHED */ | ||
} | ||
|
||
int do_get_grid(options_t *options) { | ||
if (!options) { | ||
errno = EINVAL; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (!options->lon || !options->lat) { | ||
errno = ENOENT; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
options->dst = malloc((sizeof(char) * 3) + 1); /* + 1 allows for null string terminator. */ | ||
|
||
calcLocator(options->dst, options->lat, options->lon); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
/* https://ham.stackexchange.com/a/5599 */ | ||
void calcLocator(char *dst, double lat, double lon) { | ||
int o1, o2, o3; | ||
int a1, a2, a3; | ||
double remainder; | ||
// longitude | ||
remainder = lon + 180.0; | ||
o1 = (int)(remainder / 20.0); | ||
remainder = remainder - (double)o1 * 20.0; | ||
o2 = (int)(remainder / 2.0); | ||
remainder = remainder - 2.0 * (double)o2; | ||
o3 = (int)(12.0 * remainder); | ||
|
||
// latitude | ||
remainder = lat + 90.0; | ||
a1 = (int)(remainder / 10.0); | ||
remainder = remainder - (double)a1 * 10.0; | ||
a2 = (int)(remainder); | ||
remainder = remainder - (double)a2; | ||
a3 = (int)(24.0 * remainder); | ||
dst[0] = (char)o1 + 65; | ||
dst[1] = (char)a1 + 65; | ||
dst[2] = (char)o2 + 48; | ||
dst[3] = (char)a2 + 48; | ||
dst[4] = (char)o3 + 97; | ||
dst[5] = (char)a3 + 97; | ||
/* dst[0] = (char)o1 + 'A'; | ||
dst[1] = (char)a1 + 'A'; | ||
dst[2] = (char)o2 + '0'; | ||
dst[3] = (char)a2 + '0'; | ||
dst[4] = (char)o3 + 'A'; | ||
dst[5] = (char)a3 + 'A';*/ | ||
dst[6] = (char)0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters