Simple Timer API :: Bare Metal Programming Series 9
Вставка
- Опубліковано 2 чер 2024
- In this installment of the bare metal programming series, we're taking a minute to build a very simple timer API, allowing us to quickly setup, check, and reset software based timers - accounting for any drift that may occur between checks!
=[ 🔗 Links 🔗 ]=
🎥 Series Playlist: • Blinky To Bootloader: ...
🗣 Discord: / discord
⭐️ Patreon: / lowleveljavascript
💻 Github Repo: github.com/lowbyteproductions...
Thanks
this `drift` calculation seems overly complicated, can't you just do `target_time += wait_time`, and it'll be taken care of, without touching `now`?
You wanted to wait 1 seconds but you ended up actually waiting 1.1 seconds. You reset and try to wait another second, again drifting by .1 seconds.over time, you're accruing a bunch of error that isn't being cancelled out anywhere. If that's not a problem for your application, great! No need to apply it. But there are definitely times where you want to be more or less on your target time with respect to the workload.
@@LowByteProductions no, it wouldn't. you're adding 1 second from the ideal trigger time, not 1.1 seconds. in your code, the `now` cancels out with itself.
I'm not sure I follow your objection. `drift` in this example would end up being 0.1. So we set `target_time = (now + timer->wait_time) - drift`; 1 second in the future, minus the 0.1 second drift, for a total wait time this iteration of 0.9 seconds. If the next time around, we're able to hit that target, we've cancelled out the error, and are back on the 2 second mark. The idea is to try to wait, over time, for the actual amount of time you're aiming for. I hope that's clear 👍
@@LowByteProductions If: drift = now - target, and: target = now + wait - drift, then: target = target + wait
You never need to explicitly calculate the drift because target is always the ideal time, incrementing by the period each time.
Indeed, I see what you're both saying now
Wouldn't it be more appropriate to rename "simple_timer_has_elapsed" function to something like "simple_timer_event_happened"?
Could be!