Shop OBEX P1 Docs P2 Docs Learn Events
Non blocking timer object for Propeller 2 — Parallax Forums

Non blocking timer object for Propeller 2

Several years ago I developed a small utility library for Arduino that I affectionally named Neotimer.
A neotimer object greatly simplify various timing functionalities without blocking the execution of other instructions. It is a convenient way to implement a (getms() - last >= preset) scenario. It has proven useful to me time and again so, now that I am a Propeller 2 apprentice, I decided to port it to this ecosystem. I hope it could be useful to someone out there.

There are a few ways to use neotimer:

Start - Stop - Restart Timer

We can start the timer and check with timer.done() to see if the timer reached the preset time. While the timer is not done we can stop it (and get the elapsed time) and restart it until it reaches the preset.

pinh(pin) 
timer1.init(1000)
timer1.start()

repeat
  if( timer1.done() )
    pinl(pin)
  'do something else while we are waiting for the timer to be done

Periodic Triggering

We can also use the timer to trigger some code every X ms using its repeat_execution() method.

timer1.init(500)

repeat
  if(timer1.repeat_execution())
    pintoggle(56)
  'do something else in the meantime

There are two variants of this method: repeat_execution(times) and repeat_execution(times, period). Both will repeat the execution (times) amount of time, but the second allows us to specify the period as well.

Debouncing

Another way to use the timer is to debounce discrete input signals.

timer1.init(100)
input_pin := 10

repeat
  if( timer1.debounce( pinread(input_pin) ))
    pintoggle(56)
  'do something else in the meantime

This is the full API:

Object "neotimer" Interface:

PUB null()
PUB init(ms)
PUB start()
PUB stop() : elapsed
PUB reset()
PUB restart()
PUB started() : result
PUB waiting() : result
PUB done() : result
PUB repeat_execution() : result
PUB repeat_execution_times(times) : result
PUB repeat_execution_period(times, period) : result
PUB reset_repetitions()
PUB debounce(signal) : result
PUB get_ellapsed() : elapsed
PUB get_time() : ms
PUB set_time(ms)

Program: 320 bytes
Variable: 20 bytes

The code is in my github, https://github.com/jrullan/propeller2_neotimer

If anyone knows how to submit it to the community library for the P2, please let me know.

Comments

  • Nice. Being able to do things like this is why I pushed Chip so hard to put getms() back in after it fell out.

Sign In or Register to comment.