Skip to content

Commit

Permalink
add more options to get maidenhead
Browse files Browse the repository at this point in the history
- it has pull #185 changes
- other improvements and
- implemented maidenhead grid locator on the C (conky/maidenhead.c) language:
      - get maidenhead (c implementation) with gpspipe (to get latitude and longitude) gcc is required. see conky/get-grid file.
      - get maidenhead (c implementation) with passing arguments (passing latitude and longitude as arguments) gcc is required. see conky/get-grid file.
      - get maidenhead (ruby implementation) fallback to this one if gcc is not installed (different OS maybe). see conky/get-grid.rb file.
  • Loading branch information
nandub committed Feb 5, 2021
1 parent 78edbfb commit 50233c2
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 67 deletions.
2 changes: 1 addition & 1 deletion conky/conky-7inch
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ ${font Arial:size=12}${color Yellow}${alignc}N0CALL
${voffset -30}
${font Arial:bold:size=08}${color White}Gridsquare ${alignr}${color Yellow}${execi 60 cat /run/user/1000/gridinfo.txt | cut -c1-8}
#${font Arial:bold:size=12}${color White}Radio Freq ${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq | cut -c1-6}
${color Yellow}LOCAL${alignr}ZULU
${voffset -20}
${color DarkSlateGray}${hr 2}
${color Yellow}LOCAL${alignr}ZULU
${font Arial:bold:size=08}${color White}${time %H:%M:%S}\
${alignr}${tztime Zulu %H:%M:%S}
${font Arial:bold:size=08}${color White}${time %d%b%y}\
Expand Down
3 changes: 1 addition & 2 deletions conky/conky-large
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ ${font Arial:bold:size=18}${color White}Gridsquare ${alignr}${color Yellow}${exe
#${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq VFOA | cut -c1-7}
#${font Arial:bold:size=18}${color White}Radio Freq B ${if_match "VFOB" == "${execi 5 $HOME/bin/conky/get-vfo}"}*$endif\
#${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq VFOB | cut -c1-7}

${color Yellow}LOCAL${alignr}ZULU
${voffset -45}
${color DarkSlateGray}${hr 2}
${voffset -35}
${color Yellow}LOCAL${alignr}ZULU
${font Arial:bold:size=25}${color White}${time %H:%M:%S}\
${alignr}${tztime Zulu %H:%M:%S}
${font Arial:bold:size=25}${color White}${time %d%b%y}\
Expand Down
2 changes: 1 addition & 1 deletion conky/conky-medium
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ ${font Arial:bold:size=12}${color White}Gridsquare ${alignr}${color Yellow}${exe
#${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq VFOA | cut -c1-7}
#${font Arial:bold:size=12}${color White}Radio Freq B ${if_match "VFOB" == "${execi 5 $HOME/bin/conky/get-vfo}"}*$endif\
#${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq VFOB | cut -c1-7}
${color Yellow}LOCAL${alignr}ZULU
${color DarkSlateGray}${hr 2}
${color Yellow}LOCAL${alignr}ZULU
${font Arial:bold:size=15}${color White}${time %H:%M:%S}\
${alignr}${tztime Zulu %H:%M:%S}
${font Arial:bold:size=15}${color White}${time %d%b%y}\
Expand Down
2 changes: 1 addition & 1 deletion conky/conky-small
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ ${font Arial:bold:size=08}${color White}Gridsquare ${alignr}${color Yellow}${exe
#${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq VFOA | cut -c1-7}
#${font Arial:bold:size=12}${color White}Radio Freq B ${if_match "VFOB" == "${execi 5 $HOME/bin/conky/get-vfo}"}*$endif\
#${alignr}${color Yellow}${execi 5 $HOME/bin/conky/get-freq VFOB | cut -c1-7}
${color Yellow}LOCAL${alignr}ZULU
${voffset -20}
${color DarkSlateGray}${hr 2}
${color Yellow}LOCAL${alignr}ZULU
${font Arial:bold:size=08}${color White}${time %H:%M:%S}\
${alignr}${tztime Zulu %H:%M:%S}
${font Arial:bold:size=08}${color White}${time %d%b%y}\
Expand Down
68 changes: 51 additions & 17 deletions conky/get-grid
Original file line number Diff line number Diff line change
@@ -1,23 +1,57 @@
#!/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
get_maidenhead_with_ruby
else
get_maidenhead_with_gpspipe
fi
else
get_maidenhead_with_args
fi
echo $grid

25 changes: 25 additions & 0 deletions conky/get-grid.rb
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}"

Empty file modified conky/get-vfo
100755 → 100644
Empty file.
134 changes: 134 additions & 0 deletions conky/maidenhead.c
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;
}
39 changes: 39 additions & 0 deletions conky/update
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

mkdir -p $HOME/bin/conky
cp -p $HOME/pi-build/conky/* $HOME/bin/conky/
chmod +x $HOME/bin/conky/get-grid $HOME/bin/conky/get-freq $HOME/bin/conky/grid $HOME/bin/conky/setconky

#Create files needed for autostart at login
#Fix issue https://github.com/km4ack/pi-build/issues/83

cat <<EOF > $HOME/.local/share/applications/conky.desktop
[Desktop Entry]
Name=Conky
Comment=Conky
GenericName=Conky Screen Background Monitor
Exec=conky
Icon=/home/pi/bin/conky/conky-logo.png
Type=Application
Encoding=UTF-8
Terminal=false
Categories=HamRadio
Keywords=Radio
EOF

ln -sf $HOME/.local/share/applications/conky.desktop $HOME/.config/autostart/conky.desktop

#####Add setconky to main menu
cat <<EOF > $HOME/.local/share/applications/setconky.desktop
[Desktop Entry]
Name=Conky-Prefs
Comment=Conky-Prefs
GenericName=Change Conky Preferences
Exec=/home/pi/bin/conky/setconky
Icon=/home/pi/bin/conky/conky-logo.png
Type=Application
Encoding=UTF-8
Terminal=false
Categories=Settings;DesktopSettings;GTK;X-LXDE-Settings;
Keywords=Radio,Conky
EOF
56 changes: 11 additions & 45 deletions functions/additional.function
Original file line number Diff line number Diff line change
Expand Up @@ -121,59 +121,25 @@ sudo locale-gen
# CONKY
##################################
CONKY(){
if ! hash conky 2>/dev/null; then
sudo apt install -y conky
fi
if ! hash conky 2>/dev/null; then
sudo apt install -y conky
fi
touch $HOME/Documents/mylog.txt #conky will fail to load if this file doesn't exist
touch /var/lib/misc/dnsmasq.leases #conky will fail to load if this file doesn't exist
sudo apt-get install -y ruby2.5
sudo gem install gpsd_client
sudo gem install maidenhead
if ! hash gcc 2>/dev/null; then
sudo apt-get install -y ruby2.5
sudo gem install gpsd_client
sudo gem install maidenhead
fi
cp $MYPATH/conky/.conkyrc $HOME/.conkyrc
mkdir -p $HOME/bin/conky
cp $MYPATH/conky/* $HOME/bin/conky/
chmod +x $HOME/bin/conky/get-grid $HOME/bin/conky/temp-conv $HOME/bin/conky/get-freq $HOME/bin/conky/grid
sed -i "s/N0CALL/$CALL/" $HOME/.conkyrc

#Line below is for https://github.com/km4ack/pi-build/issues/198
echo "#Update GPS maidenhead for conky" >> $TEMPCRON
echo "*/1 * * * * /home/pi/bin/conky/grid" >> $TEMPCRON

#Create files needed for autostart at login
#Fix issue https://github.com/km4ack/pi-build/issues/83

cat <<EOF > $HOME/.local/share/applications/conky.desktop
[Desktop Entry]
Name=Conky
Comment=Conky
GenericName=Conky Screen Background Monitor
Exec=conky
Icon=/home/pi/pi-build/conky/conky-logo.png
Type=Application
Encoding=UTF-8
Terminal=false
Categories=HamRadio
Keywords=Radio
EOF

ln -sf $HOME/.local/share/applications/conky.desktop $HOME/.config/autostart/conky.desktop

#####Add setconky to main menu
chmod +x $HOME/pi-build/conky/setconky

cat <<EOF > $HOME/.local/share/applications/setconky.desktop
[Desktop Entry]
Name=Conky-Prefs
Comment=Conky-Prefs
GenericName=Change Conky Preferences
Exec=/home/pi/pi-build/conky/setconky
Icon=/home/pi/pi-build/conky/conky-logo.png
Type=Application
Encoding=UTF-8
Terminal=false
Categories=Settings;DesktopSettings;GTK;X-LXDE-Settings;
Keywords=Radio,Conky
EOF

# Add the rest of the code here so that it can be use for installation and locally
bash $MYPATH/conky/update
}

##################################
Expand Down

0 comments on commit 50233c2

Please sign in to comment.