20 December, 2016

Lua Fibre-optic Christmas Tree

In 1998, I was given a fibre optic christmas tree, lit by two coloured light bulbs powered by 2 x AA batteries. In the intervening years, the electrics broke. Finally today I got round to doing something about it.

The tree now has a bunch of neopixels where the bulbs used to be, and an ESP8266 microcontroller providing the flashing (as well as a Wifi/telnet accessible Lua command prompt).

Unfortunately, I can't see much of a different between the illumination provided by the seven pixels - I was hoping that each one would light the fibres up very differently, but that hasn't turned out to be the case. No big deal though, and maybe more optical isolation between the different LEDs and the base of the fibre bundle would help.

Interestingly, the fibres manage to project an image of the layout of the LEDs on the board (see link above) onto the wall behind the tree! and all the fibres emit a very slight blue light because there's a blue LED on the controller board.

25 November, 2016

smuggling things in a dirty bottom

The Haskell unit type, (), has just one value, also written (), right?


smuggle :: Typeable t => t -> ()
discover :: Typeable t => () -> Maybe t

x :: ()
x = smuggle "hello world"

discover x :: Just String
Just "hello world"

These allow you to inject an arbitrary (Typeable) Haskell value into unit and retrieve it later. Just don't try to inspect the resulting () value.

Rather than Haskell 98, you'll need unsafePerformIO and extensible exceptions, put together in a way that lets you hide arbitrary stuff in a thunk, and force evaluation at just the right time.


smuggle :: Typeable t => t -> ()
smuggle v = unsafePerformIO $ throw (toDyn v)

discover :: Typeable t => () -> Maybe t
discover v = either (fromDynamic) (const Nothing)
  $ unsafePerformIO
  $ try
  $ case v of () -> return ()

I could write more. But it's Friday night and I want to drink my wine.

Edit: This is now available on hackage as acme-smuggler.

24 November, 2016

cafe

When you're in a cafe

64 bytes from 8.8.8.8: icmp_seq=3 ttl=59 time=26.8 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=59 time=23.9 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=59 time=26.0 ms

and then their internet dies

From 192.168.1.254 icmp_seq=28 Destination Host Unreachable
From 192.168.1.254 icmp_seq=29 Destination Host Unreachable
From 192.168.1.254 icmp_seq=30 Destination Host Unreachable

and then there is a power cut

From 169.254.6.166 icmp_seq=51 Destination Host Unreachable
From 169.254.6.166 icmp_seq=52 Destination Host Unreachable
From 169.254.6.166 icmp_seq=53 Destination Host Unreachable

but that reset the router

64 bytes from 8.8.8.8: icmp_seq=1786 ttl=59 time=20.7 ms
64 bytes from 8.8.8.8: icmp_seq=1787 ttl=59 time=24.0 ms
64 bytes from 8.8.8.8: icmp_seq=1788 ttl=59 time=21.5 ms

and all is good in the world again.

10 November, 2016

a different technical writing form

My blogger draft box is full of half written posts that I haven't finished - sometimes because I'm lazy; sometimes because the draft is a pile of notes rather than something that could form a coherent post. I've tried embracing that, by dumping a lot of stuff into github at benclifford/text/wiki. This contains a bunch of rambling notes on miscellaneous topics that I have been updating as I think of stuff.

01 August, 2016

spherical camera

This week I've been playing with a Theta S spherical camera. I'm pretty impressed at what it does, given the price. Here's a gallery of images that you can scroll around.

09 March, 2016

sdr

I got a NooElec R820T Software Defined Radio.

Only got a kernel stack trace once, trying to unload the automatically loaded digital TV drivers. And I didn't have to compile anything.

About half an hour after unboxing, I was using gqrx to get a waterfall plot of various things - BBC Radio 1, Capital, PMR446, the London bus trunked radio system, GB3LW, some local business users, and some (not-decoded) ADS-B plane traffic and some classical music being played in the CB range.

I think it needs a better antenna though - I can pick up stuff ok on my Baofeng handheld that I'm not getting through this.

Also might be interesting to see if a Raspberry Pi 2 is powerful enough to run this.

02 March, 2016

Coroutines and first order functions to flash an LED on an ESP8266

The Adafruit tutorial for flashing an LED using Lua on an ESP8266 is buggy: The main loop does not yield to the underlying operating system, and instead hogs the CPU causing eventual crash - usually after about 10 seconds for me.

Here's the code reproduced from the tutorial:

    while 1 do
    gpio.write(3, gpio.HIGH)
    tmr.delay(1000000) -- waits a second, without allowing OS to run
    gpio.write(3, gpio.LOW)
    tmr.delay(1000000) -- and again
    end

The NodeMCU people advocate using a node.js async callback style, where instead of delaying your thread, you would instead set an alarm for a callback to do the next thing. Using tmr.delay and looping a lot is strongly discouraged because it upsets the OS.

I hate that callback style of coding (for reasons).

Lua has co-routines, though, apparently because people wanted to do things like write scripts that kept yielding back to a game engine and then resuming. (See this history of Lua)

I've recently been playing with effect systems in Haskell (see blog tag extensible-effects) and realised that Lua co-routines provide enough infrastructure for (some of) this.

So I hacked up a prototype.

The "user program" looks very much like the Adafruit example:

