Hardware hacking

PIC circuits
I’ve been hardware hacking recently, back using PIC microcontrollers. I have a few specific things I’d like to build – temperature sensor network, and metal lathe tachometer. But so far, I’ve just been getting back up to speed with all my bits & bobs. Hardware is brutually binary. It either works or it doesn’t. Typically, each circuit spends an evening in “doesn’t work” and then the next evening I immediately realise my mistake and it springs into “working” mode.

So far, I’ve built working circuits for:

  • 555 timer clocking a manual switch into a 74HC164 shift register; net result: 8 LEDs go on and off.
  • 16F84 PIC microcontroller with LEDs on the output pins: net result: 8 LEDs go on and off
  • 16F84 PIC talking to DS1820 1-wire digital temperature sensor: net result, 8 LEDs go on and off, but tell you how warm it is
  • USB cable from laptop to FTDI245BM breakout board (in bitbang mode): net result, 8 LEDs go on and off.
  • FTDI245 connected to L293 H-Bridge controlling a bipolar stepper motor: net result, motor spins back and forth very precisely
  • Finally, 16F84 PIC talking to a 16×2 LCD screen: net result, LCD says “hello world”

So, a reasonable wander around the world of digital electronics. Fortunately, I still had my “no part” PIC programmer from a few years back, although it took a while to find a PC which still has a parallel port, and took even longer to remember that it had to be set to SPP mode in the bios. Oh, and I don’t own a separate keyboard anymore, so changing BIOS settings was tricky.

Electronics is so unforgiving compared to software. Here’s a list of mistakes I’ve made – several times, usually:

  • On my bench power supply, the ‘ground’ for the 0-30v range isn’t connected to the ‘ground’ for the 5v supply. That stopped the programmer working for a while, since it needs ~12.6v to program the PIC.
  • You must compile/assemble PIC programs for precisely the right model of PIC that you have. There are many variants, and HEX files produced for one don’t always work for the other
  • Ensure that the configuration word on the PIC is set up right. If you’re using a crystal oscillator, but the config word says you’re using an RC oscillator, it just won’t work.
  • Ensure that the chip is the RIGHT WAY UP. Especially when you drop it on the floor. It appears that PICs are rather tolerant of this noobish mistake .. fortunately.
  • Run your programs under gpsim first. Several times, I added an ‘include’ to the top of my main source file and accidentally ended up with the microcontroller executing the library functions as it’s main program. On a simulator, this is easy to see. IRL, it just dies silently.
  • Be wary of CBLOCK sections in third-party code. By default, they assign addresses starting at 0 to symbols. But, the free registers on a 16F84 PIC start at 0xc, so you’ll end up trouncing system registers.
  • In every circuit, have a status LED. Set up your code to blink it twice before it tries anything complicated, and use it as an “I’m alive” indicator throughout execution.

Anyhow, I now have an LCD screen working so I can get pretty output from my PIC rather than interpreting LEDs. Plus, I’m replacing my aging 16F84’s with newer 16F88’s. These have UARTs for serial comms, plus you can run a bootloader on them which simplifies programming greatly. In order to retire my parallel port, I’ve ordered an FTDI232 USB< ->serial breakout board.

My next project is to make a tachometer to measure the spindle speed on my lathe by using a metal disc with holes drilled round the edge. An LED shines through the hole onto a photo-transistor, and the PIC can measure the period of these pulses and hence the rotation speed. Then, with a few input buttons to allow me to set the material diameter, the PIC can also calculate the surface speed of the rotating material. This falls into the “kinda interesting, but not really necessary” category of projects. In other words, perfect hobby material.

Command-line buses

Xmlstarlet, aka sed for xml, makes it easy to find out when the bus is going to arrive at a stop in Edinburgh without having to leave the command line:

curl -s 'http://www.mybustracker.co.uk/display.php?clientType=b&busStopCode=36253282' | 
    xmlstarlet  sel -N x=http://www.w3.org/1999/xhtml -t -m //x:pre -c 'text()' -n

The output looks like this (bus number, destination, minutes until arrival):

10    WESTRN HARBOUR   14    
10    WESTRN HARBOUR   43    
11    ST ANDREW SQ     10    
11    OCEAN TERMINAL   22    
12    SEAFIELD         4     
16    SILVERKNOWES     3  

