First 3D print: LED Tea Light

I noticed that you can now get a Dual Extruder 3D printer sub-£1k now. This is a milestone to me and it rekindled my interest in the whole topic.

Blender render screenshot
A lantern in Blender

So over Christmas I was mucking around with 3D modeling in Blender. I did look at OpenSCAD a while back and I think I saw something that terrified me and I knee-jerked against it, but I can’t remember what it was. I’d also used Blender a little before when I dabbled in 3D so I went with that. Anyway after hours (days?) of playing with Blender, I realised I was doing two things.

One, I was doing the same translations and transformations over and over, just manipulating primitives and making heavy use of the Boolean modifier.

Two, I was getting off-track. Yes, look at the cool stuff blender can do, but this was supposed to be about a first 3D print, keep it simple.

So I did two more things: Limit scope. Changed tools.

G9 LED Cluster
G9 LED Cluster

While It’d be great to model and print little LED lanterns, I haven’t even done one test print yet. Best learn to walk, then try quickening the pace from there.

I have a fair few lanterns now, I think I might be a bit lantern-mad. I also have a bunch of G9 LED clusters that are ideal for them, but the furthest I got with them was poking some wires through the top and letting them dangle. It kind of works, but they’re almost always against one side and that annoys me. More than it should. It gives me that OCD itch. So I decided to make a tea light sized LED holder instead, remembering to keep it small and keep it simple.

I put everything aside and started again in OpenSCAD. A couple of hours later and not only was I done, but I was getting impatient to print my first thing! So I call that a win for OpenSCAD. Once I understood the basics of what I was doing in Blender, OpenSCAD turned out to be a far better tool for my workflow.

LED tea light in OpenSCAD
LED tea light in OpenSCAD

Next step, print. Now just because dual extruder printers have hit the <£1k mark, doesn’t mean I’m going to rush out and buy one. Instead, just over the road from the office is a handy-dandy 3D printer shop iMakr.

I said when it opened that it was going to make me poor one day so I’ve tried to give it a wide berth. Richer Sounds on the other hand have a proven track record in making me poor. iMakr offer a 3D printing service, MyMiniFactory, so that’s what I’ll be using to get it printed, since I can collect it on my way home. Win!

So I submitted my print request to MyMiniFactory, and when you do there’s a certain expectation of a fairly robotic experience with everything happening through the website. So I was rather pleasantly surprised to find myself in an email thread with an actual person who not only knows what they’re doing, but has a sense of humour! Michaela was very patient when it came to my ignorance and for that I thank you!

3D printed model
3D printed model

Then came the email I was waiting for. It was here! It exists and is a thing! There’s even a part of me that wants to gnaw on it a bit to confirm that this virtual thing is in fact real. There was one thing I hadn’t counted on. My measurements.

As a programmer I tend to live in a fictional world of exactness, and I totally forgot that this is the real world and I need to make allowances for that. Plastic isn’t perfect and 3D printers aren’t perfect. The result being that the 3-pin female header housing fits into the slot on top in either the X or Y axes, but not both together… turns out 3D printers can’t produce infinitely perfect inside corners. This is an outrage! Oh well, lesson learnt.

Assembled LED tea light
Assembled LED tea light

I was able to achieve some semi-success with a combination of sanding down the corners of the header housing, softening the print slightly with just a little bit of fire (don’t do that, it’s silly), and brute force (also a bit silly).

Overall I’m very happy with the whole experience. I do now have more questions than when I started, and I did catch myself thinking that it’d be a lot easier to answer these questions if I had my own printer. Dangerous thoughts indeed. So I’m going to call it a resounding success! Watch this space for Version 2, which will actually have tolerances on the header slot, a taller wire escape hole and generally more compact to save on the support structure inside it!

Illuminated Lantern
Illuminated Lantern


Arduino LED Dimmer: Part 4

PCB Production

PCB manufacture really does operate on an industrial scale. Most PCB fabrication houses will want big orders, or will just charge you an arm and a leg to do a small run simply because it’s inconvenient. In order to make your board they first have to make tools specific to your design and that’s very costly if you’re only making a handful.

Thankfully for the likes of me there are companies who specialise in low-quantity prototype PCB manufacture. They take all the individual customer’s designs, repeat them as many times as required, combine them with other customers designs until they’ve got a full panel that they can run through their machines. The company I’ll be using is PCBTrain. Originally I was going to go with BatchPCB, but PCBTrain are local to the UK, and the shipping from BatchPCB was going to double the cost of my order! Not to mention BatchPCB’s lead times are significantly longer than PCBTrain’s.

Time to get a grip on PCB design and more specifically, Design For Manufacture. The best resource I found for that was the EEVBlog. Dave has done a number of excellent videos on the topic of Design for Manufacture, and I recommend watching those. There are also some good tutorials on schematic design and PCB layout using Eagle on the Sparkfun website.

When it comes to Eagle, one thing that helped considerably was finding the Design Rules and CAM Job files for PCBTrain. There are a lot of settings to be entered and checked, such as minimum trace width, spacing between traces, drill bit sizes just to name a few. A lot of opportunities for a newbie to get it wrong. I was so glad to find those!

The libraries in Eagle can be a little hard to navigate, so I also recommend bookmarking this list of common components and where to find them in the eagle libraries, which was very helpful with getting a hang of the naming convention of all the different components and packages.

PCB Layout

Now comes the fun part, this is where we start getting creative. Start off by placing all the parts in such a way that as few of the airwires are crossing as possible. The airwires are the yellow guide lines and are there to help you visualise what connections need to be made and in what general direction your traces will have to go. Begin by clustering the components from each sub-circuit together. On this board you can see the voltage regulator circuit on the far left, the bare-bones Arduino along the top and the TLC5940 with output terminals along the bottom.

