luni64 / TeensyTimerTool

Generic Interface to Teensy Timers
MIT License
82 stars 18 forks source link

Variable One-Shot Delays (broken?) #17

Closed PowerBroker2 closed 2 years ago

PowerBroker2 commented 2 years ago

This looks like a very promising library to help with a PAL video encoder project I'm working on, but it seems that the TeensyTimerTool isn't letting me use multiple timers or updating the timers I'm using. For example:

#include "TeensyTimerTool.h"

using namespace TeensyTimerTool;

OneShotTimer timer1;
OneShotTimer timer2;

byte ledState = HIGH;

void processVideo()
{
  if (ledState == HIGH)
  {
    ledState = LOW;
    timer2.trigger(2000);
  }
  else
  {
    ledState = HIGH;
    timer1.trigger(1000);
  }

  digitalWriteFast(LED_BUILTIN, ledState);
}

void setup()
{
  Serial.begin(115200);

  pinMode(LED_BUILTIN, OUTPUT);

  timer1.begin(processVideo);
  timer2.begin(processVideo);
  timer1.trigger(1000);
}

void loop()
{

}

From my understanding of the library, the above sketch should light the Teensy 4.0's built-in LED for 1 seconds, turn off for 2 second, and repeat. However, the LED just lights and stays lit.

Can you confirm this as a bug or am I misunderstanding how the library works?

PowerBroker2 commented 2 years ago

Nvm, I got it to work with a different library.

luni64 commented 2 years ago

To avoid confusion for readers who might stumble over this later:

OneShotTimer timer1(TCK), timer2(TCK);

byte ledState = HIGH;

void processVideo() { if (ledState == HIGH) { ledState = LOW; timer2.trigger(2s); } else { ledState = HIGH; timer1.trigger(1s); } digitalWriteFast(LED_BUILTIN, ledState); }

void setup() { pinMode(LED_BUILTIN, OUTPUT);

timer1.begin(processVideo);
timer2.begin(processVideo);
timer1.trigger(1s);

}

void loop() { }

And here a more terse version of your code: 
```c++
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;

OneShotTimer timer1(TCK);

void processVideo()
{
    digitalToggleFast(LED_BUILTIN);
    timer1.trigger(digitalReadFast(LED_BUILTIN) ? 1s : 2s);
}

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    timer1.begin(processVideo);
    timer1.trigger(10ms);
}

void loop(){
}
PowerBroker2 commented 2 years ago

Thank you! The example code works great. This is a really cool library, I just wish the documentation was better. Do you ever plan on making a sort of "read the docs" page for it?

luni64 commented 2 years ago

I just wish the documentation was better. Do you ever plan on making a sort of "read the docs" page for it?

I agree that a library without documentation is quite useless. I therefore always try to document my stuff as good as possible. So, I don't know if you didn't find the documentation (https://github.com/luni64/TeensyTimerTool/wiki) or if you miss something in it?