1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/Charlieplex_8cpp_source.html
Rhys Weatherley 17f4fb4873 Update docs
2012-06-18 11:03:07 +10:00

221 lines
16 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: Charlieplex.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">Charlieplex.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;Charlieplex.h&quot;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#if defined(ARDUINO) &amp;&amp; ARDUINO &gt;= 100</span>
<a name="l00025"></a>00025 <span class="preprocessor"></span><span class="preprocessor">#include &lt;Arduino.h&gt;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#else</span>
<a name="l00027"></a>00027 <span class="preprocessor"></span><span class="preprocessor">#include &lt;WProgram.h&gt;</span>
<a name="l00028"></a>00028 <span class="preprocessor">#endif</span>
<a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00031"></a>00031
<a name="l00121"></a><a class="code" href="classCharlieplex.html#abfb0d0456bcbadbf60c21f615adacdbd">00121</a> <a class="code" href="classCharlieplex.html#abfb0d0456bcbadbf60c21f615adacdbd" title="Constructs a new charliexplexing array where the output pins are specified by the numPins entries in ...">Charlieplex::Charlieplex</a>(<span class="keyword">const</span> uint8_t *pins, uint8_t numPins)
<a name="l00122"></a>00122 : _count(((int)numPins) * (numPins - 1))
<a name="l00123"></a>00123 , _lastTime(micros())
<a name="l00124"></a>00124 , _currentIndex(-1)
<a name="l00125"></a>00125 , _pwmPhase(0xC0)
<a name="l00126"></a>00126 {
<a name="l00127"></a>00127 <span class="comment">// Determine the best hold time for 50 Hz refresh when all LED&#39;s</span>
<a name="l00128"></a>00128 <span class="comment">// are lit. Divide it again by 4 (to get 200 Hz) to manage the</span>
<a name="l00129"></a>00129 <span class="comment">// simulated PWM in refresh().</span>
<a name="l00130"></a>00130 _holdTime = 20000 / _count / 4;
<a name="l00131"></a>00131
<a name="l00132"></a>00132 <span class="comment">// Allocate the pin arrays and populate them. Doing this now makes</span>
<a name="l00133"></a>00133 <span class="comment">// refresh() more efficient later, at the expense of some memory.</span>
<a name="l00134"></a>00134 _pins1 = (uint8_t *)malloc(_count);
<a name="l00135"></a>00135 _pins2 = (uint8_t *)malloc(_count);
<a name="l00136"></a>00136 <span class="keywordtype">int</span> n = 0;
<a name="l00137"></a>00137 <span class="keywordflow">for</span> (uint8_t pass = 1; pass &lt; numPins; ++pass) {
<a name="l00138"></a>00138 <span class="keywordflow">for</span> (uint8_t pin = 0; pin &lt; (numPins - pass); ++pin) {
<a name="l00139"></a>00139 _pins1[n] = _pins2[n + 1] = pins[pin];
<a name="l00140"></a>00140 _pins2[n] = _pins1[n + 1] = pins[pin + pass];
<a name="l00141"></a>00141 n += 2;
<a name="l00142"></a>00142 }
<a name="l00143"></a>00143 }
<a name="l00144"></a>00144
<a name="l00145"></a>00145 <span class="comment">// Allocate space for the LED value array and zero it.</span>
<a name="l00146"></a>00146 _values = (uint8_t *)malloc(_count);
<a name="l00147"></a>00147 memset(_values, 0, _count);
<a name="l00148"></a>00148
<a name="l00149"></a>00149 <span class="comment">// Start with all pins configured as floating inputs (all LED&#39;s off).</span>
<a name="l00150"></a>00150 <span class="keywordflow">for</span> (uint8_t pin = 0; pin &lt; numPins; ++pin) {
<a name="l00151"></a>00151 digitalWrite(pins[pin], LOW);
<a name="l00152"></a>00152 pinMode(pins[pin], INPUT);
<a name="l00153"></a>00153 }
<a name="l00154"></a>00154 }
<a name="l00155"></a>00155
<a name="l00159"></a><a class="code" href="classCharlieplex.html#a4dbe37ccba8ba18139f4e710afdcd103">00159</a> <a class="code" href="classCharlieplex.html#a4dbe37ccba8ba18139f4e710afdcd103" title="Destroys this charlieplexed array.">Charlieplex::~Charlieplex</a>()
<a name="l00160"></a>00160 {
<a name="l00161"></a>00161 free(_pins1);
<a name="l00162"></a>00162 free(_pins2);
<a name="l00163"></a>00163 free(_values);
<a name="l00164"></a>00164 }
<a name="l00165"></a>00165
<a name="l00277"></a><a class="code" href="classCharlieplex.html#a8313edeacd8387c428b8299d52584d6a">00277</a> <span class="keywordtype">void</span> <a class="code" href="classCharlieplex.html#a8313edeacd8387c428b8299d52584d6a" title="Runs the multiplexing loop, to display the LED states on the charlieplexed array.">Charlieplex::loop</a>()
<a name="l00278"></a>00278 {
<a name="l00279"></a>00279 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> us = micros();
<a name="l00280"></a>00280 <span class="keywordflow">if</span> ((us - _lastTime) &gt;= _holdTime) {
<a name="l00281"></a>00281 _lastTime = us;
<a name="l00282"></a>00282 <a class="code" href="classCharlieplex.html#a3c961bfff866e400dad371f0376f096b" title="Refreshes the charlieplexed array by advancing to the next LED that needs to be lit.">refresh</a>();
<a name="l00283"></a>00283 }
<a name="l00284"></a>00284 }
<a name="l00285"></a>00285
<a name="l00296"></a><a class="code" href="classCharlieplex.html#a3c961bfff866e400dad371f0376f096b">00296</a> <span class="keywordtype">void</span> <a class="code" href="classCharlieplex.html#a3c961bfff866e400dad371f0376f096b" title="Refreshes the charlieplexed array by advancing to the next LED that needs to be lit.">Charlieplex::refresh</a>()
<a name="l00297"></a>00297 {
<a name="l00298"></a>00298 <span class="comment">// Find the next LED to be lit.</span>
<a name="l00299"></a>00299 <span class="keywordtype">int</span> prevIndex = _currentIndex;
<a name="l00300"></a>00300 <span class="keywordtype">int</span> limit = _count;
<a name="l00301"></a>00301 <span class="keywordflow">while</span> (limit &gt;= 0) {
<a name="l00302"></a>00302 _currentIndex = (_currentIndex + 1) % _count;
<a name="l00303"></a>00303 <span class="keywordflow">if</span> (_values[_currentIndex] != 0)
<a name="l00304"></a>00304 <span class="keywordflow">break</span>;
<a name="l00305"></a>00305 --limit;
<a name="l00306"></a>00306 }
<a name="l00307"></a>00307 <span class="keywordflow">if</span> (limit &lt; 0) {
<a name="l00308"></a>00308 <span class="comment">// No LED&#39;s are lit. Turn off the previous LED and exit.</span>
<a name="l00309"></a>00309 <span class="keywordflow">if</span> (prevIndex != -1) {
<a name="l00310"></a>00310 digitalWrite(_pins1[prevIndex], LOW);
<a name="l00311"></a>00311 digitalWrite(_pins2[prevIndex], LOW);
<a name="l00312"></a>00312 pinMode(_pins1[prevIndex], INPUT);
<a name="l00313"></a>00313 pinMode(_pins2[prevIndex], INPUT);
<a name="l00314"></a>00314 }
<a name="l00315"></a>00315 _currentIndex = -1;
<a name="l00316"></a>00316 <span class="keywordflow">return</span>;
<a name="l00317"></a>00317 }
<a name="l00318"></a>00318
<a name="l00319"></a>00319 <span class="comment">// Light the current LED.</span>
<a name="l00320"></a>00320 uint8_t value = _values[_currentIndex];
<a name="l00321"></a>00321 uint8_t pin1 = _pins1[_currentIndex];
<a name="l00322"></a>00322 uint8_t pin2 = _pins2[_currentIndex];
<a name="l00323"></a>00323 _pwmPhase += 0x40;
<a name="l00324"></a>00324 <span class="keywordflow">if</span> (prevIndex != _currentIndex) {
<a name="l00325"></a>00325 <span class="comment">// Turn off the previous LED.</span>
<a name="l00326"></a>00326 <span class="keywordflow">if</span> (prevIndex != -1) {
<a name="l00327"></a>00327 digitalWrite(_pins1[prevIndex], LOW);
<a name="l00328"></a>00328 digitalWrite(_pins2[prevIndex], LOW);
<a name="l00329"></a>00329 pinMode(_pins1[prevIndex], INPUT);
<a name="l00330"></a>00330 pinMode(_pins2[prevIndex], INPUT);
<a name="l00331"></a>00331 }
<a name="l00332"></a>00332
<a name="l00333"></a>00333 <span class="comment">// We simulate PWM using a phase counter because analogWrite()</span>
<a name="l00334"></a>00334 <span class="comment">// combined with holdTime() causes too much flickering if more</span>
<a name="l00335"></a>00335 <span class="comment">// than one LED is lit. This reduces the PWM resolution to 1 in 4.</span>
<a name="l00336"></a>00336 pinMode(pin1, OUTPUT);
<a name="l00337"></a>00337 pinMode(pin2, OUTPUT);
<a name="l00338"></a>00338 <span class="keywordflow">if</span> (value &gt; _pwmPhase)
<a name="l00339"></a>00339 digitalWrite(pin1, HIGH);
<a name="l00340"></a>00340 <span class="keywordflow">else</span>
<a name="l00341"></a>00341 digitalWrite(pin1, LOW);
<a name="l00342"></a>00342 } <span class="keywordflow">else</span> {
<a name="l00343"></a>00343 <span class="comment">// Same LED as previous. Since there is only a single LED</span>
<a name="l00344"></a>00344 <span class="comment">// that is lit, we can use analogWrite() to set the PWM state.</span>
<a name="l00345"></a>00345 <span class="keywordflow">if</span> (value == 255)
<a name="l00346"></a>00346 digitalWrite(pin1, HIGH);
<a name="l00347"></a>00347 <span class="keywordflow">else</span>
<a name="l00348"></a>00348 analogWrite(pin1, value);
<a name="l00349"></a>00349 }
<a name="l00350"></a>00350 }
</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>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></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 Mon Jun 18 2012 11:02:51 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>