You are reading a single comment by @MaBe and its replies. Click here to read the full conversation.
  • Some times you need code that is dependent on parameter. Like having one up to five analog reads depense on your device. Having it readable eats up lots of space.

    Is there a way to minify Sting module?

    count = 3;
    name = 'analogRead';
    var APINS = [ D28, D3, D4, D30, D29 ];
    var analogRead = {};
    
    var buildModule = (c) =>{
      var module = "", i = 0;
      module = `exports = function (interval) {
      var a0 = 0
        , a1 = 0
        , a2 = 0
        , a3 = 0
        , a4 = 0
      ;
      setInterval(() => {
    `;
      for (i = 0; i < count; i++) {
        module += '    a'+i+' = analogRead('+APINS[i]+');\n';
      }
      module +=`    print(`;
          module += ' a'+0+'.toFixed(3)\n';
      for (i =1; i < count; i++) {
        module += '         , a'+i+'.toFixed(3)\n';
      }
      module +=`    );
       }, interval);
    }`;
    
      require('Storage').write(name, module);
      print(module);
    };
    
    buildModule(count);
    analogRead = require(name);
    

    output

    exports = function (interval) {
      var a0 = 0
          , a1 = 0
          , a2 = 0
          , a3 = 0
          , a4 = 0
      ;
        setInterval(() => {
          a0 = analogRead(D28);
          a1 = analogRead(D3);
          a2 = analogRead(D4);
          print( a0.toFixed(3)
               , a1.toFixed(3)
               , a2.toFixed(3)
          );
       }, interval);
    
About

Avatar for MaBe @MaBe started