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

