1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/shell_serial.html
Rhys Weatherley 11f16a223a Update docs
2016-03-13 08:28:23 +10:00

165 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>ArduinoLibs: Serial port Shell example</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">ArduinoLibs
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Pages</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Serial port <a class="el" href="classShell.html" title="Command-line shell access. ">Shell</a> example </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <a class="el" href="classShell.html" title="Command-line shell access. ">Shell</a> class provides a Unix-like shell for issuing commands to the Arduino. This example shows how to use <a class="el" href="classShell.html" title="Command-line shell access. ">Shell</a> to provide command-line access via a serial port. The example has one command "led" for turning the status LED on D13 on and off.</p>
<p>We start by including the shell library definitions and declaring a variable of type <a class="el" href="classShell.html" title="Command-line shell access. ">Shell</a>:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Shell.h&gt;</span></div>
<div class="line"></div>
<div class="line"><a class="code" href="classShell.html">Shell</a> shell;</div>
</div><!-- fragment --></p>
<p>Next we need to initialize the serial port and tell the shell object to use it for communications:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> setup()</div>
<div class="line">{</div>
<div class="line"> pinMode(ledPin, OUTPUT);</div>
<div class="line"></div>
<div class="line"> Serial.begin(9600);</div>
<div class="line"> shell.<a class="code" href="classShell.html#a89d802eae5b716e80b2c4dfbce0a0a75">setPrompt</a>(<span class="stringliteral">&quot;Command: &quot;</span>);</div>
<div class="line"> shell.<a class="code" href="classShell.html#ad9acba905b6e0b661cce19d57c067990">begin</a>(Serial, 5);</div>
<div class="line">}</div>
</div><!-- fragment --></p>
<p>The call to <a class="el" href="classShell.html#a89d802eae5b716e80b2c4dfbce0a0a75">shell.setPrompt()</a> specifies the prompt to display whenever a new line of input is required. The default value is "$ " but we have changed it to "Command: " in this example. The string can be anything and can be changed later if necessary.</p>
<p>The call to <a class="el" href="classShell.html#ad9acba905b6e0b661cce19d57c067990">shell.begin()</a> starts the actual shell. The first argument is the underlying stream to use for communications, the Serial port in our case. The second argument sets the size of the history stack so that <a class="el" href="classShell.html" title="Command-line shell access. ">Shell</a> can remember previous commands and let the user navigate back to them with the up/down arrow keys.</p>
<p>The shell needs to regularly process input from the serial port and handle commands. We accomplish this by calling <a class="el" href="classShell.html#afc45cae4ccfc76321a254bb617dce08c">shell.loop()</a> from the application's main loop() function:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> loop()</div>
<div class="line">{</div>
<div class="line"> shell.<a class="code" href="classShell.html#afc45cae4ccfc76321a254bb617dce08c">loop</a>();</div>
<div class="line">}</div>
</div><!-- fragment --></p>
<p>At this point the application will have two builtin commands, "help" and "exit". But we also want a command of our own. We do this by declaring a command handler:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> ledPin = 13;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> cmdLed(<a class="code" href="classShell.html">Shell</a> &amp;shell, <span class="keywordtype">int</span> argc, <span class="keyword">const</span> <a class="code" href="classShellArguments.html">ShellArguments</a> &amp;argv)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">if</span> (argc &gt; 1 &amp;&amp; !strcmp(argv[1], <span class="stringliteral">&quot;on&quot;</span>))</div>
<div class="line"> digitalWrite(ledPin, HIGH);</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> digitalWrite(ledPin, LOW);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><a class="code" href="classShell.html#a809d96cf913bf11d02f3c0d51a120740">ShellCommand</a>(led, <span class="stringliteral">&quot;Turns the status LED on or off&quot;</span>, cmdLed);</div>
</div><!-- fragment --></p>
<p>The <a class="el" href="classShell.html#a809d96cf913bf11d02f3c0d51a120740" title="Registers a command with the shell. ">ShellCommand()</a> macro informs the shell of a new command with its name, help string, and the name of the handler function.</p>
<p>The full source code for the example follows:</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment">This example demonstrates how to create a simple shell on the serial port.</span></div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">This example is placed into the public domain.</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;Shell.h&gt;</span></div>
<div class="line"></div>
<div class="line"><a class="code" href="classShell.html">Shell</a> shell;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> ledPin = 13;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> cmdLed(<a class="code" href="classShell.html">Shell</a> &amp;shell, <span class="keywordtype">int</span> argc, <span class="keyword">const</span> <a class="code" href="classShellArguments.html">ShellArguments</a> &amp;argv)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">if</span> (argc &gt; 1 &amp;&amp; !strcmp(argv[1], <span class="stringliteral">&quot;on&quot;</span>))</div>
<div class="line"> digitalWrite(ledPin, HIGH);</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> digitalWrite(ledPin, LOW);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><a class="code" href="classShell.html#a809d96cf913bf11d02f3c0d51a120740">ShellCommand</a>(led, <span class="stringliteral">&quot;Turns the status LED on or off&quot;</span>, cmdLed);</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> setup()</div>
<div class="line">{</div>
<div class="line"> pinMode(ledPin, OUTPUT);</div>
<div class="line"></div>
<div class="line"> Serial.begin(9600);</div>
<div class="line"> shell.<a class="code" href="classShell.html#a89d802eae5b716e80b2c4dfbce0a0a75">setPrompt</a>(<span class="stringliteral">&quot;Command: &quot;</span>);</div>
<div class="line"> shell.<a class="code" href="classShell.html#ad9acba905b6e0b661cce19d57c067990">begin</a>(Serial, 5);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> loop()</div>
<div class="line">{</div>
<div class="line"> shell.<a class="code" href="classShell.html#afc45cae4ccfc76321a254bb617dce08c">loop</a>();</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Sun Mar 13 2016 08:28:01 for ArduinoLibs by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>