Fork of TxIR
Fork of TxIR by
TxIR.cpp@2:cc8371213c85, 2014-06-26 (annotated)
- Committer:
- sigveseb
- Date:
- Thu Jun 26 14:18:26 2014 +0000
- Revision:
- 2:cc8371213c85
- Parent:
- 0:7d2088337345
- Child:
- 3:bc64c2d5bc26
change timing strategy
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 |
ASaidi | 0:7d2088337345 | 7 | * for. |
ASaidi | 0:7d2088337345 | 8 | * |
ASaidi | 0:7d2088337345 | 9 | * Copyright (C) 2010 Ali Saidi |
ASaidi | 0:7d2088337345 | 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: |
ASaidi | 0:7d2088337345 | 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. |
ASaidi | 0:7d2088337345 | 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 | */ |
sigveseb | 2:cc8371213c85 | 29 | #include "mbed.h" |
ASaidi | 0:7d2088337345 | 30 | #include "TxIR.hpp" |
ASaidi | 0:7d2088337345 | 31 | |
sigveseb | 2:cc8371213c85 | 32 | bool shouldFlip; |
sigveseb | 2:cc8371213c85 | 33 | |
sigveseb | 2:cc8371213c85 | 34 | Serial pc(USBTX, USBRX); |
sigveseb | 2:cc8371213c85 | 35 | |
sigveseb | 2:cc8371213c85 | 36 | Timer timer; |
ASaidi | 0:7d2088337345 | 37 | |
ASaidi | 0:7d2088337345 | 38 | void |
ASaidi | 0:7d2088337345 | 39 | TxIR::doAction() |
ASaidi | 0:7d2088337345 | 40 | { |
ASaidi | 0:7d2088337345 | 41 | if (pos >= len) { |
ASaidi | 0:7d2088337345 | 42 | return; |
ASaidi | 0:7d2088337345 | 43 | } |
sigveseb | 2:cc8371213c85 | 44 | pc.printf("doAction, pos: %i\n", pos); |
sigveseb | 2:cc8371213c85 | 45 | shouldFlip = true; |
sigveseb | 2:cc8371213c85 | 46 | delay.attach_us(this, &TxIR::doAction, data[pos++] * 200); |
ASaidi | 0:7d2088337345 | 47 | } |
ASaidi | 0:7d2088337345 | 48 | |
ASaidi | 0:7d2088337345 | 49 | bool |
sigveseb | 2:cc8371213c85 | 50 | TxIR::txSeq(unsigned freq, unsigned len, const unsigned *data) |
ASaidi | 0:7d2088337345 | 51 | { |
sigveseb | 2:cc8371213c85 | 52 | |
ASaidi | 0:7d2088337345 | 53 | // @todo a lock or semaphore should be used here |
ASaidi | 0:7d2088337345 | 54 | if (inUse()) |
ASaidi | 0:7d2088337345 | 55 | return false; |
ASaidi | 0:7d2088337345 | 56 | _inUse = true; |
ASaidi | 0:7d2088337345 | 57 | |
ASaidi | 0:7d2088337345 | 58 | // keep a copy of the data, so it can't change |
ASaidi | 0:7d2088337345 | 59 | pos = 0; |
ASaidi | 0:7d2088337345 | 60 | // setup the PWM |
ASaidi | 0:7d2088337345 | 61 | txPin.write(0.0); |
ASaidi | 0:7d2088337345 | 62 | txPin.period_us(freq); |
ASaidi | 0:7d2088337345 | 63 | |
ASaidi | 0:7d2088337345 | 64 | // Get the interrupt ready |
ASaidi | 0:7d2088337345 | 65 | delay.detach(); |
sigveseb | 2:cc8371213c85 | 66 | |
sigveseb | 2:cc8371213c85 | 67 | int times[len]; |
sigveseb | 2:cc8371213c85 | 68 | for(int i=0;i<len;i++){ |
sigveseb | 2:cc8371213c85 | 69 | times[i] = 0; |
sigveseb | 2:cc8371213c85 | 70 | } |
ASaidi | 0:7d2088337345 | 71 | |
sigveseb | 2:cc8371213c85 | 72 | int bit = 1; |
sigveseb | 2:cc8371213c85 | 73 | pc.printf("S\n"); |
ASaidi | 0:7d2088337345 | 74 | txPin.write(0.5); |
sigveseb | 2:cc8371213c85 | 75 | timer.start(); |
sigveseb | 2:cc8371213c85 | 76 | int time = timer.read_us(); |
sigveseb | 2:cc8371213c85 | 77 | int old_time = time; |
sigveseb | 2:cc8371213c85 | 78 | int dt = 0; |
sigveseb | 2:cc8371213c85 | 79 | while(pos < len) { |
sigveseb | 2:cc8371213c85 | 80 | old_time = time; |
sigveseb | 2:cc8371213c85 | 81 | time = timer.read_us(); |
sigveseb | 2:cc8371213c85 | 82 | dt += time - old_time; |
sigveseb | 2:cc8371213c85 | 83 | while(dt > data[pos]) { |
sigveseb | 2:cc8371213c85 | 84 | dt -= data[pos]; |
sigveseb | 2:cc8371213c85 | 85 | bit = !bit; |
sigveseb | 2:cc8371213c85 | 86 | txPin.write(0.5 * bit); |
sigveseb | 2:cc8371213c85 | 87 | times[pos] = dt; |
sigveseb | 2:cc8371213c85 | 88 | pos++; |
sigveseb | 2:cc8371213c85 | 89 | } |
sigveseb | 2:cc8371213c85 | 90 | } |
sigveseb | 2:cc8371213c85 | 91 | timer.stop(); |
sigveseb | 2:cc8371213c85 | 92 | pc.printf("Success, wrote %i/%i\n", pos, len); |
sigveseb | 2:cc8371213c85 | 93 | pc.printf("Miss timings: [%i", times[0]); |
sigveseb | 2:cc8371213c85 | 94 | for(int i=1;i<len;i++){ |
sigveseb | 2:cc8371213c85 | 95 | pc.printf(", %i", times[i]); |
sigveseb | 2:cc8371213c85 | 96 | } |
sigveseb | 2:cc8371213c85 | 97 | txPin.write(0); |
sigveseb | 2:cc8371213c85 | 98 | _inUse = false; |
sigveseb | 2:cc8371213c85 | 99 | delay.detach(); |
ASaidi | 0:7d2088337345 | 100 | return true; |
ASaidi | 0:7d2088337345 | 101 | } |