how the require machinery works within espruino

Posted on
  • I think to have done nothing strange after all.
    I simply wanted to enable the SD card on the STm32F4 Discovery board.
    I wrote USE_FILESYSTEM=1 in the Makefile
    I added the pin definitions for the board in the .py file as for the Espruino board.
    After that I compiled the ESPRUINO source code in order to get a new elf and/or bin file.
    Using the same board on two different environment I got now two different results :
    a) EEPC using Windows7 plus WEB IDE but PC not linked to the Internet .
    Everything works fine.

    b) Mac osX WEB IDE ( PC linked to the Internet )
    Same board, same firmware, the program hangs up.

    To me is now evident that the behavior depends on how the require machinery works.
    I must investigate detailed, but I would appreciate if some explanation, even short, could be given by anyone.
    One could build hundreds of Espruino boards, but it is important to have a complete knowledge about it-s inner workings, otherwise the simplicity of programming with respect traditional Embedded C programming will be vain if a situation can arise when the control

  • may be lost or a strange behavior can happen.
    Espruino is surely working fine, but something must be understood about the whole machinery works.

    Here are the screenshoots for the two different situations:


    1 Attachment

    • screenshot_573.png
  • The working situation...


    1 Attachment

    • figura.jpg
  • I understood the problem.
    In order to have a local filesystem stored on a SD card and being able to use the "require " method on the needed files an initial delay must be setup inside the onInit() function.
    This is needed not when the code is executed from the terminal, but for the code saved in flash after the execution of the save() function.
    On my STM32F4Discovery board 1 second was not enough so I setup for a 2s delay.
    After having down this, all worked pretty fine. non garbage on the LCD even restarting
    It would be however interesting to know why such a delay is needed.

    My code was the following :

    var fs ;
    var lcd ; 
    // ============== LCD ======================
    var lcd;
    var l = false; // var ON =1, OFF = 0;
    var tmo_id1;
    var root;
    // LCD background LED 
    function LCD_bckgnd(pin,status ) { digitalWrite(pin,status); }
    
    // var lcd_on  =   LCD_bckgnd(ON,E7); var lcd_off = LCD_bckgnd(OFF,E7);
    /* var DELAY(func,interval) =   */
    function onInit() {
      // delay a while …(2s)
      setTimeout(function(){
        fs = require('fs');
        root = fs.readdir();
        console.log("onInit(): OK.");
        console.log("SD card File System:",root );
        console.log("--------------------------------");
           var files = fs.readdir(root);
          (function(){
             for(var i=0;i<files.length;i++){  console.log(" -->",files[i]); }
           })();
        lcd = require("HD44780").connect(C5,C4,C0,C1,C2,C3);
        console.log("--------------------------------");
        LCD_bckgnd(E7,true);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(""); // bug fist char lost
        lcd.print("Board OK ...");
        lcd.setCursor(0,1);
        lcd.print("flashing LED1..."); },2000);
       tmo_id1 = setInterval( function() { l = !l; LED1.write(l);} , 500);
        function logo(){   lcd.setCursor(0,1); lcd.print("G.Varasano @2014"); }
       setTimeout(logo,5000);
     }
    // clearInterval(tmo_id1);
    onInit();
    
  • Ahh, that's interesting. Thanks for posting up!

    I'm not 100% sure. I wonder whether it is some issue with trying to write data to the console before USB has properly initialised? Do you get the problem if you don't access the filesystem?

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

how the require machinery works within espruino

Posted by Avatar for user6350 @user6350

Actions