Design Patterns for Embedded Systems in C

iStock_000015227810XSmall
Design Patterns.

While I was attending the Embedded Systems Conference this year in San Jose, CA, there was one session that peaked my interest. The session was “Design Patterns for Embedded Systems in C” from Bruce Powel Douglass, Ph.D., Chief Evangelist from IBM IoT (Internet of Things).

If you’re wondering what a design pattern is, you’re not alone. I also did not know what a design pattern was before attending this conference. A design pattern is a general solution for a common or recurring issue. One example given during the session was a vehicle with an analog speed sensor and a digital read out of the speed.  Because the operator of the vehicle needs to know the speed of the vehicle at all times in order to adjust his throttle or know when to brake. If the system were to always send a new value to the display, even though the value did not change, more bandwidth communicating with the display would then be needed. It could cause future communication issues if anything else was added and needed to communicate with either the display or the speed sensor.  A design pattern that would work for this issue would be the observer pattern or publish/subscribe method. The display would indicate to the system manager that the display would like a new value when the speed changes, therefore reducing the communication load and increasing the robustness of the system.

This is really good information when working in an object oriented language like Java, C++, C#. However, most embedded systems are written in C. Embedded systems using C can be designed to have object oriented like features; however, the implementation is not as straight forward. During the session Bruce brought up this same issue and had a few ways to adapt this for implementation in C. They are:

  • File Based, Organizing code into pairs of files ( header and implementation) and using the files for the classes used in the pattern
  • Object Based, Using structures to represent the classes used in the pattern
  • Object Oriented, Still using the structures as described in the object based method however using function pointers for the classes used in the pattern.

I found the first adaptation, File Based, to be the most useful adaptation for most of the software that I am involved in developing.  This allows features to be written in such a way that they can be shared from one project to the next without much modification to the source software.