Mock pin behaviour for testing

I have a bunch of PulseView recordings from an existing device. Currently to verify if my Arduino handles the input properly I always use the real device. However it would be great if I could somehow test my code with existing recordings. Perhaps there is some test framework that can mock a pin based on a recording? For example when I start the test, pin 8 should be high for 10μs then low for 7 and so on. Is there some framework to mock such things? So that by mocking any changes to the pin interrupts get executed, digitalRead retuns the given value and so on.

With the FakeIt-based ArduinoFake mocking framework, which PlatformIO has an official example for, you can mock the return value that is given to your business-logic code when it calls digitalRead() etc.

(You can do this example with just the digitalRead() function equivalently).

The only problem then is how to get the arary of values to draw from to return. I don’t exactly have an answer to that except for reading the file and sampling it and turning it into a C array and inject it in the test code.

For repeating signals there may be some shortcut where you only have to give the signal for one period.

Where do you want these unit tests executed? On the microcontroller or on the desktop? Because both can be made to work, but desktop has less size restrictions.

Also there is there is the special case for AVR devices, in which simavr is used for emulating a firmware given an .elf file, it can also be given a trace file (“VCD file”, see manual) that will be used to determine what a pin should should read at a certain time. It also then saves all microcontroller output pin states to a file.

>C:\Users\Max\.platformio\packages\tool-simavr\bin\simavr.exe
Usage: simavr.exe [...] <firmware>
[...]
       [--input|-i <file>] A VCD file to use as input signals
       [--output|-o <file>] A VCD file to save the traced signals
[..]
1 Like