Hacking Coroutines into C
31–40 of 46 posts
Re: Hacking Coroutines into C
#32Re: Hacking Coroutines into C
#33Re: Hacking Coroutines into C
#34This is an alternative I wrote for my C book:
Re: Hacking Coroutines into C
#35Earlier quoted context omitted.
> there's no point in making a function that's only called in one place. There's nothing wrong with doing that if it helps make your code clearer. The compiler's optimizer will inline it when appropriate so there's no runtime overhead either.
Not only that, the compiler's optimizer might actually do a better job if you split up a big function. Because the smaller functions have less register pressure.
Re: Hacking Coroutines into C
#36Of course, the project didn’t allow us to use an RTOS. That tends to just make the project eventually implement an approximation of one... as what appears to have happened here. How I'd solve the given problem is by using the PWM peripheral (or timer interrupts if no PWM peripheral exists) and pin change interrupts, with the CPU halted nearly 100% of the time. I suspect that approach is even simpler than what's shown…
Re: Hacking Coroutines into C
#37Re: Hacking Coroutines into C
#38Re: Hacking Coroutines into C
#39Of course, the project didn’t allow us to use an RTOS. That tends to just make the project eventually implement an approximation of one... as what appears to have happened here. How I'd solve the given problem is by using the PWM peripheral (or timer interrupts if no PWM peripheral exists) and pin change interrupts, with the CPU halted nearly 100% of the time. I suspect that approach is even simpler than what's shown…
You should not use interrupts for button inputs. You will just end up hammering the processor with useless interrupts when the switch bounces. Human interfaces can be polled and still maintain responsiveness. If polling isn't fast enough for machine actuated IO or you need to stay in a low power state then interrupts could be considered but you really need a non-naive solution that disables the interrupt from within…
Re: Hacking Coroutines into C
#40Earlier quoted context omitted.
Not only that, the compiler's optimizer might actually do a better job if you split up a big function. Because the smaller functions have less register pressure.
I'm not sure I agree and I think you should try some stuff out on godbolt first. The compiler can see where variables are no longer in use, whereas unless you turn on link time optimization (which is known for being messy so nobody seems to), you'll likely get a lot of unnecessary push/pop between the function calls.