You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MicroMDAEPiano/third-party/LiquidMenu/doc/Doxygen/html/_d_buttons_menu_8ino-exampl...

80 lines
20 KiB

<!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.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>LiquidMenu: D_buttons_menu.ino</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/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></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 id="projectlogo"><img alt="Logo" src="logo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">LiquidMenu
&#160;<span id="projectnumber">1.4.1</span>
</div>
<div id="projectbrief">Menu creation Arduino library for LCDs, wraps LiquidCrystal.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</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 class="header">
<div class="headertitle">
<div class="title">D_buttons_menu.ino</div> </div>
</div><!--header-->
<div class="contents">
<p>This example demonstrates how to use buttons, callback functions and changing text variables.</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * LiquidMenu library - buttons_menu.ino</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * This example demonstrates how to use buttons, callback</span></div><div class="line"><span class="comment"> * functions and changing text variables.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * It creates three screens. The first one displays static</span></div><div class="line"><span class="comment"> * information. The second displays the analog reading and the</span></div><div class="line"><span class="comment"> * state of the LED on pin 13. And the third one displays the</span></div><div class="line"><span class="comment"> * PWM value applied on pin 3. The analog value is read periodically</span></div><div class="line"><span class="comment"> * and assigned to the attached variable &quot;const short analogValue&quot;.</span></div><div class="line"><span class="comment"> * The LED on pin 13 is changing its state periodically and its</span></div><div class="line"><span class="comment"> * attached variable is changed also. The PWM output pin 3 is</span></div><div class="line"><span class="comment"> * controlled via the callback functions attached to its line:</span></div><div class="line"><span class="comment"> * &quot;void pwm_up()&quot; and &quot;void pwm_down()&quot;. To call the functions</span></div><div class="line"><span class="comment"> * navigate to the third screen using the &quot;left&quot; or &quot;right&quot; buttons,</span></div><div class="line"><span class="comment"> * focus the line using the &quot;enter&quot; button and then click the &quot;up&quot; or</span></div><div class="line"><span class="comment"> * &quot;down&quot; button.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * The circuit:</span></div><div class="line"><span class="comment"> * https://github.com/VasilKalchev/LiquidMenu/blob/master/examples/D_buttons_menu/D_buttons_menu.png</span></div><div class="line"><span class="comment"> * - LCD RS pin to Arduino pin 12</span></div><div class="line"><span class="comment"> * - LCD E pin to Arduino pin 11</span></div><div class="line"><span class="comment"> * - LCD D4 pin to Arduino pin 5</span></div><div class="line"><span class="comment"> * - LCD D5 pin to Arduino pin 4</span></div><div class="line"><span class="comment"> * - LCD D6 pin to Arduino pin 3</span></div><div class="line"><span class="comment"> * - LCD D7 pin to Arduino pin 2</span></div><div class="line"><span class="comment"> * - LCD R/W pin to ground</span></div><div class="line"><span class="comment"> * - LCD VSS pin to ground</span></div><div class="line"><span class="comment"> * - LCD VDD pin to 5V</span></div><div class="line"><span class="comment"> * - 10k ohm potentiometer: ends to 5V and ground, wiper to LCD V0</span></div><div class="line"><span class="comment"> * - 150 ohm resistor from 5V to LCD Anode</span></div><div class="line"><span class="comment"> * - LCD Cathode to ground</span></div><div class="line"><span class="comment"> * - ----</span></div><div class="line"><span class="comment"> * - Button (left) to Arduino pin A0 and ground</span></div><div class="line"><span class="comment"> * - Button (right) to Arduino pin 7 and ground</span></div><div class="line"><span class="comment"> * - Button (up) to Arduino pin 8 and ground</span></div><div class="line"><span class="comment"> * - Button (down) to Arduino pin 9 and ground</span></div><div class="line"><span class="comment"> * - Button (enter) to Arduino pin 10 and ground</span></div><div class="line"><span class="comment"> * - A PWM controlled device (LED...) to Arduino pin 6</span></div><div class="line"><span class="comment"> * - An LED to Arduino pin 13 (optional)</span></div><div class="line"><span class="comment"> * - some analog input to Arduino pin A5 (unconnected also works)</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * Created July 24, 2016</span></div><div class="line"><span class="comment"> * by Vasil Kalchev</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * https://github.com/VasilKalchev/LiquidMenu</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;LiquidCrystal.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="_liquid_menu_8h.html">LiquidMenu.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;Button.h&quot;</span></div><div class="line"></div><div class="line"><span class="comment">// Pin mapping for the display</span></div><div class="line"><span class="keyword">const</span> byte LCD_RS = 12;</div><div class="line"><span class="keyword">const</span> byte LCD_E = 11;</div><div class="line"><span class="keyword">const</span> byte LCD_D4 = 5;</div><div class="line"><span class="keyword">const</span> byte LCD_D5 = 4;</div><div class="line"><span class="keyword">const</span> byte LCD_D6 = 3;</div><div class="line"><span class="keyword">const</span> byte LCD_D7 = 2;</div><div class="line"><span class="comment">//LCD R/W pin to ground</span></div><div class="line"><span class="comment">//10K potentiometer to VO</span></div><div class="line">LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * The Button class is not a part of the LiquidMenu library. The first</span></div><div class="line"><span class="comment"> * parameter is the button&#39;s pin, the second enables or disables the</span></div><div class="line"><span class="comment"> * internal pullup resistor (not required) and the third is the debounce</span></div><div class="line"><span class="comment"> * time (not required).</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="comment">// Button objects instantiation</span></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">bool</span> pullup = <span class="keyword">true</span>;</div><div class="line">Button left(A0, pullup);</div><div class="line">Button right(7, pullup);</div><div class="line">Button up(8, pullup);</div><div class="line">Button down(9, pullup);</div><div class="line">Button enter(10, pullup);</div><div class="line"></div><div class="line"><span class="comment">// Variables used to demonstrate PWM control with callback functions.</span></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Variable &#39;pwmLevel&#39; is later passed to &#39;pwm_line&#39; object to be</span></div><div class="line"><span class="comment"> * displayed and a function is attached to the same LiquidLine object.</span></div><div class="line"><span class="comment"> * Functions are attached to different LiquidLine objects with a number</span></div><div class="line"><span class="comment"> * for identification e.g. pwm_line.attach_function(2, func); then when</span></div><div class="line"><span class="comment"> * menu.call_function(2) is called on some event e.g. button press, the</span></div><div class="line"><span class="comment"> * function identified with 2 for the focused line is called. Similar</span></div><div class="line"><span class="comment"> * functions (e.g. for incrementing) can be attached to the different</span></div><div class="line"><span class="comment"> * LiquidLine objects with the same identification number and then called</span></div><div class="line"><span class="comment"> * on some event (e.g. &#39;UP&#39; button) with menu.call_function(2), that will</span></div><div class="line"><span class="comment"> * call only the function for the focused line that is identified with</span></div><div class="line"><span class="comment"> * the number 2.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="keyword">const</span> byte pwmPin = 6;</div><div class="line">byte pwmLevel = 0;</div><div class="line"></div><div class="line"><span class="comment">// Variables for controlling a pin and displaying the state with text.</span></div><div class="line"><span class="comment">// char* is used for adding changing text to the LiquidLine object.</span></div><div class="line"><span class="keyword">const</span> byte ledPin = LED_BUILTIN;</div><div class="line"><span class="keywordtype">bool</span> ledState = LOW;</div><div class="line"><span class="keywordtype">char</span>* ledState_text;</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Variable &#39;analogValue&#39; is later configured to be printed on the display.</span></div><div class="line"><span class="comment"> * This time a static variable is used for holding the previous analog value.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="keyword">const</span> byte analogPin = A5;</div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> analogValue = 0;</div><div class="line"></div><div class="line"></div><div class="line"><a name="_a0"></a><a class="code" href="class_liquid_line.html">LiquidLine</a> welcome_line1(1, 0, <span class="stringliteral">&quot;LiquidMenu &quot;</span>, <a name="a1"></a><a class="code" href="_liquid_menu_8h.html#a7189c07575adbb1070c89dcd0ab5e272">LIQUIDMENU_VERSION</a>);</div><div class="line"><a class="code" href="class_liquid_line.html">LiquidLine</a> welcome_line2(0, 1, <span class="stringliteral">&quot;Buttons example&quot;</span>);</div><div class="line"><a name="_a2"></a><a class="code" href="class_liquid_screen.html">LiquidScreen</a> welcome_screen(welcome_line1, welcome_line2);</div><div class="line"></div><div class="line"><a class="code" href="class_liquid_line.html">LiquidLine</a> analog_line(0, 0, <span class="stringliteral">&quot;Analog: &quot;</span>, analogValue);</div><div class="line"><a class="code" href="class_liquid_line.html">LiquidLine</a> ledState_line(0, 1, <span class="stringliteral">&quot;LED is &quot;</span>, ledState_text);</div><div class="line"><a class="code" href="class_liquid_screen.html">LiquidScreen</a> screen2(analog_line, ledState_line);</div><div class="line"></div><div class="line"><a class="code" href="class_liquid_line.html">LiquidLine</a> pwm_line(0, 0, <span class="stringliteral">&quot;PWM level: &quot;</span>, pwmLevel);</div><div class="line"><a class="code" href="class_liquid_screen.html">LiquidScreen</a> pwm_screen(pwm_line);</div><div class="line"></div><div class="line"><a name="_a3"></a><a class="code" href="class_liquid_menu.html">LiquidMenu</a> menu(lcd);</div><div class="line"></div><div class="line"><span class="comment">// Function to be attached to the pwm_line object.</span></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * This function is used for incrementing the PWM level on the</span></div><div class="line"><span class="comment"> * &#39;pwmPin&#39;. It increments the value of &#39;pwmLevel&#39; and then</span></div><div class="line"><span class="comment"> * writes it to the pin.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="keywordtype">void</span> pwm_up() {</div><div class="line"> <span class="keywordflow">if</span> (pwmLevel &lt; 225) {</div><div class="line"> pwmLevel += 25;</div><div class="line"> } <span class="keywordflow">else</span> {</div><div class="line"> pwmLevel = 250;</div><div class="line"> }</div><div class="line"> analogWrite(pwmPin, pwmLevel);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Function to be attached to the pwm_line object.</span></div><div class="line"><span class="keywordtype">void</span> pwm_down() {</div><div class="line"> <span class="keywordflow">if</span> (pwmLevel &gt; 25) {</div><div class="line"> pwmLevel -= 25;</div><div class="line"> } <span class="keywordflow">else</span> {</div><div class="line"> pwmLevel = 0;</div><div class="line"> }</div><div class="line"> analogWrite(pwmPin, pwmLevel);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> setup() {</div><div class="line"> Serial.begin(250000);</div><div class="line"></div><div class="line"> pinMode(analogPin, INPUT);</div><div class="line"> pinMode(ledPin, OUTPUT);</div><div class="line"> pinMode(pwmPin, OUTPUT);</div><div class="line"></div><div class="line"> lcd.begin(16, 2);</div><div class="line"></div><div class="line"> <span class="comment">// Function to attach functions to LiquidLine objects.</span></div><div class="line"> pwm_line.attach_function(1, pwm_up);</div><div class="line"> pwm_line.attach_function(2, pwm_down);</div><div class="line"></div><div class="line"> menu.add_screen(welcome_screen);</div><div class="line"> menu.add_screen(screen2);</div><div class="line"> menu.add_screen(pwm_screen);</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"> <span class="comment">// Check all the buttons</span></div><div class="line"> <span class="keywordflow">if</span> (right.check() == LOW) {</div><div class="line"> Serial.println(F(<span class="stringliteral">&quot;RIGHT button pressed&quot;</span>));</div><div class="line"> menu.next_screen();</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (left.check() == LOW) {</div><div class="line"> Serial.println(F(<span class="stringliteral">&quot;LEFT button pressed&quot;</span>));</div><div class="line"> menu.previous_screen();</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (up.check() == LOW) {</div><div class="line"> Serial.println(F(<span class="stringliteral">&quot;UP button pressed&quot;</span>));</div><div class="line"> <span class="comment">// Calls the function identified with one</span></div><div class="line"> <span class="comment">// for the focused line.</span></div><div class="line"> menu.call_function(1);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (down.check() == LOW) {</div><div class="line"> Serial.println(F(<span class="stringliteral">&quot;DOWN button pressed&quot;</span>));</div><div class="line"> menu.call_function(2);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">if</span> (enter.check() == LOW) {</div><div class="line"> Serial.println(F(<span class="stringliteral">&quot;ENTER button pressed&quot;</span>));</div><div class="line"> <span class="comment">// Switches focus to the next line.</span></div><div class="line"> menu.switch_focus();</div><div class="line"> }</div><div class="line"></div><div class="line"></div><div class="line"> <span class="comment">// Periodically switches the state of the LED.</span></div><div class="line"> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> lastMillis = 0;</div><div class="line"> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> period = 2000;</div><div class="line"> <span class="keywordflow">if</span> (millis() - lastMillis &gt; period) {</div><div class="line"> lastMillis = millis();</div><div class="line"></div><div class="line"> Serial.print(<span class="stringliteral">&quot;LED turned &quot;</span>);</div><div class="line"> <span class="keywordflow">if</span> (ledState == LOW) {</div><div class="line"> ledState = HIGH;</div><div class="line"> <span class="comment">// Changes the text that is printed on the display.</span></div><div class="line"> ledState_text = (<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;ON&quot;</span>;</div><div class="line"> Serial.println(ledState_text);</div><div class="line"> menu.update();</div><div class="line"> } <span class="keywordflow">else</span> {</div><div class="line"> ledState = LOW;</div><div class="line"> ledState_text = (<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;OFF&quot;</span>;</div><div class="line"> Serial.println(ledState_text);</div><div class="line"> menu.update();</div><div class="line"> }</div><div class="line"> digitalWrite(ledPin, ledState);</div><div class="line"></div><div class="line"> <span class="comment">// The &#39;analogValue&#39; is updated every second.</span></div><div class="line"> analogValue = analogRead(analogPin);</div><div class="line"> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> lastAnalogValue = 0;</div><div class="line"> <span class="keywordflow">if</span> (analogValue != lastAnalogValue) {</div><div class="line"> lastAnalogValue = analogValue;</div><div class="line"> menu.update();</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.14
</small></address>
</body>
</html>