Creating Droid RPN Functions

Your Droid RPN can be customized by you!

It's really easy to create a function to Droid RPN using JavaScript. ( for those who know a little of javascript )

The fantastic is that you can use :

  1. JavaScript funcions like Math.sqrt.
  2. JavaScript statements like for , while , if.
  3. JavaScript objects like Array

1. First a small example.

This script convert Celsius to Fahrenheit.


var celsius = drpn.popValue();
var fahrenheit = ( 9*celsius/5 ) + 32;
drpn.pushNumber( fahrenheit );

The drpn is an implicit variable that you can use without any imports.

It provides many functions to read and modify the calculator stack.

2. Now do I add this function to my Droid RPN?

Easy! Open the files on your android.

There is a folder named "Android".

Open the "Android" folder.

There is a folder named "data". ( This is the default folder to store app's data on android)

Open the "data" folder.

Now you can see a folder named "vandroidrpn" ( You can create if it's not there ).

Open the "vandroidrpn" folder.

Now you just need to copy the script file there ( /Android/data/vandroidrpn ).

The name of the file will be the name of the function on the calculator.

Important: There are some rules on the file name. See the next topic.

3. File naming rules.

    • The file name should starts with the character 'z'. This way we will be free to create new functions to improve your Droid RPN with no risk of name conflict with your script functions. It's also an easy char to type on mobiles, since it's on the main keyboard.
    • The file name should contain at least 2 and at most 6 characters.
    • The characters allowed are lowercase a-z and 0-9.
    • The extension should be ".js" or ".txt".

So let's try an example:

1. Create a file named zctof.js.

2. Copy this code into the file:

    var celsius = drpn.popValue();    
    var fahrenheit = ( 9*celsius/5 ) + 32;
    drpn.pushNumber( fahrenheit );

3. Go to Droid RPN

4. Press menu -> Exit ( So the scripts can be reloaded )

5. Open Droid RPN

6. Input a number ( temperature that will be the celsius temperature )

7. Choose scripts ( Press the red shift and the enter button )

8. The script functions should be available on the function buttons. (green buttons below the input)

9. Choose your function.

10. The result should be on the stack right now. ( try 20 it will be converted to 68 )

Examples of valid names: zavg.js, ztest.js, zsum5.js, zabc.txt.

Note: You can put as many functions as you want on your Droid RPN!

4. Droid RPN API.

drpn.popValue()

Remove and returns the first position of the stack.

drpn.pop()

Remove and returns the first position of the stack as an object of the type: StackElement.

drpn.getValue( index )

Returns the value on the "index" position of the stack.

Note that "index" should be a number between "0" (inclusive) and the "stack size" (exclusive).

drpn.get( index )

Returns the value on the "index" position of the stack as an object of the type: StackElement.

Note that "index" should be a number between "0" (inclusive) and the "stack size" (exclusive).

drpn.remove( index )

Removes the value on the "index" position of the stack.

Note that "index" should be a number between "0" (inclusive) and the "stack size" (exclusive).

drpn.pushNumber( value )

Put the "value" on the first position of the stack.

Note that "value" should be a number.


drpn.pushString( value )

Put the "value" on the first position of the stack.

Note that "value" should be a string.

drpn.size()

Returns the current size of the stack.

drpn.error( message )

This function can be used to notify an error ( like invalid parameters ).

It also kills the script.

drpn.callFunction( functionName )

Calls another function of the calculator.

For example drpn.callFunction( 'fato' );

( We blocked the call to another saved JavaScript functions using callFunction for security reasons. All the Droid RPN native functions can be used ).

Useful: See the catalog menu of Droid RPN in you mobile by pressing menu -> catalog. There is a list of the functions and you can read a help of each one.

drpn.log( value )

Prints an value on the android default log system. This function is useful for testing script behavior. All you need is to install any logcat reader.

( There are many options on Android Market like the aLogcat app ).

It's a good option to filter the log by 'DRPN_SCRIPT' tag in your favorite logcat reader app, so it will be easier to read Droid RPN script logs.

Usage example: drpn.log( 'test123' );

5. StackElement.

This object allows we to provide more information about an element on the stack than it's value.

In the current version this object have the fowllowing methods:

getValue()

Returns the value of the element.

getType()

Returns the type of the element.

The return can be the strings "number" or "string";


6. A more complex example

// This is a script that calculates the average of all elements of the stack 
//( suggested name: zavg.js )
//Function that sums all the elements in a array
function sumAll( array ){
total=0;
for(i=0; i<array.length;i++)
{
total += array[i];
}
return total;
}
//let's start by creating a JavaScript Array
var stackCopy = new Array();
//now copy the stack to the array
for( i = 0 ; i < drpn.size() ; i++ )
{
   stackCopy[i]=drpn.getValue(i);
}
//calulate the avearage 
var avearage = sumAll(stackCopy) / stackCopy.length
//put in the stack
drpn.pushNumber( avearage );