Main Page Namespace List Compound List File List Namespace Members Compound Members

# main.cpp File Reference

Fantom Driver Example. More...

`#include "fantom/iNXT.h"`
`#include "fantom/iNXTIterator.h"`
`#include "fantom/tStatus.h"`
`#include <string.h>`

## Functions

int _VI_FUNCC | main (int argc, char **argv)

## Detailed Description

Fantom Driver Example.

    /*
       © Copyright 2006,
       National Instruments Corporation.
       All rights reserved.

       Originated:  10 March 2006
    */

    // includes...

    #include "fantom/iNXT.h"
    #include "fantom/iNXTIterator.h"
    #include "fantom/tStatus.h"

    #include <string.h>

    // globally-scoped globals...

    // private globals...

    // methods...

    int _VI_FUNCC main( int argc, char** argv )
    {
       nFANTOM100::tStatus status;
       nFANTOM100::iNXTIterator* nxtIteratorPtr = NULL;
       nFANTOM100::iNXT* nxtPtr = NULL;
       nFANTOM100::iFile* filePtr = NULL;
       nFANTOM100::iFileIterator* fileIteratorPtr = NULL;

       // Create an NXT iterator object which is used to find all accessible NXT devices.
       nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(
             false /* don't search for NXTs over Bluetooth (only search USB) */,
             0 /* timeout for Bluetooth discovery ignored */, status );

       // Creating the NXT iterator object could fail, better check status before dereferencing a
       //    potentially NULL pointer.
       if( status.isNotFatal())
       {
          // Create an NXT object for the first NXT that was found.  Note that if a NXT is found
          //    over BT, the computer and the NXT must be paired before an NXT object can be
          //    created.  This can be done programatically using the iNXT::pairBluetooth method.
          nxtPtr = nxtIteratorPtr->getNXT( status );

          // Destroy the NXT iterator object which we no longer need
          nFANTOM100::iNXT::destroyNXTIterator( nxtIteratorPtr );
       }

       // Creating the NXT object could fail, better check status before dereferencing a potentially
       //    NULL pointer.
       if( status.isNotFatal())
       {
          ViUInt8 protocolVersionMajor = 0;
          ViUInt8 protocolVersionMinor = 0;
          ViUInt8 firmwareVersionMajor = 0;
          ViUInt8 firmwareVersionMinor = 0;

          // Query the version numbers for the protocol and firmware installed on the NXT.
          nxtPtr->getFirmwareVersion( protocolVersionMajor, protocolVersionMinor,
                firmwareVersionMajor, firmwareVersionMinor, status );

          // This is a direct command to play a tone.
          ViUInt8 directCommandBuffer[] = { 0x03, 0x00, 0x18, 0x10, 0x00 };

          // Send the direct command to the NXT.
          nxtPtr->sendDirectCommand( false /* a response is not required for this direct command */,
                reinterpret_cast< ViByte* >( directCommandBuffer ), sizeof( directCommandBuffer ),
                NULL /* no response buffer */, 0 /* no response buffer, specify 0 for size */, status );

          // Create a file object
          filePtr = nxtPtr->createFile( "example.log", status );
       }

       // Creating the file object could fail, better check status before dereferencing a
       //    potentially NULL pointer.
       if( status.isNotFatal())
       {
          ViUInt8 fileBuffer[100] = {};
          ViUInt32 fileSizeInBytes = 100;
          for( ViUInt8 index = 0; index < fileSizeInBytes; ++index )
          {
             fileBuffer[index] = index;
          }

          // Open the file for writing, this will also create this file on the NXT.
          filePtr->openForWrite( fileSizeInBytes, status );

          // Write the file contents.
          filePtr->write( fileBuffer, fileSizeInBytes, status );

          // Close the file.
          filePtr->close( status );

          // Destroy the file object.  Note that this does not affect the file on the NXT.
          nxtPtr->destroyFile( filePtr );

          // Create a file iterator object which is used to find files on the NXT.
          fileIteratorPtr = nxtPtr->createFileIterator( "*.*" /* find all files on the NXT */,
                status );
       }

       // Creating the file iterator object could fail, better check status before dereferencing a
       //    potentially NULL pointer.
       if( status.isNotFatal())
       {
          ViChar fileName[20];

          // Iterate through all of the files on the NXT until we find our log file.  Obviously,
          //    this isn't necessary in this simple example but is for illustrative purposes.
          while( status.isNotFatal())
          {
             fileIteratorPtr->getName( fileName, status );
             if( 0 == ::strcmp( fileName, "example.log" ))
             {
                break;
             }

             fileIteratorPtr->advance( status );
          }

          // Now that we have found our log file, create a file object that corresponds to it.
          filePtr = fileIteratorPtr->getFile( status );

          // Destroy the file iterator.
          nxtPtr->destroyFileIterator( fileIteratorPtr );
       }

       // Creating the file object could fail, better check status before dereferencing a
       //    potentially NULL pointer.
       if( status.isNotFatal())
       {
          ViUInt8 fileBuffer[100] = {};
          ViUInt32 fileSizeInBytes = 100;

          // Open the file for reading.
          filePtr->openForRead( status );

          // Read the file contents.
          filePtr->read( fileBuffer, fileSizeInBytes, status );

          // Close the file.
          filePtr->close( status );

          // Remove the file.  This deletes the file from the NXT.
          filePtr->remove( status );

          // Destroy the file object.
          nxtPtr->destroyFile( filePtr );
       }

       // Destroy the NXT object.
       nFANTOM100::iNXT::destroyNXT( nxtPtr );

       return 0;
    }

© Copyright 2005-2006, National Instruments Corporation. All rights reserved.