Category Archives: Programming

Clock face worksheet

With a little extra effort the clock face generator can produce a worksheet with several different clock faces on a page. The page is produced with Freemarker / LaTeX. Producing vector graphics for inclusion in the LaTeX document instead of bitmaps is done with FreeHEP.

Just in case you missed it, this link will download a PDF document for you with different times every time!

Enjoy!

Tell the time with online clock face test

Yet another homework-inspired addition to the API – a clock face image generator. My son brought home a worksheet from school with badly-drawn clock faces on it. On some clock faces the minute hand was pointing to the “6” (half-past) while the hour hand pointed directly at the 3, when it should have pointed either half way between 2-3 or half way between 3-4.

The clock-face image URL creates a random clock face if no arguments are present. Query arguments are ‘time’ to specify time in milliseconds since midnight. A ‘resolution’ argument controls the resolution of randomly-generated times and also to hide the seconds hand for anything greater than second. Values for ‘resolution’ are one of [second, minute, fives, quarter, half, hour]. The ‘width’ argument is a 3-digit size in pixels of the generated image.

A .json API URL returns some data useful for embedding the clock face as an interactive test:





What is the time?

Server-side JSON/image creation: partitioning maths homework

Another homework-inspired demo of generating JSON + images on the server. This kind of thing can also easily be done with client side scripting. The advantages of doing it on the server is simplified client-side code (for older / less powerful devices) and nothing valuable on the client side for pirates! Arrr.

A script embedded in the page source requests a JSON response containing a sum with two ‘hundred square’ images showing the summands. Also in the response is an URL for an image showing a method of working out the sum using tens-and-units partitioning.

Enjoy!

What is the sum of ? and ??

NBC/NXC on the LEGO NXT “Error: Undefined Identifier printf”

This had me mystified for a few hours: converting some old C code that worked on BrickOS with the gcc toolchain to use the Not-eXactly-C from bricxcc. The c sources must be compiled with the Next Byte Code compiler nbc. The c source file must have the file extension .nxc. I could see in the NXC Programmer’s Guide that printf is listed as a “Standard-C API function“, that nxc has a ‘cstdio API‘ which contains printf and a #include preprocessor command, but I couldn’t find a ‘cstdio.h’ anywhere.

The solution is to check the nbc command line arguments with:

nbc -help

and see the ‘-EF’ switch for ‘Enhanced Firmware’. A command line like:

nbc -EF testprintf.nxc -O=testprintf.rex

will JustWork™

Starting a class with One-JAR that’s not your Main-Class

I use One-JAR to package a number of .jars from different project into a single jar file which is my distributable application. Within that jar file are a multitude of utility classes. To run my application I use

java -jar myonejar.jar

which launches my application’s main class com.firtl.MainClass just fine. I’d also like to be able to run some utility classes from the same jar like this:

java -cp myonejar.jar com.firtl.UtilityClass input1.bin input2.bin

This doesn’t work because One-JAR produces a jar file for an application class com.simontuffs.onejar.Boot – your own application’s jar file ends up in a /lib ‘directory’ in the jar file. You need to tell the Boot class to load and execute your utility class. You can pass command-line ‘switches’ to the Boot class via the Java system property facility. One-JAR supports a few, some useful help text is available by:

java -jar myonejar.jar --one-jar-help

The system property you need to set is one-jar.main.class – make sure you set the property before -jar:

java -Done-jar.main.class=com.firtl.UtilityClass -jar myonejar.jar input1.bin input2.bin

One-JAR’s Boot class will load UtilityClass and pass {“input1.bin”, “input2.bin”} to its main method. Easy once you know how!