Avatar for gerardwr

gerardwr

Member since Jun 2020 • Last active Aug 2020
  • 4 conversations
  • 57 comments

Most recent activity

  • in JavaScript
    Avatar for gerardwr

    Tested my code once more to verify that both news.txt and news.txt\1 are recognised.
    IT DOES NOT :O

    var Storage = require("Storage");
    l=Storage.list();
    print(l);
    function exists(name){
      return Boolean(Storage.open(name,"r").addr);
    }
    print(exists("news.txt"));
    print(exists("news.txt\1"));
    print(exists("nonews.txt"));
    print(exists("ABC.txt"));
    

    Output is:

    [
    "news.txt\1",
    "weath0.txt\1",
    "weath1.txt\1",
    "stocks.txt\1",
    "messages.txt\1",
    "ABC.txt"
    ]
    true
    false
    false
    false

    The mix of the 2 storage versions does not feel like a blessing anymore :)

  • in JavaScript
    Avatar for gerardwr

    When I check for files without an added chunk number (f.i. file ABC.txt) it is recognised perfectly with your code, output is:

    [
    "news.txt\1",
    "weath0.txt\1",
    "weath1.txt\1",
    "stocks.txt\1",
    "messages.txt\1",
    "ABC.txt"
    ]
    exists

    As @fanoush explained there are 2 methods to store a file, I happened to use the files with the added chunk number.

  • in JavaScript
    Avatar for gerardwr

    Fair enough @MaBe but news.txt\1 is not recognised either.

    s = require('Storage');
    print(s.list());
    filename = 'news.txt\1';
    if (s.read(filename)!==undefined) {
      print("exists");
    } else {
      print("does not exists");
    }
    

    Output is:

    [
    "news.txt\1",
    "weath0.txt\1",
    "weath1.txt\1",
    "stocks.txt\1",
    "messages.txt\1"
    ]
    does not exists

  • in JavaScript
    Avatar for gerardwr

    OK, thanks, but all of my files have a .txt extension, and those are not recognised by your code :(

    s = require('Storage');
    
    print(s.list());
    
    filename = 'news.txt';
    if (s.read(filename)!==undefined) {
      print("exists");
    } else {
      print("does not exists");
    }
    

    Output is:

    [
    "news.txt\1",
    "weath0.txt\1",
    "weath1.txt\1",
    "stocks.txt\1",
    "messages.txt\1"
    ]
    does not exists

  • in JavaScript
    Avatar for gerardwr

    This works for me. It's based on the fact that addr is zero when a file does not exist. When a file exists addr is not zero but points to an address in Storage.

    var Storage = require("Storage");
    
    l=Storage.list();
    print(l);
    
    function exists(name){
      return Boolean(Storage.open(name,"r").addr);
    }
    
    print(exists("news.txt"));
    print(exists("nonews.txt"));
    

    The output is:

    [
    "news.txt\1",
    "weath0.txt\1",
    "weath1.txt\1",
    "stocks.txt\1",
    "messages.txt\1"
    ]
    true
    false

  • in JavaScript
    Avatar for gerardwr

    Great, I was just looking for some "file exists" code ;-)

    The suggestions work OK for filenames without extension like "ABC". But I want to check for file "news.txt" and all variants report "false/missing".

    This is my code

    s = require('Storage');
    file = { name : 'news.txt', exist : 0};
    
    print(s.list());
    
    // variant I
    if (s.list().find(e => (e == file.name))) {
          file.exist = 1;
          console.log("I",file.name,"exists");
    } else {
          file.exist = 1;
          console.log("I",file.name,"is missing ");
    }
    // variant II
    if (s.read(file.name)) {
          file.exist = 1;
          console.log("II",file.name,"exists");
    } else {
          file.exist = 1;
          console.log("II",file.name,"is missing ");
    }
    
    // variant III
    print("III",s.list(/^file.name/));
    
    // variant IV
    print("IV",s.read(file.name)!==undefined­);
    
    // variant V
    print("V",s.read(file.name));
    

    Output is
    `

    [
    "news.txt\1",
    "weath0.txt\1",
    "weath1.txt\1",
    "stocks.txt\1",
    "messages.txt\1",
    "ABC",
    "news"
    ]
    I news.txt is missing
    II news.txt is missing
    III [ ]
    IV false
    V undefined

    `

  • in Bangle.js
    Avatar for gerardwr

    This is a working example I have come up with with your help, maybe it's of any use to others.

    /*
    
    Example of an Espruino Object that fires an event when the button on pin D17 is pressed.
    
    */
    
    var P8 = {
      init : function(){
        setWatch(function(){
          P8.emit("button","D17");
        },D17 ,{ repeat:true,debounce:25 }
        );
      }
    }; // end of P8 Object
    
    // ============================= test of P8 object  ===============
    
    P8.init();
    
    
    P8.on('button', function(btn){ print("Button",btn, "press detected");});
    

    Output of the script when the button is pressed:
    Button D17 press detected

    In this example the init function needs to be called at start of the main script. Not sure if there is a way to move this init into the Object, but this works for me.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for gerardwr

    BTW I had to reboot the Bangle when I wanted to reconnect it to WebIDE. Without rebooting is did not show up in the WebBluetooth devices of the WebIde.

    And I mad sure it was not still connected to NrfConnect :-)

  • in Puck.js, Pixl.js and MDBT42
    Avatar for gerardwr

    I tried the script on my Bangle:

    • uploaded the script to the Bangle using WebIDE
    • disconnected the Bangle from the WebIDE
    • Using NrfConnect on iPhone reveals the Bangle, connected to it, and it shows the advertised services. Writing to 78290002..... works OK.
    • To make sure I disconnected the Bangle from NrfConnect, and reconnected. The data I had written was displayed as I sent it. I GUESS this means that the data is written to the Bangle succesfully.

    Maybe this helps?

Actions