Final component layout (just to illustrate the thin yellow airwires)

Now that the parts are laid out, the Route tool is used lay down the traces in the copper layers of the board between the pads of the components. This part of the process I found to be very enjoyable and somewhat therapeutic. I’d hate to think just how much time I spent fiddling with the layout and traces.

Traces on the top side


Traces on the bottom side (seen through the board as it were)


Dimmer PCB Layout
Both layers including ground pour

The image above shows the traces for both sides including the ground pour. A ground pour is where all the remaining space between the traces is filled in and connected to ground. This means that all your ground pins will have a good low-resistance connection. If I’m honest, I don’t know if it’s of much importance on this board, I think I might need to brush up on the fundamentals! The impression I got was that it was generally recommended and couldn’t hurt, we’ll see if it paid off.

Now we arrive at that dreaded moment, to commit to the design and hope that there’s nothing wrong with it that you may have overlooked! More on that later.

First, run a Design Rule Check (DRC) and make sure that the design is within the capabilities of the manufacturer.  That’s what the Design Rules and CAM Job files are for. This process looks to those files to determine exactly what your fabricator is capable of producing, and checking your design against those specs to ensure that there’s nothing in your design that they can’t do. The main error I got was just having traces too close to pads or other traces, just tweak the design until the errors go away.

Now it’s time to generate the CAM files. In this case I have six Gerber files, one for each layer of the board. One is the Dimensions, just the outline of the board. Then there are two copper layers and soldermask layers, one for top and one for bottom, that’s four more. Lastly there’s the top silkscreen layer. I could have had silkscreen on the bottom too, but I got everything I wanted on the top so there was no need. The last file to be generated is an Excellon Drill file, which contains information on what size drill holes are required where.

Finally it’s time to upload the files to the manufacturer and wait patiently for the boards to arrive. That really depends on who your manufacturer is, but PCBTrain was pretty straightforward.

Stay tuned for part five! No idea when that’ll be, but I do actually have the boards now and there’s certainly more to tell.

PHPUK 2013 Conference

PHPUK 2013 LogoI got to go to PHPUK 2013 last week courtesy of my employer, Mendeley, who sprang for our team to go for both days. This is just a few thoughts on each of the talks I attended written up for the PHP Hampshire group.

Unfortunately, I managed to catch the conference bug. I guess when you have that many people in an enclosed space, it was bound to happen. So that’s why this post is a little late.


The Brewery
Wow, really swish place.  It snowed a little while we were there, didn’t settle, but it did give it a certain dickensian quality. The WiFi was strong throughout, the food was excellent, the booze… well they ran out alarmingly quickly. Not sure if that’s their fault or ours, so I won’t hold it against them! My only complaint is that track 3, the Inviqa/Sensio track was split across two rooms, one had a big projector and pa system. It wasn’t ideal, but we could live with it. The issue was that the cable hooking up the projector was obviously dodgy. There was static on screen, and it kept cutting out. Could’a done something about that considering it was an IT conference. There were one or two facepalms.


Aral Balkan: You are a designer
Good talk and a great start. It was all about user experience as opposed to graphic design and what he referred to as “prettifying”. He had a selection of amusing pictures of people who clearly don’t get it, and a few stories of people who really did. Apparently ticket machines were sent straight from the centre of hell. He also went through his idea of what using a washing machine should be like. Fascinating ideas, but he’s going to need a purpose built house!

Nick Belhomme: PHP Traits, Trick or Treat?
As James is I’m sure aware, I’m not a fan of traits, there be sleeping dragons. I went to this one hoping he could change my mind and show me why they’re actually amazing and what really cool stuff can be achieved with them. When he likened them to runtime copy-and-paste, yeah… no. He used the word “caveat” a lot so the moral I took from the talk was… RUN AWAY!

Helgi Thorbjoernsson: Cranking Nginx up to 11
Good speaker, definitely going to play with nginx. He talked about some of the various caching techniques and popular modules. Seems you can do a lot more with it than I had first thought. There was even talk of hooking it straight up to MySQL and skipping PHP entirely. Though he did note that sort of thing is more of a “hey! look what we can do! now let’s find a way to make it useful…” type of thing.

Rowan Merewood: From the Ground Up – Coding, Deploying, and Scaling with AWS
Jeez, there was a lot of material to cover there. It really was a quick aerial tour of AWS and how to get started. As someone with no experience with AWS as a whole, I was left with just a fuzzy overview of how it all fits together. Talking to some of the guys there, I think I’ll play with Heroku first.

Andrei Zmievski: “Small Data” Machine Learning
Really damn interesting topic, but I’m somewhat at a disadvantage having never studied maths at an advanced level. The talk was easy enough to follow, but there got to a point where I had some big gaps in my knowledge. I think I’ve got to go and google a few things and maybe take another look at those videos Stanford put out.

Richard Johnson: PHP is evil and wants to eat your babies
To be honest, I was expecting a little more from this one. Good fun speaker, but didn’t really tell me anything I didn’t know. $$var is bad. Be careful with null characters. Use prepared statements. Don’t include() on a variable. That kind of thing.

