Skip to content
This repository was archived by the owner on Feb 26, 2022. It is now read-only.

timer id and event index are independent now #7

Closed
wants to merge 9 commits into from

Conversation

thomo
Copy link
Contributor

@thomo thomo commented Nov 3, 2013

Using the index of the event array as timer id has the disadvantage that the index may be reused/reassigned to an new timer while the original index does not realize this. (example: #5)

My solution is to use an index-independent event-id.

To make it more clear where an event-id is expected I also introduce the type event_id.

SandyWalsh and others added 9 commits October 17, 2013 18:07
…en a valid timer event ID. Given an invalid (out of bounds) ID, it simply returns the same ID that it was given.

Converted the ReadMe file to Markdown, added examples, reference, etc. from Dr. Monk's site.
Minor cosmetic editing, tabs to spaces.
Closes JChristensen#5.
use predefined constant instead of magic number
@SandyWalsh
Copy link
Contributor

I assume we can close this one out now, as the other implementation was merged?

@niesteszeck
Copy link
Contributor

As @thomo pointed in #5 (comment) there will be a problem when a timer ends by natural causes (time elapsed and no repeat timer) and someone try to stop it.

I have a work around in my code:

....
void resetTimeoutTimer(void* context) {
    displayTimeut = true;
    tDisplayUpdate= t.every(PERIOD_DISPLAY, displayTimeout, 0);
}
...
void setup() {
    tTimeout = t.after(SCREEN_TIMEOUT, resetTimeoutTimer, 0);
    displayTimeut = false;
}
....
void loop() {
    t.update(); // if tTimeout has ended displayTimeut will be true
    if (displayTimeut) {
            t.stop(tDisplayUpdate);
            tTimeout = t.after(SCREEN_TIMEOUT, resetTimeoutTimer, 0);
            displayTimeut = false;
    } else {
            t.stop(tTimeout);
            tTimeout = t.after(SCREEN_TIMEOUT, resetTimeoutTimer, 0);
     }
}

For my case it works fine, but I'm with @thomo that there could be a situation where someone try to stop an already stopped timer and it stop another timer

void restartT1() {
    t.stop(t1);
    t1 = t.after(1000, ledA_off);
    ledA_on();
}

void restartT2() {
    t.stop(t2);
    t2 = t.after(1000, ledB_off);
    ledB_on();
}

void stopTimer() {
    t.stop(t1);
    t.stop(t2);
}

void stopTimer1() {
    t.stop(t1);
}

void stopTimer2() {
    t.stop(t2);
}

...

void loop() {
    if eventA {
        restartT1();
    }

    if eventB {
      restartT2();
    }

    if eventC {
        stopTimer();
    }

    if eventD {
        stopTimer1();
    }

    if eventE {
        stopTimer2();
    }
}

So, what can happend:

Time | Event | t1     | t2     | desc
0    | -     | -1     | -1     | -
1    | A     | 0      | -1     | start t1 (ID=0)
...                            
10   | B     | 0      | 1      | start t2 (ID=1)
...
50   | -     | 0      | 1      | t2 ends (ID=1)
51   | -     | 0      | 1      | t1 ends (ID=0)
...                            
100  | B     | 0      | 1->0   | start t2 (ID=0)
101  | C     | 0      | 0      | stop(0) => it stops t2

I think is clear that in time 101 timer 2 (t2) get stooped by trying to stop timer 1 (t1) arising the problem mentioned by @thomo

@JChristensen
Copy link
Owner

Archiving this repo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants