I'm working on a little Arduino game project that uses neopixel strips as a display. Uploading every code change to the Arduino and testing it on the LEDs is slow and drains hardware lifetime.
Usually I test as much I can using the Serial Monitor, but I can't get it to clear from code which I learned is only possible e.g. when using a terminal. I did that, but it comes with other issues.
Is there a better way to test code on the dev machine instead of the Arduino? Is there a best practice to have a Serial Monitor (or anything else) act like a display that I can actually refresh/clear? Serial.println() empty lines to scroll the content is not very practical.
2 Answers 2
You can unit test your code by creating stubs of the functions you are calling, like replacing a couple of includes with their dumb counterparts. My friend Jan Beayens did something very valuable on that front and he is also the main author of a good replacement for the Arduino IDE when you start needing some more advanced capabilities: Sloeber is the product name and it also contains an improved version of the serial monitor with clear and scroll lock capabilities.
Go check it out.
Disclaimer : I'm a contributor of the project
-
2Warning: It's a plugin for Eclipse.user31481– user314812017年12月16日 09:16:02 +00:00Commented Dec 16, 2017 at 9:16
-
Why are you issuing a warning? Do you believe there's anything wrong with Eclipse? It is OSS and works perfectly on Linux, which are two factors you should appreciate... BTW, Sloeber also exists as a product, but that it is still dependent on Eclipse CDT.Roberto Lo Giacco– Roberto Lo Giacco2017年12月17日 10:10:22 +00:00Commented Dec 17, 2017 at 10:10
-
1Eclipse is an horrible IDE. I will not touch Eclipse with a 10 meters pole.user31481– user314812017年12月17日 10:20:52 +00:00Commented Dec 17, 2017 at 10:20
-
I respect your opinion, but I completely disagree: Eclipse is my IDE of choice.Roberto Lo Giacco– Roberto Lo Giacco2017年12月17日 10:26:03 +00:00Commented Dec 17, 2017 at 10:26
As you've probably noticed, trying to run tests over the serial port is frustrating. Worse, the relatively large delays associated with writing serial data can affect the accuracy of your measurements.
To cope with this, I wrote my own unit testing framework for Arduino, which lets you fully mock (simulate) the hardware so that you can verify the operation of the software.
There are some examples of how my library accomplishes that, in the project README. Here is one for pins:
unittest(pin_history)
{
GodmodeState* state = GODMODE();
int myPin = 3;
state->reset(); // pin will start LOW
digitalWrite(myPin, HIGH);
digitalWrite(myPin, LOW);
digitalWrite(myPin, LOW);
digitalWrite(myPin, HIGH);
digitalWrite(myPin, HIGH);
assertEqual(6, state->digitalPin[1].size());
bool expected[6] = {LOW, HIGH, LOW, LOW, HIGH, HIGH};
bool actual[6];
// convert pin history to an array, and verify expected results
int numMoved = state->digitalPin[myPin].toArray(actual, 6);
assertEqual(6, numMoved);
// verify each element
for (int i = 0; i < 6; ++i) {
assertEqual(expected[i], actual[i]);
}
}
You may also like the example for mocking Serial port data, although I probably need to improve that documentation.
The beauty of all this is that I was able to develop this framework and all these tests without ever plugging an Arduino board into my computer.
cat
on a terminal if it's only for logging. I find a terminal quite handy for that sort of work.x.substring(0,x.indexOf(y))
instead ofx.split(y)[0]
. Once it works, i drop it into the sketch and add the types. Once it compiles, you've got a pretty good shot at it working as expected with minimal debugging. of course, you can run C w/o uploading too, but i've not found a decent "emulator", if that's the question.