Skip to content

Commit

Permalink
Clean up timer interface
Browse files Browse the repository at this point in the history
explicitly initialize the timer,
don't pass the value around every time
  • Loading branch information
smurfix committed May 8, 2015
1 parent ad1b618 commit 1125a91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
21 changes: 17 additions & 4 deletions timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,28 @@ static uint8_t nsub = SUB;
#endif

/* return True every MS milliseconds */
char every(int16_t sec, timer_t *t)
char timer_done(timer_t *t)
{
if((current-t->current) >= 0) {
t->current += sec;
if((current - t->last) >= 0)
return 1;
}
return 0;
}

int16_t timer_remaining(timer_t *t)
{
return t->last-current;
}

void timer_start(int16_t sec, timer_t *t)
{
t->last += sec;
}

void timer_reset(timer_t *t)
{
t->last = current;
}

void timer_init(void)
{
TCCR0A=0;
Expand Down
22 changes: 19 additions & 3 deletions timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@
#if defined(HAVE_TIMER)

typedef struct {
int16_t current;
int16_t last;
} timer_t;

/* return True every S seconds */
char every(int16_t sec, timer_t *t);
/* return True every sec tenth seconds */
char timer_done(timer_t *t);
void timer_start(int16_t sec, timer_t *t);
int16_t timer_remaining(timer_t *t);

void timer_reset(timer_t *t);

/**
* The point of 'reset_delta' is: assume every(20,&t) controls a 50% PWM output
* and is called half a second too late at the end of the 'off' phase. You have
* three choices:
* = do nothing: the 'on' phase will last 1.5 seconds, thus the next on>off
* transition isn't delayed.
* = call reset(): 'on' will be 2.0 seconds, thus keeping individual timing
* as accurately aspossible
* = call reset_delta(20,&t): 'on' will be 2.5 seconds, thereby making sure
* that the on/off ratio isn't disturbed too much.
*/

void timer_init(void);
void timer_poll(void);
Expand Down

0 comments on commit 1125a91

Please sign in to comment.