Panel Discussion: Frameworks – The Good, The Bad and The Ugly
In all honesty, this was a bit of a farce and pissed me right off. There was a twitter stream up on the projector that was more-or-less ignored, they took a couple of questions from the audience, really inane stuff like “what’s your favourite…”, “do you think X is any better now?”, “Which ones do you think should go die in a fire?” that kind of thing. Lots of obsessing over what’s “better” like there is such a thing, and a lot of agreeing with each other. Maybe not such a balanced discussion. I would have like to have seen a devil’s advocate fighting the corner of roll-your-own. The twitter stream was 90% people saying “Hurry up! Beer now!”. I shit you not.


Martijn Verburg: The Diabolical Developer
Very funny talk, the man’s a total troll. I’m pretty sure he wasn’t going to rest until he’d offended 90% of the crowd! But he had a lot of very good points, and I think it was a pretty balanced talk. He played devil’s advocate for all those things that typically make you face-palm, I liked that. He questioned design patterns, best practices, the agile methodology. No one was spared, it was refreshing.

Ben Longden: The Hypermedia API
Good speaker, covered the topic well. Nicely done talk and it was interesting. But if you’ve used the Atom Publishing Protocol at all, then again it’ll be vaguely familiar to you, some of the ideas are just abstracted and given a new name.

Sara Golemon: Scaling with HipHop
Really interesting. She blazed through the presentation quite quickly, but freestyled pretty well for the rest of the time. It actually worked out pretty well, it didn’t feel like filling time at all. She did make a few mentions to various projects going on internally to Facebook that she wasn’t allowed to tell us about. Which is fair enough I guess, but still, it’s not nice to tease. I think I might have a play with HipHop. But I don’t see myself actually using it anywhere. It has lots of fancy features to squeeze as much performance out as possible, including lose typing system so you can typehint integers and the like. Lots of cool stuff, but unless you’re Facebook, it probably won’t make that much difference.

Derick Rethans: OpenStreetMap for the Web
Really fascinating talk, definitely one of the ones I enjoyed the most. OpenStreetMap really has a lot going for it, but is perhaps having a hard time competing with google in some areas for obvious reasons. I really want to look into that some more, there are a lot of good ideas in there and it’s really open ended. One of the things I liked was that it was also a reflection on the communities that use it. In germany, there are parks with every individual bench marked out, including what times it gets the sun, whereas in Kibera, Nairobi, they’ve mapped out the slum including all the little walkways, back alleys and basic necessities like toilets, clinics, chemists and butchers. Gives you pause for thought when you see what a society thinks is worth mapping.

Michael Maclean: Making PHP See
I really don’t know what I was expecting from this one. OpenCV library and PHP, c’mon. I was a little disappointed when he blasted through the slides in about 20 minutes, and when there weren’t very many questions it ended ahead of schedule. A lot of what he talked about was stuff you can do with GraphicsMagick. Reducing noise, highlighting edges, that kind of thing. He gave a few ideas of what could be done with OpenCV, but kept saying “but you wouldn’t use PHP for that” so I’m not really sure what the goal was, other than sheer curiosity that is. I think a working example from start to finish wouldn’t have gone a miss. Something like, “find the edge between the red and the blue, and tell me the angle” or “tell me when there’s something obstructing the driveway”.

Phil Leggetter: Realtime Web Apps in the Wild
Interesting, but again, not much that I wasn’t already aware of. WebSockets are the shit. He had his “second screen” demo which worked, so you could load his page and log in on your phone, then various slides had extra stuff pushed to the phones which was pretty cool. He talked a little about the platform he works on, but when I looked at the overview diagram of basically what it does and how it sits in your application, I realised that I had actually already written something similar. Don’t get me wrong, I don’t have a finished product, but as a basic HTTP/WebSocket bridge, it works. The only disappointment was that he had a number of things he wanted to demo but they just didn’t want to work. He was quite disappointed too, I believe he said “Noo! This one is really fun! Aw…”.

The full list of talks at PHPUK 2013 on They recorded them all too, but don’t appear to have posted the videos yet. I’ll add a link when they do.

The Mendeley Web Team
The Mendeley Web Team: (Left to right) Nick, Chris, Antonio, Paul and My ugly mug. Hiding behind the camera is Rosario.

Arduino LED Dimmer: Part 3


First thing’s first, what software to use? I went with CadSoft Eagle primarily because there are lots of tutorials out there covering it, and that seemed like a good place to start. Once I understand the process I can worry about what software I use, but for now, there’s a free-to-use version of Eagle, it’ll do.

On my Kubuntu box, installing Eagle was just a case of installing the “eagle” package. For the Mac I had to download the installer from CadSoft’s website. I also downloaded and installed the Sparkfun Eagle Library and the Adafruit Eagle Library. They really helped to simplify things, though I did find that I had to modify one or two of the packages, mainly just prettifying the silkscreen. I just couldn’t help myself!

I’m going to skip the intricacies of using Eagle, these posts are really just a log of what I’ve been up to and my own commentary on the process. There are plenty of tutorials out there on how to use Eagle. A good place to start would be the Sparkfun Beginning Embedded Electronics Tutorials.

Eagle’s UI is unlike any I’ve come across. I’m still not quite sure if it’s genius or just really old. I suspect it’s the latter and that the paradigm used to interact with a computer has somewhat shifted since it’s day. However, if it ain’t broke, don’t fix it, and if you’re clever enough to be designing electronics, then you’re clever enough to work it! All I will say is that there are times when it seems staggeringly counter-intuitive, particularly the library editor. Also, I found the naming conventions used in some of the component libraries to be perverse. Yeah-yeah, I know, don’t blame the libraries, blame the person who doesn’t know what he’s looking at! All I’m saying is, the component descriptions could be a little more descriptive!

Designing the Schematic

