Archive | August, 2014

Making a custom 3D printer controller

20 Aug14

Making a custom 3D printer controller

While working on my new printer, I realised how ugly most reprap controllers are. All those I've seen use large SD cards & bulky oldskool LCD's with really poor contrast and viewing angles. Why not use Micro SD & an OLED screen?

You can actually build your own custom controller using any combination of screen, SD reader and encoder, but I couldn't find a good description of how to do it, so here it is. Setting it up with the Marlin firmware can however be a daunting task, so this article aims to document how I went about doing it.

I should start by saying that this is not "the right way" to do it, but I'm describing two of the many possible ways to do it. This description is for Marlin. If you are using Repetier on your printer, you may pick up some advice but the specifics will be different.

Picking the parts

My biggest annoyance with the typical LCD's used in these controllers is that if you're not standing directly in front of it, you can't really read the display. OLED displays use less energy than traditional LCD displays. They're easily viewed from any angle and have a fantastic contrast. I settled on one from that has the required 20 (wide) x 4 (height) character displays. These are less than 4mm (1/64 inch) thick and has good mounting holes. Cost $34.

The SD card reader was something I had picked up from DX a long time ago A straight SD reader based on SPI. I got the encoder from Adafruit and I found a standard buzzer that was fairly small in a drawer. Pretty much any screen, SD reader, encoder and buzzer can be used.

Putting it together

Next is the fun part of figuring out how your controller should look! Grab your components and try to lay them out as smart as possible. Next you'll need a way to connect them together. A standard perfboard cut to 125 x 35 mm (48 x 13 holes) is what I needed. Your needs will probably be different, but using perfboard makes it easier to lay things out and solder it once you know how you want stuff.

Another alternative is to design your own professional PCB using software like Fritzing. It would be a good first project, but it takes time to get the PCB produced. You can also add indicator LED's and other custom components if you want to, but these will require you to add more custom code.

Finding your way around Marlin

I have a somewhat varied programming background, but C++ isn't exactly my native tongue. That's also my reason for doing projects like this - to learn more. I find the Marlin codebase a bit confusing. The root file is Marlin_main.cpp. The Arduino IDE will make a copy with .ino extension of this file when you start up the Arduino IDE.

At the top of this file is a load of #include statements. These are mostly files full of #define statements that are replaced by the compiler every time you hit "compile", but they also contain methods that will reside in a "global/root" namespace. Take the fairly central file "stepper.h". This is the header file for "stepper.cpp" that controls how the printer moves. None of the methods here belong to a class. They're just "there".

Coming from an object oriented programming background, I didn't expect this. I guess there's things/limitations such as dependency on interrupts that can force certain ways of doing things?

As far as I know, the first control panel for Marlin was added by fellow Ultimaker owner Bernhard Kubicek (thanks Bernhard!). After this, many others have hacked in support for their own panels. Over time, the codebase, and especially the config files, have become a little messy so it can be kind of hard to navigate.

Implementing your own screen

More or less any screen can be used, but it will require some modifications to the Marlin codebase. The first you'll need is a small library that can print characters to your screen. Most screens will have standard functionality for this, so if you're lucky you won't need to write more than a little wrapper-code. In my case I received a barely functioning code snippet, so I had to do a little more work. You can find a ZIP-file with my Marlin implementation here.

These are the main functions that your screen class will need:

createChar(uint8_t, uint8_t[]); // custom characters such as temp meter
void printChar(char);

The clear() function obviously clears the screen and setCursor tells where the next character is placed on the screen. With these two and a print-command, you'll have all you need. But wait - print() isn't on the list? No, but if you look at my example OLedI2C.h class, almost at the top it says:

class OLedI2C : public Print

This means that the OLedI2C-class inherits all the functions in the print-class (Print.h) and expands on this. Without inheriting these methods, you'd have to write 18 functions to handle printing of all data types (uint8,uint16,char, bool, double and so on). The OLedI2C-class also "overwrites" the write-method. It changes the default behaviour of the Print-class to instead print to the screen we're implementing.

