141 lines
9.4 KiB
HTML
141 lines
9.4 KiB
HTML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="6" MadCap:lastHeight="94" MadCap:lastWidth="853" MadCap:disableMasterStylesheet="true" MadCap:tocPath="PBASIC Language Reference" MadCap:InPreviewMode="false" MadCap:PreloadImages="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" lang="en-us" xml:lang="en-us" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="Default.xml" MadCap:SearchType="Stem">
|
|
<head><title> Conditional Compilation</title>
|
|
<link href="../../SkinSupport/MadCap.css" rel="stylesheet" />
|
|
<link href="../../Resources/Stylesheets/BSE_Help.css" rel="stylesheet" />
|
|
<script src="../../SkinSupport/MadCapAll.js" type="text/javascript">
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div class="MCBreadcrumbsBox_0"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="AlphaRef.htm">PBASIC Language Reference</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Conditional Compilation</span>
|
|
</div>
|
|
<h1> Conditional Compilation</h1>
|
|
<div class="ImagePlusCaption">
|
|
<div class="Col2">
|
|
<p>
|
|
<img src="../../graphics/pgm_icon2.gif" border="0" alt="BS2 icon" title="BS2 icon" />
|
|
<img src="../../graphics/pgm_icon2e.gif" border="0" alt="BS2e icon" title="BS2e icon" />
|
|
<img src="../../graphics/pgm_icon2sx.gif" border="0" alt="BS2sx icon" title="BS2sx icon" />
|
|
<img src="../../graphics/pgm_icon2p.gif" border="0" alt="BS2p icon" title="BS2p icon" />
|
|
<img src="../../graphics/pgm_icon2pe.gif" border="0" alt="BS2pe icon" title="BS2pe icon" />
|
|
<img src="../../graphics/pgm_icon2px.gif" border="0" alt="BS2px icon" title="BS2px icon" /><span class="code_in_text"> {PBASIC 2.5}</span>
|
|
</p>
|
|
</div>
|
|
<p style="text-align: right;"> </p>
|
|
<p> </p>
|
|
</div>
|
|
<p class="clear"> </p>
|
|
<p> </p>
|
|
<p class="PlainText">In order to facilitate the creation of programs that can be compiled for and
|
|
downloaded to the connected model of BASIC Stamp2, several conditional compile directives
|
|
have been added to PBASIC 2.5: <span class="keyword_in_text">#DEFINE</span>, <span class="keyword_in_text">#IF...#THEN...#ELSE</span>, <span class="keyword_in_text">#SELECT...#CASE</span>, and <span class="keyword_in_text">#ERROR</span>. Conditional compile directives are evaluated
|
|
before the program is compiled, so variables and named constants cannot be
|
|
referenced within a conditional compilation definition.</p>
|
|
<h2 class="code"><a name="#DEFINE"></a>#DEFINE</h2>
|
|
<p class="PlainText">Syntax: <span class="keyword_in_text">#DEFINE</span> <![CDATA[ ]]><i>Symbol {= Value}</i></p>
|
|
<p class="PlainText"><span class="keyword_in_text">#DEFINE</span> allows the programmer to create custom symbols for use within
|
|
conditional compilation control structures. </p>
|
|
<ul>
|
|
<li value="1"><b><i>Symbol</i></b>  is a string (up to 32 characters).</li>
|
|
<li value="2"><b><i>Value</i></b>  is an optional constant/expression specifying the value
|
|
of the defined symbol. If the value parameter is omitted, <i>Symbol</i> is
|
|
defined as True.</li>
|
|
</ul>
|
|
<p>Example:</p><pre class="BScode" xml:space="preserve">#DEFINE DebugMode = 1</pre>
|
|
<p>In the example above, the "= 1" is optional. Note that when the compiler
|
|
encounters an undefined symbol it will evaluate that symbol as False (0). You
|
|
can define a symbol as False by removing the definition with a comment character
|
|
( ' ) or change its value to zero (recommended)</p>
|
|
<h2 class="code"><a name="#IF...#T"></a>#IF...#THEN...#ELSE</h2>
|
|
<p class="PlainText">Syntax: <br /><br />   <span class="keyword_in_text">#IF</span> <![CDATA[ ]]><i>Condition</i> <![CDATA[ ]]><span class="keyword_in_text">#THEN</span><br />     <i>Statement(s)</i><br /> { <span class="keyword_in_text">#ELSE</span><br />     <i>Statement(s)</i> }<br />   <span class="keyword_in_text">#ENDIF</span></p>
|
|
<h2>Function</h2>
|
|
<p class="PlainText">Evaluate <i>Condition</i> and, if it is True, compile the statement(s) following
|
|
<span class="keyword_in_text">#THEN</span>, otherwise compile the statements following <span class="keyword_in_text">#ELSE</span>.
|
|
|
|
</p>
|
|
<ul>
|
|
<li value="1"><b><i>Condition</i></b> is a statement, such that can be evaluated as True
|
|
or False.</li>
|
|
<li value="2"><b><i>Statement</i></b> is any valid PBASIC statement.</li>
|
|
</ul>
|
|
<p class="PlainText">Example:</p><pre class="BScode" xml:space="preserve">
|
|
' set Baud for 9600-N81
|
|
|
|
#IF ($STAMP = BS2sx) OR ($STAMP = BS2p) #THEN
|
|
 Baud CON 240
|
|
#ELSE
|
|
 Baud CON 84
|
|
#ENDIF
|
|
</pre>
|
|
<p class="PlainText">In this example, the constant Baud is set to match the connected BASIC Stamp.
|
|
This code will work with the BS2, BS2e, BS2sx, BS2p and BS2pe.</p>
|
|
<h2 class="code"><a name="#SELECT."></a>#SELECT...#CASE</h2>
|
|
<p class="PlainText">Syntax: <br /><br />   <span class="keyword_in_text">#SELECT</span> <![CDATA[ ]]><i>Expression</i><br />     <span class="keyword_in_text">#CASE</span> <![CDATA[ ]]><i>Condition(s)</i><br />       <i>Statement(s)</i><br />   { <span class="keyword_in_text">#CASE</span> <![CDATA[ ]]><i>Condition(s)</i><br />       <i>Statement(s)</i><br />     <span class="keyword_in_text">#CASE #ELSE</span><br />       <i>Statement(s)</i> }<br />   <span class="keyword_in_text">#ENDSELECT</span></p>
|
|
<h2>Function</h2>
|
|
<p class="PlainText">Evaluate <i>Expression</i> and then conditionally compile a block of code based
|
|
on comparison to <i>Condition(s)</i>. If no conditions are found True and a
|
|
<span class="keyword_in_text">#CASE ELSE</span> block is included, the <span class="keyword_in_text">#CASE #ELSE</span> code statements will
|
|
compiled. </p>
|
|
<ul>
|
|
<li value="1"><b><i>Expression</i></b> is a variable, a constant or an expression.</li>
|
|
<li value="2"><b><i>Condition</i></b> is a statement, that can be evaluated as True or
|
|
False. Multiple conditions within the same <span class="keyword_in_text">#CASE</span> can be separated by
|
|
commas ( , ).</li>
|
|
<li value="3"><b><i>Statement(s)</i></b> is any valid PBASIC statement or statements.</li>
|
|
</ul>
|
|
<p class="PlainText">Example:</p><pre class="BScode" xml:space="preserve">
|
|
#SELECT $STAMP
|
|
 #CASE BS2, BS2e, BS2sx
|
|
   GOSUB LCD_Write
|
|
 #CASE #ELSE
|
|
   LCDOUT LCDpin, cmd, [char]
|
|
#ENDSELECT
|
|
</pre>
|
|
<h2 class="code"><a name="#ERROR"></a>#ERROR</h2>
|
|
<p class="PlainText">Syntax: <span class="keyword_in_text">#ERROR</span> <![CDATA[ ]]><i>Message</i></p>
|
|
<p class="PlainText"><span class="keyword_in_text">#ERROR</span> allows the programmer to create custom a custom error dialog.</p>
|
|
<ul>
|
|
<li value="1"><b><i>Message</i></b>  is the error message string.</li>
|
|
</ul>
|
|
<p class="PlainText">Example:</p><pre class="BScode" xml:space="preserve">
|
|
#DEFINE I2CReady = (($STAMP = BS2p) OR ($STAMP = BS2pe))
|
|
#IF NOT I2CReady #THEN
|
|
 #ERROR "BS2p or BS2pe is required for this program."
|
|
#ENDIF
|
|
</pre>
|
|
<p class="PlainText">The example above will halt the program and display the dialog below if you
|
|
attempt to compile the program and run it on a BASIC Stamp other than the BS2p or
|
|
BS2pe:</p>
|
|
<img src="../../graphics/error_popup.gif" border="0" alt="" />
|
|
<div class="Col2">
|
|
<div class="MasterFoot">
|
|
<p MadCap:conditions="BSEconditions.BSEWebHelp (Primary)-INCLUDE"><a href="../../HomeTopics/HomePage.htm">Go to Welcome page</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class="Col2">
|
|
<div class="MasterFoot">
|
|
<p style="text-align: right;"><span class="ContactInfoProjectName">BASIC Stamp Help</span> <![CDATA[ ]]><span class="ContactInfoVersion#">Version 2.5.4</span> <![CDATA[ ]]></p>
|
|
<p style="text-align: right;">Copyright © <span class="ContactInfoCompanyName">Parallax Inc.</span></p>
|
|
<p style="text-align: right;"><span class="SystemShortDate">8/8/2012</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">/* <![CDATA[ */
|
|
|
|
var _gaq = _gaq || [];
|
|
_gaq.push(['_setAccount', 'UA-285614-1']);
|
|
_gaq.push(['_trackPageview']);
|
|
|
|
(function() {
|
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
|
})();
|
|
|
|
/* ]]> */</script>
|
|
<script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
|
|
</script>
|
|
</body>
|
|
</html> |