Pixl.JS 'D0' pin is false-y?

Posted on
  • I think the subject says all.
    Here is the output I got testing couple of pins:

    >reset(1)
    Erasing saved code.
    Done!
    =undefined
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v02 (c) 2018 G.Williams
    >!!A0
    =true
    >!!A1
    =true
    >!!D0
    =false
    >!!D1
    =true
    
    // and here comes the fun part:
    >!! (D0.getMode)
    =true
    
  • 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 as digitalWrite(D1,0) (on many platforms) to behave the same as on Arduino.

    That means that D3==3 and D0==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?

  • 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's

    if (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 of 0 in Espruino :)

    Don't know how much work would be to add a special handling from Pin to bool.

  • 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

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Pixl.JS 'D0' pin is false-y?

Posted by Avatar for AkosLukacs @AkosLukacs

Actions