1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/LCD_8cpp_source.html
Rhys Weatherley 35ab5bd85e Doc fixes
2012-05-25 16:23:59 +10:00

246 lines
20 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>ArduinoLibs: LCD.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.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div id="top">
<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>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><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 class="current"><a href="files.html"><span>Files</span></a></li>
<li id="searchli">
<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>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&#160;List</span></a></li>
</ul>
</div>
<div class="header">
<div class="headertitle">
<div class="title">LCD.cpp</div> </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2012 Southern Storm Software, Pty Ltd.</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</span>
<a name="l00005"></a>00005 <span class="comment"> * copy of this software and associated documentation files (the &quot;Software&quot;),</span>
<a name="l00006"></a>00006 <span class="comment"> * to deal in the Software without restriction, including without limitation</span>
<a name="l00007"></a>00007 <span class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</span>
<a name="l00008"></a>00008 <span class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</span>
<a name="l00009"></a>00009 <span class="comment"> * Software is furnished to do so, subject to the following conditions:</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> * The above copyright notice and this permission notice shall be included</span>
<a name="l00012"></a>00012 <span class="comment"> * in all copies or substantial portions of the Software.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS</span>
<a name="l00015"></a>00015 <span class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<a name="l00016"></a>00016 <span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<a name="l00017"></a>00017 <span class="comment"> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<a name="l00018"></a>00018 <span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span>
<a name="l00019"></a>00019 <span class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</span>
<a name="l00020"></a>00020 <span class="comment"> * DEALINGS IN THE SOFTWARE.</span>
<a name="l00021"></a>00021 <span class="comment"> */</span>
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <span class="preprocessor">#include &quot;LCD.h&quot;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;avr/pgmspace.h&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#if defined(ARDUINO) &amp;&amp; ARDUINO &gt;= 100</span>
<a name="l00026"></a>00026 <span class="preprocessor"></span><span class="preprocessor">#include &lt;Arduino.h&gt;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#else</span>
<a name="l00028"></a>00028 <span class="preprocessor"></span><span class="preprocessor">#include &lt;WProgram.h&gt;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#endif</span>
<a name="l00030"></a>00030 <span class="preprocessor"></span>
<a name="l00031"></a>00031 <span class="preprocessor">#define LCD_BACK_LIGHT 3 // LCD backlight is on D3</span>
<a name="l00032"></a>00032 <span class="preprocessor"></span><span class="preprocessor">#define LCD_BUTTON_PIN A0 // Button state is on A0</span>
<a name="l00033"></a>00033 <span class="preprocessor"></span>
<a name="l00034"></a>00034 <span class="preprocessor">#define DEBOUNCE_DELAY 10 // Delay in ms to debounce buttons</span>
<a name="l00035"></a>00035 <span class="preprocessor"></span>
<a name="l00115"></a>00115 <span class="keywordtype">void</span> LCD::init()
<a name="l00116"></a>00116 {
<a name="l00117"></a>00117 <span class="comment">// The Freetronics display is 16x2.</span>
<a name="l00118"></a>00118 begin(16, 2);
<a name="l00119"></a>00119
<a name="l00120"></a>00120 <span class="comment">// Set the LCD back light to be initially on.</span>
<a name="l00121"></a>00121 pinMode(LCD_BACK_LIGHT, OUTPUT);
<a name="l00122"></a>00122 digitalWrite(LCD_BACK_LIGHT, HIGH);
<a name="l00123"></a>00123
<a name="l00124"></a>00124 <span class="comment">// Initialise button input.</span>
<a name="l00125"></a>00125 pinMode(LCD_BUTTON_PIN, INPUT);
<a name="l00126"></a>00126 digitalWrite(LCD_BUTTON_PIN, LOW);
<a name="l00127"></a>00127 prevButton = LCD_BUTTON_NONE;
<a name="l00128"></a>00128 debounceButton = LCD_BUTTON_NONE;
<a name="l00129"></a>00129 lastDebounce = 0;
<a name="l00130"></a>00130 eatRelease = <span class="keyword">false</span>;
<a name="l00131"></a>00131
<a name="l00132"></a>00132 <span class="comment">// Initialize screen saver.</span>
<a name="l00133"></a>00133 timeout = 0;
<a name="l00134"></a>00134 lastRestore = millis();
<a name="l00135"></a>00135 screenSaved = <span class="keyword">false</span>;
<a name="l00136"></a>00136 mode = <a class="code" href="classLCD.html#a264bf94308c95d8598426e13dc8cdb28a3f1e62d5fcd314d6ff067d3e74c4bf5f" title="Turn off both the display and the backlight when the screen saver is activated.">DisplayOff</a>;
<a name="l00137"></a>00137 }
<a name="l00138"></a>00138
<a name="l00148"></a><a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89">00148</a> <span class="keywordtype">void</span> <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">LCD::display</a>()
<a name="l00149"></a>00149 {
<a name="l00150"></a>00150 <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">LiquidCrystal::display</a>();
<a name="l00151"></a>00151 digitalWrite(LCD_BACK_LIGHT, HIGH);
<a name="l00152"></a>00152 screenSaved = <span class="keyword">false</span>;
<a name="l00153"></a>00153 lastRestore = millis();
<a name="l00154"></a>00154 }
<a name="l00155"></a>00155
<a name="l00163"></a><a class="code" href="classLCD.html#af3974da6d988ba2d21c25135ada12108">00163</a> <span class="keywordtype">void</span> <a class="code" href="classLCD.html#af3974da6d988ba2d21c25135ada12108" title="Turns off the display of text on the LCD and the back light.">LCD::noDisplay</a>()
<a name="l00164"></a>00164 {
<a name="l00165"></a>00165 <span class="keywordflow">if</span> (mode == <a class="code" href="classLCD.html#a264bf94308c95d8598426e13dc8cdb28a3f1e62d5fcd314d6ff067d3e74c4bf5f" title="Turn off both the display and the backlight when the screen saver is activated.">DisplayOff</a>)
<a name="l00166"></a>00166 <a class="code" href="classLCD.html#af3974da6d988ba2d21c25135ada12108" title="Turns off the display of text on the LCD and the back light.">LiquidCrystal::noDisplay</a>();
<a name="l00167"></a>00167 digitalWrite(LCD_BACK_LIGHT, LOW);
<a name="l00168"></a>00168 screenSaved = <span class="keyword">true</span>;
<a name="l00169"></a>00169 }
<a name="l00170"></a>00170
<a name="l00206"></a><a class="code" href="classLCD.html#a56d1f68532c779c65fbbd071fb444801">00206</a> <span class="keywordtype">void</span> <a class="code" href="classLCD.html#a56d1f68532c779c65fbbd071fb444801" title="Sets the current screen saver mode.">LCD::setScreenSaverMode</a>(<a class="code" href="classLCD.html#a264bf94308c95d8598426e13dc8cdb28" title="Screen saver mode that controls the display and back light.">ScreenSaverMode</a> mode)
<a name="l00207"></a>00207 {
<a name="l00208"></a>00208 <span class="keywordflow">if</span> (this-&gt;mode != mode) {
<a name="l00209"></a>00209 this-&gt;mode = mode;
<a name="l00210"></a>00210 <span class="keywordflow">if</span> (screenSaved)
<a name="l00211"></a>00211 <a class="code" href="classLCD.html#af3974da6d988ba2d21c25135ada12108" title="Turns off the display of text on the LCD and the back light.">noDisplay</a>();
<a name="l00212"></a>00212 <span class="keywordflow">else</span>
<a name="l00213"></a>00213 <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">display</a>();
<a name="l00214"></a>00214 }
<a name="l00215"></a>00215 }
<a name="l00216"></a>00216
<a name="l00232"></a><a class="code" href="classLCD.html#af9a2326d034fa159d384ec16223c924f">00232</a> <span class="keywordtype">void</span> <a class="code" href="classLCD.html#af9a2326d034fa159d384ec16223c924f" title="Enables the screen saver and causes it to activate after timeoutSecs of inactivity on the buttons...">LCD::enableScreenSaver</a>(<span class="keywordtype">int</span> timeoutSecs)
<a name="l00233"></a>00233 {
<a name="l00234"></a>00234 <span class="keywordflow">if</span> (timeoutSecs &lt; 0)
<a name="l00235"></a>00235 timeout = 0;
<a name="l00236"></a>00236 <span class="keywordflow">else</span>
<a name="l00237"></a>00237 timeout = ((<span class="keywordtype">unsigned</span> long)timeoutSecs) * 1000;
<a name="l00238"></a>00238 <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">display</a>();
<a name="l00239"></a>00239 }
<a name="l00240"></a>00240
<a name="l00246"></a><a class="code" href="classLCD.html#a85c3a4694b105731404df36e35e5b26e">00246</a> <span class="keywordtype">void</span> <a class="code" href="classLCD.html#a85c3a4694b105731404df36e35e5b26e" title="Disables the screen saver.">LCD::disableScreenSaver</a>()
<a name="l00247"></a>00247 {
<a name="l00248"></a>00248 timeout = 0;
<a name="l00249"></a>00249 <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">display</a>();
<a name="l00250"></a>00250 }
<a name="l00251"></a>00251
<a name="l00259"></a>00259 <span class="comment">// Button mapping table generated by genlookup.c</span>
<a name="l00260"></a>00260 <span class="keyword">static</span> prog_uint8_t <span class="keyword">const</span> buttonMappings[] PROGMEM = {
<a name="l00261"></a>00261 2, 0, 0, 0, 3, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 1,
<a name="l00262"></a>00262 1, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0
<a name="l00263"></a>00263 };
<a name="l00264"></a>00264 <span class="preprocessor">#define mapButton(value) (pgm_read_byte(&amp;(buttonMappings[(value) &gt;&gt; 5])))</span>
<a name="l00265"></a>00265 <span class="preprocessor"></span>
<a name="l00291"></a><a class="code" href="classLCD.html#ac1e80e2603bd1cf0276c36092c416292">00291</a> <span class="keywordtype">int</span> <a class="code" href="classLCD.html#ac1e80e2603bd1cf0276c36092c416292" title="Gets the next button press, release, or idle event.">LCD::getButton</a>()
<a name="l00292"></a>00292 {
<a name="l00293"></a>00293 <span class="comment">// Read the currently pressed button.</span>
<a name="l00294"></a>00294 <span class="keywordtype">int</span> button = mapButton(analogRead(LCD_BUTTON_PIN));
<a name="l00295"></a>00295
<a name="l00296"></a>00296 <span class="comment">// Debounce the button state.</span>
<a name="l00297"></a>00297 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> currentTime = millis();
<a name="l00298"></a>00298 <span class="keywordflow">if</span> (button != debounceButton)
<a name="l00299"></a>00299 lastDebounce = currentTime;
<a name="l00300"></a>00300 debounceButton = button;
<a name="l00301"></a>00301 <span class="keywordflow">if</span> ((currentTime - lastDebounce) &lt; DEBOUNCE_DELAY)
<a name="l00302"></a>00302 button = prevButton;
<a name="l00303"></a>00303
<a name="l00304"></a>00304 <span class="comment">// Process the button event if the state has changed.</span>
<a name="l00305"></a>00305 <span class="keywordflow">if</span> (prevButton == LCD_BUTTON_NONE &amp;&amp; button != LCD_BUTTON_NONE) {
<a name="l00306"></a>00306 prevButton = button;
<a name="l00307"></a>00307 <span class="keywordflow">if</span> (screenSaved) {
<a name="l00308"></a>00308 <span class="comment">// Button pressed when screen saver active.</span>
<a name="l00309"></a>00309 <span class="keywordflow">if</span> (mode == <a class="code" href="classLCD.html#a264bf94308c95d8598426e13dc8cdb28a781f3c5e42506bf4f86ba06d69b23d35" title="Same as BacklightOff but the screen saver is only deactivated when Select is pressed; other buttons h...">BacklightOnSelect</a>) {
<a name="l00310"></a>00310 <span class="comment">// Turn on the back light only if Select was pressed.</span>
<a name="l00311"></a>00311 <span class="keywordflow">if</span> (button == LCD_BUTTON_SELECT) {
<a name="l00312"></a>00312 digitalWrite(LCD_BACK_LIGHT, HIGH);
<a name="l00313"></a>00313 screenSaved = <span class="keyword">false</span>;
<a name="l00314"></a>00314 }
<a name="l00315"></a>00315 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (mode == <a class="code" href="classLCD.html#a264bf94308c95d8598426e13dc8cdb28a3f1e62d5fcd314d6ff067d3e74c4bf5f" title="Turn off both the display and the backlight when the screen saver is activated.">DisplayOff</a>) {
<a name="l00316"></a>00316 <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">display</a>();
<a name="l00317"></a>00317 eatRelease = <span class="keyword">true</span>;
<a name="l00318"></a>00318 <span class="keywordflow">return</span> LCD_BUTTON_NONE;
<a name="l00319"></a>00319 } <span class="keywordflow">else</span> {
<a name="l00320"></a>00320 <a class="code" href="classLCD.html#a5b07cf05e8e5e7c53654f5ca0cf58b89" title="Turns on the display of text on the LCD and the back light.">display</a>();
<a name="l00321"></a>00321 }
<a name="l00322"></a>00322 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (mode == <a class="code" href="classLCD.html#a264bf94308c95d8598426e13dc8cdb28a781f3c5e42506bf4f86ba06d69b23d35" title="Same as BacklightOff but the screen saver is only deactivated when Select is pressed; other buttons h...">BacklightOnSelect</a> &amp;&amp; button != LCD_BUTTON_SELECT) {
<a name="l00323"></a>00323 eatRelease = <span class="keyword">false</span>;
<a name="l00324"></a>00324 <span class="keywordflow">return</span> button;
<a name="l00325"></a>00325 }
<a name="l00326"></a>00326 eatRelease = <span class="keyword">false</span>;
<a name="l00327"></a>00327 lastRestore = currentTime;
<a name="l00328"></a>00328 <span class="keywordflow">return</span> button;
<a name="l00329"></a>00329 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (prevButton != LCD_BUTTON_NONE &amp;&amp; button == LCD_BUTTON_NONE) {
<a name="l00330"></a>00330 button = -prevButton;
<a name="l00331"></a>00331 prevButton = LCD_BUTTON_NONE;
<a name="l00332"></a>00332 lastRestore = currentTime;
<a name="l00333"></a>00333 <span class="keywordflow">if</span> (eatRelease) {
<a name="l00334"></a>00334 eatRelease = <span class="keyword">false</span>;
<a name="l00335"></a>00335 <span class="keywordflow">return</span> LCD_BUTTON_NONE;
<a name="l00336"></a>00336 }
<a name="l00337"></a>00337 <span class="keywordflow">return</span> button;
<a name="l00338"></a>00338 } <span class="keywordflow">else</span> {
<a name="l00339"></a>00339 <span class="keywordflow">if</span> (!screenSaved &amp;&amp; prevButton == LCD_BUTTON_NONE &amp;&amp;
<a name="l00340"></a>00340 timeout != 0 &amp;&amp; (currentTime - lastRestore) &gt;= timeout)
<a name="l00341"></a>00341 <a class="code" href="classLCD.html#af3974da6d988ba2d21c25135ada12108" title="Turns off the display of text on the LCD and the back light.">noDisplay</a>(); <span class="comment">// Activate screen saver.</span>
<a name="l00342"></a>00342 <span class="keywordflow">return</span> LCD_BUTTON_NONE;
<a name="l00343"></a>00343 }
<a name="l00344"></a>00344 }
</pre></div></div>
</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>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</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>
<hr class="footer"/><address class="footer"><small>Generated on Fri May 25 2012 16:23:36 for ArduinoLibs by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>