ina219

Posted on
  • I had a small project in Arduino that used the INA219 and I thought I'd port it over to Espruino so I can more easily to prototype development.

    I found that while there is a module for the INA226 there isn't for the INA219 so I thought I'd take a stab at making a module for it: https://github.com/espruino/EspruinoDocs/pull/562

    It's my first time contributing an actual module to Espruino, so feedback would be greatly appreciated :)

    Also big thanks to Ákos Lukács who apparently made the INA226 from which I took major inspiration from :)

  • I probably need help in fully converting this into a module.

    Copy pasting into the left hand side as suggested in https://www.espruino.com/Writing+Modules works and I'm able to get the sample code working.

    However, doing a

    const ina219 = require("https://raw.githubusercontent.com/parasquid/EspruinoDocs/master/devices/INA219.js").connect(I2C1);
    

    gives some tokenization errors depending on whether or not I have minification turned on.

    Disabling all options for minification now gives me

    Uncaught Error: Function "getBusRaw" not found!
     at line 1 col 25
    const busVoltage = this.getBusRaw();
                            ^
    in function "getBusMilliVolts" called from line 158 col 37
    console.log(ina219.getBusMilliVolts() / 1000 + 'V');
                                        ^
    

    I'm not really sure where I'm tripping up :(

  • It looks like after I converted the arrow functions into the old ES5 syntax, things are working again :D

  • However, enabling the Pretokenise code before upload (BETA) option in minification gives a red toast message saying:

    Error parsing JavaScript, but uploading anyway.
    SyntaxError: Unexpected token (1:127)
    Error: Line 1: Unexpected token {
    

    I'm not sure if being able to be pretokenised is a requirement for getting a module merged though. If so, I'd appreciate any hints on how to debug them (as copy-pasting the code on to the IDE seems to work just fine and I'm unable to reproduce the error)

  • Thanks! This looks great - just merged and should go live in the next day or so. I wouldn't worry about the pretokenisation - I'm not actually sure why it'd be giving you any problems, but it might be combination of that and minification.

    after I converted the arrow functions into the old ES5 syntax, things are working again

    Yeah, this is actually a common thing with arrow functions. They store this from when they are defined (and it overwrites the current this). Yet another fantastic JS design decision that can trip you up!

    var outer = {
      data : "oops.",
      go : function() {
        return {
          data : "Yay!",
          arrow : () => console.log(this.data),
          normal : function() { console.log(this.data); }
        };
      }
    };
    var a = outer.go();
    
    a.arrow();
    //oops.
    a.normal();
    //Yay!
    

    (Having said that I did notice a bug in Espruino's arrow function implementation which I'll have to get fixed)

  • In general I try to stay away from using this altogether. If you have to use it, use apply or bind. Another trick that works in some cases is to reassign it to a variable that will be available to your scope.

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

ina219

Posted by Avatar for parasquid @parasquid

Actions