# Has anybody a collection of functions or framework that can be used for replacing real hardware?

**URL:** <https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004>\
**Category:** Programming\
**Created:** [October 28, 2023, 12:03am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004 "2023-10-28T00:03:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 28, 2023, 12:03am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/1 "2023-10-28T00:03:08Z")

</div>

Hi everybody,

the WOKWI-simulator offers quite a lot of hardware. But thinks like a limit-switch that can be (still virtual as it is a simulation) but **virtual-physically** connected to a steppermotor is not possible.

If it would be possible the limit-switch would really switch if the stepper-motor-axle rotates to a certain position. I guess lot's of developping-effort.

For testing the logic of code - in case all the hardware is working as expected - functions could be used.

For testing the code-logic it would be sufficient to write a function that takes the step-pulses as input and counts up/down depending on the direction-signal and if a certain number is reached change logic level of a simulated input-pin.

So what I am thinking about is to have macros that insert a

```arduino
digitalRead(input_Pin);

```

for real hardware  
or  
when in simulation-mode a  
digitalSimRead(simlation-function)  
returning LOW / HIGH based on whatever input

As an example giving the framework how to realise it

- with some kind of a wrapper-function
- #ifdef precompiler macros

best regards Stefan

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 28, 2023, 2:56am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/2 "2023-10-28T02:56:05Z")

</div>

> [@StefanL38](#):
>
> As an example giving the framework how to realise it

More detail is needed, but it sounds like an application for a class.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 28, 2023, 7:47am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/3 "2023-10-28T07:47:42Z")

</div>

Wokwi is extensible

You could create a component that is like the stepper motor or servo and add two outputs which would be your limit switch and parameters which would say when the switches trigger (angle)

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 28, 2023, 9:30am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/4 "2023-10-28T09:30:26Z")

</div>

> [@J-M-L](#):
>
> Wokwi is extensible

Do you have to pay for this?  
If yes how much?  
Is it poorly documented the same way as the JSON-attributes or is it **well** documented how to create such extensions?

best regards Stefan

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [October 28, 2023, 9:42am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/5 "2023-10-28T09:42:05Z")

</div>

You might be interested in this project:

> **[GitHub - Arduino-CI/arduino\_ci: Unit testing and Continuous Integration (CI)...](https://github.com/Arduino-CI/arduino_ci)**
>
> Unit testing and Continuous Integration (CI) for Arduino libraries, from a Ruby gem - GitHub - Arduino-CI/arduino\_ci: Unit testing and Continuous Integration (CI) for Arduino libraries, from a Ruby...

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 28, 2023, 10:33am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/6 "2023-10-28T10:33:21Z")

</div>

> [@StefanL38](#):
>
> Is it poorly documented the same way as the JSON-attributes or is it **well** documented how to create such extensions?

depends how knowledgeable you are with the various technologies used

see [GitHub - wokwi/wokwi-elements: Web Components for Electronics and IoT Parts](https://github.com/wokwi/wokwi-elements)

and also youTube if you are a visual learner

> **[Wokwi](https://www.youtube.com/@Wokwi)**
>
> Share your videos with friends, family, and the world

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 28, 2023, 10:50am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/7 "2023-10-28T10:50:09Z")

</div>

> [@ptillisch](#):
>
> You might be interested in this project:

Hi @ptillisch

thank you for posting the link. At first glance it looked interesting.  
Especially this sentence

- enables running unit tests against the library **without hardware present**

So I started reading.  
Though it is the same as with WOKWI

> [@J-M-L](#):
>
> > [@StefanL38](#):
> >
> > Is it poorly documented the same way as the JSON-attributes or is it **well** documented how to create such extensions?
> 
> depends how knowledgeable you are with the various technologies used

The documentation expects more knowledge than I have.  
Now this sounds as I don't want to learn. Well if the documentation does start at a knowledge-level that is much higher as my own knowledge-level I turn away for practicial reasons.  
If it would take me some **weeks** to learn the knowledge just to **start** with it for me this is too long.

I see this the same as using a computer or a microcontroller in general:  
I want to **use** it without knowing

- how the designing of the logical circuits is done
- how the die manufacturing process of the die is done
- how to design a PCB / a 4-layer or 8 layer-motherboard with whatever software
- etc. etc. etc.

With a computer as the complete product you can just use it.  
It is developed to a stage that is

## easy to use.

Using the WOKWI-simulator on the level of  
drag and drop components, click-move-mouse to add a wire, lookup button-contacts  
is very easy.

This is not the case with WOKWI-elements nor with Arduino\_CI

I can't blame anyone for whatever documentation because it is all voluntary.  
Though if a community is interested in **more** contribution than  
from a few nerdy-geeky-freaky experts the documentation must **have a real high** quality-level.

best regards Stefan

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 28, 2023, 10:59am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/8 "2023-10-28T10:59:36Z")

</div>

you could always try to ask nicely there

> **[Issues · wokwi/wokwi-elements](https://github.com/wokwi/wokwi-elements/issues)**
>
> Web Components for Electronics and IoT Parts. Contribute to wokwi/wokwi-elements development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [October 28, 2023, 11:26am UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/9 "2023-10-28T11:26:50Z")

</div>

We use microcontrollers to run motors that MUMT OS's (like Windoze, Crapple, and Linux) CAN'T DO because their tasking is not finely grained enough. In Arduino terms. they block a lot.

So if I run a simulator with trick code on such an OS, it won't miss 10 millis here and there? I wouldn't bet much on that.

I don't think that WOKWI is going to get Arduino timing right. If you make the code synchronous then the order will be forced right but the timing will suffer and asynchronous code won't work the same on an Arduino. There's a limit to what WOKWI can test.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 28, 2023, 12:59pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/10 "2023-10-28T12:59:02Z")

</div>

> [@GoForSmoke](#):
>
> There's a limit to what WOKWI can test.

I agree 100% with that. Really timecritical things can only be tested on real 1 to 1 hardware.

My initial question was more into the direction of having some kind of a framework that makes it easier to write code like

```arduino
#ifdef realHardware
  if( digitalRead(myIO_Pin) == HIGH) {
//...    
  }
#endif

#ifdef SimHardware
  if( mySimulatedRead(myIO_Pin) == HIGH) {
    //..
  }
#endif

```

Just as pseudo-code  
or even some kind of wrapper-functionality that does all  
ifdefs internal

so from "outside" the code looks like

```arduino
if (digRead(myIO_Pin) == HIGH) {
  //...
}

```

and depending on a **global** flag that indicates real hardware or simulated hardware

```arduino
digRead(myIO_Pin) 

```

will be replaced by function-calls to

```arduino
digitalRead(myIO_Pin) 

```

or

```arduino
mySimulatedIO(myIO_Pin)

```

This thread was inspired by this other thread

> [@Sequencing Events](https://forum.arduino.cc/t/sequencing-events/1181479):
>
> My code is to control 3 stepper motor driven linear actuators on the XYZ plane and a servo to control the opening and closing of the tweezers. I have no trouble moving any of the motors, my issue is with sequencing event to happen one after another. What I'm trying to achieve is: 1- The steppers are zeroed with the use of photo interrupters positioned at one end of the actuator. 2- The Y stepper motor will the move into position. 3- X stepper will move into position followed by the Z stepper. …

where a user works on a project that has three stepper-motors and a servo.

The machine has to do a lot of steps in a particular manner driving the three stepper-motors to different positions. Again and again in the same order.

For this state-machines are used. And now for testing the state-machines **logic**  
it would be nice to have the possability to simulate the movement of the stepper-motors  
by a software-mechanism as roughly described above

best regards Stefan

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 28, 2023, 1:01pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/11 "2023-10-28T13:01:49Z")

</div>

So as a related but somehow different question:

How would you write compiler-directives that would choose between making the compiler see and compile  
the real digitalRead()  
or a simulated  
mySimulatedDigRead()

And how can this be done in the most compact way ?

best regards Stefan

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 28, 2023, 1:04pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/12 "2023-10-28T13:04:42Z")

</div>

> [@GoForSmoke](#):
>
> CAN'T DO because their tasking is not finely grained enough

well they do have "low latency" API (like Thread QoS) that are necessary for playing 8K videos with sound synchronisation etc... what they really lack is the pin interface.

All operations compete to use finite resources—CPU, memory, timers, and so on. if you want the system to remain responsive and efficient, you need to prioritise the various asks and make intelligent decisions about when to execute them. This is true as well for your arduino - if you saturate the interrupt system with higher priority interrupts than your PWM or have code disabling interrupts, you'll have issues too.

PS:

> [@GoForSmoke](#):
>
> Windoze, Crapple, and Linux

you forgot an S for linux — 'Linsux' — if you don't want to start a religion war 🙂

"Windblows, Crapple and Linsux" all in the same bag (and I happily depend on Crapple for typing this - not so crap in the grand scheme of things)

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [October 28, 2023, 1:07pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/13 "2023-10-28T13:07:12Z")

</div>

> [@J-M-L](#):
>
> "Windblows, Crapple and Linsux"

lol lmao rofl 😃 😁 😆 😅 🤣 😂

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [October 28, 2023, 1:10pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/14 "2023-10-28T13:10:39Z")

</div>

Yeah nowadays you gotta stay politically correct =)

What about FreeBSD?

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 28, 2023, 1:28pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/15 "2023-10-28T13:28:59Z")

</div>

> [@GoForSmoke](#):
>
> I don't think that WOKWI is going to get Arduino timing right.

I'm sure I don't know what you mean.

wokwi runs the code that the IDE toochain produces on an emulation of the processor that code runs on.

The wokwi can have trouble keeping up with real time. Usually on the desktop, however, I get reports of 100 perecent, meaning it is executing the same program at the same speed as real life.

I do wish there was a mechatronic wokwi version that had physical modeling for limit switches and stuff like that.

I have had some fun using leftover processor resources to model the physical conditions. Being able to use multiple Arduino boards would be nice.

And the custom parts look interesting. I looked then looked away - life too short.

a7

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 28, 2023, 1:54pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/16 "2023-10-28T13:54:28Z")

</div>

> [@ledsyn](#):
>
> What about FreeBSD?

do you mean FreeZzzzBSD?

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [October 28, 2023, 2:19pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/17 "2023-10-28T14:19:49Z")

</div>

> [@J-M-L](#):
>
> > [@GoForSmoke](#):
> >
> > Windoze, Crapple, and Linux
> 
> you forgot an S for linux — 'Linsux' — if you don't want to start a religion war 🙂

> [@J-M-L](#):
>
> do you mean FreeZzzzBSD?

I get really depressed now, because I use all of them.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 28, 2023, 2:38pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/18 "2023-10-28T14:38:07Z")

</div>

😉

I use Apple and Linux - the other not so often but they are out there for a reason, so some people must use them

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [October 28, 2023, 3:40pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/19 "2023-10-28T15:40:30Z")

</div>

I feel like we could go on longer, but I realize it's a thread 'bout something else 🙃

Sorry threadcreator! 🌺

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [October 28, 2023, 11:41pm UTC](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004/20 "2023-10-28T23:41:53Z")

</div>

> [@alto777](#):
>
> wokwi runs the code that the IDE toochain produces on an emulation of the processor that code runs on.

On an OS that slices tasks thicker than a whole millisecond where Arduino can task 10's of times each.

In the case of a virtual stepper, it won't see the blocking either!

[Next page](https://forum.arduino.cc/t/has-anybody-a-collection-of-functions-or-framework-that-can-be-used-for-replacing-real-hardware/1183004.md?page=2)