Once Eagle was installed and I’d gone though a couple of tutorials, it was time to draw out the schematic. That, I found to be fairly straightforward, more time consuming however was finding the right parts from the vast libraries and making sure the part had the right footprint.

I mostly referenced the Adafruit Boarduino schematic and the Standalone Arduino. I did look at the official Arduino Duemilanove schematic, since that’s the board I have. But on taking one look at the board layout, I quickly realised there’s a little more going on with them than I needed on my boards! So rather than hack down a large schematic at the risk of breaking it, I figured I’d just find a simpler one to begin with!

Most of the components I wanted, I found in the Adafruit or Sparkfun libraries, including some handy variations on standard parts, such as the ICSP and TTL Serial headers with the pins labelled. Resistors and capacitors are in the “rcl” library and the 7805 regulator was in the “linear” library. Rcl and Linear are standard libraries that come with Eagle, for navigating those, I stumbled across a list of common components and where to find them. I quickly bookmarked that before I lost it!

Anyway, I now have a schematic!

Arduino LED Dimmer Schematic
Arduino LED Dimmer Schematic (There’s an error in it. Ahh the power of hindsight!)

Here you can see the ATMega chip (left), the TLC5940 (right) and the various other bits of supporting circuitry around them. I broke out the ICSP and TTL Serial headers, as well breaking out the analogue inputs and an interrupt pin. Since I have no specific use in mind for these dimmers but rather a multitude of rough ideas, I’m keeping my options open. TTL serial, I2C, and six GPIO pins complete with ADCs, I think that should just about cover it!

Next up, designing the board!

Arduino LED Dimmer: Part 2

So, where were we? Oh yes, I had built the web of things into what I wanted and it was time to extract the circuit from the prototyping tools.

Breadboard Prototype

Before I can design a PCB, I need to know exactly what the circuit is. Currently the Arduino side of it is still a ball of electronics wrapped in a user friendly board with all kinds of additional features. Needless to say, there’s a lot on there that I don’t need or can move off board. Like the USB/Serial converter chip, I can get a cable with that in it for when I do want to reprogram it so I don’t need that. If I do want to use the serial port for any prolonged time, which I probably will do, it’ll be to the Raspberry Pi, and I can do that with only a couple of resistors.

Time to break out the breadboard! I simply followed the instructions on the Standalone Arduino page, and the TLC5940 library page to assemble the circuit. The only differences are an additional button on the arduino digital “pin 2″ (physical pin 4) which happens to be interrupt 0, and I’m using a single 16Mhz oscillator part rather than a crystal and pair of capacitors (the little blue disc).

Breadboard Prototype
Breadboard Prototype - Left to right: Empty Arduino board used as 5vdc source, Stand-alone Arduino circuit using the removed chip, and the TLC5940 circuit connected to it.

Look at them shine!

The button on the left is just the reset button, the button on the right is connected to interrupt 0 and changes the controller’s mode, similar to those single-button controller boxes you get on strings of christmas lights. Of course, being Arduino based, this one will blow those out of the water!

I love the random colours you get just from using RGB LEDs and the “Fades” example sketch that comes with the Arduino TLC5090 library. It’s not ideal like that since the red, green and blue LEDs inside the package all have slightly different specs. But for the purposes of creating some funky lighting effects, it’ll do. I’m not looking for precision colour matching right now. I’ll save that for later, and for when I have some better LEDs!

Well that was fun! But this isn’t a PCB and I’ve got a long way to go yet, so I really should stop staring at that and get on with getting to grips with some PCB design software!

Arduino LED Dimmer: Part 1

I’ve been playing with the Arduino platform for a while now. I’ve got two, a Duemilanove with the ATMega168 MCU and the new Mega 2560 ADK as well as an Ethernet Shield. Though I really think I need to get another one! I’ve found it to be a great toy-like introduction to the world of micro controllers. With it’s simple IDE and on board USB to just plug it straight it into the computer, it is awfully quick and convenient to prototype an idea or even just as a constructive distraction.

I love LEDs, I have what is starting to become a collection of Moroccan style lanterns and a whole bunch (eight to be precise) of LED jar lights that I made to hang in the patio umbrella in the summer. The lanterns look great when lit by the jar lights, so I decided to build a controller for them. Something a little more sophisticated than those cheap little flashing pattern controllers you tend to see on strings of lights, I want smooth fades, sensor inputs, serial interface, the works.

While poking around the Arduino Playground some time back, I came across the page detailing the TLC5940 chip from Texas Instruments and the Arduino TLC5940 library page. A 16 channel PWM driver specifically designed for LEDs, already supported by the Arduino platform! Winning! After eventually getting hold of some, first thing was to put together a breakout board for them. Yes I have breadboards, but I like the ruggedness of a breakout board with screw-terminals.

Stripboard TLC5940 Breakout

There isn’t much to this at all, it just adds screw terminals to the chip. I didn’t include any pull-up or down resistors to it since I wanted to leave my options open. Same goes for the current setting resistor which you can see inserted into a two-way terminal block in the picture below. Outputs are along the bottom, and the power, control signals and resistors are along the top. The wire I used for it wasn’t great, the insulation shrank away the moment I touched the wire with the soldering iron, which was pretty annoying.

My DIY TLC5940 Breakout Board
My DIY TLC5940 Breakout Board

If you’re wondering why I didn’t snap the terminal blocks together, their pins are 5mm spaced, not 5.08mm (0.2 inch), an easy mistake to make when you’re not paying attention! The strip board is 0.1 inch pitch, so while two-way and three-way blocks would fit, any more than that and you start bending things.

The Web of Things

