ardupilot/libraries/doc/html/menu_8cpp_source.html

181 lines
17 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"/>
<title>ArduPilot Libraries: menu.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generated by Doxygen 1.7.1 -->
<div class="navigation" id="top">
<div class="tabs">
<ul class="tablist">
<li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&nbsp;List</span></a></li>
<li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
</ul>
</div>
<div class="header">
<div class="headertitle">
<h1>menu.cpp</h1> </div>
</div>
<div class="contents">
<a href="menu_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-</span>
<a name="l00002"></a>00002
<a name="l00003"></a>00003 <span class="comment">//</span>
<a name="l00004"></a>00004 <span class="comment">// Simple commandline menu system.</span>
<a name="l00005"></a>00005 <span class="comment">//</span>
<a name="l00006"></a>00006
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;<a class="code" href="_fast_serial_8h.html">FastSerial.h</a>&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;<a class="code" href="_a_p___common_8h.html" title="Common definitions and utility routines for the ArduPilot libraries.">AP_Common.h</a>&gt;</span>
<a name="l00009"></a>00009
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;ctype.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;avr/pgmspace.h&gt;</span>
<a name="l00013"></a>00013
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;<a class="code" href="menu_8h.html" title="Simple commandline menu subsystem. The Menu class implements a simple CLI that accepts commands type...">include/menu.h</a>&quot;</span>
<a name="l00015"></a>00015
<a name="l00016"></a>00016 <span class="comment">// statics</span>
<a name="l00017"></a>00017 <span class="keywordtype">char</span> Menu::_inbuf[MENU_COMMANDLINE_MAX];
<a name="l00018"></a>00018 <a class="code" href="struct_menu_1_1arg.html">Menu::arg</a> Menu::_argv[<a class="code" href="menu_8h.html#a78a3dfeac59b72f430d58683a06644fe" title="maximum number of arguments">MENU_ARGS_MAX</a> + 1];
<a name="l00019"></a>00019
<a name="l00020"></a>00020 <span class="comment">// constructor</span>
<a name="l00021"></a>00021 <a class="code" href="class_menu.html#ac6862dec531cc9b42410390985a1c731">Menu::Menu</a>(<span class="keyword">const</span> prog_char *prompt, <span class="keyword">const</span> <a class="code" href="struct_menu_1_1command.html">Menu::command</a> *commands, uint8_t entries, preprompt ppfunc) :
<a name="l00022"></a>00022 _prompt(prompt),
<a name="l00023"></a>00023 _commands(commands),
<a name="l00024"></a>00024 _entries(entries),
<a name="l00025"></a>00025 _ppfunc(ppfunc)
<a name="l00026"></a>00026 {
<a name="l00027"></a>00027 }
<a name="l00028"></a>00028
<a name="l00029"></a>00029 <span class="comment">// run the menu</span>
<a name="l00030"></a>00030 <span class="keywordtype">void</span>
<a name="l00031"></a><a class="code" href="class_menu.html#a8424e0886499d6901c1d2406c568709c">00031</a> <a class="code" href="class_menu.html#a8424e0886499d6901c1d2406c568709c" title="menu runner">Menu::run</a>(<span class="keywordtype">void</span>)
<a name="l00032"></a>00032 {
<a name="l00033"></a>00033 uint8_t len, i, ret;
<a name="l00034"></a>00034 uint8_t argc;
<a name="l00035"></a>00035 <span class="keywordtype">int</span> c;
<a name="l00036"></a>00036 <span class="keywordtype">char</span> *s;
<a name="l00037"></a>00037
<a name="l00038"></a>00038 <span class="comment">// loop performing commands</span>
<a name="l00039"></a>00039 <span class="keywordflow">for</span> (;;) {
<a name="l00040"></a>00040
<a name="l00041"></a>00041 <span class="comment">// run the pre-prompt function, if one is defined</span>
<a name="l00042"></a>00042 <span class="keywordflow">if</span> ((NULL != _ppfunc) &amp;&amp; !_ppfunc())
<a name="l00043"></a>00043 <span class="keywordflow">return</span>;
<a name="l00044"></a>00044
<a name="l00045"></a>00045 <span class="comment">// loop reading characters from the input</span>
<a name="l00046"></a>00046 len = 0;
<a name="l00047"></a>00047 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_better_stream.html#aa735777635173dca53f4d9a4f6a1e0b8">printf</a>(<span class="stringliteral">&quot;%S] &quot;</span>, _prompt);
<a name="l00048"></a>00048 <span class="keywordflow">for</span> (;;) {
<a name="l00049"></a>00049 c = <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a6b63b52d3dedb762340675f0dbb29622">read</a>();
<a name="l00050"></a>00050 <span class="keywordflow">if</span> (-1 == c)
<a name="l00051"></a>00051 <span class="keywordflow">continue</span>;
<a name="l00052"></a>00052 <span class="comment">// carriage return -&gt; process command</span>
<a name="l00053"></a>00053 <span class="keywordflow">if</span> (<span class="charliteral">&#39;\r&#39;</span> == c) {
<a name="l00054"></a>00054 _inbuf[len] = <span class="charliteral">&#39;\0&#39;</span>;
<a name="l00055"></a>00055 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9">write</a>(<span class="charliteral">&#39;\r&#39;</span>);
<a name="l00056"></a>00056 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9">write</a>(<span class="charliteral">&#39;\n&#39;</span>);
<a name="l00057"></a>00057 <span class="keywordflow">break</span>;
<a name="l00058"></a>00058 }
<a name="l00059"></a>00059 <span class="comment">// backspace</span>
<a name="l00060"></a>00060 <span class="keywordflow">if</span> (<span class="charliteral">&#39;\b&#39;</span> == c) {
<a name="l00061"></a>00061 <span class="keywordflow">if</span> (len &gt; 0) {
<a name="l00062"></a>00062 len--;
<a name="l00063"></a>00063 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9">write</a>(<span class="charliteral">&#39;\b&#39;</span>);
<a name="l00064"></a>00064 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9">write</a>(<span class="charliteral">&#39; &#39;</span>);
<a name="l00065"></a>00065 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9">write</a>(<span class="charliteral">&#39;\b&#39;</span>);
<a name="l00066"></a>00066 <span class="keywordflow">continue</span>;
<a name="l00067"></a>00067 }
<a name="l00068"></a>00068 }
<a name="l00069"></a>00069 <span class="comment">// printable character</span>
<a name="l00070"></a>00070 <span class="keywordflow">if</span> (isprint(c) &amp;&amp; (len &lt; (<a class="code" href="menu_8h.html#a7bc4df209e0261ea6ab5303dd8405705" title="maximum input line length">MENU_COMMANDLINE_MAX</a> - 1))) {
<a name="l00071"></a>00071 _inbuf[len++] = c;
<a name="l00072"></a>00072 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9">write</a>((<span class="keywordtype">char</span>)c);
<a name="l00073"></a>00073 <span class="keywordflow">continue</span>;
<a name="l00074"></a>00074 }
<a name="l00075"></a>00075 }
<a name="l00076"></a>00076
<a name="l00077"></a>00077 <span class="comment">// split the input line into tokens</span>
<a name="l00078"></a>00078 argc = 0;
<a name="l00079"></a>00079 _argv[argc++].<a class="code" href="struct_menu_1_1arg.html#ad5a8d057d82e7300c34b648c796c8df4" title="string form of the argument">str</a> = strtok_r(_inbuf, <span class="stringliteral">&quot; &quot;</span>, &amp;s);
<a name="l00080"></a>00080 <span class="comment">// XXX should an empty line by itself back out of the current menu?</span>
<a name="l00081"></a>00081 <span class="keywordflow">while</span> (argc &lt;= <a class="code" href="menu_8h.html#a78a3dfeac59b72f430d58683a06644fe" title="maximum number of arguments">MENU_ARGS_MAX</a>) {
<a name="l00082"></a>00082 _argv[argc].<a class="code" href="struct_menu_1_1arg.html#ad5a8d057d82e7300c34b648c796c8df4" title="string form of the argument">str</a> = strtok_r(NULL, <span class="stringliteral">&quot; &quot;</span>, &amp;s);
<a name="l00083"></a>00083 <span class="keywordflow">if</span> (<span class="charliteral">&#39;\0&#39;</span> == _argv[argc].str)
<a name="l00084"></a>00084 <span class="keywordflow">break</span>;
<a name="l00085"></a>00085 _argv[argc].<a class="code" href="struct_menu_1_1arg.html#ad6913164599db6376da758a0f1a9ee1c" title="integer form of the argument (if a number)">i</a> = atol(_argv[argc].str);
<a name="l00086"></a>00086 _argv[argc].<a class="code" href="struct_menu_1_1arg.html#aee33499a1cd6a4edfe5c5b8e6cf42766" title="floating point form of the argument (if a number)">f</a> = atof(_argv[argc].str); <span class="comment">// calls strtod, &gt; 700B !</span>
<a name="l00087"></a>00087 argc++;
<a name="l00088"></a>00088 }
<a name="l00089"></a>00089
<a name="l00090"></a>00090 <span class="comment">// populate arguments that have not been specified with &quot;&quot; and 0</span>
<a name="l00091"></a>00091 <span class="comment">// this is safer than NULL in the case where commands may look</span>
<a name="l00092"></a>00092 <span class="comment">// without testing argc</span>
<a name="l00093"></a>00093 i = argc;
<a name="l00094"></a>00094 <span class="keywordflow">while</span> (i &lt;= <a class="code" href="menu_8h.html#a78a3dfeac59b72f430d58683a06644fe" title="maximum number of arguments">MENU_ARGS_MAX</a>) {
<a name="l00095"></a>00095 _argv[i].<a class="code" href="struct_menu_1_1arg.html#ad5a8d057d82e7300c34b648c796c8df4" title="string form of the argument">str</a> = <span class="stringliteral">&quot;&quot;</span>;
<a name="l00096"></a>00096 _argv[i].<a class="code" href="struct_menu_1_1arg.html#ad6913164599db6376da758a0f1a9ee1c" title="integer form of the argument (if a number)">i</a> = 0;
<a name="l00097"></a>00097 _argv[i].<a class="code" href="struct_menu_1_1arg.html#aee33499a1cd6a4edfe5c5b8e6cf42766" title="floating point form of the argument (if a number)">f</a> = 0;
<a name="l00098"></a>00098 i++;
<a name="l00099"></a>00099 }
<a name="l00100"></a>00100
<a name="l00101"></a>00101 <span class="comment">// look for a command matching the first word (note that it may be empty)</span>
<a name="l00102"></a>00102 <span class="keywordflow">for</span> (i = 0; i &lt; _entries; i++) {
<a name="l00103"></a>00103 <span class="keywordflow">if</span> (!strcasecmp_P(_argv[0].str, _commands[i].<a class="code" href="struct_menu_1_1command.html">command</a>)) {
<a name="l00104"></a>00104 ret = _call(i, argc);
<a name="l00105"></a>00105 <span class="keywordflow">if</span> (-2 == ret)
<a name="l00106"></a>00106 <span class="keywordflow">return</span>;
<a name="l00107"></a>00107 <span class="keywordflow">break</span>;
<a name="l00108"></a>00108 }
<a name="l00109"></a>00109 }
<a name="l00110"></a>00110
<a name="l00111"></a>00111 <span class="comment">// implicit commands</span>
<a name="l00112"></a>00112 <span class="keywordflow">if</span> (i == _entries) {
<a name="l00113"></a>00113 <span class="keywordflow">if</span> (!strcmp(_argv[0].str, <span class="stringliteral">&quot;?&quot;</span>) || (!strcasecmp_P(_argv[0].str, <a class="code" href="_a_p___common_8h.html#a05ca900ebf7cd121be73c654d9ccb3eb">PSTR</a>(<span class="stringliteral">&quot;help&quot;</span>)))) {
<a name="l00114"></a>00114 _help();
<a name="l00115"></a>00115 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!strcasecmp_P(_argv[0].str, <a class="code" href="_a_p___common_8h.html#a05ca900ebf7cd121be73c654d9ccb3eb">PSTR</a>(<span class="stringliteral">&quot;exit&quot;</span>))) {
<a name="l00116"></a>00116 <span class="keywordflow">return</span>;
<a name="l00117"></a>00117 }
<a name="l00118"></a>00118 }
<a name="l00119"></a>00119 }
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121
<a name="l00122"></a>00122 <span class="comment">// display the list of commands in response to the &#39;help&#39; command</span>
<a name="l00123"></a>00123 <span class="keywordtype">void</span>
<a name="l00124"></a>00124 Menu::_help(<span class="keywordtype">void</span>)
<a name="l00125"></a>00125 {
<a name="l00126"></a>00126 <span class="keywordtype">int</span> i;
<a name="l00127"></a>00127
<a name="l00128"></a>00128 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.println(<span class="stringliteral">&quot;Commands:&quot;</span>);
<a name="l00129"></a>00129 <span class="keywordflow">for</span> (i = 0; i &lt; _entries; i++)
<a name="l00130"></a>00130 <a class="code" href="_fast_serial_8h.html#a8e6f49b84135f0158fe250cb7338ddad">Serial</a>.<a class="code" href="class_better_stream.html#aa735777635173dca53f4d9a4f6a1e0b8">printf</a>(<span class="stringliteral">&quot; %S\n&quot;</span>, _commands[i].command);
<a name="l00131"></a>00131 }
<a name="l00132"></a>00132
<a name="l00133"></a>00133 <span class="comment">// run the n&#39;th command in the menu</span>
<a name="l00134"></a>00134 int8_t
<a name="l00135"></a>00135 Menu::_call(uint8_t n, uint8_t argc)
<a name="l00136"></a>00136 {
<a name="l00137"></a>00137 <a class="code" href="class_menu.html#aaa251c33ec1cf26ad33095565f0a1125">func</a> fn;
<a name="l00138"></a>00138
<a name="l00139"></a>00139 fn = (func)pgm_read_word(&amp;_commands[n].<a class="code" href="class_menu.html#aaa251c33ec1cf26ad33095565f0a1125">func</a>);
<a name="l00140"></a>00140 <span class="keywordflow">return</span>(fn(argc, &amp;_argv[0]));
<a name="l00141"></a>00141 }
</pre></div></div>
</div>
<hr class="footer"/><address class="footer"><small>
Generated for ArduPilot Libraries by&nbsp;<a href="http://www.doxygen.org/index.html"><img class="footer" src="doxygen.png" alt="doxygen"/></a></small></address>
</body>
</html>