ServoESP32 icon indicating copy to clipboard operation
ServoESP32 copied to clipboard

Compile-time check attach() parameters?

Open JarekParal opened this issue 5 years ago • 1 comments

Is it possible to have some compile-time check of the attach() parameters, e.g. with constexp?

https://github.com/RoboticsBrno/ESP32-Arduino-Servo-Library/blob/4b1f4a560bc0e25c51faf7ac7a7fc35773a4ba7c/src/Servo.cpp#L60

I am now thinking about modification like this:

    _minAngle = constrain(minAngle, MIN_ANGLE, MAX_ANGLE);
    _maxAngle = constrain(maxAngle, MIN_ANGLE, MAX_ANGLE);
    _minPulseWidth = constrain(minPulseWidth, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
    _maxPulseWidth = constrain(maxPulseWidth, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);

But that is a hidden side effect and I would like rather make the check in the compile-time and show same compile message (if the parameters would be constexp). Is that possible @yaqwsx ?

Or is there some better way? Are exceptions available in the Arduino EPS32 framework or how the ESP-IDF throws the error message?

JarekParal avatar Mar 01 '19 10:03 JarekParal

You cannot have a compile-time check on function argument as you are not guaranteed to get a compile-time constant. You would have to make the constructor templated by the servo parameters and hence, prevent them from being computed in run-time.

Isn't a simple assert enough in this case? You can log the error using the ESPlog library (https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/log.html)

yaqwsx avatar Mar 02 '19 01:03 yaqwsx