There's also two "special" commands in the list above. One creates custom characters (createChar) and the other prints it (printChar). These allows us the output graphical symbols for folder navigation, temperature, time and more. A standard "Character LCD" will typically have up to 8 slots for creating such symbols. If you're using a "Graphical LCD", you can create all the characters you want as long as you have the memory required. My implementation also has other methods, but you may not need these.

When you have a well working screen-class it's time to make the actual implementation that Marlin will use. You set this up as shown below.

Method 1 - modifying the existing classes

When you're building your own machine, you will need to make modifications to the Marlin firmware. There's two main methods to adding LCD support. The first involves adding it to the existing file "ultralcd_implementation_hitachi_HD44780.h" and use

pins.h - all I did here was to change the SDSS pin for my Megatronics 2.0 hardware (board type 701). Other than that I've kept all the default pins as defined. If you're using Ramps or something else, you'll need to look up what pins to use in the online documentation.

Configuration.h - Here I added a custom section for my screen:

#define OLED_SSD1311 // Oled based on the SSD131x chip series character display
#ifdef OLED_SSD1311
  #define ULTIPANEL
  #define NEWPANEL
  #define SDSUPPORT
  #define LCD_WIDTH 20
  #define LCD_HEIGHT 4
  #define LCD_I2C_ADDRESS 0x3c   // I2C Address of the display

The OLED_SSD1311 define is my main switch. If this is defined, it'll include the things below.

The ULTIPANEL enables encoder support. This is one of the more messier things in the codebase. This also enables other things but it does not cause problems for me. It would however be great if there was a generic define that just enabled just the Encoder and not other things related to SD card, menus & Ultimaker specific features.

NEWPANEL enables lots of LCD-related features, but is also used for Encoder-specific features. SDSUPPORT is really the only include that does exactly as it's name says. Width/height is used for menu scrolling and the I2C address is just there as it's "convenient to have". In my case I could just as well hardcode it in the class as all the screens of this type use the same address.

ultralcd.cpp - This is the main class controlling everything related to the screen and SD card file selection. It has static stub methods that serves as an interface for other implementations. Here I had to add an include-statement for my custom class:

#include "OLedI2C.h"

OLedI2C.h & OLedI2C.cpp - header and implementation of the custom OLED character display. You can also use a non-character display, as long as you provide the same methods mentioned above. Put these files together with your other Marlin files. I created most of this class in a small, separate project since uploading the whole of Marlin takes quite some time. For testing it's better to have a tiny project to test with or you'll be over-caffeinated well before lunch.

ultralcd_implementation_hitachi_HD44780.h - this is where you integrate an instance of your own screen. All that is needed is two edits. The first is to add the instantiation of your screen beneath the section that has the heading "Create LCD class instance and chipset-specific information". I added mine at the end of the if/elif/endif statements:

(other screens)
#elif defined(OLED_SSD1311) // OLED
  #include <OLedI2C.h>
  #define LCD_CLASS OLedI2C
  LCD_CLASS lcd;
  // Standard directly connected LCD implementations

These blocks are compiler arguments, so the whole block will be replaced at compile time and only the parts that are #defined will be included in the compiled code. Next is that you add your init-call to the lcd_implementation_init() method:

(other screens)
#elif defined(OLED_SSD1311)

This is the method I opted for as I generally like the way the typical Marlin LCD works. To go completely custom - read on…

Method 2 - creating your own implementation

What if you don't want the default contents on the display? What if you want to make a super-fancy, full color with lots of bitmaps-screen? Then you'll do the things above, but omit the changes to "ultralcd_implementation_hitachi_HD44780.h". Instead you'll want to replace the default instance of "ultralcd_implementation_hitachi_HD44780.h" and use your own file instead:

ultralcd.cpp - Remove the include at the top and instead add a toggle at the top of this file for the specific screen that included the right class if the OLED_SSD1311 was set:

#if defined(DOGLCD)
    #include "dogm_lcd_implementation.h"