What I now have, I call my Web-of-Things. Not to be confused with the Internet of Things, mine is just a bunch of circuit boards all wired together into a quite literal web-of-things! It can change at a moments notice and with ease. That’s the nature and beauty of a prototyping system like the Arduino.

Here we have the completed web of things/16 channel dimmer. You can see the Arduino controller, TLC5940 breakout board, two voltage regulators I made on strip board previously and a potentiometer as an input for adjusting fade speed and whatever other value I wanted to manipulate.

Web-of-Things stuffed into a box
Web-of-Things stuffed into a box

It works well and I rather like it, I could just tidy it up a bit, fix the boards down in the box and call it done, but that presents it’s own problem, I’d lose my prototyping tools! While the level of flexibility that the Web-of-Things affords me is great for prototyping, it’s precisely that quality that makes it less than ideal for any kind of permanent installation.

So, to tackle that problem, I decided to scale a new learning curve. To take a circuit from initial concept, where it’s prototyped using development boards, all the way through to having a finished, custom made PCB just for me. Not just some shoddy home etching kit job, because I know what I’m like. I’ll make a mess, and I don’t want the hassle of disposing of the used etchant responsibly. So instead, I’m going to turn it up to 11 and get the boards made by the professionals, with professional tools.

So next up, build the circuit on a breadboard, draw up a schematic, design the board, send the designs off for manufacture, solder it up, and get it working. How hard can it be?

What is a Domain Model?

This is not a fully fledged introduction to Domain Driven Design (DDD). If you want that, go get Eric Evans’ book, Domain Driven Design. It is definately worth it and I whole heartedly recommend reading it. Instead, what I’ve got here is a general overview of what a “Domain Model” is, how it differs from a typical “MVC Model” and why a Domain Layer is a good thing to have in any large scale application. This applies to any language really, but I’m primarily a PHP guy, so my points of reference are all Zend Framework based. It’s a fairly large topic, I’m aware that I’m barely scratching the surface, and as usual I welcome your comments!

What are Models?

A very good question! One definition of “Models”, and probably the one you’re thinking of, is that they are one third of the Model-View-Controller (MVC) architecture. In my opinion, the most important yet also the most undervalued and misunderstood. I’m not going to go into the merits of MVC here, but the basic aim of the MVC architecture is to separate application code into three distinct areas, DataPresentation and Logic. In this context, “Models” are Data.

Let’s remind ourselves of some other definitions of “model”. To a mathematician, a model might be an equation. To an artist, it’s probably something sculpted, or the original subject matter itself. To an architect, it might be a small-scale representation of a building. To a particle physicist, it’s probably some kind of simulation. Just remember that the definition of “model” is somewhat varied. Not to mention, that was before software developers came along and invented about a hundred and one different definitions of our own! So, if you’re confused by it all, it’s totally understandable! The thing to keep in mind, is that what MVC calls a Model and what Domain Driven Design calls a model, or Entity as Eric Evans calls them in his book, are subtly different things.

Models in MVC

Whenever I start a new PHP project, I first lay down Zend Framework. That’s just my preference. Zend provides Views through Zend_View, Controllers through Zend_Controller and Models through, Zend_Model? No, there isn’t one of those. Ok, Zend_Db then? Well, yes and no. The truth is, Zend Framework is only really a VC framework, there’s no M as such.

As far as MVC is concerned, “models” are just your interface to data. That’s all really. So from that viewpoint, it’s perfectly acceptable to use an Active Record style interface like the one that Zend_Db provides. Those objects act as an interface to your database schema. If your application’s requirements are simple enough, then you can get away with just that. It qualifies as MVC and gets the job done. But don’t be fooled, while it is a “Model”, it’s not a “Domain Model”. Besides, since when was anything ever that simple?

What is a Domain Model?

One of the problems is that the terms “Model” (from MVC) and “Domain Model” (from DDD) have had their definitions stretched so much that it’s becoming unclear whether or not they’re the same thing, let alone what either one is. To a lot of people, the terms “domain model” and “model” have become interchangeable. Unfortunately, that’s wrong. There is more to Domain Models than just moving data from the database, to your application and back again. Things like Zend_Db are actually Data Access Objects. Since that’s what they do.

In some ways the Domain Model is a diagram, in others it’s a concept and in the way that most attention is given to, it’s a piece of code. But funnily enough, a Domain Model has very little to do with code at all, code is just the final representation of it.

When you think about a particular piece of software, say a system that controls a railway network, you build up a picture of everything in the system. So, you have trains comprised of one engine and one or more carriage. Those trains stop at stations, located along lines of railway tracks, connected with junctions, controlled by signals.

That is the beginning of a Domain Model. It is a conceptual model that contains all the tracked objects or Entities in the system and how they’re related to each other. Also known as “a clear picture of what’s going on” or business logic. These Entities are what many call their “Domain Models”.

Trains and stations are significant, and thus entities, but an object representing a time-stamp of when a train is going to arrive at a station or the money object representing the ticket cost isn’t. Note I said “ticket cost” and not “fare”. So I’m referring simply to the object holding the value, or the Value Object. An instance of Zend_Currency is a good example of a value object.

Thin Controller/Fat Model

Approaching from an MVC viewpoint, you may have come across the phrase “Thin Controller/Fat Model”. This is half-way towards a Domain Layer. In our trains example, you may have a controller action for finding the train you want to catch between two stations. For the sake of argument, we’ll say it’s part of the public facing website and is triggered with an HTTP request.

