DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:listing_nxt_files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:listing_nxt_files [2015/03/10 19:09] (current)
octazid created
Line 1: Line 1:
 +======Listing files on an NXT======
  
 +//(Sample code by Dave Smart taken from the DroidScript Google Group)//
 + \\ \\
 +Here is an example of how to list all files on your Lego NXT brick:
 +<code javascript>​
 +//Called when application is started.
 +function OnStart()
 +{
 + //Create our screen layout.
 + CreateLayout();​
 +
 + //Create NXT controller object.
 + nxt = app.CreateNxt();​   
 +}
 +
 +//Create the screen layout.
 +function CreateLayout()
 +{
 + //Create the layout (with repeating pattern background).
 + lay = app.CreateLayout( "​Linear",​ "​VCenter,​FillXY"​ );
 + lay.SetBackground( "/​Sys/​Img/​Tile.png",​ "​repeat"​ );
 +
 + //Create '​Connect'​ button.
 + btn = app.CreateButton( "​Connect",​ 0.3,0.1, "​Lego"​ );
 + btn.SetMargins( 0,0,0,0.1 );
 + btn.SetOnTouch( btn_OnTouch );  
 + lay.AddChild( btn );
 +     
 + //Create 'List Files' button.
 + btnCon = app.CreateButton( "List Files",​ 0.3, 0.1, "​Lego"​ );    
 + btnCon.SetOnTouch( btnCon_OnTouch );     
 + lay.AddChild( btnCon ); 
 +
 + //Add layout to the app.
 + app.AddLayout( lay );
 +}
 +
 +//Called when user presses connect.
 +function btn_OnTouch()
 +{        
 + //Show list of NXT devices.
 + nxt.ShowDevices();​
 +}
 +
 +//Called when 'List Files' button is pressed.
 +function btnCon_OnTouch( e )
 +{                           
 + alert( ListFiles( "​*.*"​ ) );
 +}
 +
 +//List files on the NXT using a search pattern.
 +//eg. "​*.rso"​ finds all sound files.
 +function ListFiles( pattern )
 +{   
 +    app.ShowProgress("​Working..."​);​
 +    
 +    //Find first matching file on nxt.
 +    var list = "";​
 +    var inf = nxt.FileFindFirst( pattern );
 +    if( inf!=null ) list = inf.GetName();​    
 +    
 +    //Find remaining files.
 +    while( 1 ) {
 +    inf = nxt.FileFindNext( inf.GetHandle() );
 +    if( inf==null ) break;
 +    list += ","​ + inf.GetName();​    
 +    }
 +    app.HideProgress();​
 +    return list;
 +}</​code>​
sample_code/listing_nxt_files.txt · Last modified: 2015/03/10 19:09 by octazid