#elif defined(OLED_SSD1311)
    #include "oled_1311.h"
    #include "ultralcd_implementation_hitachi_HD44780.h"

myCustomOled.h - In this file (can have any name) you need to implement your own version of all the methods in ultralcd.cpp. The class holds an instance of the custom screen and handles all the actual hardware.

Be advised that this approach is more time consuming, but you could of course just copy the contents of "ultralcd_implementation_hitachi_HD44780.h" and modify only the things you need.

Things learned

There's always something to learn when doing a new project that you can carry over to coding in general. For instance - when you get the Arduino/C++ error "someClassName does not name a type" this basically means that the compiler can't find the definition of the class "someClassName". If you look closely at the error message it will usually tell you where you need to add an #include statement so that the compiler knows what to do. Probably obvious to seasoned C coders, important info for me :-)

By doing this I've now implemented a full library for a custom piece of hardware with only a little to start from. I had to read the datasheet carefully and there were several things I could have added. This is how I hooked it up to my Megatronics 2 board:

End result

Size, readability and looks was my main reason to do this project. I'm quite happy with the results when I'm comparing it to the two most common Reprap Controllers:

Not only is my custom controller smaller - it's also thinner despite using standard components on a perfboard:

Is it worth it? I think so, but it could certainly be a good product just to sell a super-thin controller like this. I only need a single one, so it's not for me , but someone should make a standard reprap controller that looks as good as the Panucatt Viki LCD, but with an OLED :-)
Next up: printing a nice enclosure for it!

Useful reading

Here's some links that I found useful when solving this project:

Partial description on how to do this
#define's & pre-processing
Schematics for UltiPanel with rotary encoder

This also gave me a chance to do some extra documentation around the Megatronics 2.0 electronics that I use for the printer.

Why I’ll never use Windows Phone again

04 Aug14

Why I’ll never use Windows Phone again

It's been a while since I posted a good rant. Here's one that's been brewing for some time. I refuse to belive that ANY Microsoft employes that ever had an iPhone or Android, actually tried to use Windows Phone for a full year. I have and I can tell you - it's utter bullshit. I don't doubt that a lot of talented people have worked hard to make it happen, but without someone capable of telling what is good and bad, you cannot succeed. The whole OS reeks of a lack of interest and use.

I gave it a full year as I figured they'd get som time to fix the major bugs. Despite three fixes delivered over the air, they have not solved anything that could help me like the platform. They have however added a menu with "Extra settings" that I'll never use.

Here's my 6 biggest annoyances:

The Search Button

This is my single biggest gripe with Windows Phone and the person that made this choice should be fired immediately. He/she has done irreversible damage to the company in insisting on this bug/feature. When I'm pressing the search button - it is ALWAYS in a context! I have ZERO interest in opening Bing (see below) when I'm clicking the search button - EVER! If I'm in Spotify - I want to search Spotify for a song. If I'm in my contacts list - I want to search for my contacts. If I'm in a browser, I want my PREFERRED search engine. This would be so easy to understand if you ever used this phone.