What are some possible steps you’d have to take to determine what train someone should get? Well, where are you coming from? Where are you going to? How many people want to travel? When do you want to leave or arrive? Will you be taking a bicycle with you? What class ticket do you want? Do you want a reserved seat? How many connections are you willing to make? There’s quite a bit that needs to go into deciding what train services to recommend to someone.

In a typical MVC scenario, the controller would take all that input from the HTTP request object, it would make requests to the database, search the time tables in accordance with your business rules and come up with a list of possible trains that could be caught. That then gets passed to the view for presentation to the user.

When you adopt a Thin Controller/Fat Model methodology, you would simply move all that business logic into your MVC Models. So for example you might call a method on your Train Model called “findTrains” that accepts a list of criteria to satisfy when searching for train services. At this point all your controller is doing is marshalling the data from the HTTP request parameters and passing it straight into the Model. Your “findTrains” method crunches away and eventually returns a result set. The controller then simply takes that and hands it to the view object for presentation to the user.

Thus, your controller doesn’t contain a great deal of code, or at least, doesn’t do any real crunching. Instead, the model does it all, rendering the controller “thin” and the model “fat”. I say this is a half step towards a Domain Layer because it at least it contains all the business logic to one part of your system, the models.

Now the only thing your controllers need to worry about are the stages up to invoking your business logic, and deciding how to output the result. Things like massaging input parameters in preparation to being fed into the models, or emailing the results rather than displaying them on a page. The same controller action that finds train services for the website, could now also provide a Json-RPC interface without interfering with the business logic in any way, those files remain untouched. The more your changes are contained, the less likely it is that there will be any knock-on consequences, also known as, bugs!

Why bother?

Ultimately, if you want your software to become a long-term success, you’ve got to accept that it’s going to become bigger than any one developer, team or company. It’ll take on a life force of it’s own. It will grow and evolve with the passage of time and the ebbs and flows of demands being made on it. It needs to be highly adaptable, but also reliable. Perhaps most importantly of all, especially if you want this software to last, is that it must be easy to maintain. Specifically, by developers who aren’t you, or aren’t members of the original team.

By having a clear and concise Domain Model, we can we guide it’s evolution and ensure a result that we want. It’s software husbandry, just like with animals. If you don’t keep on top of it, then you’ll end up inbreeding your codebase and you’ll have all kinds of unforeseen consequences on your hands before you know it. But also, just like animals, your code has to be friendly. Both user friendly and developer friendly.

Ok, but what does that mean in technical terms? What does having a domain layer in my application buy me exactly? Well, what it essentially allows you to do is create your application as, for want of a better phrase, “pure code”. That is to say it has no persistence, no interfaces and generally has no concept of anything outside it’s own domain. It has no idea what an HTTP request is, let alone how to map a URL to a dynamically generated page. It doesn’t matter one iota to the domain whether the request was made through HTTP, HTTPS, XMPP, or MQTT.  The method by which the request came in is irrelevant when determining the arrival time of the next train to London Waterloo!

Your controllers will become a lot more concise, since you can then interact with your business logic like it was any other library. Rather than opening and parsing files, querying databases and performing calculations right in front of you, all that gets packaged away into methods and neatly labelled. You end up breaking down all your business logic into very small methods, which has the additional benefit that it makes unit testing your business logic a darn sight easier because you know where it all is, and you can test it directly!

By implementing a Domain layer in your system it makes it a relatively trivial matter to add or remove interfaces. Suddenly there’s demand for a REST interface? No problem. Nobody uses the IRC interface anymore, pull it out. It doesn’t make any difference to the domain or any other interfaces to it that you’ve set up. The same goes for data persistence. If you have to port your application from MySQL to CouchDB, you can do that without any risk to your business logic.

Hello Erlang!

Erlang Logo

It’s been a while since I’ve posted anything, again. So here’s something!

I first heard about Erlang a few years back now but I was having too much fun learning PHP at the time and didn’t look into it very far. I’ve noticed lately that Erlang seems to be popping up more often, so I thought it was about time I gave it a look.

Erlang, created by Joe Armstrong and originating from Ericsson, is an open-source language designed with concurrency, distribution and the communication between independent, isolated processes as the main focus. Spawning processes is a quick and cheap operation in Erlang and a single Erlang application could run across multiple machines just as easily as it could run on a single machine.

Here are some observations from the point of view of a PHP/Zend developer. I won’t even mention performance, one of the generally more talked about features of Erlang. I haven’t run any benchmarks, and I’m not particularly inclined to do so. All I’ll say is, it’s fast.

This is by no means an “Introduction to Erlang”, it’s just some ramblings. If you want a proper thought-through and organised introduction, check the links at the end of this post.

The Erlang Mentality

Erlang does seem to require you to re-learn how you think about problems to a degree. Or at least I’m having to. The mantra of “lots of small processes” needs a bit of hammering home for me. I mean, tiny processes. As far as I can tell, if each process only had a single task, then that’d probably be just fine. It’s all about distribution, doing things concurrently, asynchronously, and mostly, it’s all about the communication between isolated processes.

Another Erlang mantra is “let it crash”. Since processes in Erlang are all isolated, the idea is that it’s better just to let the process crash and log as much as you can about why it crashed than it is to attempt a recovery, potentially with broken data.

In the case of an IRC bot, on receiving each line from the IRC server, I would spawn a parser process, pass it the line, and it would go off and parse the line, sending back a native Erlang term representing the parsed IRC packet. Since the parser processes only have a single responsibility, “couldn’t parse line” and “crashed while trying to parse line” are essentially the same message. So we don’t bother with identifying that the line couldn’t be parsed, we just let the process die and log why it died.

Functional Language

Way back when I first started in Object Orientated development, I started with Objective-C. I didn’t stick with it. I found it quite confusing at the time, but thinking back, that was probably because the Cocoa/Objective-C example of “Currency Converter” is also MVC, which just confused matters further! I do remember mention of “message passing” in reference to Objective-C’s Smalltalk inspiration, but it meant nothing to me since Smalltalk is “a bit before my time” and I couldn’t see any message passing, all I could see were function/method calls, so I ignored it and muddled on.

Once it clicked with me that what Erlang calls “processes” are in fact “objects”, what I had read years previously about message passing suddenly made sense and I realised that Erlang’s more object orientated than any other language I’ve used! Oh, and that everything I thought I knew about object-orientated programming is wrong. Ok, it’s not wrong, just pretty useless to me in Erlang!

It’s isolation and message-passing that’s key in OOP, just as it is in Erlang. I was very pleased when I later found this interview with Ralph Johnson and Joe Armstrong on the state of Object Orientated Programming. It’s nice when one’s gut instinct is reinforced by the people who actually know what they’re talking about.

Side effects are pretty important in Erlang too, so it’s not as functional as all that anyway. You send messages to processes, you can print to the shell. That’s just the way it works.

Ugly Syntax

When I first started looking at screens of Erlang code, I thought to myself, “It ain’t pretty”. I totally take it back. Erlang’s syntax borrows heavily from Prolog, rather than the C-like “curly bracket” languages (of which PHP is a member), so if that’s what you’re used to then yeah, it may seem a bit weird at first. But it makes sense to me both from a logical point of view and a natural language point of view too. I found it surprisingly easy to switch to Erlang’s mode of syntax, to the point where I keep inadvertently using it in other languages! I thought the semicolon-return habit would be the hardest one to break.

Yes, occasionally it bites you when you’re copying and pasting code. But I’ve always been anti-copy-paste so in my opinion, you, I, deserve it for copying-and-pasting code in the first place! Anything that helps us break that habit is A Good Thing. Sure, it has it’s moments when it can be difficult to read, but no worse than some of the PHP or Javascript that I’ve seen! Even in those cases, it tends to be a use of whitespace issue, and that’s subjective. Often what’s “messy” to one developer is “concise” to another.

Despite this, when it comes to pure code alone, no comments and no real use of whitespace to speak of, then I have found Erlang code to be a lot easier to follow than the equivalent software in a typical object orientated language. So far I’ve seen and understood the inner workings of an HTTP server, OSC server and MQTT broker quite quickly. In the latter cases, including an introduction to the protocols themselves!

Pattern Matching

Wow, I love it. It’s just a beautiful way to express software in my opinion. But again, a bit of a shock from a PHP point of view. I think the best way for me to describe it is “Switch()es everywhere!”, except that you can switch() on an entire data structure. A bit like PHP switch abuse, except sanctified!

Take an simple example function that converts HTTP status codes to message strings called status_string/1.

The /1 specifies the function’s arity (but is not part of the name). Arity isn’t something that’s given much thought in PHP, it’s the number of arguments that the function takes. So a function called “status_string” that takes no arguments and a function called “status_string” that takes one argument are different functions. If you wanted to emulate optional arguments, you’d implement wrapper functions recursively for each optional argument, like Java.

status_string( 200 ) -> "OK";
status_string( 403 ) -> "Forbidden";
status_string( 404 ) -> "Not Found";
status_string( 500 ) -> "Internal Server Error";
status_string( Unknown ) when is_integer( Unknown ) ->
    Error = { unknown_status_code, Unknown },
    io:format( "Oh No! ~p~n", [ Error ] ),
    throw( Error ).

It’s like having a built-in switch() on every function definition! It gets right to the point. Less syntax, more power. You’ll notice the last clause on that function doesn’t match against a fixed value, it’s an unbound variable. This means that the data that occupies that position in the structure (in this case just a single value, the entire argument) gets put into that variable. Something a switch() can’t do!

We can also use guards, which is the “when is_integer( Unknown )” bit. Admittedly they’re a little more complicated than at first glance and you can only use certain built-in functions in them but it gives us a “catch all integer” in this case, and you can use them to ensure an argument is within a certain range and things like that. They allow you to fine-tune your patterns, negating the requirement of extra validation steps inside the function. In the case of status_string/1, if you were to pass, say a string into it, then we would just let it crash with a “badmatch” error. Since none of the clauses allow for an argument that isn’t an integer. The function doesn’t accept that input, rather than the function accepting and rejecting that input. Conceptually, that makes a lot of sense to me!

It doesn’t stop there though! You can do pattern matches against bitstreams! I’m not going to go into it here since it’s quite fiddly, but I will say that I’m finding it a great introduction to binary protocols!

Yes, I can see it being open to abuse, and I think it would make my soul cry if I saw patterns matching against tuples of tuples, lists of tuples and tuples of lists. But as long as you follow the Erlang principal of “lots of small processes” then the structures you’re dealing with shouldn’t get very big anyway.

String Handling

Virtually non-existant. I won’t lie to you. There’s no string data type, they’re just lists of integers (Latin1). Which makes sense, but it’s a bit of a shock when you’ve come from the world of PHP. There is the string module that accommodates a lot of the things you’d want to do and since strings are just integer lists, you can also use the lists module to handle them in other regards.

Personally, I find it a bit confusing sometimes knowing what module a function is in. It requires me to stop and think about exactly what it is I want to do with the string. To tokenise a string, you’d use string:tokens/2, but if you wanted to strip certain characters, then you’d use something more like lists:dropwhile/2, with a callback function that checks each character one at a time, or a “list comprehension” that would essentially do the same thing.