You can get the bus stop codes from the bus tracker website. Of course, you could just get the times from there too, but where’s the fun in that?

STM with time limit

This is basically a rehash of a useful Simon Marlow post to haskell-cafe, but with a few tweaks. It’s a useful way of wrapping an STM action with a timeout. In my case, I want to wait until some mutable state satisfies a predicate, and I want a timeout to fire if it takes too long. I hate thinking in microseconds, so there’s a helper type to improve that. And where Simon used a nested Maybe, I created some explicitly named constructors.

import Control.Concurrent.STM
import Control.Concurrent
import Control.Monad

data TimeLimited a = Timeout | Result a deriving Show

data Timeout = TimeoutSecs Int
             | TimeoutMs Int

waitUntil :: TVar a -> (a -> Bool) -> Timeout -> IO (TimeLimited a)
waitUntil var pred timeout = do
  timer < - registerDelay $ case timeout of 
                             TimeoutSecs n -> 1000000 * n
                             TimeoutMs n -> 1000 * n
  let
      check_timeout = do 
        b < - readTVar timer
        if b then return Timeout else retry 
      check_t = do 
          m <- readTVar var
          when (not $ pred m) retry
          return $ Result m
  atomically $ check_timeout `orElse` check_t 

main = do
  tvar <- atomically $ newTVar 44
--  tvar <- atomically $ newTVar 41
  waitUntil tvar (>43) (TimeoutSecs 1) >>= print

Quickrun for incremental haskell/emacs development

I often add quick test functions (like “testme”) inside the haskell module I’m developing. They’re short-lived and soon turn into tests. But I find this approach works better than putting the same lines into ghci because they ‘persist’ for longer.

Since I do this so often, and I use emacs, I automated it. With the following code, I now just hit C-c C-c and emacs loads my module into ghci and runs my test function. Stick a C-u in front, and it’ll let you choose a different function as the default to run.

(add-hook 'haskell-mode-hook 'my-haskell-hook)

(defun my-haskell-hook ()
  (local-set-key "\C-c\C-c" 'haskell-quickrun)
)

(defvar haskell-quickrun-command "main")

(defun haskell-quickrun (arg)
  (interactive "P")

  (unless (null arg)
	(setq haskell-quickrun-command (read-from-minibuffer "Function to run: ")))

  (inferior-haskell-load-file)
  (inferior-haskell-send-command (inferior-haskell-process) haskell-quickrun-command)
  (end-of-buffer-other-window 0)
)

Applicative arrows for XML &&& return to pure

Last time I had lots of success using Control.Applicative with Data.Binary parsers. Now I’m onto parsing XML. I’ve done this a few times in Haskell using HXT and its funky Arrows. However, this time I was keen to see if I could factor out Yet More Code in an applicative stylee … and indeed I can.

Let’s start with an example of a round-trip XML parse-then-print. We’ll need all the imports and the arrow language extension eventually, so let’s get them out of the way now.

{-# LANGUAGE Arrows #-}

import Control.Applicative
import Text.XML.HXT.Arrow
import Text.XML.HXT.XPath.Arrows
import Text.Printf

xml = "<menu>\
      \  <course name='starter'>Soup</course>\
      \  <course name='mains'>Pie</course>\
      \  <course name='dessert'>Cake</course>\
      \</menu>"

testRoundtrip = runX $ readString [(a_remove_whitespace,v_1)] xml >>> writeDocumentToString [] 

Now lets parse the xml properly. Firstly, we’ll have a datatype to represent the menu itself:

data Menu = Menu { starter :: String, mains :: String, dessert :: String } deriving (Show)

The parsing code itself starts off by converting the String into a Tree, skipping down over the root node, and then trying to parse out a menu:

testParse = runX $ readString [(a_remove_whitespace,v_1)] xml >>> getChildren >>> parseMenu

Since we want to do multiple things with the “menu” node, we’ll use the special arrow syntax to name things:

parseMenu :: ArrowXml a => a XmlTree Menu
parseMenu = proc menu -> do
              starter < - parseCourse "starter" -< menu
              mains   <- parseCourse "mains" -< menu
              dessert <- parseCourse "dessert" -< menu
              returnA -< Menu starter mains dessert

parseCourse name = getChildren >>> hasName "course" >>> hasAttrValue "name" (==name) >>> getChildren >>> getText 

BTW, whilst the implementation of parseCourse is very combinatorically worthy, in practise I think I’d probably use the following version instead:

parseCourse name = getXPathTrees (printf "//course[@name='%s']/text()" name) >>> getText

Anyhow, looking back at the implementation of parseMenu, something bugs me. I had to give names to “starter”, “mains” and “dessert” despite only ever intending to use them to construct the final return value.

Sounds like a job for Control.Applicative!

However, sadly typing “:i Arrow” into ghci told me that my Arrow types weren’t Functors and therefore I couldn’t immediately perform the < $> and < *> trick to make a one-liner.

Boo!

But, a quick glance at Control.Applicative showed that the WrappedArrow type was a Functor. So, all I need to do is use the WrapArrow constructor to package up my arrows and, hey presto, applicative city here I come!

parseCourse name = WrapArrow $ getXPathTrees (printf "//course[@name='%s']/text()" name) >>> getText
parseMenu = unwrapArrow $ Menu < $>
             (parseCourse "starter") < *>
             (parseCourse "mains")   < *>
             (parseCourse "dessert")

Okay, so that’s all nice and concise. But it occurs to me that Control.Applicative is still all about *sequencing* computations. But why does parsing XML require sequencing? Surely it’s pure?

Indeed, this is where some of the flexibility of HXT kicks in. HXT supplies a variety of arrow types to fit your needs. If you want to be able to do some disk IO mid-parse, there’s an ArrowIO for you. If you want to maintain some user state during the parse, ArrowState is your friend. But you have the freedom to choose which one you want to use. If you are NOT performing IO and don’t need state, an ArrowList will do you nicely.

Furthermore, most the HXT combinators are uber-flexible. Their types are minimally constrained, allowing you to choose which arrow you want to work in (ie. an IO arrow, a State arrow or a pure List arrow). For example, the “getChildren” function works with any of those three. The HXT combinators which actually do IO (such as readDocument) understandably have a more specific type.

In the running Menu example, parseCourse is pretty flexible. It works with any arrow in the ArrowXml typeclass, which in turns only requires ArrowList behaviour (and a few others which aren’t relevant here).

Therefore, we can use parseCourse as a pure ArrowList, and do some ArrowList-plumbing to supply the three arguments to the Menu constructor. The end result will be a pure function between XmlTree’s and Menu’s. Furthermore, since we’re not using any effectful arrow combinators, we don’t need to nail down the sequencing.

The arr3 combinator converts a 3-arg function into an arrow whose input is a 3-tuple. Well, actually it’s a tuple that looks like (a,(b,c)). But that’s happily what the ‘plumbing’ combinators which we’ll use to plug in the three input arrows produce. So, without further ado:

parseMenuPure :: LA XmlTree Menu
parseMenuPure =  arr3 Menu <<< (parseCourse "starter") &&& (parseCourse "mains") &&& (parseCourse "dessert")

testParsePure = do 
  [xml] < - runX $ readString [(a_remove_whitespace,v_1)] xml
  return $ runLA (getChildren >>> parseMenuPure) xml

So, I’ve come full circle. Arrow syntax looks like monad syntax, and therefore suggests an ordering (I always make this mistake). Control.Applicative provides a concise way to express some common ordering patterns. But not all arrow types have effects which need ordering. If your arrows are pure, you can plumb them directly.

Update: I later realised where my confusion came from. I still had an early version of “testParse” in my file which did the xml reading and the call to parseMenu in one pipeline. This caused type inference to decide that the type of parseMenu was IOSLA (IO + State + List) just like readString is. I somehow assumed this was due to the explicit ordering demanded by the applicative operators – wrong! Once I noticed and removed my original “testParse”, the type of parseMenu reverted back to its natural type of “ArrowXml a => a XmlTree Entry). And that’s the case whether I use the arr3 version, the applicative version or the arrow syntax version. Hmm, so this tells me that I need to play around with Control.Applicative for some non-Monad types … I have an overly specific notion of “sequencing” which is tricking me.