-
Notifications
You must be signed in to change notification settings - Fork 100
set timer_at to use relativce tics #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ static void root_insert(alarm_t* alarm) { | |
| alarm_t **cur = &root; | ||
| alarm_t *prev = NULL; | ||
| while (*cur != NULL) { | ||
| if (cmp_exp(alarm->t0, alarm->expiration, (*cur)->expiration) < 0) { | ||
| if (cmp_exp(alarm->t0, alarm->interval, (*cur)->interval) < 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be referenced from |
||
| // insert before | ||
| alarm_t *tmp = *cur; | ||
| *cur = alarm; | ||
|
|
@@ -68,26 +68,26 @@ static void callback( __attribute__ ((unused)) int unused0, | |
| uint32_t now = alarm_read(); | ||
| // has the alarm not expired yet? (distance from `now` has to be larger or | ||
| // equal to distance from current clock value. | ||
| if (alarm->expiration - alarm->t0 > now - alarm->t0) { | ||
| alarm_internal_set(alarm->expiration); | ||
| if (alarm->interval > now - alarm->t0) { | ||
| alarm_internal_relative_set(alarm->interval); | ||
| break; | ||
| } else { | ||
| root_pop(); | ||
|
|
||
| if (alarm->callback) { | ||
| tock_enqueue(alarm->callback, now, alarm->expiration, 0, alarm->ud); | ||
| tock_enqueue(alarm->callback, now, alarm->interval, 0, alarm->ud); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void alarm_at(uint32_t expiration, subscribe_cb cb, void* ud, alarm_t* alarm) { | ||
| alarm->t0 = alarm_read(); | ||
| alarm->expiration = expiration; | ||
| alarm->callback = cb; | ||
| alarm->ud = ud; | ||
| alarm->prev = NULL; | ||
| alarm->next = NULL; | ||
| void alarm_at(uint32_t interval, subscribe_cb cb, void* ud, alarm_t* alarm) { | ||
| alarm->t0 = alarm_read(); | ||
| alarm->interval = interval; | ||
| alarm->callback = cb; | ||
| alarm->ud = ud; | ||
| alarm->prev = NULL; | ||
| alarm->next = NULL; | ||
|
|
||
| root_insert(alarm); | ||
| int i = 0; | ||
|
|
@@ -97,7 +97,7 @@ void alarm_at(uint32_t expiration, subscribe_cb cb, void* ud, alarm_t* alarm) { | |
|
|
||
| if (root_peek() == alarm) { | ||
| alarm_internal_subscribe((subscribe_cb*)callback, NULL); | ||
| alarm_internal_set(alarm->expiration); | ||
| alarm_internal_relative_set(alarm->interval); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -112,7 +112,7 @@ void alarm_cancel(alarm_t* alarm) { | |
| if (root == alarm) { | ||
| root = alarm->next; | ||
| if (root != NULL) { | ||
| alarm_internal_set(root->expiration); | ||
| alarm_internal_relative_set(root->interval); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -128,11 +128,9 @@ uint32_t alarm_read(void) { | |
| // Timer implementation | ||
|
|
||
| void timer_in(uint32_t ms, subscribe_cb cb, void* ud, tock_timer_t *timer) { | ||
| uint32_t frequency = alarm_internal_frequency(); | ||
| uint32_t interval = (ms / 1000) * frequency + (ms % 1000) * (frequency / 1000); | ||
| uint32_t now = alarm_read(); | ||
| uint32_t expiration = now + interval; | ||
| alarm_at(expiration, cb, ud, &timer->alarm); | ||
| uint32_t frequency = alarm_internal_frequency(); | ||
| uint32_t interval = (ms / 1000) * frequency + (ms % 1000) * (frequency / 1000); | ||
| alarm_at(interval, cb, ud, &timer->alarm); | ||
| } | ||
|
|
||
| static void repeating_cb( uint32_t now, | ||
|
|
@@ -141,9 +139,8 @@ static void repeating_cb( uint32_t now, | |
| void* ud) { | ||
| tock_timer_t* repeating = (tock_timer_t*)ud; | ||
| uint32_t interval = repeating->interval; | ||
| uint32_t expiration = now + interval; | ||
| uint32_t cur_exp = repeating->alarm.expiration; | ||
| alarm_at(expiration, (subscribe_cb*)repeating_cb, | ||
| uint32_t cur_exp = repeating->alarm.interval; | ||
| alarm_at(interval, (subscribe_cb*)repeating_cb, | ||
| (void*)repeating, &repeating->alarm); | ||
| repeating->cb(now, cur_exp, 0, repeating->ud); | ||
| } | ||
|
|
@@ -156,10 +153,7 @@ void timer_every(uint32_t ms, subscribe_cb cb, void* ud, tock_timer_t* repeating | |
| repeating->cb = cb; | ||
| repeating->ud = ud; | ||
|
|
||
| uint32_t now = alarm_read(); | ||
| uint32_t expiration = now + interval; | ||
|
|
||
| alarm_at(expiration, (subscribe_cb*)repeating_cb, | ||
| alarm_at(interval, (subscribe_cb*)repeating_cb, | ||
| (void*)repeating, &repeating->alarm); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably want to move documentation away from implying an absolute clock value with the new interface