Skip to Content
Programming Embedded Systems in C and C++
book

Programming Embedded Systems in C and C++

by Michael Barr
January 1999
Beginner to intermediate
200 pages
7h 43m
English
O'Reilly Media, Inc.
Content preview from Programming Embedded Systems in C and C++
Das Blinkenlights, Revisited 97
Timer::cancel(void)
{
//
// Remove the timer from the timer list.
//
if (state == Active)
{
timerList.remove(this);
}
//
// Reset the timer's state.
//
state = Idle;
} /* cancel() */
Of course, there is also a destructor for the Timer class, though I won’t show the
code here. Suffice it to say that it just checks to see if the software timer is active
and, if so, removes it from the timer list. This prevents a periodic timer that has
gone out of scope from remaining in the timer list indefinitely and any pointers to
the “dead” timer from remaining in the system.
For completeness, it might be nice to add a public method, perhaps called poll,
that allows users of the Timer class to test the state of a software timer without
blocking. In the interest of space, I have left this out of my implementation, but it
would be easy to add such a routine. It need only return the current value of the
comparison state == Done. However, in order to do this, some technique would
need to be devised to restart periodic timers for which waitfor is never called.
Another potential feature of the Timer class is asynchronous callbacks. In other
words, why not allow the creator of a software timer to attach a function to it. This
function could then be called automatically—via timerList.tick—each time that
timer expires. As you read the next section, be sure to think about how different ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

You might also like

Embedded Systems Object-Oriented Programming in C and C++

Embedded Systems Object-Oriented Programming in C and C++

Israel Gbati

Publisher Resources

ISBN: 1565923545Catalog PageErrata