Converting between strings and array of bytes?

Posted on
  • Say I have an I2C EEPROM... Now, obviously I'm going to want to read and write strings, not just arrays of bytes.

    I see that I2C.writeTo() is described as accepting a string - so that handles the writing part.

    How do I go the other direction?
    I2C.readFrom() returns an array of bytes - and you can't eval() an array of bytes - is there any way other than looping through each byte and looking up what character it should be?

    (I know the whole I2C EEPROM bit is kinda silly, since we have the microSD card slot... but they're so dirt cheap, I couldn't resist ordering one)

  • That's cool - do you think you'll be able to contribute a module to interface to the EEPROM when you're done?

    Hopefully if you get a SOIC one you'll be able to solder it onto the proto area :)

    I'm afraid that at the moment I think the best way of doing it is to just loop over the array and append to the string:

    function arrayToString(arr) {
      var s = "";
      for (var i in arr)
        s+=String.fromCharCode(arr[i])
      return s;
    }
    

    In 'normal' JS you could do arr.map(String.fromCharCode).join(""), but it's not possible (referencing built-in functions) at the moment. If you desperately wanted to do it now you'd have to do arr.map(function(c){ return String.fromCharCode(c); }).join("")

  • Yeah, will definitely contribute module for it. It'll be a pretty simple module, though. I like writing modules.

    I got a breakout board with the EEPROM in DIP-8 package (socketed), so no soldering onto the prototyping area for now. Will probably get some SOIC-8 ones if this works out well, and do just that. The I2C EEPROMs, in the largest sizes I see available, are limited to 4 per I2C bus (each one has 2 pins dedicated to setting address) - which means one could just fit the maximum loadout of I2C EEPROM's onto the prototyping area.

    One of the things that I'm imagining doing with this is having the Espruino check that the modules it needs are present on the SD card, in node_modules directory - and if they're not, extract them from the EEPROM and write them to the SD card (so when we pull the SD card to get the recorded data, we can just put in a blank micro SD card in it's place, hit reset, and have it sort itself out).

    You say things like "at the moment" - does that mean that you're planning to change something that would make this more graceful in the near future, or just that someone might at some point do so?

  • Well, once the code is written to Espruino, you don't actually need the SD card for modules at all (they're all stored internally). Even if you want to load stuff from scratch, you can use Modules.addCached to directly load the module from EEPROM into memory :)

    For the built-in functions, there's a bug open about it, and I have a branch here that's working towards fixing it. It could be some time before it's done though as it's quite a big change :)

  • Oh, well, that makes life easier! It also means I need to be somewhat more aware of what is and isn't loaded when I save(). For example my DISK_ERR generator, if you paste in the whole block of code, the module won't be loaded when it saves, because onInit() hasn't been called, and the require()'s are in the init functions, as I ran into when the SD card mysteriously failed (that's how I knew it happened - it started complaining that the modules were missing when I restarted it).

  • Yes, that's tricky - but it only happens when you paste code into the left (rather than using the right). I guess this is something that the Web IDE could be aware of (and could warn you about)...

  • Wait, so if I do...

    var x;
    function onInit() {
    x= require("icbm").connect(I2C1);
    x.launch(55.7,37.6);
    }
    save();
    

    It will not have the modules loaded if I paste it into the left side, and after saving it, it will need them on the SD card. But if I do it on the right side, and send it like that, the modules will get loaded by the IDE and be save()ed, and I won't need to have them on the SD card? (I never use the right-hand side of the IDE because it's hard to read on on a retina class screen without a way to scale up the text - I just do everything in sublime text and copy/paste it into the left side).

    If there are two versions of a module with the same name, the one on the website, and the one on the SD card (say, because I'm updating it), which one will be used if I send it through the IDE?
    At what point in the process would the IDE tell the Espruino to load that?

    (for example, would this force it to load off the SD card, or does it do it at the end?):

    var x;
    function onInit() {
    x= require("icbm").connect(I2C1);
    x.launch(55.7,37.6);
    }
    Modules.removeAllCached();
    save();
    

    I'm just trying to make sure I understand how this module loading system works. It's a little disconcerting how it isn't made clear when the IDE is summoning a module from the internet and adding it to the Espruino's module cache. For that matter, is there a location on the local filesystem that the IDE will check before it goes online?

  • But if I do it on the right side, and send it like that, the modules will get loaded by the IDE and be save()ed

    Yes - well, they'll be loaded by the Web IDE and will be save()d when you type save().

    If there are two versions of a module with the same name, the one on the website, and the one on the SD card which one will be used?

    The one on the website (if you use the right-hand side). You can force loading from SD by doing:

    var foo = "mymodule";
    require(foo);
    

    At what point in the process would the IDE tell the Espruino to load that?

    When you click Send to Espruino it prepends the contents of the module to the code that you wrote.

    for example, would this force it to load off the SD card, or does it do it at the end?

    No, that would force a load off the SD card.

    It's a little disconcerting how it isn't made clear when the IDE is summoning a module

    Well, it's meant to be virtually invisible. It might make more sense if it wrote some description onto the console I guess...

    is there a location on the local filesystem that the IDE will check before it goes online?

    No, but you can change the URL used for modules in settings, and can set it to a local webserver.

  • Thanks, I think I have a much better idea of how the module system works now.

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

Converting between strings and array of bytes?

Posted by Avatar for DrAzzy @DrAzzy

Actions