flashDelay = 200 -- ms
function flasher()
  while 1 do
    gpio.write(3, gpio.HIGH)
    coroutine.yield(flashDelay)
    gpio.write(3, gpio.LOW)
    coroutine.yield(flashDelay)
  end
end
and can be run like this;
driveCoroutineGood(flasher)

You can also use driveCoroutineBad which uses the blocking tmr.delay instead of the asynchronous tmr.alarm, and get the same ESP-crashing behaviour as the original example.

The main difference is that calls to tmr.delay are replaced by a call to yield. In effect system language, that means the code is asking the effect handler (driveCoroutineGood or driveCoroutineBad) to perform an effect (to delay by the appropriate time) rather than performing the effect itself. How that actually happens is down to the effect handler: in the bad case, just calls tmr.delay; and in the good case, does all the wrapping up of callbacks for tmr.alarm.

This ends up looking almost identical to the code in this GitHub issue, tmr.delay() is synchronous and blocks the network stack.

On a very simple example, this is a bit overkill, but on a bigger example is probably more interesting: you can call coroutine.yield deep down inside a whole stack of functions, which becomes tangly if you're trying to build this manually as an async callback.

Other callback style stuff is hopefully amenable to this - for example, socket handling.

25 February, 2016

19 February, 2016

pebble time retrospective

I got a Pebble Time smartwatch, but it turns out I don't wear it much, despite wearing my original (OG) Pebble all the time. I don't even keep it habitually charged.

There isn't one particular reason:

  • There are two reasons why I have to keep taking the watch off, something I never had to do with the OG Pebble which I wore 24/7:
    • The charge port is inaccessible while wearing the watch. I didn't realise before hand how much of a problem that would be: I tend to take it off to charge and forget to put it back on.
    • The wristband and/or shape does something weird to my skin that neither the OG pebble nor the 10 wristbands I wear do; especially if the Pebble gets wet (which it otherwise would often, several times a day).
  • Application loading is heavily coupled to the phone. I got the Pebble because it has long battery life, but I've found myself in situations where I can't use (e.g.) the compass because my phone has gone flat (which is does by the end of every day). That turns the compass from something I can rely on into a gimmick.
  • Not really an ongoing reason, but as I'm in grumble mood, I found the software upgrade experience from OG to Time quite frustrating: I needed a different app from the Pebble app that I already had installed, and it seemed to conflict in a weird way with the existing app.

I have been hoping someone would make a smartstrap to expose charging, but to no avail. Now there's an SDK available for the serial port (which is also the charge point), I might start fixing that problem myself. One day.

08 February, 2016

Porting todaybot to use extensible-effects

I wrote todaybot (blog, github) a while ago, and it is chugging along just fine, with the occasional bugfix (e.g. due to people writing Swedish).

I've been meaning to play with the Haskell extensible-effects package, so on a lazy Sunday afternoon I started hacking away porting todaybot. My goal was more to get a hands-on feel for extensible-effects rather than actually replace the existing todaybot implementation.

The existing non-effect-based code is pretty rough-and-ready/script-like. It all runs in the IO monad, and state is threaded round manually in a couple of places.

I started by replacing all the IO actions with calls to the (Lift IO) effect. The problems I encountered here were:

  • type signatures/type checking errors, with initially impenetrable type errors (reminiscent of what happens when you work with Lens). The constraint based style of effect types gives verbose type errors in a form that I was not used to.
  • Some interaction that I haven't understood between type signatures/type inference on effects and on lens types and parsec types(!). I needed to add type signatures on top level lens and parser definitions, which I got using typed holes.
  • Handling IO exceptions - I was unsure how exceptions would work so for this first cut I ignored exception handling and let the whole bot die if anything goes wrong.

So now I had a bot with a single effect, Lift IO, with worse error handling than before. I wanted to get this exception handling back in so I wasn't losing functionality. I didn't (and still don't) know of the idiomatic way to handle IO exceptions in (Lift IO).

extensible-effects has exceptions (Exc) already, and I wanted IO exceptions to be handled using those, with the Exc IOError effect. I made a wrapper for lift, called lift' which called an IO action and translated IO errors into Exc IOError exceptions. IO errors then could be handled straightforwardly. Later on it turned out this wasn't enough: code is throwing errors other than IOError which need to be caught (although maybe this is also a bug in the mainline todaybot).

Next, I wanted to start breaking down the use of the IO effect into more focused effects. The one people talk about a lot is logging. There's a Writer effect in extensible-effects already, and logging by writing a string seemed pretty obvious. There's also a Trace effect which is pretty similar. Neither had effect handlers that did what I want: translate the logging effect into IO effects (which in turn would be handled by the IO effect handler). This was pretty straightforward to write, though. There was some messing round with type signatures but I got it worked out in the end.

And the last bit I did before I went to bed was put in a couple of Reader effects: one for configuration (which comes from a YAML file), and one for the authentication bearer token, which is requested during runtime. Writing the handlers for these had a similar feel to writing the handler for logging - a few lines of code inserted into boilerplate, messing round with type errors, raising more questions that I might get round to writing about.

Next I would like to implement effects to handle the last two pieces of IO, which are are access to the current time, and HTTP GET/POST calls; and see if I can use a choice effet instead of mapM_ to iterate.

The (very messy) code is on the exteff branch on github - at the time of writing, up to commit 7ccc0a92....