There do seem to be a variety of different techniques for dealing with strings including some UTF types in bitstream handling that I haven’t delved into too far yet. I’m still not sure how you’re supposed to tell the difference between a string and a list of strings however. I guess the answer is that I probably don’t need them.


Apparently there’s no native ability to use a serial port. I was quite surprised. But then when I thought about it, it does make sense. Erlang uses a VM, it’s big on isolation. Why would it get it’s hands dirty dealing with hardware level stuff? It tackles the high-up problems, it’s not meant for fiddling with hardware, all that is abstracted from it.

As such, it has absolutely no problem with handing stuff off to external code. The external code can be written in a more suitable language, typically C, and Erlang can maintain it’s isolation. You can open “ports” to external system processes that run outside Erlang, and it also supports NIFs (Natively Implemented Functions) which allow you to implement an Erlang function in C like it was a Built-In Function (BIF). A bit like a PHP Extension though the impression I’m getting is they should only be a very last resort solution! So all in all I guess it’s not really Erlang’s problem, get C to deal with it!

Undocumented Functions

There seem to be an awful lot of undocumented functions, as you’d expect. But what’s unclear is whether or not you’re allowed to use them. I’m sure I read somewhere that you absolutely shouldn’t and that they can change with no notice. Which seems totally sensible. But then I’ve seen examples where people are using them, such as for accepting sockets asynchronously, and claiming to be using them in production systems. So I really don’t know where I stand on that one yet. I’ll stick with the documented ones I think!


I love Erlang. It’s not for everyone that’s for sure, but for the kind of software I’m most interested in, back end server type stuff, it’s seems perfectly suited. I don’t think I would build a website with Erlang, but I would definitely use it to build a webservice.


I can whole-heartedly recommend Erlang and OTP in Action, it’s well written and is easy to hop around from section to section (provided you have the basics down). It’s proving to be a great pick-up-put-down reference as well as an easy and engaging continuous read. It also includes a real world example application that starts out as a simple cache server and evolves as topics such as distribution and resource discovery are introduced, all the way to packaging up and deploying a release.

Free eBook about Erlang: Learn You Some Erlang

Interview with Ralph Johnson and Joe Armstrong on the state of OOP

Beebole Erlang Web Applications (with Mochiweb) screencast

Part one of a ten part lecture given by Joe Armstrong

Banish Mac OS X Resource Forks from FAT32 volumes

I bought a digital photo frame for Mum’s birthday. The first thing I encountered while getting it set up for her was that Mac OS X Resource Forks seriously confuse it. Resulting in every other “picture” being unreadable and rather than skipping past them, it would display a grey screen with an error in the middle for the allotted time (well done genius!).

As usual I discovered this problem while demoing it to her! How embarrassing! “Give me five minutes.” I said. I knew exactly what needed to be done. I needed a way to automatically delete all the resource fork files from the FAT32 SD card. I figured someone else would have done it already, and they probably have, but I couldn’t find it. I guess my GoogleFu was weak that day. Many hours later, I cracked it. So much for five minutes!

Mac OS X has a nifty feature called “Folder Actions”. What this does is allow you to execute scripts when the contents of a folder change. When files are added to the SD card, I want to run the command:

find . -name "._*" -exec rm -rf {} \;

If you open a Terminal window and run that command, it will find all the files that begin with “._” and execute the “rm -rf” command on each one. Resource forks gone.

If you test that command in the Terminal then be sure that you are in your FAT32 volume. It won’t do anything on a HFS volume. If you remove the “-exec rm -rf {} \;” from the end of the command you can see the results of the find command, the files it’ll be deleting. Nothing is returned on HFS volumes, which is exactly what should happen.

I had hoped that I could just stick that command in a Bash shell script and attach that as a Folder Action, but I ended up having to do it through Automator. Which isn’t as straightforward as I’d hoped. In order to do that the command needs to be modified slightly so that it will still work when invoked by Automator. That’s the bit I found time consuming, given that this is the first time I’d even looked at Automator!

The final command we’ll be using is:

find "`dirname "$@"`" -name "._*" -exec rm -rf {} \;

From what I understand, the script will be run once for every file in the folder and Automator will pass the full path to each file in the folder to the command by means of the $@ variable. We use the dirname command to get the folder path from the file path. So /Volumes/SDCARD/DSC0001.JPG would become /Volumes/SDCARD. Then the command can run as normal.

This does mean that the command gets run more times than is nessessary. But to be honest, I don’t care. It’s only going to run when the contents of the card are being changed, which won’t be that often and it’s not as if anyone’ll notice!

Now, I find the integration between Automator and Folder Actions, well, it’s not great. I’m pretty clued up on most things computery, I’m a programmer! But even I find it confusing.

Open Automator and create a new workflow from the “Folder Action” template. At the top of the workflow, you need to select the volume that you want the script attached to. Next you want to add the “Run Shell Script” action. Make sure that “Pass Input” is set to “as arguments” and enter the final command above into the Automator action. Save it and you should be done. Check to be sure by control-clicking on the volume icon in Finder and select “Folder Action Setup”. A small window will appear and drop a sheet in front. Dismiss the sheet and verify that your volume name is in the left column, click it, and check that your folder action is present in the right column. If it isn’t, click the little plus button at the bottom of the right column to get that sheet back and select your folder action from the list. If your volume doesn’t appear in the left column, you can add it there using the plus button at the bottom of that column.

Once that’s all done, the resource forks will be deleted automatically, even after you eject the volume and remount it, it’ll still work.