pigpio/DOC/HTML/ex_ir_remote.html

309 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="description" content="Raspberry Pi Reg. C GPIO library and Python GPIO module and shell command utilities to control the GPIO, including SPI, I2C, and serial links." />
<meta name="keywords" content="raspberry, pi, C, Python, GPIO, library, shell, command, utilities, module, SPI, I2C, serial" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>pigpio library</title>
<link rel="stylesheet" type="text/css" href="scripts/index.css">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
<table style="padding:0px; border:0px; margin:0px; width:780px; background-color:#e0e0e0;">
<td style="background:#EAF2E6 url('images/sidebar.gif') repeat-y; width:35px; height:100%"></td>
<td>
<table>
<div style="background:url('images/topbar.gif') repeat-x; height: 70px; font-size:1.5em; vertical-align: top;"><a href="index.html"><img src="images/pigpio-logo.gif" border="0" /></a>pigpio library</div>
</table>
<table><div>
<td><img src="images/driver.jpg" width="250"></td>
<td><img src="images/imu-3.jpg" width="250"></td>
<td><img src="images/ir-motion.jpg" width="250"></td>
</div></table>
<table>
<td style="vertical-align: top; background-color: #98bf21;"><a class="l1" href="index.html">pigpio</a>
<a class="l1" href="cif.html">pigpio C I/F</a>
<a class="l1" href="pigpiod.html">pigpiod</a>
<a class="l1" href="pdif2.html">pigpiod C I/F</a>
<a class="l1" href="python.html">Python</a>
<a class="l1" href="pigs.html">pigs</a>
<a class="l1" href="piscope.html">piscope</a>
<a class="l1" href="misc.html">Misc</a>
<a class="l1" href="examples.html">Examples</a>
<a class="l1" href="download.html">Download</a>
<a class="l1" href="faq.html">FAQ</a>
<a class="l1" href="sitemap.html">Site Map</a>
</td>
<td><center><h2>IR Remote Example</h2></center>
<p>The following code shows one way to read an infrared remote
control device (the sort used in TVs and stereo systems).</p>
<h3>SETUP</h3>
<img alt="fritzing diagram" style="width: 200px; height: 317px;"
src="images/ir-fritz.png" align="left" hspace="10">The device used
is a <span itemprop="name">SFH5110 (IR Receiver for remote control,
carrier 38 kHz).<br>
<br></span> Pin 1 (left from front) may be connected to any spare
gpio.&nbsp; Here it's connected via a 4K7 current limiting
resistor.&nbsp; This isn't really needed as the device has an
internal 23K resistor in-line.&nbsp; It does no harm though.<br>
<br>
Pin 2 should be connected to a Pi ground pin.<br>
<br>
Pin 3 should be connected to a Pi 5V pin.<br>
<p>Here pin 1 to gpio7 (P1-26) via a 4K7 resistor, pin 2 to ground
(P1-14), and pin 3 to 5V (P1-2).<br clear="all"></p>
<p><img src="images/ir-photo.jpg" style=
"width: 500px; height: 500px;" alt="photo of set-up"></p>
<h3>CODE</h3>
<code>#include &lt;stdio.h&gt;<br>
<br>
#include &lt;pigpio.h&gt;<br>
<br>
#define IR_PIN 7<br>
<br>
#define OUTSIDE_CODE 0<br>
#define INSIDE_CODE&nbsp; 1<br>
<br>
#define MIN_MESSAGE_GAP 3000<br>
#define MAX_MESSAGE_END 3000<br>
<br>
#define MAX_TRANSITIONS 500<br>
<br>
/*<br>
&nbsp;&nbsp; using the FNV-1a
hash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp; from
http://isthe.com/chongo/tech/comp/fnv/#FNV-param<br>
*/<br>
<br>
#define FNV_PRIME_32 16777619<br>
#define FNV_BASIS_32 2166136261U<br>
<br>
static volatile uint32_t ir_hash = 0;<br>
<br>
typedef struct<br>
{<br>
&nbsp;&nbsp; int state;<br>
&nbsp;&nbsp; int count;<br>
&nbsp;&nbsp; int level;<br>
&nbsp;&nbsp; uint16_t micros[MAX_TRANSITIONS];<br>
} decode_t;<br>
<br>
/* forward declarations */<br>
<br>
void&nbsp;&nbsp;&nbsp;&nbsp; alert(int gpio, int level, uint32_t
tick);<br>
uint32_t getHash(decode_t * decode);<br>
void&nbsp;&nbsp;&nbsp;&nbsp; updateState(decode_t * decode, int
level, uint32_t micros);<br>
<br>
int main(int argc, char * argv[])<br>
{<br>
&nbsp;&nbsp; if (gpioInitialise()&lt;0)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1 ;<br>
&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp; /* IR pin as input */<br>
<br>
&nbsp;&nbsp; gpioSetMode(IR_PIN, PI_INPUT);<br>
<br>
&nbsp;&nbsp; /* 5ms max gap after last pulse */<br>
<br>
&nbsp;&nbsp; gpioSetWatchdog(IR_PIN, 5);<br>
<br>
&nbsp;&nbsp; /* monitor IR level changes */<br>
<br>
&nbsp;&nbsp; gpioSetAlertFunc(IR_PIN, alert);<br>
<br>
&nbsp;&nbsp; while (1)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ir_hash)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* non-zero means
new decode */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("ir code is
%u\ ", ir_hash);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ir_hash = 0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gpioDelay(100000); /* check remote
10 times per second */<br>
&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp; gpioTerminate();<br>
}<br>
<br>
void alert(int gpio, int level, uint32_t tick)<br>
{<br>
&nbsp;&nbsp; static int inited = 0;<br>
<br>
&nbsp;&nbsp; static decode_t activeHigh, activeLow;<br>
<br>
&nbsp;&nbsp; static uint32_t lastTick;<br>
<br>
&nbsp;&nbsp; uint32_t diffTick;<br>
<br>
&nbsp;&nbsp; if (!inited)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inited = 1;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; activeHigh.state = OUTSIDE_CODE;
activeHigh.level = PI_LOW;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; activeLow.state&nbsp; =
OUTSIDE_CODE; activeLow.level&nbsp; = PI_HIGH;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lastTick = tick;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp; diffTick = tick - lastTick;<br>
<br>
&nbsp;&nbsp; if (level != PI_TIMEOUT) lastTick = tick;<br>
<br>
&nbsp;&nbsp; updateState(&amp;activeHigh, level, diffTick);<br>
&nbsp;&nbsp; updateState(&amp;activeLow, level, diffTick);<br>
}<br>
<br>
void updateState(decode_t * decode, int level, uint32_t micros)<br>
{<br>
&nbsp;&nbsp; /*<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; We are dealing with active high as
well as active low<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; remotes.&nbsp; Abstract the common
functionality.<br>
&nbsp;&nbsp; */<br>
<br>
&nbsp;&nbsp; if (decode-&gt;state == OUTSIDE_CODE)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (level == decode-&gt;level)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (micros &gt;
MIN_MESSAGE_GAP)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
decode-&gt;state = INSIDE_CODE;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
decode-&gt;count = 0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp; }<br>
&nbsp;&nbsp; else<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (micros &gt; MAX_MESSAGE_END)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* end of message
*/<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* ignore if last
code not consumed */<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!ir_hash)
ir_hash = getHash(decode);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; decode-&gt;state =
OUTSIDE_CODE;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if
(decode-&gt;count &lt; (MAX_TRANSITIONS-1))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (level != PI_TIMEOUT)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
decode-&gt;micros[decode-&gt;count++] = micros;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp; }<br>
}<br>
<br>
int compare(unsigned int oldval, unsigned int newval)<br>
{<br>
&nbsp;&nbsp; if&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (newval &lt; (oldval
* 0.75)) {return 1;}<br>
&nbsp;&nbsp; else if (oldval &lt; (newval * 0.75)) {return 2;}<br>
&nbsp;&nbsp;
else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{return 4;}<br>
}<br>
<br>
uint32_t getHash(decode_t * decode)<br>
{<br>
&nbsp;&nbsp; /* use FNV-1a */<br>
<br>
&nbsp;&nbsp; uint32_t hash;<br>
&nbsp;&nbsp; int i, value;<br>
<br>
&nbsp;&nbsp; if (decode-&gt;count &lt; 6) {return 0;}<br>
<br>
&nbsp;&nbsp; hash = FNV_BASIS_32;<br>
<br>
&nbsp;&nbsp; for (i=0; i&lt;(decode-&gt;count-2); i++)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value =
compare(decode-&gt;micros[i], decode-&gt;micros[i+2]);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hash = hash ^ value;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hash = (hash * FNV_PRIME_32);<br>
&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp; return hash;<br>
}<br></code>
<h3>BUILD</h3>
<code>cc -o ir_remote ir_remote.c -lpigpio -lrt
-lpthread<br></code>
<h3>RUN</h3>
<code>sudo ./ir_remote</code><br>
<p>A hash code is formed from the level transitions detected during
a remote key press.&nbsp; This is likely to be unique over multiple
remotes and keys.</p>
<p>While the program is running you can capture the waveform using
the notification feature built in to pigpio.&nbsp; Issue the
following commands on the Pi.</p>
<code>pigs no<br>
pig2vcd&nbsp; &lt;/dev/pigpio0 &gt;ir.vcd &amp;<br>
pigs nb 0 0x80 # set bits for gpios 7 (0x80)<br></code>
<p>Press a few different remotes and keys.&nbsp; Then enter<br></p>
<code>pigs nc 0</code><br>
<p>The file ir.vcd will contain the captured waveform, which can be
viewed using GTKWave.</p>
<p>Overview</p>
<img src="images/ir-wave-1.png" style=
"width: 600px; height: 100px;" alt="ir remote waveform 1"><br>
<p>Remote A typical waveform</p>
<img src="images/ir-wave-2.png" style=
"width: 600px; height: 100px;" alt="ir remote waveform 2"><br>
<p>Remote B typical waveform</p>
<img style="width: 600px; height: 100px;" alt=
"ir remote waveform 3" src="images/ir-wave-3.png">
</td>
</table>
<div style="vertical-align: center; text-align: center; background-color:#98bf21; font-size:0.8em; height:30px"><a class="l2" href="index.html">[pigpio]</a>
<a class="l2" href="cif.html">[pigpio C I/F]</a>
<a class="l2" href="pigpiod.html">[pigpiod]</a>
<a class="l2" href="pdif2.html">[pigpiod C I/F]</a>
<a class="l2" href="python.html">[Python]</a>
<a class="l2" href="pigs.html">[pigs]</a>
<a class="l2" href="piscope.html">[piscope]</a>
<a class="l2" href="misc.html">[Misc]</a>
<a class="l2" href="examples.html">[Examples]</a>
<a class="l2" href="download.html">[Download]</a>
<a class="l2" href="faq.html">[FAQ]</a>
<a class="l2" href="sitemap.html">[Site Map]</a>
</div>
<table><tr>
<td style="width: 200px"><div style="text-align: left;"><small>&copy; 2012-2020</small></div></td>
<td style="width: 350px"><div style="text-align: center;">e-mail: pigpio @ abyz.me.uk</div></td>
<td style="width: 200px"><div style="text-align: right;"><small>Updated: 29/04/2020</small></div></td>
</tr></table>
</td>
</table>
</body>
</html>