Lib for sending IR signals from mbed.

Dependents:   ir-puck ir-puck2 ir-puck

Committer:
cristea
Date:
Tue Aug 05 08:31:41 2014 +0000
Revision:
2:4576fcd93ffd
Parent:
0:741de6c95ae9
Add license to the project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cristea 2:4576fcd93ffd 1 /**
cristea 2:4576fcd93ffd 2 * Low level infrared transmission
cristea 2:4576fcd93ffd 3 * This library provides a low level interface to send IR commands. in principle
cristea 2:4576fcd93ffd 4 * other higher level IR type remotes could be build on top of this. I wrote
cristea 2:4576fcd93ffd 5 * this code to send IR commands to a Nikon camera, which it works rather well
cristea 2:4576fcd93ffd 6 * for.
cristea 2:4576fcd93ffd 7 *
cristea 2:4576fcd93ffd 8 * Copyright (C) 2010 Ali Saidi
cristea 2:4576fcd93ffd 9 *
cristea 2:4576fcd93ffd 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
cristea 2:4576fcd93ffd 11 * of this software and associated documentation files (the "Software"), to deal
cristea 2:4576fcd93ffd 12 * in the Software without restriction, including without limitation the rights
cristea 2:4576fcd93ffd 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cristea 2:4576fcd93ffd 14 * copies of the Software, and to permit persons to whom the Software is
cristea 2:4576fcd93ffd 15 * furnished to do so, subject to the following conditions:
cristea 2:4576fcd93ffd 16 *
cristea 2:4576fcd93ffd 17 * The above copyright notice and this permission notice shall be included in
cristea 2:4576fcd93ffd 18 * all copies or substantial portions of the Software.
cristea 2:4576fcd93ffd 19 *
cristea 2:4576fcd93ffd 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cristea 2:4576fcd93ffd 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cristea 2:4576fcd93ffd 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cristea 2:4576fcd93ffd 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cristea 2:4576fcd93ffd 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cristea 2:4576fcd93ffd 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cristea 2:4576fcd93ffd 26 * THE SOFTWARE.
cristea 2:4576fcd93ffd 27 */
cristea 2:4576fcd93ffd 28
cristea 2:4576fcd93ffd 29 /**
cristea 2:4576fcd93ffd 30 * Copyright 2014 Nordic Semiconductor
cristea 2:4576fcd93ffd 31 *
cristea 2:4576fcd93ffd 32 * Licensed under the Apache License, Version 2.0 (the "License");
cristea 2:4576fcd93ffd 33 * you may not use this file except in compliance with the License.
cristea 2:4576fcd93ffd 34 * You may obtain a copy of the License at
cristea 2:4576fcd93ffd 35 *
cristea 2:4576fcd93ffd 36 * http://www.apache.org/licenses/LICENSE-2.0
cristea 2:4576fcd93ffd 37 *
cristea 2:4576fcd93ffd 38 * Unless required by applicable law or agreed to in writing, software
cristea 2:4576fcd93ffd 39 * distributed under the License is distributed on an "AS IS" BASIS,
cristea 2:4576fcd93ffd 40 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cristea 2:4576fcd93ffd 41 * See the License for the specific language governing permissions and
cristea 2:4576fcd93ffd 42 * limitations under the License
cristea 2:4576fcd93ffd 43 */
cristea 2:4576fcd93ffd 44
cristea 0:741de6c95ae9 45
cristea 0:741de6c95ae9 46 #include "mbed.h"
cristea 0:741de6c95ae9 47 #include "IRSender.h"
cristea 0:741de6c95ae9 48
cristea 0:741de6c95ae9 49 bool IRSender::irSeq(unsigned freq, unsigned len, const unsigned *data)
cristea 0:741de6c95ae9 50 {
cristea 0:741de6c95ae9 51 // TODO: implement a lock or semaphore here
cristea 0:741de6c95ae9 52 if (_inUse) {
cristea 0:741de6c95ae9 53 return false;
cristea 0:741de6c95ae9 54 }
cristea 0:741de6c95ae9 55 _inUse = true;
cristea 0:741de6c95ae9 56
cristea 0:741de6c95ae9 57 int pos = 0;
cristea 0:741de6c95ae9 58 senderPin.write(0.0);
cristea 0:741de6c95ae9 59 senderPin.period_us(freq);
cristea 0:741de6c95ae9 60
cristea 0:741de6c95ae9 61 int times[len];
cristea 0:741de6c95ae9 62 for (int i=0; i<len; i++) {
cristea 0:741de6c95ae9 63 times[i] = 0;
cristea 0:741de6c95ae9 64 }
cristea 0:741de6c95ae9 65
cristea 0:741de6c95ae9 66 int bit = 1;
cristea 0:741de6c95ae9 67 LOG("Start\n");
cristea 0:741de6c95ae9 68 senderPin.write(0.5);
cristea 0:741de6c95ae9 69 timer.start();
cristea 0:741de6c95ae9 70
cristea 0:741de6c95ae9 71 int time = timer.read_us();
cristea 0:741de6c95ae9 72 int old_time = time;
cristea 0:741de6c95ae9 73 int dt = 0;
cristea 0:741de6c95ae9 74
cristea 0:741de6c95ae9 75 while (pos < len) {
cristea 0:741de6c95ae9 76 // TODO: Handle timer overflow
cristea 0:741de6c95ae9 77 // Consider using GPIOTE
cristea 0:741de6c95ae9 78 old_time = time;
cristea 0:741de6c95ae9 79 time = timer.read_us();
cristea 0:741de6c95ae9 80 dt += time - old_time;
cristea 0:741de6c95ae9 81
cristea 0:741de6c95ae9 82 while (dt > data[pos] && pos < len) {
cristea 0:741de6c95ae9 83 dt -= data[pos];
cristea 0:741de6c95ae9 84 bit = !bit;
cristea 0:741de6c95ae9 85 senderPin.write(0.5 * bit);
cristea 0:741de6c95ae9 86 times[pos] = dt;
cristea 0:741de6c95ae9 87 pos++;
cristea 0:741de6c95ae9 88 }
cristea 0:741de6c95ae9 89 }
cristea 0:741de6c95ae9 90
cristea 0:741de6c95ae9 91 timer.stop();
cristea 0:741de6c95ae9 92 senderPin.write(0);
cristea 0:741de6c95ae9 93 LOG("Success, wrote %i/%i\n", pos, len);
cristea 0:741de6c95ae9 94 LOG("Miss timings: %i", times[0]);
cristea 0:741de6c95ae9 95 for (int i=1; i<len; i++) {
cristea 0:741de6c95ae9 96 APPEND(", %i", times[i]);
cristea 0:741de6c95ae9 97 }
cristea 0:741de6c95ae9 98 APPEND("]\n");
cristea 0:741de6c95ae9 99 _inUse = false;
cristea 0:741de6c95ae9 100 return true;
cristea 0:741de6c95ae9 101 }