4 ms·
I use duff's all the time in micro-embedded; best way to have 'coroutines' without all the necessary stack switching interface. It has it's limitations of cours
by buserror 10y ago
I use duff's all the time in micro-embedded; best way to have 'coroutines' without all the necessary stack switching interface. It has it's limitations of course, but it's a terrific tool in certain circumstances.
- doppioandante 10y agoCan you make an example?
- arianvanp 10y agohttps://github.com/arianvp/MidiProject/blob/master/MidiProject.ino https://github.com/arianvp/MidiProject/blob/master/MidiProje... I use duffs device to concurrently read and debounce multiple inputs in my old dj midi controller http://dunkels.com/adam/pt/expansion.html http://dunkels.com/adam/pt/expansion.html
- phkahler 10y ago>> I use duff's all the time in micro-embedded; best way to have 'coroutines' without all the necessary stack switching interface. Can you provide an example? We use state machines all the time, but I have not seen a good example of mixing other flow control statement into a switch/case outside of Duff's Device.
- buserror 10y agoI posted a gist [0] sorry it's not really made for public scrutiny, it's an old pet project clock, a video of most of that code is still there: https://plus.google.com/+MichelPollet/posts/ARDMrY4jSxp https://plus.google.com/+MichelPollet/posts/ARDMrY4jSxp That code does timer, radio clock decoding, scrolling, blinking and all that using multiple 'protothreads'. As you can see the main loop sleep() and wait for a timer interrupt (or anything) to fire to wake up, so it's not even running most of the time; only when the 'tick' timer fires. [0] https://gist.github.com/buserror/9407adb6d52153e16caad5e8a0844023 https://gist.github.com/buserror/9407adb6d52153e16caad5e8a08...
- pcmonk 10y agoCan you point to the usage of Duff's device there? I'm not really seeing it.
- iainmerrick 10y agoFollowing footnotes on Wikipedia led me to this nice clear explanation: http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- mzs 10y agoThank you, when I saw this blog post it reminded me that years ago I had seen something similar but even more brain melting - it was PuTTY!
- buserror 10y agoThe trick is on the PT_ macros really, they hide the switch, case and gotos. I'll post the header later, but you probably can find it by googling for protothreads
- pcmonk 10y agoAh, I see it now. Thanks!
- josephg 10y agoWhoa, PT is fantastic. A full explanation of how it works is here: http://dunkels.com/adam/pt/expansion.html http://dunkels.com/adam/pt/expansion.html Its actually a really simple and beautiful. I worry that we give up a lot of this sort of flow expressiveness in more modern languages like Rust.
- i336_ 10y agoThe one thing I really really like: you can see the hour/minute colon separate flashing as it scrolls. This is really cool.
- phaedrus 10y agoCheck out the Castor C++ logic programming library - it makes extensive use of coroutines based on the principles in common with Duff's device. The Castor macros co_begin, co_end, co_yield, and co_return contain the implementation code.