Morse Code – Revisited using the BBC micro:bit

As mentioned last week, I have found a new way to provide a front end processor for my Pi, the BBC micro:bit. This week, I have started putting the new system through its paces: The microbit class in our GitHub repository has been beefed up to make it more resilient in the case where serial I/O is not initialised, or the MicroPython REPL is not running, and I have added a trivial Python program which can be used to intialise the REPL using the mu editor.

To test it all, I have revisited the Morse Code example, originally written to use GPIO support provided by Quick2Wire, to see if I could get it up and running with my new infrastructure. The micro:bit has a 5×5 LED display which makes the result a bit more impressive to look at:

 

The new code is significantly simpler than the original, for a couple of reasons:

  1. Making calls to the micro:bit API via MicroPython is easier than using GPIO via a C library.
  2. Version 15.0 of Dyalog APL contains new system functions for working with text files, so we no longer need the “Files” library, we can just use the built-in ⎕NGET function to read the contents of a file.

Raspberry APL Pi and Python on the micro:bit

A couple of years ago I spent many happy hours writing APL code to control robots which each embedded a Raspberry Pi. It was fun but it was a bumpy ride – my interest eventually faded when I discovered that it was just too difficult to make sense of raw accelerometer data, which I was hoping to use for precise navigation. After I managed to fry my MPU-9150, I decided to pursue other interests, as they say.

Recently, Romilly Cocking has been waxing lyrical about a number of new components which he thinks will make the whole experience more enjoyable – and I have decided to dive back in. So far, it has indeed been a very pleasant experience.

The Raspberry Pi Model 3

The Raspbery Pi Model 3While I was away, the Raspberry Pi and the software available for it have come a VERY long way. The speed of a Pi 3 makes it a perfectly usable computer. I am writing this blog entry on the Pi using a standard Chromium browser. APL starts up immediately, using our new graphical IDE. It is quite simply amazing!

The BBC micro:bit and MicroPython

The first generation of APL-based robots used an Arduino to manage the motors and sensors because timing was too critical for the Pi to handle it. I spent some rather unhappy hours writing a control program in C, debugging that without any tools. You could not even do “printf debugging” to the serial port because that interfered with the timing so much that it stopped everything from working.

Romilly suggested I take a look at the micro:bit, and since I have been using an interactive REPL all my life, he suggested that I run MicroPython on it.

He was right, I’m loving it!

Getting Started

I purchased the Kitronik Inventors’s Kit for the micro:bit from Pimoroni, and quickly found videos on Youtube explaining how to assemble it. I installed the “mu” editor on my Pi and was immediately able to edit a simple Python program and run it on the micro:bit.

My next step was, of course, to create a little class to drive MicroPython from APL. This involves using serial I/O: when the micro:bit is attached to your Pi via USB it appears as a folder and as a serial (tty) device, typically /dev/ttyACM0. You can find my code in the APLPi/microbit GitHub repository.

If you clone the repository to /home/pi/microbit and start APL, you can make calls to the MicroPython repository along the lines of:

    ]load /home/pi/microbit/microbit    
    mb←⎕NEW microbit ''
    mb.PyREPL '2+2'
4

Using Python on the micro:bit to do maths isn’t that interesting when you have APL on the Pi; the exciting thing is that we can call the micro:bit Python API and access the hardware. My first experiment was to display my favourite APL symbol (Grade Up – aka Imperial Cruiser) in the built-in 5×5 LED display:

Imperial Cruiser on  the micro:bit

APL displaying an Imperial Cruiser on the micro:bit

Which of the Inventor’s Kit experiments would you like to see me do next?

The 2016 Year Game

Our 2016 Year Game was launched in January 2016 and ran until the end of the year. The idea was simple – to find APL expressions involving exactly the digits 2 0 1 6 in that order to equal the numbers 0 to 100 using the fewest characters possible. The minimum number of characters for an expression is 5 (the four digits 2 0 1 6 and a single primitive function/operator) so the smallest number of characters that a possible solution can have is 505.

A lovely elegant solution was pointed out by Lars Stampe Villadsen:

      ⎕IO←0
      {⍪⍳+/⍵*⌽⍵}2 0 1 6

and Bernard Legrand submitted two one-line solutions:

      (⍴⍬),⍳¯20+!1-⍨6

and

      (⍴⍬),⍳⍴'Be happy: this solution to Game 2016 will be the most stupid one, but it needs only five APL symbols'

These three each return all the numbers from 0 to 100. Not valid submissions for this particular game, but I did find them rather pleasing. Anthony Cipriano was inspired to go one step further and submitted an expression that creates the first 8 Fibonacci numbers:

      (⊢,(+/{(¯2∘↑)⍵}))/(⊂0 1),⍨⍳6

The wonderfully varied ways in which APL can be used (and in which APL users think!) was illustrated beautifully by some of the solutions. Although some numbers were attained in the same way by everyone (36, for example, was always 20+16), others lent themselves to multiple solutions. No-one managed to calculate 100 in fewer than 7 characters, but the 7-character answers included 20∧⌹.16, 20ׯ1+6, -20×1-6, 20×-1-6, ¯20×1-6 and various others on the same theme. Unity experienced similar creativity, with 1 being reached in the minimum possible number of characters (5) using ×2016, 201≢6, 20>16, 20≠16, 2<016, etc.

