Fork of TxIR
Fork of TxIR by
TxIR.cpp@3:bc64c2d5bc26, 2014-07-09 (annotated)
- Committer:
- aleksanb
- Date:
- Wed Jul 09 14:46:25 2014 +0000
- Revision:
- 3:bc64c2d5bc26
- Parent:
- 2:cc8371213c85
A wide plethora of bugfixes and code formatting changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ASaidi | 0:7d2088337345 | 1 | |
ASaidi | 0:7d2088337345 | 2 | /****************************************************************************** |
ASaidi | 0:7d2088337345 | 3 | * Low level infrared transmission |
ASaidi | 0:7d2088337345 | 4 | * This library provides a low level interface to send IR commands. in principle |
ASaidi | 0:7d2088337345 | 5 | * other higher level IR type remotes could be build on top of this. I wrote |
ASaidi | 0:7d2088337345 | 6 | * this code to send IR commands to a Nikon camera, which it works rather well |
aleksanb | 3:bc64c2d5bc26 | 7 | * for. |
ASaidi | 0:7d2088337345 | 8 | * |
ASaidi | 0:7d2088337345 | 9 | * Copyright (C) 2010 Ali Saidi |
aleksanb | 3:bc64c2d5bc26 | 10 | * |
ASaidi | 0:7d2088337345 | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
ASaidi | 0:7d2088337345 | 12 | * of this software and associated documentation files (the "Software"), to deal |
ASaidi | 0:7d2088337345 | 13 | * in the Software without restriction, including without limitation the rights |
ASaidi | 0:7d2088337345 | 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ASaidi | 0:7d2088337345 | 15 | * copies of the Software, and to permit persons to whom the Software is |
ASaidi | 0:7d2088337345 | 16 | * furnished to do so, subject to the following conditions: |
aleksanb | 3:bc64c2d5bc26 | 17 | * |
ASaidi | 0:7d2088337345 | 18 | * The above copyright notice and this permission notice shall be included in |
ASaidi | 0:7d2088337345 | 19 | * all copies or substantial portions of the Software. |
aleksanb | 3:bc64c2d5bc26 | 20 | * |
ASaidi | 0:7d2088337345 | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ASaidi | 0:7d2088337345 | 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ASaidi | 0:7d2088337345 | 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ASaidi | 0:7d2088337345 | 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ASaidi | 0:7d2088337345 | 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ASaidi | 0:7d2088337345 | 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ASaidi | 0:7d2088337345 | 27 | * THE SOFTWARE. |
ASaidi | 0:7d2088337345 | 28 | */ |
aleksanb | 3:bc64c2d5bc26 | 29 | |
aleksanb | 3:bc64c2d5bc26 | 30 | #include "mbed.h" |
aleksanb | 3:bc64c2d5bc26 | 31 | #include "TxIR.hpp" |
ASaidi | 0:7d2088337345 | 32 | |
aleksanb | 3:bc64c2d5bc26 | 33 | Serial pc(USBTX, USBRX); |
aleksanb | 3:bc64c2d5bc26 | 34 | |
aleksanb | 3:bc64c2d5bc26 | 35 | Timer timer; |
ASaidi | 0:7d2088337345 | 36 | |
ASaidi | 0:7d2088337345 | 37 | bool |
aleksanb | 3:bc64c2d5bc26 | 38 | TxIR::txSeq(unsigned freq, unsigned len, const unsigned *data) |
ASaidi | 0:7d2088337345 | 39 | { |
ASaidi | 0:7d2088337345 | 40 | // @todo a lock or semaphore should be used here |
aleksanb | 3:bc64c2d5bc26 | 41 | if (inUse()) { |
ASaidi | 0:7d2088337345 | 42 | return false; |
aleksanb | 3:bc64c2d5bc26 | 43 | } |
ASaidi | 0:7d2088337345 | 44 | _inUse = true; |
aleksanb | 3:bc64c2d5bc26 | 45 | |
ASaidi | 0:7d2088337345 | 46 | // keep a copy of the data, so it can't change |
aleksanb | 3:bc64c2d5bc26 | 47 | int pos = 0; |
ASaidi | 0:7d2088337345 | 48 | // setup the PWM |
ASaidi | 0:7d2088337345 | 49 | txPin.write(0.0); |
ASaidi | 0:7d2088337345 | 50 | txPin.period_us(freq); |
aleksanb | 3:bc64c2d5bc26 | 51 | |
sigveseb | 2:cc8371213c85 | 52 | int times[len]; |
aleksanb | 3:bc64c2d5bc26 | 53 | for (int i=0; i<len; i++) { |
aleksanb | 3:bc64c2d5bc26 | 54 | times[i] = 0; |
sigveseb | 2:cc8371213c85 | 55 | } |
aleksanb | 3:bc64c2d5bc26 | 56 | |
sigveseb | 2:cc8371213c85 | 57 | int bit = 1; |
sigveseb | 2:cc8371213c85 | 58 | pc.printf("S\n"); |
ASaidi | 0:7d2088337345 | 59 | txPin.write(0.5); |
sigveseb | 2:cc8371213c85 | 60 | timer.start(); |
aleksanb | 3:bc64c2d5bc26 | 61 | |
sigveseb | 2:cc8371213c85 | 62 | int time = timer.read_us(); |
sigveseb | 2:cc8371213c85 | 63 | int old_time = time; |
sigveseb | 2:cc8371213c85 | 64 | int dt = 0; |
aleksanb | 3:bc64c2d5bc26 | 65 | |
aleksanb | 3:bc64c2d5bc26 | 66 | while (pos < len) { |
aleksanb | 3:bc64c2d5bc26 | 67 | // TODO: Handle timer overflow |
aleksanb | 3:bc64c2d5bc26 | 68 | // Consider using GPIOTE |
sigveseb | 2:cc8371213c85 | 69 | old_time = time; |
sigveseb | 2:cc8371213c85 | 70 | time = timer.read_us(); |
aleksanb | 3:bc64c2d5bc26 | 71 | dt += time - old_time; |
aleksanb | 3:bc64c2d5bc26 | 72 | |
aleksanb | 3:bc64c2d5bc26 | 73 | while (dt > data[pos] && pos < len) { |
sigveseb | 2:cc8371213c85 | 74 | dt -= data[pos]; |
sigveseb | 2:cc8371213c85 | 75 | bit = !bit; |
sigveseb | 2:cc8371213c85 | 76 | txPin.write(0.5 * bit); |
sigveseb | 2:cc8371213c85 | 77 | times[pos] = dt; |
sigveseb | 2:cc8371213c85 | 78 | pos++; |
sigveseb | 2:cc8371213c85 | 79 | } |
sigveseb | 2:cc8371213c85 | 80 | } |
aleksanb | 3:bc64c2d5bc26 | 81 | |
sigveseb | 2:cc8371213c85 | 82 | timer.stop(); |
sigveseb | 2:cc8371213c85 | 83 | pc.printf("Success, wrote %i/%i\n", pos, len); |
sigveseb | 2:cc8371213c85 | 84 | pc.printf("Miss timings: [%i", times[0]); |
aleksanb | 3:bc64c2d5bc26 | 85 | for (int i=1; i<len; i++) { |
sigveseb | 2:cc8371213c85 | 86 | pc.printf(", %i", times[i]); |
sigveseb | 2:cc8371213c85 | 87 | } |
aleksanb | 3:bc64c2d5bc26 | 88 | pc.printf("]\n"); |
sigveseb | 2:cc8371213c85 | 89 | txPin.write(0); |
sigveseb | 2:cc8371213c85 | 90 | _inUse = false; |
ASaidi | 0:7d2088337345 | 91 | return true; |
ASaidi | 0:7d2088337345 | 92 | } |