Soft timers in object-oriented C

January 15, 2013

Ken.Wada-January 15, 2013

When time isn't of the essence, you can use a soft timer. Here's how to create one in object-oriented C.

Time is perhaps one of the most important and often used resources on any embedded project. There are times when we wish to do "hard" real time. These are cases where some service must run predictively and preemptively on a very precisely-defined time interval. Typically these "hard" time resources are dedicated to running one or more services in a very precise time fashion.

In this how-to article, I won't be explaining how to implement a "hard" real-time service. I can discuss hard real-time services in another article. All you have to do is ask!

Many times, we don't need such hard real-time precision in our implementation. There are many instances where we just wish to make sure a section of code will execute when "at least" some amount of time has elapsed. Also, we shall discuss object-oriented C programming and how to implement some of these resources on demand, that is, in object-C fashion. Yes, I too have jumped onto the object-C bandwagon. I did this many years ago when I discovered just how powerful this notion of data and function encapsulation really is. I can say this much for certain:

  1. Object-oriented C is really an advanced form of encapsulation
  2. To truly get object-oriented C, you need to define the main data type and a set of operations
  3. Object-oriented C really does take some getting used to
  4. Object-oriented C can be made to be very extensible; especially by other team members

Soft timer objective

Since we're talking objects here, we need to define what our objects need to do. In other words, for this kind of programming, a meta-style programming is involved. What you really are doing is creating something useful that may be used within your current or future project(s). This is what meta-programming for embedded is really about. Instead of programming for your customer, or the application, you really are programming for yourself for future use!

So, now that we see what we wish to do. Let us define some attributes that this soft timer shall have. Here are some attributes that I think I would like to see with this new "use everywhere" style of object.

Instantiate anywhere: Yes, these objects should be allowed to be used anywhere anytime. The soft timer should be allowed to be instanced in any of the following:

  1. Local variable stack, (yes even the local stack on an interrupt service routine)
  2. Heap, (if your system uses one)
  3. Public global instance
  4. Private global instance

Used anywhere: The soft timer object should be allowed for usage anywhere in the project application. However, one must be careful about using this object within an interrupt service routine. This is because of preemption issues affecting the behavior of the soft timer object.

Extensible object: One of the truly amazing things about object oriented C is the ability to extend these objects without affecting code outside of the object. This extensibility is what makes the object oriented C really remarkable.

< Previous
Page 1 of 5
Next >

Loading comments...

Parts Search Datasheets.com

KNOWLEDGE CENTER