Interrupts and synchronisation

Hello there,

I’m starting my adventure with Daisy and it looks really interesting, but I’m confused by one thing (so far). In some examples I notice that there is no synchronisation between the main loop, and AudioCallback (which as I understand is running inside an interrupt handler). E.g. in DaisyExamples/WavPlayer.cpp at master · electro-smith/DaisyExamples · GitHub when accessing the ‘sampler’ variable. A similar concern would apply to code reading user input (pots, buttons, etc.) in the main loop, and AudioCallback. A change in a pot value could affect two variables, and AudioCallback could “see” inconsistent state.
state.a = pot.value;
// an interrupt happening here
state.a_sqr = pot.value * pot.value;

Of course, one could use a mutex, or restructure the code to use atomic variables.
What are the best practices when programming Daisy to ensure consistent state between an interrupt handler and the main loop?

1 Like