Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile-time check attach() parameters? #7

Open
JarekParal opened this issue Mar 1, 2019 · 1 comment
Open

Compile-time check attach() parameters? #7

JarekParal opened this issue Mar 1, 2019 · 1 comment

Comments

@JarekParal
Copy link
Member

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?

@yaqwsx
Copy link
Member

yaqwsx commented Mar 2, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants