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

201 lines
14 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: Melody.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">Melody.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;Melody.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>
<a name="l00085"></a><a class="code" href="classMelody.html#a9edc4165a49368dd5d78eedf982c38b9">00085</a> <a class="code" href="classMelody.html#a9edc4165a49368dd5d78eedf982c38b9" title="Constructs a new melody playing object for pin.">Melody::Melody</a>(uint8_t pin)
<a name="l00086"></a>00086 : _pin(pin)
<a name="l00087"></a>00087 , playing(false)
<a name="l00088"></a>00088 , _loopCount(0)
<a name="l00089"></a>00089 , loopsLeft(0)
<a name="l00090"></a>00090 , notes(0)
<a name="l00091"></a>00091 , lengths(0)
<a name="l00092"></a>00092 , size(0)
<a name="l00093"></a>00093 , posn(0)
<a name="l00094"></a>00094 , duration(0)
<a name="l00095"></a>00095 , startNote(0)
<a name="l00096"></a>00096 {
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098
<a name="l00131"></a><a class="code" href="classMelody.html#ae88ad06c2acb728f56dd213d5dad6006">00131</a> <span class="keywordtype">void</span> <a class="code" href="classMelody.html#ae88ad06c2acb728f56dd213d5dad6006" title="Sets the maximum number of loops to last no longer than ms milliseconds.">Melody::setLoopDuration</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> ms)
<a name="l00132"></a>00132 {
<a name="l00133"></a>00133 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> duration = 0;
<a name="l00134"></a>00134 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> index = 0; index &lt; size; ++index)
<a name="l00135"></a>00135 duration += (1000 / lengths[index]) * 13 / 10;
<a name="l00136"></a>00136 _loopCount = (int)(ms / duration);
<a name="l00137"></a>00137 <span class="keywordflow">if</span> (!_loopCount)
<a name="l00138"></a>00138 _loopCount = 1; <span class="comment">// Play the melody at least once.</span>
<a name="l00139"></a>00139 }
<a name="l00140"></a>00140
<a name="l00146"></a><a class="code" href="classMelody.html#a9fd8e0d48833d8da3cd3b3b58408b0b5">00146</a> <span class="keywordtype">void</span> <a class="code" href="classMelody.html#a9fd8e0d48833d8da3cd3b3b58408b0b5" title="Starts playing the melody, or restarts it if already playing.">Melody::play</a>()
<a name="l00147"></a>00147 {
<a name="l00148"></a>00148 <a class="code" href="classMelody.html#ac0c552233c41d85f2766d2e4df376b2b" title="Stops playing the melody.">stop</a>();
<a name="l00149"></a>00149 <span class="keywordflow">if</span> (size == 0)
<a name="l00150"></a>00150 <span class="keywordflow">return</span>; <span class="comment">// No melody to play.</span>
<a name="l00151"></a>00151 loopsLeft = _loopCount;
<a name="l00152"></a>00152 posn = 0;
<a name="l00153"></a>00153 playing = <span class="keyword">true</span>;
<a name="l00154"></a>00154 nextNote();
<a name="l00155"></a>00155 }
<a name="l00156"></a>00156
<a name="l00162"></a><a class="code" href="classMelody.html#aecc9185c9cb1246e8a55521b17d72932">00162</a> <span class="keywordtype">void</span> <a class="code" href="classMelody.html#aecc9185c9cb1246e8a55521b17d72932" title="Plays the melody once and then stops.">Melody::playOnce</a>()
<a name="l00163"></a>00163 {
<a name="l00164"></a>00164 <a class="code" href="classMelody.html#ac0c552233c41d85f2766d2e4df376b2b" title="Stops playing the melody.">stop</a>();
<a name="l00165"></a>00165 <span class="keywordflow">if</span> (size == 0)
<a name="l00166"></a>00166 <span class="keywordflow">return</span>; <span class="comment">// No melody to play.</span>
<a name="l00167"></a>00167 loopsLeft = 1;
<a name="l00168"></a>00168 posn = 0;
<a name="l00169"></a>00169 playing = <span class="keyword">true</span>;
<a name="l00170"></a>00170 nextNote();
<a name="l00171"></a>00171 }
<a name="l00172"></a>00172
<a name="l00178"></a><a class="code" href="classMelody.html#ac0c552233c41d85f2766d2e4df376b2b">00178</a> <span class="keywordtype">void</span> <a class="code" href="classMelody.html#ac0c552233c41d85f2766d2e4df376b2b" title="Stops playing the melody.">Melody::stop</a>()
<a name="l00179"></a>00179 {
<a name="l00180"></a>00180 <span class="keywordflow">if</span> (!playing)
<a name="l00181"></a>00181 <span class="keywordflow">return</span>;
<a name="l00182"></a>00182 playing = <span class="keyword">false</span>;
<a name="l00183"></a>00183 noTone(_pin);
<a name="l00184"></a>00184 }
<a name="l00185"></a>00185
<a name="l00199"></a><a class="code" href="classMelody.html#adb6ad8e8cfe8c9a137e470f4e85c7254">00199</a> <span class="keywordtype">void</span> <a class="code" href="classMelody.html#adb6ad8e8cfe8c9a137e470f4e85c7254" title="Sets the melody to the size elements of notes and lengths.">Melody::setMelody</a>(<span class="keyword">const</span> <span class="keywordtype">int</span> *notes, <span class="keyword">const</span> uint8_t *lengths, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size)
<a name="l00200"></a>00200 {
<a name="l00201"></a>00201 <a class="code" href="classMelody.html#ac0c552233c41d85f2766d2e4df376b2b" title="Stops playing the melody.">stop</a>();
<a name="l00202"></a>00202 this-&gt;notes = notes;
<a name="l00203"></a>00203 this-&gt;lengths = lengths;
<a name="l00204"></a>00204 this-&gt;size = size;
<a name="l00205"></a>00205 }
<a name="l00206"></a>00206
<a name="l00214"></a><a class="code" href="classMelody.html#ad1103b970be1f59058cc7d927be68955">00214</a> <span class="keywordtype">void</span> <a class="code" href="classMelody.html#ad1103b970be1f59058cc7d927be68955" title="Runs the melody control loop.">Melody::run</a>()
<a name="l00215"></a>00215 {
<a name="l00216"></a>00216 <span class="keywordflow">if</span> (!playing)
<a name="l00217"></a>00217 <span class="keywordflow">return</span>;
<a name="l00218"></a>00218 <span class="keywordflow">if</span> ((millis() - startNote) &gt;= duration) {
<a name="l00219"></a>00219 noTone(_pin);
<a name="l00220"></a>00220 nextNote();
<a name="l00221"></a>00221 }
<a name="l00222"></a>00222 }
<a name="l00223"></a>00223
<a name="l00224"></a>00224 <span class="keywordtype">void</span> Melody::nextNote()
<a name="l00225"></a>00225 {
<a name="l00226"></a>00226 <span class="keywordflow">if</span> (posn &gt;= size) {
<a name="l00227"></a>00227 <span class="keywordflow">if</span> (loopsLeft != 0 &amp;&amp; --loopsLeft &lt;= 0) {
<a name="l00228"></a>00228 <a class="code" href="classMelody.html#ac0c552233c41d85f2766d2e4df376b2b" title="Stops playing the melody.">stop</a>();
<a name="l00229"></a>00229 <span class="keywordflow">return</span>;
<a name="l00230"></a>00230 }
<a name="l00231"></a>00231 posn = 0;
<a name="l00232"></a>00232 }
<a name="l00233"></a>00233 duration = 1000 / lengths[posn];
<a name="l00234"></a>00234 <span class="keywordflow">if</span> (notes[posn] != NOTE_REST)
<a name="l00235"></a>00235 tone(_pin, notes[posn], duration);
<a name="l00236"></a>00236 ++posn;
<a name="l00237"></a>00237 duration = duration * 13 / 10; <span class="comment">// i.e., duration * 1.3</span>
<a name="l00238"></a>00238 startNote = millis();
<a name="l00239"></a>00239 }
</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>