-
• #2
The built-in pins are a special
Pin
type, but they can also be converted to numbers.For instance:
digitalWrite(1,0)
does the same asdigitalWrite(D1,0)
(on many platforms) to behave the same as on Arduino.That means that
D3==3
andD0==0
- and when you convert a pin to a boolean, there's no specific conversion for bools so it's converted to an integer first, which is then converted like an integer would be (eg!!D0==!!0==false
).If you want to check if a pin is defined, it's best to do
pin!==undefined
Having said all that, JS does have some hacks in it - for instance
"0"-0==0
but!!"0"==true
- so it may make sense to add similar functionality to Espruino? -
• #3
Thanks for the explanation, so it's expected and by design?
But that means, that a couple of libraries that support optional pins, just "accidentally" work? IIRC in most cases the check is usually
if(options.whateverPin){...}
For example in the CCS811, it'sif (ccs.options.int) { ...
Thinking back, I may have had an issue with A0 on the Pixl, but just moved to another pin without thinking too much. Or maybe not :)
Consistency with JS I think usually would be beneficial, for example setting the length of an array in a browser empties that array. But creates a
length
property with value of0
in Espruino :)Don't know how much work would be to add a special handling from
Pin
tobool
. -
• #4
Thanks - I've just changed it, so it should be in 2v03 - as you say, it seems there's a bunch of stuff that relies on it
I think the subject says all.
Here is the output I got testing couple of pins: