Saturday, 13 May 2017

scout compass bearing game generator - python script

I made a script for generating post labels and bearing following instructions around paths in a compass bearing game, it is here:
https://github.com/Gaoithe/scout_compass_bearing_game

scout_compass_bearing_game

Calculate bearings and generate: post labels, bearing following instructions and game cheat sheet instructions.

Scout compass bearing challenge game

Posts are set out in a circle a good distance apart. One post starting at magnetic North, other posts at each compass point N NNE NE ENE E ESE . . . e.t.c. Each post is labeled with name and has a scrambled alphabet in a grid.
Scouts receive an instruction sheet with start post and series of bearings to follow. At each post visited a letter of a word is obtained.
A simple grid reference for each post describes how to obtain a letter. The grid reference at N post comes from the Irish grid reference system. The letters in the grid are scrambled at each other post. https://en.wikipedia.org/wiki/Irish_grid_reference_system#/media/File:Irish_Grid.svg
A full scrambled alphabet is on each post. This allows a large combination of word instructions to be used. It allows interesting quotes or song lyrics to be used as a challenge for a group of scouts.
e.g. of post label with post name and scrambled alphabet in grid:
########################################
#
# POST: 337.5 => NNW
#
#        1   2   3   4   5 - Easting
#
#    5   Y   H   T   M   G   
#    4   W   B   C   I   U   Q   
#    3   K   P   Z   R   L   
#    2   S   X   O   N   J   
#    1   F   V   E   D   A   
#     \
#      Northing
#
########################################
e.g. of instruction sheet for a two letter word:
########################################
#
# Word 0
# START at POST: 67.5 => ENE
#
# 0. Follow bearing 123.75 to next Post,
#    Record letter @ Easting 1, Northing 1  _______
# 0. Follow bearing 146.25 to next Post,
#    Record letter @ Easting 6, Northing 4  _______
#
########################################
e.g. of portion of cheat sheet
########################################
# Cheat Sheet
#
# Word 0 is We, post path is [67.5, 45.0, 22.5]
#    DEBUG post:45.0 c:W
#    DEBUG post:22.5 c:e
#
.
.
.
A python script is used . . . 16 posts are currently used. 8 posts or other number can wasily be used with script adjustment. The word list or quote used can be changed in script. TODO: map and allow arbitrary positioning of posts e.g. in rectangle or other shape would be suitable for a permanently marked compass bearing challenge.
The script is quite messy currently . . WIP . . beware of roadworks and dragons.

FILES:

The script, run:
  • python scout_bearing_to_compasspoint.py 
Numpy is used a little bit, matplotlib is called but not needed to generate game instructions, there is some not used curses at end of script.
Output of script text post labels and word instructions:
  • scout_bearing_to_compasspointDEBUG.txt
Put the script in an open-office doc, insert a section around the post labels and in format->section make it two columns. Do the same around word instructions. Adjust labels/instructions so they do not wrap around a column or page. print. Cut up. Attach labels to posts. Have fun!
See files:
  • scout_compass_bearing_posts.odt
  • scout_compass_bearing_posts.pdf 




Monday, 10 April 2017

simple bash 'logrotate'

'logrotate' = not a logrotate but more of a multiple result handle
e.g. test results, core files, log files

'infinate' (max bash integer) 'logrotate' pattern for bash:


    # multiple runs of same test, . . logrotate test result
    [[ -e dir/testresult_${testname}_${host}.xml ]] && {
        K=$(find dir/testresult_${testname}_*_${host}.xml 2>/dev/null |wc -l)
        ((K++))
        mv dir/testresult_${testname}_{,${K}_}${host}.xml
    }

Coupled with test harvest e.g. . . 
rsync -avzh --remove-source-files ${user}@${host}:XML/*${host}.xml ./ || true

optional || true so that lack of xml files doesn't mark job/script as fail


Sunday, 12 March 2017

Scout County Survivor - Air drop Token hunt challenge

3 rock Scout County
Survivor Token Map Challenge


An aircraft has been heard flying overhead. Your team catch
a brief glimpse of the aircraft through storm clouds. It is being
severely buffeted by strong winds. You see multiple things
are dropped. Some items fall quickly, other items blow in the
wind. Shortly afterwards some leaflets are seen floating down
from the sky.

The leaflets say:

Dear Survivors,

Please be assured help is on the way. We hope that rescue
teams will reach you in approx. 24 hours.
Due to rough terrain and poor visibility we have had difficulty
pinpointing your location from the air. Supplies have been
dropped at several locations that we think you may be able to
reach. Please see approximate locations of drops on the
map.

DO NOT RISK INJURY TO YOURSELVES as you will have
to survive independantly until rescue!

(IN REAL LIFE: Look for triangular survivor tokens. You should retrieve the token, bring it to Scout Leaders Area and redeem it for items recovered from supply drop cache.)


PDF and Inkscape SVGZ TOKENS: Food, Shelter, Fire, First Aid

https://drive.google.com/drive/folders/0B03nt669Z-O0Mms0UGhfRHU3NXM?usp=sharing

Tuesday, 17 January 2017

weekend B(ullet?)Log, fix guitar, bike maintenance, photo sync, visit Limerick

Thur
 x repair Maeve's guitar using wood glue, leave for 48hrs

Sat
 - Drove down & back from Limerick. Met Eimear&Stephen&Aoife.
 x rsync'd photos james laptop -> USB drive
 x look for Maeve school photos -  found a few cute ones but not for sending to journal
 x Strung/tuned guitar. But as strings tightened glue stretched :-(

Sun
  x fix guitar with superglue
  x fix Kate bike one set brake pads replaced
  x fix Maeve bike brake squeal (brake cables oiled, tightened // front brake pads fully off and back on - nuts were in ok order // BALANCED each side of brakes front and back (spring adjusted screws both sides)
  x fix James bike disc brake pads (from Halfords) back brake was worn just down to metal, front brake less worn but replaced
  x fixed 3 bike tubes
  x Strung/tuned guitar. But as strings tightened glue stretched :-(

Mon
  x Strung/tuned guitar. But the superglue is not holding.
  x Repair guitar using glue + bracket. (cut the bracket)
  x Strung/tuned guitar. repair is holding.
  . guitar bracket flush, round bottom corner, hammer a curve in it
  - need a mini-anvil :)

Tue
  x morn: tuned guitar - it's starting to hold in tune ok, Maeve tested it

Books
  x gave rationality.pdf Harry Potter to Kate/...
  . reading graphic novel last man Y

for Wed/future . . .
  . Glenmalure hike plan
  . scouts plan hike for Jan/Feb ?
  . scouts PLC . . long term plan . . adventures, skills, special interest, Survivor, County Cup
  . scouts Annual Camp plans
  . scouts County meetings/activities
  . scouts James leader training
  . Cycling submission: DART crossing/Booterstown
  . DLR fernhill submission


Looked @ Bullet Journal mechanics:

http://bulletjournal.com/get-started/

https://www.buzzfeed.com/rachelwmiller/how-to-start-a-bullet-journal

https://youtu.be/fm15cmYU0IM



This blog acts as an occasional diary or article log for me.
I have a book for Scouts and work which is diary/planner journal.
I have often scrappy bits of paper with task lists or planning ideas.
Also task manager app, google calendar, gmail, .. .



 

Thursday, 3 November 2016

rsync from android MTP . . freezy/hangy problems. s/rsync/crude script/

Samsung Galaxy Nexus.
3.8G of photos in internal storage.
Options are MTP "mounting" or PTP.
Ubuntu 14.xx

1.

First attempt copying using file manager . . . started out okay. Reported 3.8G will be copied over in 7 mins . . 15 mins . . later 1 hour and x mins. And it was getting stuck / hanging up (looked like at the same point).

Unplug/replug USB did not bring back MTP connection after hang. Needed to go to phone USB computer connection Settings and un-check and then re-check MTP to recover it.

2.

rsync
cd /run/user/1000/gvfs/
cd mtp* # e.g. mtp::host=%5Busb%3A001%2C017%5D/
cd "Internal storage/DCIM"
rsync -avzhP ./ ~/Pictures/201611FionnOldMobilePhone/DCIM/
see errors like this:

          1.59M 100%    1.67MB/s    0:00:00 (xfr#1834, to-chk=957/2800)
Camera/IMG_20130808_123846.jpg
          1.98M 100%    1.25MB/s    0:00:01 (xfr#1835, to-chk=956/2800)
rsync: read errors mapping "/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C017%5D/Internal storage/DCIM/Camera/IMG_20130808_123846.jpg": No data available (61)
Camera/IMG_20130808_123902.jpg
          1.43M 100%    1.57MB/s    0:00:00 (xfr#1836, to-chk=955/2800)
rsync: read errors mapping "/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C017%5D/Internal storage/DCIM/Camera/IMG_20130808_123902.jpg": No data available (61)
Camera/IMG_20130808_124320.jpg
          1.58M 100%    1.21MB/s    0:00:01 (xfr#1837, to-chk=954/2800)
rsync: read errors mapping "/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C017%5D/Internal storage/DCIM/Camera/IMG_20130808_124320.jpg": No data available (61)
Camera/IMG_20130808_124435.jpg
          1.54M 100%    2.00MB/s    0:00:00 (xfr#1838, to-chk=953/2800)
rsync: read errors mapping "/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C017%5D/Internal storage/DCIM/Camera/IMG_20130808_124435.jpg": No data available (61)
Camera/IMG_20130808_124530.jpg
          1.90M 100%    1.54MB/s    0:00:01 (xfr#1839, to-chk=952/2800)
rsync: read errors mapping "/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C017%5D/Internal storage/DCIM/Camera/IMG_20130808_124530.jpg": No data available (61)
Camera/IMG_20130808_124625.jpg
         32.77K   1%  126.98kB/s    0:00:17 
. . restarted . .
. . stuck again . . 

. . seemed to be getting stuck on the same files . . 
 
These errors seem to be encountered if rsyncing from files which get changed while sync happens.  I think android should not be changing them. I don't think MTP would expose corrupt disk problem ? Something like antivirus locking the files should not be problem. Anyway, MTP connection a bit flaky . . maybe something on the android side is locking files or MTP drivers are a bit funny.
 
3.
 
Tried rsync with --remove-source-files on whole and on file selections (e.g. Camera/IMG_201606*) still got stuck. Each time after hang needed to go into droid unselect MTP and select again to connect again. At least files to be checked/transferred are being reduced now by removing them after they are synced.



    rsync -avzhP --remove-source-files ./ ~/Pictures/201611FionnOldMobilePhone/DCIM/

4.
 
rsync was getting stuck on the same file all the time. cp seemed to have less of a hanging problem. more tolerant of read errors? So made a script to do copy and remove if copy successful.

    # Script using cp -p (and remove files as we go)
    DEST=/home/jamesc/Pictures/201611FionnOldMobilePhone/DCIM/Camera/
    cd /run/user/1000/gvfs/mtp*/Intern*/DCIM
    FILES=$(ls Camera/)
    for f in $FILES; do echo -n $f; cp -p Camera/$f $DEST/ && rm Camera/$f && echo success; done

This was run in a while true loop periodically recovering MTP connection when it hung.
 
      while true; do ~/bin/sync_flaky_mtp.sh; sleep 1; done

The script saw some funny errors e.g. :
 
    IMG_20130814_113108.jpgsuccess
    IMG_20130815_145354.jpgsuccess
    IMG_20130815_145455.jpgcp: error reading ‘Camera/IMG_20130815_145455.jpg’: Input/output error
    cp: failed to extend ‘/home/jamesc/Pictures/201611FionnOldMobilePhone/DCIM/Camera/IMG_20130815_145455.jpg’: Input/output error
    IMG_20130815_145500.jpgsuccess
    IMG_20130815_151552.jpgsuccess
    . . .
    IMG_20130824_095526.jpgsuccess
    IMG_20130824_095537.jpg

And it would hang . . . periodically recovered MTP connection as already described.
 
5.
 
The script evolved . . . 

    Binary files Camera/IMG_20160104_095626.jpg and /home/jamesc/Pictures/201611FionnOldMobilePhone/DCIM/Camera//IMG_20160104_095626.jpg differ
    IMG_20160104_100128.jpgalready
    RM
    IMG_20160104_100157.jpgalready

       #!/bin/bash
DEST=/home/jamesc/Pictures/201611FionnOldMobilePhone/DCIM/Camera/
cd /run/user/1000/gvfs/mtp*/Intern*/DCIM
FILES=$(ls Camera/)
#echo FILES=$FILES
ls Camera/ |wc -l
for f in $FILES; do
   echo -n $f;
   if [[ ! -e $DEST/$f ]]; then
      # the timeout doesn't work on frozen mtp file
             # we run this full script in a while true; do ~/bin/sync_flaky_mtp.sh; sleep 1; done loop
             # and periodically visit, if stuck unlock phone, deselect MTP and select it again
             # (to interrupt and restart MTP connection)
             # simply unplug & replug doesn't bring MTP connection back ok
      timeout 20s cp -p Camera/$f $DEST/ && rm Camera/$f && echo success;
   else
      echo already done by earlier script which did not tidy up;
      diff -u Camera/$f $DEST/$f
      if [[ $? == 0 ]]; then
         rm Camera/$f && echo RM
      else
         # doing this copy on the weird files causes nasty freezy freezes.
         #cp -p Camera/$f $DEST/$f && rm Camera/$f && echo success;
                # ALOT of files found to be a few bytes different.
                # another copy caused hanging . . . so if dest file kindof looks ok we just remove source
         identify $DEST/$f && rm Camera/$f && echo RM
      fi
   fi;
done

beep

    # a few bytes in size different
    ls -al Camera/IMG_20150117_171621.jpg $DEST/Camera/IMG_20150117_171621.jpg
      -rw------- 1 jamesc jamesc 1277901 Jan 17  2015 Camera/IMG_20150117_171621.jpg
      -rwxr----- 1 jamesc jamesc 1275306 Nov  3 01:09 $DEST/Camera/IMG_20150117_171621.jpg
 
    # copy again ? results in same size difference, date/time fixed up though
    cp -p Camera/IMG_20150117_171621.jpg $DEST/Camera/IMG_20150117_171621.jpg
    ls -al Camera/IMG_20150117_171621.jpg $DEST/Camera/IMG_20150117_171621.jpg
      -rw------- 1 jamesc jamesc 1277901 Jan 17  2015 Camera/IMG_20150117_171621.jpg
      -rw------- 1 jamesc jamesc 1275306 Jan 17  2015 $DEST/Camera/IMG_20150117_171621.jpg
    diff -u Camera/IMG_20150117_171621.jpg $DEST/Camera/IMG_20150117_171621.jpg
      Binary files Camera/IMG_20150117_171621.jpg and $DEST/Camera/IMG_20150117_171621.jpg differ

So EVENTUALLY by removing files we had already copied we had less and less to do and got through all files. Phew. And yay.

    du -h $DEST/.. --max-depth=0
      3.6G    /home/jamesc/Pictures/201611FionnOldMobilePhone/DCIM/Camera//..


Various other reading . . . 



Tuesday, 18 October 2016

char c; int i; c[&i]; // index a simple var using pointer in c compiles (and runs often without segfault)


    char c;
    int i;
    printf("%02x",c[&i]);
    //printf("%02x",c[i]); // doesn't compile, error, cannot access c as array
    // "error: subscripted value is neither array nor pointer nor vector"


c[&i] there is SOOOO BAD and WROOONG. Semantically wrong. But apparently not syntactically wrong in c.

Why does it compile?
Because 1. array[i] == *(array+i) == *(i+array) == i[array]
And 2. c casts char to int happily

In case of c[&i] c is the index added to pointer &i.
Now &i + value of c indexes an undefined area of memory so it is not good to use this!!


    char c;
    int i[10];
    // normal access into array (bad to use a char as array index though)
    for(c=0;c<10;c++) printf("%02x",i[c]); // normal-ish
    for(c=0;c<10;c++) printf("%02x",*(i+c)); // equivalent pointer arithmetic
    for(c=0;c<10;c++) printf("%02x",*(c+i)); // equivalent
    for(c=0;c<10;c++) printf("%02x",c[i]);    // allowed BUT NOT NORMAL



    char c;
    int i;
    int buffer[20]; // buffer helps prog not seg fault!
    // abnormal access into memory at offset of (int)c from &i
    //for(c=0;c<10;c++) printf("%02x",&i[c]); // COMPILE ERROR
    for(c=0;c<10;c++) printf("%02x",*(&i+c)); // a bit weird
    for(c=0;c<10;c++) printf("%02x",*(c+&i)); // not normal
    for(c=0;c<10;c++) printf("%02x",c[&i]);    // but it compiles and runs(mostly)

    for(c=0;c<10;c++) printf("%02x,",i[&c]); // this is all
    printf(" ");
    for(c=0;c<10;c++) printf("%02x,",*(i+&c)); // a bit weird
    printf(" ");
    for(c=0;c<10;c++) printf("%02x,",*(&c+i)); // not normal
    //for(c=0;c<10;c++) printf("%02x,",&c[i]);    // COMPILE ERROR
    printf("\n");



While answering this question:
http://stackoverflow.com/questions/40116073/c-duplicate-character-character-by-character/40117178#40117178

Asked this question:
http://stackoverflow.com/questions/40118549/in-c-ci-compiles-where-c-and-i-are-variables-not-arrays-pointers-but-i