<repo_name>jzorjnz/ST_SENSOR_JS_DEMO<file_sep>/mbedos5/build.py
import os
import subprocess
from subprocess import call
import sys

# default board name
board_name = "NUCLEO_F429ZI"

# get the input argument
if len(sys.argv) != 2:
    print 'usage : build.py <BOARD> \ne.g build.py ' + board_name +'\nYou must specify which board to target'
    sys.exit(1)
else:
    board_name = sys.argv[1]

# mbed configuration
call("mbed config root .", shell=True)
call("mbed toolchain GCC_ARM", shell=True)
call("mbed target " + board_name, shell=True)
#call("mbed deploy", shell=True)

# make dirs
if not os.path.exists("./source"):
    os.makedirs("./source")

if not os.path.exists("./js"):
    os.makedirs("./js")

# generate target.js
target_board = subprocess.check_output(['mbed', 'target']).split( )

if(len(target_board) != 2):
    print 'Error generating target!'
    sys.exit(1)
with open("js/target.js", "w") as text_file:
    text_file.write("var TARGET = '%s';" % target_board[1])

# generate pins.js
call("python tools/generate_pins.py " + board_name, shell=True)
call("python ../../tools/js2c.py --dest ./source --js-source js --ignore pins.js", shell=True)


# compile using mbed cli
call("mbed compile -j0 --source . --source ../../ -D \"CONFIG_MEM_HEAP_AREA_SIZE=(1024*16)\" -t GCC_ARM", shell=True)


<file_sep>/mbedos5/source/main.cpp

var timeOut = 1000;
var timeStatus = timeOut/10;

var led1 = null;

//led2.write(0);

var i2c = null;
var spi = null;
var hts221 = null;
var lps22hb = null;
var lsm6dsl = null;
var lsm303agr = null;
var acc = null;
var gyr = null;
var acc2 = null;
var mag2 = null;

/*
if(window.TARGET_SENSOR_TILE !== undefined){
    print("TARGET_SENSOR_TILE exists!");
}
else{
    print("TARGET_SENSOR_TILE does not exist!");
}
*/

//print ("TARGET = " + TARGET);
/* 
if(TARGET == "NUCLEO_F429ZI"){
}
else if (TARGET == "NUCLEO_L476RG"){
    spi = SPI(PB_15, NC, PB_13);
    led1 = DigitalOut(0x6C);
    //spi = SPI(31, 4294967295, 29);
}
*/


//var spi = SPI(PB_15, NC, PB_13);
//var spi = SPI(31, 4294967295, 29);
//var TARGET = "NUCLEO";

var count = 0;

var iv = setInterval(function() {
    
    if(count < 2){
        print(".");
        count = count + 1;
    }
    else if(count == 2){
        count++;
        print ("TARGET = " + TARGET);
        if(TARGET == "NUCLEO_F429ZI"){
            i2c = DevI2C(D14, D15);
            led1 = DigitalOut(LED1);
            print("Loading HTS221 sensor...");
            hts221 = HTS221_JS();
            hts221.init_i2c(i2c);
            print("Loading complete.");
            print("Loading LPS22HB sensor...");
            lps22hb = LPS22HB_JS();
            lps22hb.init_i2c(i2c);
            print("Loading complete.");
            print("Loading LSM6DSL sensor...");
            lsm6dsl = LSM6DSL_JS();
            lsm6dsl.init_i2c(i2c);
            print("Loading complete.");
            print("Loading LSM303AGR sensor...");
            lsm303agr = LSM303AGR_JS();
            lsm303agr.init_acc_i2c(i2c);
            lsm303agr.init_mag_i2c(i2c);
            print("Loading complete.");
        }
        else if (TARGET == "NUCLEO_L476RG"){
            spi = SPI(PB_15, NC, PB_13);
            led1 = DigitalOut(0x6C);
            print("Loading LPS22HB sensor...");
            lps22hb = LPS22HB_JS();
            lps22hb.init_spi(spi, PA_3, NC, 3);
            print("Loading complete.");
            print("Loading LSM6DSL sensor...");
            lsm6dsl = LSM6DSL_JS();
            lsm6dsl.init_spi(spi, PB_12, NC, PA_2, 3);
            print("Loading complete.");
            print("Loading LSM303AGR sensor...");
            lsm303agr = LSM303AGR_JS();
            lsm303agr.init_acc_spi(spi, PC_4);
            lsm303agr.init_mag_spi(spi, PB_1);
            print("Loading complete.");
        }

        led1.write(0);

        

        /*
        
        print("Loading LSM303AGR sensor...");
        lsm303agr = LSM303AGR_JS(i2c);
        print("Loading complete.");
        */
        //hts221.led_on(led2);
    }
    else{    
        led1.write(led1.read()? 0: 1);
        
        print("");
        
        if(TARGET == "NUCLEO_F429ZI"){
            print("HTS221: [Temperature] " + hts221.get_temperature() + " C,   [Humidity] " + hts221.get_humidity() + "%");
        }
        
        print("LPS22HB: [Temperature] " + lps22hb.get_temperature() + " C,   [Pressure] " + lps22hb.get_pressure() + " mbar");
        
        acc = JSON.parse(lsm6dsl.get_accelerometer_axes());
        gyr = JSON.parse(lsm6dsl.get_gyroscope_axes());
    
        print("LSM6DSL: [Gyroscope]: " + gyr.x + ", " + gyr.y + ", " + gyr.z + 
        "   [Accelerometer]: " + acc.x + ", " + acc.y + ", " + acc.z);
        
        
        
        
        print("Printing LSM303AGR");
        acc2 = JSON.parse(lsm303agr.get_accelerometer_axes());
        mag2 = JSON.parse(lsm303agr.get_magnetometer_axes());
        print("LSM303AGR: [Magnetometer]: " + mag2.x + ", " + mag2.y + ", " + mag2.z + 
        "   [Accelerometer]: " + acc2.x + ", " + acc2.y + ", " + acc2.z);
        
        print("");
        setTimeout(function(){ led1.write(led1.read()? 0: 1) }, timeStatus);
        
        
    }
}, timeOut);



// Call this function to clear the interval started above
//clearInterval(iv);

print("main.js has finished executing.");
<file_sep>/README.md
# ST_SENSOR_JS_DEMO
Demo for accessing ST sensors using Javascript on Mbed OS.

## Installation
* Before you begin, first follow this tutorial to have a pre built Mbed OS project with Jerryscript here: https://github.com/ARMmbed/mbed-js-example

* After you have built the project, you should have a binary for your board. If not, resolve the errors to continue. If you have errors related to generation of pins, checkout the known issues below.

* Now, clone this repository and copy to the following directory of the mbed-js-example project:
```
mbed-js-example/build/jerryscript/targets/
```
Make sure to replace existing files in mbedos5 directory.

## Build
* When you are building the project for the first time, run this command:
```
cd mbed-js-example/build/jerryscript/targets/mbedos5
python ./build_all.py <BOARD>
```
This makes sure that all the required libraries are downloaded.

* If building next time, run this command:
```
cd mbed-js-example/build/jerryscript/targets/mbedos5
python ./build.py <BOARD>
```

## Development
Your Mbed OS project resides in mbedos5 directory, put js files in mbedos5/js directory. Building will take care of everything.

## Known issues
### Pin generation error
For some boards, pin generation is failing in Jerryscript. For the time being, you can skip some pin expressions in configuration to build for most boards by following these steps:

Open mbed-js-example/jerryscript/targets/mbedos5/tools/generate_pins.py

Put this line in try statement:
```
        try:
            pins[pin.name] = evaluator.eval(expr.strip())
        except:
            print("[Warning] Skipping pin name: " + expr.strip())
```

After doing this, do installation again and then try building.
