Avatar for fanThomas

fanThomas

Member since Oct 2018 • Last active Jan 2019
  • 6 conversations
  • 15 comments

Most recent activity

  • in General
    Avatar for fanThomas

    I want to call a method of the calling class within a module. This worked fine in Version 2v00 of espruino. After updating to 2v01 I get an error with the same code.

    This is the calling program:

    class a {
      constructor() {
        this.mB = null;
      }
      
      init() {
        this.mB = new (require('http://192.168.88.117:8081/b.j­s'))(this);
        this.mB.perform();
      }
    
      say(text) {
        console.log(text);
      }
    }
    
    var x = new a();
    x.init();
    

    And this is the module:

    class b {
      constructor(a) {
        this.a = a;
      }
      
      perform() {
        console.log('class b called');
        this.a.say("I'm in b");
      }
    }
    exports = b;
    

    The output shows as follows:

    Error: Line 8: Unexpected reserved word in http://192.168.88.117:8081/b.js
    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v00 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xef chip 0x4016
    >class b called
    I'm in b
    Disconnected
    >
    Found ESP8266_4MB, 2v01
    >
    Connected to COM4
    Error: Line 8: Unexpected reserved word in http://192.168.88.117:8081/b.js
    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v01 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xef chip 0x4016
    >class b called
    Uncaught Error: Cannot read property 'say' of undefined
     at line 2 col 11
        this.a.say("I'm in b");
              ^
    in function "perform" called from line 10 col 21
        this.mB.perform();
                        ^
    in function "init" called from line 19 col 8
    x.init();
           ^
    > 
    

    It seems that the parameter for the constructor is not passed anymore in 2v01

  • in General
    Avatar for fanThomas

    I want to call a method of the calling class within a module. This worked fine in Version 2v00 of espruino. After updating to 2v01 I get an error with the same code.

    This is the calling program:

    class a {
      constructor() {
        this.mB = null;
      }
      
      init() {
        this.mB = new (require('http://192.168.88.117:8081/b.j­s'))(this);
        this.mB.perform();
      }
    
      say(text) {
        console.log(text);
      }
    }
    
    var x = new a();
    x.init();
    

    And this is the module:

    class b {
    constructor(a) {
        this.a = a;
      }
      
      perform() {
        console.log('class b called');
        this.a.say("I'm in b");
      }
    }
    exports = b;
    

    The output shows as follows:

    Error: Line 8: Unexpected reserved word in http://192.168.88.117:8081/b.js
    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v00 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xef chip 0x4016
    >class b called
    I'm in b
    Disconnected
    >
    Found ESP8266_4MB, 2v01
    >
    Connected to COM4
    Error: Line 8: Unexpected reserved word in http://192.168.88.117:8081/b.js
    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v01 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xef chip 0x4016
    >class b called
    Uncaught Error: Cannot read property 'say' of undefined
     at line 2 col 11
        this.a.say("I'm in b");
              ^
    in function "perform" called from line 10 col 21
        this.mB.perform();
                        ^
    in function "init" called from line 19 col 8
    x.init();
           ^
    > 
    
  • in General
    Avatar for fanThomas

    I have found the problem of the mentioned error. After calling Storage.compact() the error vanished.

  • in General
    Avatar for fanThomas

    This is my module saver program. It works now and can be used to save modules stored on a webserver onto esp8266 flash memory. Start the program and then key in on the left side:
    s.save('MyModule');

    class StModule {
      constructor () {
        this.st = require('Storage');
        this.wifi = require('Wifi');
        this.http = require('http');
        this.server = null;
      }
      
      init (ssid, pw, server) {
        this.wifi.connect(ssid, {password: pw}, (err) => {
          if (err) {
            console.log('Connection error, cannot connect to ', ssid);
          } else {
            console.log('Connected to ', ssid);
            this.server = server;
          }
        });
      }
      
      save(name) {
        var url = 'http://'+this.server+'/'+name+'.js';
        this.st.erase(name);
        this.http.get(url, (res) => {
          var offset = 0;
          var sz = res.headers['Content-Length'];
          res.on('data', (data) => {
            this.st.write(name, data, offset, sz);
            offset += data.length;
          });
          res.on('close', () => {
            console.log('Module '+name+' saved, size: '+this.st.read(name).length);
            console.log(this.st.list());
          });
        });
      }
      
      show(name) {
        print(this.st.read(name));
      }
    }
    
    exports = StModule;
    
    var s = new StModule();
    s.init('MyWlan', 'mypassword', '192.168.88.117:8081');
    //        SSID        Password       webserver's url
    

    I had to find out the size of the module. It was stored in the header of the received code.
    then it needs to specify the total module size for each chunk to be written to the flash:

    this.st.write(name, data, offset, sz);
    

    Altough some times i get an error which might be from a buffer overrun in the http.get module.

  • in General
    Avatar for fanThomas

    I found out that it does not get ignored. You can specify always the total file size.

  • in General
    Avatar for fanThomas

    Does the size parameter always determine the total file size even when I use it with a offset > 0? E.g.

    f.write("a","World!!!",6, 14);
    
  • in General
    Avatar for fanThomas

    I am trying out the Sorage module. The example in the documentation works fine on my board. However a small change in the code brought me to a problem:
    I would start with an empty file of the required size and then populate the file with content:

    var f = require("Storage");
    f.write("a", "", 0, 20);
    console.log("File-Size: ", f.read("a").length);
    f.write("a","Hello",0);
    f.write("a"," ",5);
    f.write("a","World!!!",6);
    print(f.read("a"));
    

    This results in an error:

    File-Size:  14
    Uncaught Error: Too much data for file size
     at line 1 col 18
    f.write("a"," ",5);
                     ^
    Uncaught Error: Too much data for file size
     at line 1 col 25
    f.write("a","World!!!",6);
                            ^
    Hello
    

    And another question: How to determine the size of a file without reading it? The file might be bigger than the available memory and the "list()" method just lists the file name.

Actions