Files
NXT-Dev-Kits/LEGO MINDSTORMS NXT Software Developer Kit/fantom/documentation/html/main_8cpp.html

189 lines
11 KiB
HTML
Executable File

<html><body>
<!-- Generated by Doxygen 1.2.11.1 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="namespacemembers.html">Namespace Members</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>main.cpp File Reference</h1>Fantom Driver Example.
<a href="#_details">More...</a>
<p>
<code>#include "<a class="el" href="iNXT_8h-source.html">fantom/iNXT.h</a>"</code><br>
<code>#include "<a class="el" href="iNXTIterator_8h-source.html">fantom/iNXTIterator.h</a>"</code><br>
<code>#include "<a class="el" href="tStatus_8h-source.html">fantom/tStatus.h</a>"</code><br>
<code>#include &lt;string.h&gt;</code><br>
<table border=0 cellpadding=0 cellspacing=0>
<tr><td colspan=2><br><h2>Functions</h2></td></tr>
<tr><td nowrap align=right valign=top><a name="a0" doxytag="main.cpp::main"></a>
int _VI_FUNCC&nbsp;</td><td valign=bottom><b>main</b> (int argc, char **argv)</td></tr>
</table>
<hr><a name="_details"></a><h2>Detailed Description</h2>
Fantom Driver Example.
<p>
<p>
<p>
<div class="fragment"><pre>
<font class="comment">/*</font>
<font class="comment"> © Copyright 2006,</font>
<font class="comment"> National Instruments Corporation.</font>
<font class="comment"> All rights reserved.</font>
<font class="comment"></font>
<font class="comment"> Originated: 10 March 2006</font>
<font class="comment">*/</font>
<font class="comment">// includes...</font>
<font class="preprocessor">#include "<a class="code" href="iNXT_8h.html">fantom/iNXT.h</a>"</font>
<font class="preprocessor">#include "<a class="code" href="iNXTIterator_8h.html">fantom/iNXTIterator.h</a>"</font>
<font class="preprocessor">#include "<a class="code" href="tStatus_8h.html">fantom/tStatus.h</a>"</font>
<font class="preprocessor">#include &lt;string.h&gt;</font>
<font class="comment">// globally-scoped globals...</font>
<font class="comment">// private globals...</font>
<font class="comment">// methods...</font>
<font class="keywordtype">int</font> _VI_FUNCC main( <font class="keywordtype">int</font> argc, <font class="keywordtype">char</font>** argv )<font class="keyword"></font>
<font class="keyword"></font>{
<a class="code" href="classnFANTOM100_1_1tStatus.html">nFANTOM100::tStatus</a> status;
<a class="code" href="classnFANTOM100_1_1iNXTIterator.html">nFANTOM100::iNXTIterator</a>* nxtIteratorPtr = NULL;
<a class="code" href="classnFANTOM100_1_1iNXT.html">nFANTOM100::iNXT</a>* nxtPtr = NULL;
<a class="code" href="classnFANTOM100_1_1iFile.html">nFANTOM100::iFile</a>* filePtr = NULL;
<a class="code" href="classnFANTOM100_1_1iFileIterator.html">nFANTOM100::iFileIterator</a>* fileIteratorPtr = NULL;
<font class="comment">// Create an NXT iterator object which is used to find all accessible NXT devices.</font>
nxtIteratorPtr = <a class="code" href="classnFANTOM100_1_1iNXT.html#d2">nFANTOM100::iNXT::createNXTIterator</a>(
<font class="keyword">false</font> <font class="comment">/* don't search for NXTs over Bluetooth (only search USB) */</font>,
0 <font class="comment">/* timeout for Bluetooth discovery ignored */</font>, status );
<font class="comment">// Creating the NXT iterator object could fail, better check status before dereferencing a</font>
<font class="comment">// potentially NULL pointer.</font>
<font class="keywordflow">if</font>( status.<a class="code" href="classnFANTOM100_1_1tStatus.html#a11">isNotFatal</a>())
{
<font class="comment">// Create an NXT object for the first NXT that was found. Note that if a NXT is found</font>
<font class="comment">// over BT, the computer and the NXT must be paired before an NXT object can be</font>
<font class="comment">// created. This can be done programatically using the iNXT::pairBluetooth method.</font>
nxtPtr = nxtIteratorPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXTIterator.html#a2">getNXT</a>( status );
<font class="comment">// Destroy the NXT iterator object which we no longer need</font>
<a class="code" href="classnFANTOM100_1_1iNXT.html#d3">nFANTOM100::iNXT::destroyNXTIterator</a>( nxtIteratorPtr );
}
<font class="comment">// Creating the NXT object could fail, better check status before dereferencing a potentially</font>
<font class="comment">// NULL pointer.</font>
<font class="keywordflow">if</font>( status.<a class="code" href="classnFANTOM100_1_1tStatus.html#a11">isNotFatal</a>())
{
ViUInt8 protocolVersionMajor = 0;
ViUInt8 protocolVersionMinor = 0;
ViUInt8 firmwareVersionMajor = 0;
ViUInt8 firmwareVersionMinor = 0;
<font class="comment">// Query the version numbers for the protocol and firmware installed on the NXT.</font>
nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a8">getFirmwareVersion</a>( protocolVersionMajor, protocolVersionMinor,
firmwareVersionMajor, firmwareVersionMinor, status );
<font class="comment">// This is a direct command to play a tone.</font>
ViUInt8 directCommandBuffer[] = { 0x03, 0x00, 0x18, 0x10, 0x00 };
<font class="comment">// Send the direct command to the NXT.</font>
nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a9">sendDirectCommand</a>( <font class="keyword">false</font> <font class="comment">/* a response is not required for this direct command */</font>,
reinterpret_cast&lt; ViByte* &gt;( directCommandBuffer ), <font class="keyword">sizeof</font>( directCommandBuffer ),
NULL <font class="comment">/* no response buffer */</font>, 0 <font class="comment">/* no response buffer, specify 0 for size */</font>, status );
<font class="comment">// Create a file object</font>
filePtr = nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a0">createFile</a>( <font class="stringliteral">"example.log"</font>, status );
}
<font class="comment">// Creating the file object could fail, better check status before dereferencing a</font>
<font class="comment">// potentially NULL pointer.</font>
<font class="keywordflow">if</font>( status.<a class="code" href="classnFANTOM100_1_1tStatus.html#a11">isNotFatal</a>())
{
ViUInt8 fileBuffer[100] = {};
ViUInt32 fileSizeInBytes = 100;
<font class="keywordflow">for</font>( ViUInt8 index = 0; index &lt; fileSizeInBytes; ++index )
{
fileBuffer[index] = index;
}
<font class="comment">// Open the file for writing, this will also create this file on the NXT.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a4">openForWrite</a>( fileSizeInBytes, status );
<font class="comment">// Write the file contents.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a10">write</a>( fileBuffer, fileSizeInBytes, status );
<font class="comment">// Close the file.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a8">close</a>( status );
<font class="comment">// Destroy the file object. Note that this does not affect the file on the NXT.</font>
nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a1">destroyFile</a>( filePtr );
<font class="comment">// Create a file iterator object which is used to find files on the NXT.</font>
fileIteratorPtr = nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a2">createFileIterator</a>( <font class="stringliteral">"*.*"</font> <font class="comment">/* find all files on the NXT */</font>,
status );
}
<font class="comment">// Creating the file iterator object could fail, better check status before dereferencing a</font>
<font class="comment">// potentially NULL pointer.</font>
<font class="keywordflow">if</font>( status.<a class="code" href="classnFANTOM100_1_1tStatus.html#a11">isNotFatal</a>())
{
ViChar fileName[20];
<font class="comment">// Iterate through all of the files on the NXT until we find our log file. Obviously,</font>
<font class="comment">// this isn't necessary in this simple example but is for illustrative purposes.</font>
<font class="keywordflow">while</font>( status.<a class="code" href="classnFANTOM100_1_1tStatus.html#a11">isNotFatal</a>())
{
fileIteratorPtr-&gt;<a class="code" href="classnFANTOM100_1_1iFileIterator.html#a2">getName</a>( fileName, status );
<font class="keywordflow">if</font>( 0 == ::strcmp( fileName, <font class="stringliteral">"example.log"</font> ))
{
<font class="keywordflow">break</font>;
}
fileIteratorPtr-&gt;<a class="code" href="classnFANTOM100_1_1iFileIterator.html#a1">advance</a>( status );
}
<font class="comment">// Now that we have found our log file, create a file object that corresponds to it.</font>
filePtr = fileIteratorPtr-&gt;<a class="code" href="classnFANTOM100_1_1iFileIterator.html#a0">getFile</a>( status );
<font class="comment">// Destroy the file iterator.</font>
nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a3">destroyFileIterator</a>( fileIteratorPtr );
}
<font class="comment">// Creating the file object could fail, better check status before dereferencing a</font>
<font class="comment">// potentially NULL pointer.</font>
<font class="keywordflow">if</font>( status.<a class="code" href="classnFANTOM100_1_1tStatus.html#a11">isNotFatal</a>())
{
ViUInt8 fileBuffer[100] = {};
ViUInt32 fileSizeInBytes = 100;
<font class="comment">// Open the file for reading.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a3">openForRead</a>( status );
<font class="comment">// Read the file contents.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a9">read</a>( fileBuffer, fileSizeInBytes, status );
<font class="comment">// Close the file.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a8">close</a>( status );
<font class="comment">// Remove the file. This deletes the file from the NXT.</font>
filePtr-&gt;<a class="code" href="classnFANTOM100_1_1iFile.html#a11">remove</a>( status );
<font class="comment">// Destroy the file object.</font>
nxtPtr-&gt;<a class="code" href="classnFANTOM100_1_1iNXT.html#a1">destroyFile</a>( filePtr );
}
<font class="comment">// Destroy the NXT object.</font>
<a class="code" href="classnFANTOM100_1_1iNXT.html#d1">nFANTOM100::iNXT::destroyNXT</a>( nxtPtr );
<font class="keywordflow">return</font> 0;
}
</pre></div>
<p>
<p>
&copy; Copyright 2005-2006, National Instruments Corporation. All rights reserved.
</p>
</body></html>