DigitalRead() returns wrong type

Hi. I recently filed a bug in the particle docs repo here, as I noticed that digitalRead returns int32_t, not PinState on my device. At first, I figured it was just a documentation issue, but now I’ve realized it’s an API issue. Consider the following legal code:

  static auto last_state{HIGH};
  auto current_state{digitalRead(mypin)};
  last_state = current_state;

This throws the compiler error: invalid conversion from 'long int' to 'PinState' [-fpermissive], despite using the enum designed for reading pin states.

It’s wrong, but probably not practial to fix.

Originally in Arduino, digitalRead, digitalWrite, etc. returned and took an int. And HIGH and LOW were #defines, which was awful.

The PinState enum for HIGH and LOW is better than the #define since it can be namespace scoped.

However, changing the return type of digitalRead is probably never going to happen because it would break all source code that stores the value in an int, which is basically standard practice.

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.