Ali Saidi / TxIR

Description: This library is a very low level interface to a transmit IR codes. Other libraries could be built on top of it to transmit different manufactures codes, or a specific code. I use the library to remotely trigger a Nikon DSLR camera the same way an ML3 Nikon remote does.

Committer:
user Ali Saidi
Date:
Sun Dec 12 05:36:26 2010 +0000
Revision:
0:7d2088337345
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
user Ali Saidi0:7d2088337345 1
user Ali Saidi0:7d2088337345 2/******************************************************************************
user Ali Saidi0:7d2088337345 3 * Low level infrared transmission
user Ali Saidi0:7d2088337345 4 * This library provides a low level interface to send IR commands. in principle
user Ali Saidi0:7d2088337345 5 * other higher level IR type remotes could be build on top of this. I wrote
user Ali Saidi0:7d2088337345 6 * this code to send IR commands to a Nikon camera, which it works rather well
user Ali Saidi0:7d2088337345 7 * for.
user Ali Saidi0:7d2088337345 8 *
user Ali Saidi0:7d2088337345 9 * Copyright (C) 2010 Ali Saidi
user Ali Saidi0:7d2088337345 10 *
user Ali Saidi0:7d2088337345 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
user Ali Saidi0:7d2088337345 12 * of this software and associated documentation files (the "Software"), to deal
user Ali Saidi0:7d2088337345 13 * in the Software without restriction, including without limitation the rights
user Ali Saidi0:7d2088337345 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
user Ali Saidi0:7d2088337345 15 * copies of the Software, and to permit persons to whom the Software is
user Ali Saidi0:7d2088337345 16 * furnished to do so, subject to the following conditions:
user Ali Saidi0:7d2088337345 17 *
user Ali Saidi0:7d2088337345 18 * The above copyright notice and this permission notice shall be included in
user Ali Saidi0:7d2088337345 19 * all copies or substantial portions of the Software.
user Ali Saidi0:7d2088337345 20 *
user Ali Saidi0:7d2088337345 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
user Ali Saidi0:7d2088337345 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
user Ali Saidi0:7d2088337345 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
user Ali Saidi0:7d2088337345 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
user Ali Saidi0:7d2088337345 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
user Ali Saidi0:7d2088337345 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
user Ali Saidi0:7d2088337345 27 * THE SOFTWARE.
user Ali Saidi0:7d2088337345 28 */
user Ali Saidi0:7d2088337345 29
user Ali Saidi0:7d2088337345 30 #include "TxIR.hpp"
user Ali Saidi0:7d2088337345 31
user Ali Saidi0:7d2088337345 32
user Ali Saidi0:7d2088337345 33void
user Ali Saidi0:7d2088337345 34TxIR::doAction()
user Ali Saidi0:7d2088337345 35{
user Ali Saidi0:7d2088337345 36 if (pos >= len) {
user Ali Saidi0:7d2088337345 37 delete [] data;
user Ali Saidi0:7d2088337345 38 _inUse = false;
user Ali Saidi0:7d2088337345 39 txPin.write(0);
user Ali Saidi0:7d2088337345 40 return;
user Ali Saidi0:7d2088337345 41 }
user Ali Saidi0:7d2088337345 42
user Ali Saidi0:7d2088337345 43 delay.attach_us(this, &TxIR::doAction, data[pos++]);
user Ali Saidi0:7d2088337345 44 if (high)
user Ali Saidi0:7d2088337345 45 txPin.write(0);
user Ali Saidi0:7d2088337345 46 else
user Ali Saidi0:7d2088337345 47 txPin.write(0.5);
user Ali Saidi0:7d2088337345 48 high = !high;
user Ali Saidi0:7d2088337345 49}
user Ali Saidi0:7d2088337345 50
user Ali Saidi0:7d2088337345 51bool
user Ali Saidi0:7d2088337345 52TxIR::txSeq(unsigned freq, unsigned _len, const unsigned *_data)
user Ali Saidi0:7d2088337345 53{
user Ali Saidi0:7d2088337345 54 // @todo a lock or semaphore should be used here
user Ali Saidi0:7d2088337345 55 if (inUse())
user Ali Saidi0:7d2088337345 56 return false;
user Ali Saidi0:7d2088337345 57 _inUse = true;
user Ali Saidi0:7d2088337345 58
user Ali Saidi0:7d2088337345 59 // keep a copy of the data, so it can't change
user Ali Saidi0:7d2088337345 60 len = _len;
user Ali Saidi0:7d2088337345 61 data = new unsigned[len];
user Ali Saidi0:7d2088337345 62 memcpy(data, _data, len * sizeof(unsigned));
user Ali Saidi0:7d2088337345 63 pos = 0;
user Ali Saidi0:7d2088337345 64
user Ali Saidi0:7d2088337345 65 // setup the PWM
user Ali Saidi0:7d2088337345 66 txPin.write(0.0);
user Ali Saidi0:7d2088337345 67 txPin.period_us(freq);
user Ali Saidi0:7d2088337345 68
user Ali Saidi0:7d2088337345 69 // Get the interrupt ready
user Ali Saidi0:7d2088337345 70 delay.detach();
user Ali Saidi0:7d2088337345 71 high = true;
user Ali Saidi0:7d2088337345 72 delay.attach_us(this, &TxIR::doAction, data[pos++]);
user Ali Saidi0:7d2088337345 73
user Ali Saidi0:7d2088337345 74 // Begin
user Ali Saidi0:7d2088337345 75 txPin.write(0.5);
user Ali Saidi0:7d2088337345 76
user Ali Saidi0:7d2088337345 77 return true;
user Ali Saidi0:7d2088337345 78}