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).
loop()
andisr()
would share some variables, meaning that the interrupt routine would modify a variable used in the main function. I'm not sure if that would cause some problems. I'm pretty sure you meanstd::condition_variable
, right? I will give it a try, thanks! – Arborizationvolatile
keyword is used to indicate that a variable may be changed from outside the main program. Because of this I think halting the simulator thread is more accurate. – ArborizationUART1IncomingData(array with bytes)
which in turn would set some interrupt status flags, some UART registers and then call the interrupt handler if interrupts are enabled. The control thread should have some command queue it reads. (marshalling) – Merchantable