-
• #2
What minification settings are used to make this trick work?
There are no special minification settings required. Your module will be minified during the build of the website with the Google Closure Compiler, more precisly the Closure Compiler Service API in optimization level 'SIMPLE_OPTIMIZATIONS'.
The minifier knows among other things that
CONFIG.OS_NOTBUSY
is not used and removed this variable/property. This works without additional development effort.If you want further minification (about 1/3 smaller) then you could use ADVANCED_MINIFACTION. You have to create an *.externs file which references your public module API. This is an easy task, but additional testing of the minified module is required!
For the ADS1X15 it would look like:
ADS1X15.externs:
var ads = exports.connect( null ); ads.setGain; ads.getADC; ads.setAddr; ads.getADCVoltage;
The BME280 is a module which uses advance minification:
https://github.com/espruino/EspruinoDocs/blob/f09eaf4d68c9d772f020291341439b2560100d20/devices/BME280.externs
https://github.com/espruino/EspruinoDocs/blob/f09eaf4d68c9d772f020291341439b2560100d20/devices/BME280.js -
• #3
Thanks @luwar.
I did some trials using the following module code located in my local projects/modules directory/****************************************************************************** Starting to recode this for Espruino 11/30/2016 LSM9DS1_Registers.h SFE_LSM9DS1 Library - LSM9DS1 Register Map Jim Lindblom @ SparkFun Electronics Original Creation Date: April 21, 2015 https://github.com/sparkfun/LSM9DS1_Breakout This file defines all registers internal to the gyro/accel and magnetometer devices in the LSM9DS1. Development environment specifics: IDE: Arduino 1.6.0 Hardware Platform: Arduino Uno LSM9DS1 Breakout Version: 1.0 This code is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round! Distributed as-is; no warranty is given. ******************************************************************************/ ///////////////////////////////////////// // LSM9DS1 Accel/Gyro (XL/G) Registers // ///////////////////////////////////////// var Regs={ ACT_THS:0x04, ACT_DUR:0x05, INT_GEN_CFG_XL: (0x06), INT_GEN_THS_X_XL: (0x07), INT_GEN_THS_Y_XL: (0x08), INT_GEN_THS_Z_XL: (0x09), INT_GEN_DUR_XL: (0x0A), REFERENCE_G: (0x0B), INT1_CTRL: (0x0C), INT2_CTRL: (0x0D), WHO_AM_I_XG: (0x0F), CTRL_REG1_G: (0x10), CTRL_REG2_G: (0x11), CTRL_REG3_G: (0x12), ORIENT_CFG_G: (0x13), INT_GEN_SRC_G: (0x14), OUT_TEMP_L: (0x15), OUT_TEMP_H: (0x16), STATUS_REG_0: (0x17), OUT_X_L_G: (0x18), OUT_X_H_G: (0x19), OUT_Y_L_G: (0x1A), OUT_Y_H_G: (0x1B), OUT_Z_L_G: (0x1C), OUT_Z_H_G: (0x1D), CTRL_REG4: (0x1E), CTRL_REG5_XL: (0x1F), CTRL_REG6_XL: (0x20), CTRL_REG7_XL: (0x21), CTRL_REG8: (0x22), CTRL_REG9: (0x23), CTRL_REG10: (0x24), INT_GEN_SRC_XL: (0x26), STATUS_REG_1: (0x27), OUT_X_L_XL: (0x28), OUT_X_H_XL: (0x29), OUT_Y_L_XL: (0x2A), OUT_Y_H_XL: (0x2B), OUT_Z_L_XL: (0x2C), OUT_Z_H_XL: (0x2D), FIFO_CTRL: (0x2E), FIFO_SRC: (0x2F), INT_GEN_CFG_G: (0x30), INT_GEN_THS_XH_G: (0x31), INT_GEN_THS_XL_G: (0x32), INT_GEN_THS_YH_G: (0x33), INT_GEN_THS_YL_G: (0x34), INT_GEN_THS_ZH_G: (0x35), INT_GEN_THS_ZL_G: (0x36), INT_GEN_DUR_G: (0x37) }; /////////////////////////////// // LSM9DS1 Magneto Registers // /////////////////////////////// var Mags={ OFFSET_X_REG_L_M: 0x05, OFFSET_X_REG_H_M: 0x06, OFFSET_Y_REG_L_M: 0x07, OFFSET_Y_REG_H_M: 0x08, OFFSET_Z_REG_L_M: 0x09, OFFSET_Z_REG_H_M: 0x0A, WHO_AM_I_M: 0x0F, CTRL_REG1_M: 0x20, CTRL_REG2_M: 0x21, CTRL_REG3_M: 0x22, CTRL_REG4_M: 0x23, CTRL_REG5_M: 0x24, STATUS_REG_M: 0x27, OUT_X_L_M: 0x28, OUT_X_H_M: 0x29, OUT_Y_L_M: 0x2A, OUT_Y_H_M: 0x2B, OUT_Z_L_M: 0x2C, OUT_Z_H_M: 0x2D, INT_CFG_M: 0x30, INT_SRC_M: 0x30, INT_THS_L_M: 0x32, INT_THS_H_M: 0x33 }; //////////////////////////////// // LSM9DS1 WHO_AM_I Responses // //////////////////////////////// var WHO_AM_I_AG_RSP=0x68; var WHO_AM_I_M_RSP=0x3D; ///////////////////////////////////////////// function LSM9DS1(i2c) { this.i2c = i2c; this.addr = 0x48; this.gain = 2048; } // Create an instance of LSM9DS1 exports.connect = function(i2c1) { return new LSM9DS1(i2c1); };
I invoke this experimental module using the following:
/* Test Module for the LSM9DS1 */ console.log(process.memory()); I2C1.setup({ scl :B6, sda: B9} ); var W=require("LSM9DS1").connect(I2C1); console.log(process.memory());
The output with No Module minification
>{ "free": 4565, "usage": 535, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 } { "free": 4535, "usage": 565, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 }
Module minification Esprima (offline)
>{ "free": 4765, "usage": 335, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 } { "free": 4735, "usage": 365, "total": 5100, "history": 0,
Module minification Closure (online) –Whitespace only
>{ "free": 4762, "usage": 338, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 } { "free": 4732, "usage": 368, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 }
Module minification Closure (online) – Simple optimizations
>{ "free": 4765, "usage": 335, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 } { "free": 4735, "usage": 365, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 }
Module minification Closure (online) – Advanced
>{ "free": 5027, "usage": 73, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 } { "free": 4999, "usage": 101, "total": 5100, "history": 0, "stackEndAddress": 536959408, "flash_start": 134217728, "flash_binary_end": 379112, "flash_code_start": 134234112, "flash_length": 393216 }
From the Usage values the Advanced option seems to do the best job..
-
• #4
I don't know the current state of the WebIDE but a few weeks ago the "Advanced" option did not work in general. The reason is that calls to the Espruino API will be minified/renamed too.
http://forum.espruino.com/conversations/292590/#comment13205984
-
• #5
@luwar, https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler may be the solution here...
Encapsulation is handled better or worse in other languages... better means: more obvious and and enforced, where as JavaScript gives a lot of freedom and therefore expects more from the discipline of the coder...
-
• #6
Yes, advanced optimisation works in small cases, but tends to optimise out a lot of Espruino functions which causes problems for anything sensibly sized.
I'd definitely recommend just simple optimisations. These work pretty well and don't require you to do anything special to your code. The main things are:
- whitespace
- optimising out unused values
- inlining constants and collapsing maths - so
myreg = C.ACCEL_ON|C.MAG_ON|C.GYRO_ON
can just compile down tomyreg=7
(as an example)
Also: I think that if you look in the console (via settings) you might be able to see what the code has minified down to - which could be some help?
- whitespace
I’m starting work on an I2C driver for the Sparkfun 9DoF Sensor Stick
https://www.sparkfun.com/products/13944
3-axis Magnetometer, Gyro and Accelerometer.
As a starting point I’ve downloaded the Arduino Library from
https://github.com/sparkfun/SparkFun_LSM9DS1_Arduino_Library
I’ve edited the LSM9DS1_register.h header file changing the #define to a variable object.
I looked at some existing modules and found an interesting comment in this one.
https://www.espruino.com/modules/ADS1X15.js
The comment:
What minification settings are used to make this trick work?