Creating and Using a Local Module with WebIDE

Posted on
  • Setting up a project in WebIDE
    In the upper right click on the gear icon and then select project.
    Click on Select Directory for Sandbox.
    I created a directory called EP1.

    If you use Windows Explorer to examine the EP1 directory you will find several sub directories created by WebIDE named:
    EP1
    Projects
    Modules
    Binary
    Firmware
    Snippets
    Testing
    Testinglog
    All are empty except for one txt file in snippets.

    Copy the attached file testAdd.js into the projects folder.
    Copy the attached file Add.js into the modules folder.
    Load the file testAdd.js from EP1/Projects into the right side of the WebIDE.
    Connect to the Espruino device and the run the program.
    The testAdd.js code:

    /*Playing with modules
    
    Comments
    
    Example of use of local module in code
    */
    var r = new (require("Add"))(1,2);
      console.log(r.read());
    
    

    The output:

    >echo(0);
    3
    =undefined
    > 
    
    

    The module code in Add.js

    /* Copyright (c) 2016 MyName See the file LICENSE for copying permission. */
    /*Playing with modules
    
    Comments: Demo of a simple local module.
    Dependencies: no other modules are required 
    Example of use code
    var r = new (require("Add"))(1,2);
      console.log(r.read());
    */
    function Add(a,b) {
      this.A=a;
      this.B=b;
    }
    exports = Add;
    /* Returns sum of A and B*/
    Add.prototype.read = function() { return this.A+this.B; };
    
    

    For you to try:
    Also attached is an upgraded module timeobj.js and a project file testTimeObj.js.
    http://forum.espruino.com/conversations/­286378/
    The output of testTimeObj.js

    >echo(0);
    Thu
    5/5/2016
    5/5/2016
    2016/5/5
    5/May/2016
    May/5/2016
    2016/May/5
    5
    5
    May
    2016
    5-5
    May 5
    21/30/38
    21:30:38
    30/38:21
    30:38
    38
    73.672  ms
    =undefined
    >
    
    

    The code in TestTimeObj.js

    //Code to test TimeObj
    function Testit(){
    //Create an instance of TimeObj from module file
    var T= new (require("TimeObj"))();
    //Get current time
     T.updateTime();
    
    //Start stopwatch
     T.SWstart();
    
    //Day of week
     console.log(T.getDay());
    
    //Formats for Date
    var aa=[['D','/','M','/','Y'],
            ['M','/','D','/','Y'],
            ['Y','/','M','/','D'],
            ['D','/','m','/','Y'],
            ['m','/','D','/','Y'],
            ['Y','/','m','/','D'],
            ['D'],['M'],['m'],['Y'],
            ['D','-','M'],['m',' ','D']
            ];
    
    //Display the different Date formats
      for(var i=0;i in aa;i++)
      console.log(T.getDate(aa[i]));
    
    //Formats for time
    var bb=[['H','/','M','/','S'],
            ['H',':','M',':','S'],
            ['M','/','S',':','H'],
            ['M',':','S'],
            ['S']
           ];
    
    //Display the different Time formats
      for(i=0;i in bb;i++)
      console.log(T.getTime(bb[i]));
    
    //Stop and display stopwatch
     console.log(T.SWstop().toFixed(3)," ms");
    }//end Testit
    
    Testit();
    
    

    4 Attachments

  • Great - thanks! It's good to have some nice examples of using local modules...

  • Is there a simple example of how to make and use snippets when using the webIDE?

  • Load WebIDE and connect to a board.
    Click on the gear icon
    Click on projects
    Take the snippets tour

  • Just what I needed. Thank you very much.

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

Creating and Using a Local Module with WebIDE

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions