Blockly save() function

Posted on
  • I need some help writing a save() block in blockly. This code moves the servo on pin B14 to two positions 0.5 seconds apart then saves it to the microcontroller so the code executes next time I plug it in. This code works when I write it in the JavaScript window:

    setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500);
    setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000);
    save();
    

    Similarly, this code works to call the first two lines from a Blockly block, so I know I'm doing all of that correctly:

    Blockly.Language.save = {
        category: 'Espruino',
        init: function() {
            this.appendDummyInput()
                .appendTitle('Save');
          this.setPreviousStatement(true);
          this.setNextStatement(true);
          this.setColour(100);
          this.setInputsInline(true);
          this.setTooltip('Save Code to Board');
        }
      };
    
    Blockly.JavaScript.save = function() {
      var move = "setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500);\n";
      var move2 = "setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000);\n";
      var saveCmd = "save();\n";
      return move + move2;
    };
    

    But as soon as I call the save() command from Blockly by changing line 18 to:

    return move + move2 + saveCmd;
    

    then the block does nothing. Meaning, the servo doesn't move, and there's no indication that anything was sent to the board. The frustrating part is that when I click on the command prompt side of the IDE and press up to see the commands sent, I see all three of the commands that I wanted to see:

    setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500);
    setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000);
    save();
    

    Please help,
    Rehman

  • I'd be really wary about actually calling the save() command from anything on the 'right-hand side' - especially as you're never sure of the order. For example if you put a second block up, the code would then be:

    setTimeout(function () {analogWrite(B14, 1.8 / 50.0, {freq:20});},500);
    setTimeout(function () {analogWrite(B14, 1.6 / 50.0, {freq:20});},1000);
    save();
    mySecondBlocksCode();
    

    And mySecondBlocksCode() would never be saved. The best thing would be to somehow detect that you had one of those magic blocks and to then append 'save();' to the end of the code that was sent. Maybe the block could append /*SAVE*/ and then editorBlockly.js:getCode() could search for this in the code string and could put save() right on the end of the code.

    Having said all that, I just tried what you said (except I changed analogWrite(...) to digitalWrite(LED1, ...)) and it works perfectly - so I'm really not sure what the problem is - unless for some reason the code gets sent so slowly that the timeouts have already executed by the time you have saved.

    If that's the case I'd really suggest you try and find a way of putting those timeouts inside the onInit() function - which is a special function that gets called when Espruino starts up.

    Now I've added the functions tab it's as easy as just creating a new function block and calling it onInit.

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

Blockly save() function

Posted by Avatar for rsm @rsm

Actions