It was clear early on that submissions fell into two distinct groups – those who wrote a program to find the expressions and those who derived their expressions manually. I found the creativity evident in the manually-derived solutions very satisfying; the results may not have been as terse as those resulting from programs, but the individuality of the creators shows people inspired by their subject matter. Who can fail to appreciate the thought process behind arriving at 96 using (×/+\+\(!2 0 1))+6 rather than the more prosaic ⌈⍟!20+16 or ⎕IO-⍨+⍨20+16 rather than 20+⌈○16 for 71?

Having said that, the goal was conciseness and individual character counts are now tabulated on the 2016 Year Game page; an amalgamation of the lowest-character expressions that anyone achieved can also be downloaded from this page. Congratulations to Jonas Stensiö and Veli-Matti Jantunen, who both achieved the same character count as the amalgamated minimum (693).

(most names omitted to protect the not-so-innocent)

Dan Baronet

Dan Baronet

Dan Baronet

We do not know the details, but Dan’s luck ran out this week. On Tuesday, as he headed South from Nevada into California, en route to Mexico on the first leg of a four month motor cycle adventure to South America, something went wrong … Dan lost control of the bike and collided with a motor home, and many of us are now struggling to comprehend that Dan won’t be back.

Dan made friends everywhere that he went. On our annual Dyalog skiing “retreat” (we frequently did real work on prototypes of new tools, honest!), I was always impressed (while trying to hide my slight embarrassment) by the way Dan always kicked off conversations with hotel staff. Whether the waiters and waitresses were native Italians from the resort town – or migrants as hotel staff frequently are – Dan would typically know enough words in some shared language to get the conversation off to a good start (or he would extend his vocabulary on the spot). This always guaranteed us service with a smile, and excellent advice on which wine to order for dinner.

Dan gave as well as he got: on the slopes, Dan was always the first to stop by anyone who had fallen over and was struggling to get back up. Dan has friends all over the world and always took the time to visit them when he could, to keep the friendships alive. He was also happy to open his own home to travellers, both my kids had little Canadian adventures as guests of Dan and his family in Quebec, and Dyalog team members would also “couch-surf” chez Dan.

At work, Dan also loved extending his vocabulary. He remembered more details of different APL dialects than anyone else that I know, and was an expert in migrating code from one system to another. He also knew the weaknesses of each system, and was a feared visitor to vendor booths at APL conferences. Dan would mischievously sit down to play with a new APL system, try a couple of his favourite edge cases, and usually manage to crash new (and some old) APL interpreters within seconds.

But even when crashing interpreters, Dan was really trying to help the vendor produce a more stable platform. At work, as on the ski slopes, Dan was one of the few people who was always going out of his way to help. When he had spent time learning a new technique, or how to use a new tool, he would go the extra mile to create a utility library, document it, presented it at conferences and APL user meetings. He also did the really hard part – writing an article about it, making frequent contributions to Vector and other publications: http://archive.vector.org.uk/?qry=baronet&submit=search. He also enjoyed teaching APL, as an instructor at I.P.Sharp Associates and in the modern era as the author of numerous tutorials at https://www.youtube.com/user/APLtrainer.

More than 30 years ago, as an APL rookie at I.P.Sharp Associates, I was fortunate to meet, work with, and learn from Dan when he and I both found ourselves at IPSA Copenhagen. In addition to a general interest in tools, I shared Dan’s interest in the migration of code between APL systems, and flying airplanes. Dan actually flew small aircraft (and helicopters!) and I, like so many other friends and colleagues, admired him and got invited along for rides.

A decade and a half later in 2005, as the new CTO of Dyalog, I was keen to add an APL toolsmith to the company. Despite his location in Montreal, Dan was at the top of my list. Dan continued to live in Montreal but was a frequent guest in Bramley, coming over several times a year to spend some weeks to work with the growing “APL Tools Team” – and with the core development team on improving the quality of the interpreter by crashing it in as many ways as he could think of.

When his imagination ran out, he wrote code to generate even more ways to crash the interpreter. Dan has been cursed loudly many times, and will be remembered – if for no other reason than that it will be a long time before we are able to close the last “issues” that he logged in our problem tracking system. The quality of Dyalog APL has increased enormously in the last decade, and we have much to thank Dan for. Dan also “poured the concrete” for the foundations of the source code management tool (SALT) and the User Command framework that we are all starting to take for granted, and contributed to many other pieces of our growing collection of tools.

C-GSXN in Toronto

Dan often flew himself to APL User Meetings

Dan was an adventurer. He skied, snowboarded, snowshoe’d, biked, skated, hiked and flew – and with his great sense of humour and warm personality, he was able to entice many of us to come along with him to share. He photographed and brought his video camera, although he would often manage a 3-week trip to Europe which included a skiing trip, carrying only a small backpack.

Dan had just turned 60, and he had taken four months leave this winter to pursue an old dream to revisit South America with a friend – on motor cycles. They had wisely gone on a trial ride in the late summer, riding from Canada to Las Vegas and parking the bikes there in order to test the equipment and not have to ride two-wheelers in the Canadian winter. As a pilot, Dan understood risks very well – or rather, how not to take unnecessary ones.

“If only he had been a little more cautious”, some will say. Was that trip really necessary? These thoughts are unavoidable – but we have to remember that his decision to ride was natural to the adventurous and mischievous spirit that we all knew and loved.