I am working on a crude Arduino simulator. It's main function should be to test simple code consisting of control structures, loops, switches and subroutines.
My main idea is to simply provide the functions of the Arduino library myself,
for example functions like digitalWrite()
or digitalRead()
, which would read and send the
pin states from and to an external application (like a virtual breadboard).
The following diagram shows my current concept. The simulator is basically a thread which
executes the setup()
function once and then starts to execute the loop()
function until stopped.
It can be stopped or paused from the control (main) thread.
The implemention of the setup()
and loop()
function, as well as some variables, are provided by the user
and cannot be modified or accessed.
So far, so good. Now I want so simulate interrupts. While the simulator thread is executing the loop()
function
the external application triggers an interrupt. This should result in the execution of the interrupt handler isr()
,
which is also provided by the user and cannot be changed.
I had two different approaches to this problem:
- Suspend the simulator thread, execute the interrupt handler in a different thread and resume the simulator thread.
- Use a signal handler instead, send a signal to the process when an interrupt occurs.
Both approaches have their own problems. With the first one, I need to synchronize state somehow, and it seems more like a horrible hack. For the second option, as far as I know, I can't specify which thread will execute the signal handler.
If possible, the solution should be platform independent. However, the solution absolutely needs to compile and run under Windows (MinGW or even Cygwin).