Fork of TxIR

Fork of TxIR by Nordic Pucks

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TxIR.cpp Source File

TxIR.cpp

00001 
00002 /******************************************************************************
00003  * Low level infrared transmission
00004  * This library provides a low level interface to send IR commands. in principle
00005  * other higher level IR type remotes could be build on top of this. I wrote
00006  * this code to send IR commands to a Nikon camera, which it works rather well
00007  * for.
00008  *
00009  * Copyright (C) 2010 Ali Saidi
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029 
00030 #include "mbed.h"
00031 #include "TxIR.hpp"
00032 
00033 Serial pc(USBTX, USBRX);
00034 
00035 Timer timer;
00036 
00037 bool
00038 TxIR::txSeq(unsigned freq, unsigned len, const unsigned *data)
00039 {
00040     // @todo a lock or semaphore should be used here
00041     if (inUse()) {
00042         return false;
00043     }
00044     _inUse = true;
00045 
00046     // keep a copy of the data, so it can't change
00047     int pos = 0;
00048     // setup the PWM
00049     txPin.write(0.0);
00050     txPin.period_us(freq);
00051 
00052     int times[len];
00053     for (int i=0; i<len; i++) {
00054         times[i] = 0;
00055     }
00056 
00057     int bit = 1;
00058     pc.printf("S\n");
00059     txPin.write(0.5);
00060     timer.start();
00061 
00062     int time = timer.read_us();
00063     int old_time = time;
00064     int dt = 0;
00065 
00066     while (pos < len) {
00067         // TODO: Handle timer overflow
00068         // Consider using GPIOTE
00069         old_time = time;
00070         time = timer.read_us();
00071         dt += time - old_time;
00072 
00073         while (dt > data[pos] && pos < len) {
00074             dt -= data[pos];
00075             bit = !bit;
00076             txPin.write(0.5 * bit);
00077             times[pos] = dt;
00078             pos++;
00079         }
00080     }
00081 
00082     timer.stop();
00083     pc.printf("Success, wrote %i/%i\n", pos, len);
00084     pc.printf("Miss timings: [%i", times[0]);
00085     for (int i=1; i<len; i++) {
00086         pc.printf(", %i", times[i]);
00087     }
00088     pc.printf("]\n");
00089     txPin.write(0);
00090     _inUse = false;
00091     return true;
00092 }