[Core2] M5Timer-Lib // How to use "numTimer" in e.g. "setInterval()"



  • I want to use the M5Timer Library with Core2.
    In <M5Timer.h> there are functions having a <numTimer> argument and those without.

    Questions:
    How can I use
    0_1612800101761_89beb75e-65e8-4440-ba61-01fbb4c67451-image.png
    with a certain timer?
    I am simply missing the argument "numTimer" in the function.

    A working example for Core2 would be helpful.
    Thank you in advance.



  • Hello @wa-berlin

    you can't. Functions w/o numTimer argument will return the internal number assigned to a specific timer. You don't have direct control about which timer gets which number, but you can keep a record of the number assigned to a given timer to further manipulate it.

    Below is an example with 2 timers (A and B). Timer A counts up ever 2 seconds, whereas timer B counts up every second and stops at 10.

    With button A you can enable and disable timer A.
    With button B you can restart timer B once it has stopped.

    #include <M5Core2.h>
    #include <utility/M5Timer.h>
    
    M5Timer M5T;
    
    int mytimerA = -1;
    int mytimerB = -1;
    
    int mycountA = 0;
    int mycountB = 0;
    
    void myTimerACB(void)
    {
      mycountA++;
    }
    
    void myTimerBCB(void)
    {
      mycountB++;
    }
    
    void setup()
    {
      M5.begin();
      M5.Lcd.setTextSize(3);
    
      mytimerA = M5T.setInterval(2000, myTimerACB);
      mytimerB = M5T.setTimer(1000, myTimerBCB, 10);
    }
    
    void loop()
    {
      M5.update();
      M5T.run();
      if(M5.BtnA.wasPressed() == true)
      {
        if(M5T.isEnabled(mytimerA) == true)
        {
          M5T.disable(mytimerA);
        }
        else
        {
          M5T.enable(mytimerA);
        }
      }
      if(M5.BtnB.wasPressed() == true)
      {
        if(M5T.isEnabled(mytimerB) == false)
        {
          M5T.deleteTimer(mytimerB);
          mycountB = 0;
          mytimerB = M5T.setTimer(1000, myTimerBCB, 10);
        }
      }
    
      M5.Lcd.setCursor(0, 0);
      M5.Lcd.printf("Timer A: %s\n", M5T.isEnabled(mytimerA) ? "enabled " : "disabled");
      M5.Lcd.printf("Timer A: %04d", mycountA);
    
      M5.Lcd.setCursor(0, 100);
      M5.Lcd.printf("Timer B: %s\n", M5T.isEnabled(mytimerB) ? "enabled " : "disabled");
      M5.Lcd.printf("Timer B: %04d", mycountB);
    }
    

    Good luck!
    Felix