When more than 50% of all apps (including Microsoft's own) needs to implement it's own search button in software AND you have a physical one that does not do anything sensible - it would be obvious to most developers to combine these. In many apps there's actually a menu bar at the bottom of the screen just to show a search button. It must be some incredibly stubborn person inside the company that insists on this. That person cannot be using Windows Phone at all. After using it for a year, I now have a long list of (mostly shitty) applications. To change a setting I have to scroll quite far. Imagine how neat it would have been to search this list of apps instead of going to Bing.


Settings menu is presented as text, but non alphabetical. Why on earth is Flight Mode and (screen) Brightness not on first page, when Theme & Internet Sharing is at the top? How often will anyone Microsoft developers actually change their theme? And how many "average" users will ever use the internet sharing? And why use text rather than icons for a non-alphabetical list? I refuse to believe that a single hour of user testing went into this.

Internet sharing

This feature turns off at it's own will. If I turn it on - I really want it to be on ALL the time UNTIL I turn it off myself. I have no interest in power saving for this feature. I just want it to work an not turn off every time I take a minute to answer an email. It does not work the way it's implemented now. It's simply too annoying to use.

Common volume

There is one common volume setting on the whole phone. This controls both media playback, ringtones, speaker volume and haptic feedback. Turn it off and you'll walk for half a day enjoying silence. Then you'll panic and realize you've lost tons of phone calls, Skype calls, messages and other notifications. Smart smartphones knows that AT LEAST the ringtone must be a separate volume or people will screw up and if this is due to the smart-phone not being smart enough, they'll get a better phone.

Lack of apps

It's ok not to have thousands of apps just when you're launching something. It's ok that not all the major app vendors support you right away. You MUST however do some filtering and make some CORE apps. Not having a youtube app is unforgivable. I know Youtube is a Google property, but that's irrelevant. Without a proper Youtube-app, you only got half a smartphone. It's that important. Allowing more than 30 wrappers around the web version of youtube is not adding value. It is destroying value. If I have to download 10 bad apps just to get one that is good, I'll stop downloading apps. It's that simple. Throw out the shit, retain and promote quality.

It's ok to not make just as good apps as Google, but you need to have the basics working. The Maps-application that come with Windows Phone, fails to locate pretty much anything other than cities. If that's a feature, you should probably name it "City-search", but you should not call it "maps" if it cannot find features on a map. I've viewed maps in the app and stared on the name (written on the map) and despite this, the search feature is unable to find that very location. I dunno why, but there apparently no relation between the map and the search. This makes the app somewhat useless.

If you want to get thousands of QUALITY apps in your store, you need to make sure that the vendors of cross platform authoring systems add your export. Adding Windows Phone export in Adobe AIR could give you millions of new installs. All they'd need to do is to recompile and export. Unity already exports to Windows Phone and I'm sure quite a bit of the current apps come from that. Nobody will learn a new platform/OS unless it's from Apple/Google and you know you have a huge market waiting. If you're entering this market to compete with them, you'll need to make it super-easy to make apps. You cannot force everyone to get a PC and learn Visual Studio.


My WinPhone is a Samsung Ativ S. The specs are pretty neat. It should be lightning fast. When I type a URL in the browser, suggestions will pop up as I type. This is nice, but not when the first suggestion pops up 3 whole seconds after I started typing. As I try to tap any of these suggestions, the list will re-order, so and I'll open some random URL. Come on. This is just looking up and displaying a list. It's a very basic programming task. How can this possibly take three seconds on this hardware and why on earth didn't anybody notice this and fix it after a whole year?

Bonus grudge: Bing as a search engine

I refuse to believe that it isn't possible to make something better than Bing. If I'm at a silent party I will sometimes pull up my phone and have people suggest random things that we should search for using Bing. We'll then laugh together at the results. Yeah - the results are so bad that it's fun. They are for some reason much worse if searching from a phone than from the desktop. If I worked at Microsoft search department I'd be crying.

One example: at Bitraf I hosted a workshop about "Myke kretser" (Soft Circuits written in Norwegian). Just before the event I used Bing to see if they had indexed the page. This was a month after I put it up on The search for "Myke Kretser" returned 1970's NBA player "Mike Kretzer" first and then even more irrelevant results. Come on. Not even one of the two words are alike or has even remotely the same meaning? Google had it right two days after I posted the event on Meetup. Bing now returns the correct phrase, but links to my slides from the workshop (derivate) rather then the Meetup page (source).

Bye, bye

If I spent more time, I could probably come up with more than 30 solid software and usability bugs in Windows Phone. The whole OS is incredibly unpolished. In addition, my Samsung Ativ S phone has been randomly rebooting at least once a day. I originally attributed this to a software bug, but none of the updates from Microsoft/Samsung ever solved it. To be honest, I now doubt it's a hardware bug. Good riddance Windows Phone. It's been an annoying year with you. I won't go back…