mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
250 lines
19 KiB
HTML
250 lines
19 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: IRreceiver.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 Page</span></a></li>
|
|
<li><a href="pages.html"><span>Related 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 List</span></a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">IRreceiver.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 "Software"),</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 "AS IS", 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 "IRreceiver.h"</span>
|
|
<a name="l00024"></a>00024 <span class="preprocessor">#if defined(ARDUINO) && ARDUINO >= 100</span>
|
|
<a name="l00025"></a>00025 <span class="preprocessor"></span><span class="preprocessor">#include <Arduino.h></span>
|
|
<a name="l00026"></a>00026 <span class="preprocessor">#else</span>
|
|
<a name="l00027"></a>00027 <span class="preprocessor"></span><span class="preprocessor">#include <WProgram.h></span>
|
|
<a name="l00028"></a>00028 <span class="preprocessor">#endif</span>
|
|
<a name="l00029"></a>00029 <span class="preprocessor"></span>
|
|
<a name="l00159"></a>00159 <span class="keyword">static</span> <a class="code" href="classIRreceiver.html" title="Manages the reception of RC-5 commands from an infrared remote control.">IRreceiver</a> *receiver = 0;
|
|
<a name="l00160"></a>00160
|
|
<a name="l00161"></a>00161 <span class="keywordtype">void</span> _IR_receive_interrupt(<span class="keywordtype">void</span>)
|
|
<a name="l00162"></a>00162 {
|
|
<a name="l00163"></a>00163 receiver->handleInterrupt();
|
|
<a name="l00164"></a>00164 }
|
|
<a name="l00165"></a>00165
|
|
<a name="l00176"></a><a class="code" href="classIRreceiver.html#a06eccb8b8311eac395e4b20c4e0163e3">00176</a> <a class="code" href="classIRreceiver.html#a06eccb8b8311eac395e4b20c4e0163e3" title="Constructs a new infrared remote control receiver that is attached to interruptNumber.">IRreceiver::IRreceiver</a>(<span class="keywordtype">int</span> interruptNumber)
|
|
<a name="l00177"></a>00177 : _system(0)
|
|
<a name="l00178"></a>00178 , _systemFilter(-1)
|
|
<a name="l00179"></a>00179 , started(false)
|
|
<a name="l00180"></a>00180 , halfChange(false)
|
|
<a name="l00181"></a>00181 , lastChange(0)
|
|
<a name="l00182"></a>00182 , bits(0)
|
|
<a name="l00183"></a>00183 , bitCount(0)
|
|
<a name="l00184"></a>00184 , buffer(0)
|
|
<a name="l00185"></a>00185 , lastBuffer(0)
|
|
<a name="l00186"></a>00186 {
|
|
<a name="l00187"></a>00187 <span class="keywordflow">switch</span> (interruptNumber) {
|
|
<a name="l00188"></a>00188 <span class="keywordflow">case</span> 0: <span class="keywordflow">default</span>: pin = 2; <span class="keywordflow">break</span>;
|
|
<a name="l00189"></a>00189 <span class="keywordflow">case</span> 1: pin = 3; <span class="keywordflow">break</span>;
|
|
<a name="l00190"></a>00190 <span class="keywordflow">case</span> 2: pin = 21; <span class="keywordflow">break</span>; <span class="comment">// Arduino Mega only</span>
|
|
<a name="l00191"></a>00191 <span class="keywordflow">case</span> 3: pin = 20; <span class="keywordflow">break</span>; <span class="comment">// Arduino Mega only</span>
|
|
<a name="l00192"></a>00192 <span class="keywordflow">case</span> 4: pin = 19; <span class="keywordflow">break</span>; <span class="comment">// Arduino Mega only</span>
|
|
<a name="l00193"></a>00193 <span class="keywordflow">case</span> 5: pin = 18; <span class="keywordflow">break</span>; <span class="comment">// Arduino Mega only</span>
|
|
<a name="l00194"></a>00194 }
|
|
<a name="l00195"></a>00195 receiver = <span class="keyword">this</span>;
|
|
<a name="l00196"></a>00196 attachInterrupt(interruptNumber, _IR_receive_interrupt, CHANGE);
|
|
<a name="l00197"></a>00197 }
|
|
<a name="l00198"></a>00198
|
|
<a name="l00220"></a><a class="code" href="classIRreceiver.html#a4b021592a2b089dc2f1e138a38506fda">00220</a> <span class="keywordtype">int</span> <a class="code" href="classIRreceiver.html#a4b021592a2b089dc2f1e138a38506fda" title="Returns the next command from the remote control.">IRreceiver::command</a>()
|
|
<a name="l00221"></a>00221 {
|
|
<a name="l00222"></a>00222 <span class="keywordtype">unsigned</span> buf;
|
|
<a name="l00223"></a>00223
|
|
<a name="l00224"></a>00224 <span class="comment">// Read the last-delivered sequence from the buffer and clear it.</span>
|
|
<a name="l00225"></a>00225 cli();
|
|
<a name="l00226"></a>00226 buf = buffer;
|
|
<a name="l00227"></a>00227 buffer = 0;
|
|
<a name="l00228"></a>00228 sei();
|
|
<a name="l00229"></a>00229
|
|
<a name="l00230"></a>00230 <span class="comment">// Bail out if no sequence or it is not for us.</span>
|
|
<a name="l00231"></a>00231 <span class="keywordflow">if</span> (!buf) {
|
|
<a name="l00232"></a>00232 _system = -1;
|
|
<a name="l00233"></a>00233 <span class="keywordflow">return</span> -1;
|
|
<a name="l00234"></a>00234 }
|
|
<a name="l00235"></a>00235 <span class="keywordflow">if</span> (_systemFilter != -1) {
|
|
<a name="l00236"></a>00236 <span class="keywordflow">if</span> (((buf >> 6) & 0x1F) != _systemFilter) {
|
|
<a name="l00237"></a>00237 _system = -1;
|
|
<a name="l00238"></a>00238 <span class="keywordflow">return</span> -1;
|
|
<a name="l00239"></a>00239 }
|
|
<a name="l00240"></a>00240 }
|
|
<a name="l00241"></a>00241
|
|
<a name="l00242"></a>00242 <span class="comment">// Extract the command.</span>
|
|
<a name="l00243"></a>00243 <span class="keywordtype">int</span> cmd = buf & 0x3F;
|
|
<a name="l00244"></a>00244 <span class="keywordflow">if</span> ((buf & 0x1000) == 0)
|
|
<a name="l00245"></a>00245 cmd += 64;
|
|
<a name="l00246"></a>00246
|
|
<a name="l00247"></a>00247 <span class="comment">// Is this a new command or an auto-repeat of the previous command?</span>
|
|
<a name="l00248"></a>00248 <span class="comment">// Bit 11 will toggle whenever a new button press is started.</span>
|
|
<a name="l00249"></a>00249 <span class="keywordflow">if</span> (lastBuffer == buf)
|
|
<a name="l00250"></a>00250 cmd += <a class="code" href="classIRreceiver.html#a9c37631cc1291dc47cabcfef2f631cf9" title="Flag that is added to the output of command() when the command is an auto-repeated button press rathe...">AUTO_REPEAT</a>;
|
|
<a name="l00251"></a>00251 <span class="keywordflow">else</span>
|
|
<a name="l00252"></a>00252 lastBuffer = buf;
|
|
<a name="l00253"></a>00253 _system = (buf >> 6) & 0x1F;
|
|
<a name="l00254"></a>00254 <span class="keywordflow">return</span> cmd;
|
|
<a name="l00255"></a>00255 }
|
|
<a name="l00256"></a>00256
|
|
<a name="l00304"></a>00304 <span class="comment">// Number of microseconds that the signal is HIGH or LOW for</span>
|
|
<a name="l00305"></a>00305 <span class="comment">// indicating a bit. A 1 bit is transmitted as LOW for 889us</span>
|
|
<a name="l00306"></a>00306 <span class="comment">// followed by HIGH for 889us. A 0 bit is HIGH, then LOW.</span>
|
|
<a name="l00307"></a>00307 <span class="preprocessor">#define IR_BIT_TIME 889</span>
|
|
<a name="l00308"></a>00308 <span class="preprocessor"></span>
|
|
<a name="l00309"></a>00309 <span class="comment">// Number of microseconds to detect a long gap in the coding</span>
|
|
<a name="l00310"></a>00310 <span class="comment">// corresponding to 2 time units HIGH or LOW. We actually check</span>
|
|
<a name="l00311"></a>00311 <span class="comment">// for at least 1.5 time units to allow for slight variations</span>
|
|
<a name="l00312"></a>00312 <span class="comment">// in timing on different remote controls.</span>
|
|
<a name="l00313"></a>00313 <span class="preprocessor">#define IR_LONG_BIT_TIME (889 * 6 / 4)</span>
|
|
<a name="l00314"></a>00314 <span class="preprocessor"></span>
|
|
<a name="l00315"></a>00315 <span class="comment">// Maximum timeout for a single bit. If we don't see a rising edge</span>
|
|
<a name="l00316"></a>00316 <span class="comment">// within this time, then we have lost sync and need to restart.</span>
|
|
<a name="l00317"></a>00317 <span class="preprocessor">#define IR_MAX_TIME (IR_BIT_TIME * 4)</span>
|
|
<a name="l00318"></a>00318 <span class="preprocessor"></span>
|
|
<a name="l00319"></a>00319 <span class="comment">// Protocol details from http://en.wikipedia.org/wiki/RC-5</span>
|
|
<a name="l00320"></a>00320 <span class="keywordtype">void</span> IRreceiver::handleInterrupt()
|
|
<a name="l00321"></a>00321 {
|
|
<a name="l00322"></a>00322 <span class="keywordtype">bool</span> value = digitalRead(pin);
|
|
<a name="l00323"></a>00323 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> currentTime = micros();
|
|
<a name="l00324"></a>00324 <span class="keywordflow">if</span> (!value) {
|
|
<a name="l00325"></a>00325 <span class="comment">// Rising edge (input is active-LOW)</span>
|
|
<a name="l00326"></a>00326 <span class="keywordflow">if</span> (started && (currentTime - lastChange) > IR_MAX_TIME) {
|
|
<a name="l00327"></a>00327 <span class="comment">// Too long since the last received bit, so restart the process.</span>
|
|
<a name="l00328"></a>00328 started = <span class="keyword">false</span>;
|
|
<a name="l00329"></a>00329 }
|
|
<a name="l00330"></a>00330 <span class="keywordflow">if</span> (started) {
|
|
<a name="l00331"></a>00331 <span class="comment">// We recognize bits on the falling edges, so merely</span>
|
|
<a name="l00332"></a>00332 <span class="comment">// adjust the "changed at last half-cycle" flag.</span>
|
|
<a name="l00333"></a>00333 <span class="keywordflow">if</span> ((currentTime - lastChange) > IR_LONG_BIT_TIME) {
|
|
<a name="l00334"></a>00334 <span class="comment">// Long time since last falling edge indicates that the</span>
|
|
<a name="l00335"></a>00335 <span class="comment">// next bit will definitely be a 1.</span>
|
|
<a name="l00336"></a>00336 halfChange = <span class="keyword">true</span>;
|
|
<a name="l00337"></a>00337 } <span class="keywordflow">else</span> {
|
|
<a name="l00338"></a>00338 halfChange = !halfChange;
|
|
<a name="l00339"></a>00339 }
|
|
<a name="l00340"></a>00340 lastChange = currentTime;
|
|
<a name="l00341"></a>00341 } <span class="keywordflow">else</span> {
|
|
<a name="l00342"></a>00342 <span class="comment">// Encountered the start bit - start receiving up to 14 bits.</span>
|
|
<a name="l00343"></a>00343 lastChange = currentTime;
|
|
<a name="l00344"></a>00344 started = <span class="keyword">true</span>;
|
|
<a name="l00345"></a>00345 halfChange = <span class="keyword">true</span>;
|
|
<a name="l00346"></a>00346 bits = 0;
|
|
<a name="l00347"></a>00347 bitCount = 14;
|
|
<a name="l00348"></a>00348 }
|
|
<a name="l00349"></a>00349 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (started) {
|
|
<a name="l00350"></a>00350 <span class="comment">// Falling edge</span>
|
|
<a name="l00351"></a>00351 <span class="keywordflow">if</span> ((currentTime - lastChange) > IR_LONG_BIT_TIME) {
|
|
<a name="l00352"></a>00352 <span class="comment">// Long time since last rise indicates 1 followed by 0.</span>
|
|
<a name="l00353"></a>00353 bits = (bits << 2) | 0x02;
|
|
<a name="l00354"></a>00354 --bitCount;
|
|
<a name="l00355"></a>00355 halfChange = <span class="keyword">true</span>;
|
|
<a name="l00356"></a>00356 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (halfChange) {
|
|
<a name="l00357"></a>00357 <span class="comment">// Rise was halfway through, so falling edge indicates a 1.</span>
|
|
<a name="l00358"></a>00358 bits = (bits << 1) | 0x01;
|
|
<a name="l00359"></a>00359 halfChange = <span class="keyword">false</span>;
|
|
<a name="l00360"></a>00360 } <span class="keywordflow">else</span> {
|
|
<a name="l00361"></a>00361 <span class="comment">// Rise was at the start, so falling edge indicates a 0.</span>
|
|
<a name="l00362"></a>00362 bits <<= 1;
|
|
<a name="l00363"></a>00363 halfChange = <span class="keyword">true</span>;
|
|
<a name="l00364"></a>00364 }
|
|
<a name="l00365"></a>00365 lastChange = currentTime;
|
|
<a name="l00366"></a>00366 --bitCount;
|
|
<a name="l00367"></a>00367 <span class="keywordflow">if</span> (bitCount <= 0) {
|
|
<a name="l00368"></a>00368 <span class="comment">// All 14 bits have been received, so deliver the value.</span>
|
|
<a name="l00369"></a>00369 started = <span class="keyword">false</span>;
|
|
<a name="l00370"></a>00370 buffer = bits;
|
|
<a name="l00371"></a>00371 }
|
|
<a name="l00372"></a>00372 }
|
|
<a name="l00373"></a>00373 }
|
|
</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"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </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 Wed Jun 13 2012 15:22:29 for ArduinoLibs by 
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
|
|
</body>
|
|
</html>
|