Lib for sending IR signals from mbed.
Dependents: ir-puck ir-puck2 ir-puck
IRSender.cpp@0:741de6c95ae9, 2014-07-25 (annotated)
- Committer:
- cristea
- Date:
- Fri Jul 25 07:43:47 2014 +0000
- Revision:
- 0:741de6c95ae9
- Child:
- 2:4576fcd93ffd
Initial commit of cleaned up IR sending lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cristea | 0:741de6c95ae9 | 1 | |
cristea | 0:741de6c95ae9 | 2 | #include "mbed.h" |
cristea | 0:741de6c95ae9 | 3 | #include "IRSender.h" |
cristea | 0:741de6c95ae9 | 4 | |
cristea | 0:741de6c95ae9 | 5 | bool IRSender::irSeq(unsigned freq, unsigned len, const unsigned *data) |
cristea | 0:741de6c95ae9 | 6 | { |
cristea | 0:741de6c95ae9 | 7 | // TODO: implement a lock or semaphore here |
cristea | 0:741de6c95ae9 | 8 | if (_inUse) { |
cristea | 0:741de6c95ae9 | 9 | return false; |
cristea | 0:741de6c95ae9 | 10 | } |
cristea | 0:741de6c95ae9 | 11 | _inUse = true; |
cristea | 0:741de6c95ae9 | 12 | |
cristea | 0:741de6c95ae9 | 13 | int pos = 0; |
cristea | 0:741de6c95ae9 | 14 | senderPin.write(0.0); |
cristea | 0:741de6c95ae9 | 15 | senderPin.period_us(freq); |
cristea | 0:741de6c95ae9 | 16 | |
cristea | 0:741de6c95ae9 | 17 | int times[len]; |
cristea | 0:741de6c95ae9 | 18 | for (int i=0; i<len; i++) { |
cristea | 0:741de6c95ae9 | 19 | times[i] = 0; |
cristea | 0:741de6c95ae9 | 20 | } |
cristea | 0:741de6c95ae9 | 21 | |
cristea | 0:741de6c95ae9 | 22 | int bit = 1; |
cristea | 0:741de6c95ae9 | 23 | LOG("Start\n"); |
cristea | 0:741de6c95ae9 | 24 | senderPin.write(0.5); |
cristea | 0:741de6c95ae9 | 25 | timer.start(); |
cristea | 0:741de6c95ae9 | 26 | |
cristea | 0:741de6c95ae9 | 27 | int time = timer.read_us(); |
cristea | 0:741de6c95ae9 | 28 | int old_time = time; |
cristea | 0:741de6c95ae9 | 29 | int dt = 0; |
cristea | 0:741de6c95ae9 | 30 | |
cristea | 0:741de6c95ae9 | 31 | while (pos < len) { |
cristea | 0:741de6c95ae9 | 32 | // TODO: Handle timer overflow |
cristea | 0:741de6c95ae9 | 33 | // Consider using GPIOTE |
cristea | 0:741de6c95ae9 | 34 | old_time = time; |
cristea | 0:741de6c95ae9 | 35 | time = timer.read_us(); |
cristea | 0:741de6c95ae9 | 36 | dt += time - old_time; |
cristea | 0:741de6c95ae9 | 37 | |
cristea | 0:741de6c95ae9 | 38 | while (dt > data[pos] && pos < len) { |
cristea | 0:741de6c95ae9 | 39 | dt -= data[pos]; |
cristea | 0:741de6c95ae9 | 40 | bit = !bit; |
cristea | 0:741de6c95ae9 | 41 | senderPin.write(0.5 * bit); |
cristea | 0:741de6c95ae9 | 42 | times[pos] = dt; |
cristea | 0:741de6c95ae9 | 43 | pos++; |
cristea | 0:741de6c95ae9 | 44 | } |
cristea | 0:741de6c95ae9 | 45 | } |
cristea | 0:741de6c95ae9 | 46 | |
cristea | 0:741de6c95ae9 | 47 | timer.stop(); |
cristea | 0:741de6c95ae9 | 48 | senderPin.write(0); |
cristea | 0:741de6c95ae9 | 49 | LOG("Success, wrote %i/%i\n", pos, len); |
cristea | 0:741de6c95ae9 | 50 | LOG("Miss timings: %i", times[0]); |
cristea | 0:741de6c95ae9 | 51 | for (int i=1; i<len; i++) { |
cristea | 0:741de6c95ae9 | 52 | APPEND(", %i", times[i]); |
cristea | 0:741de6c95ae9 | 53 | } |
cristea | 0:741de6c95ae9 | 54 | APPEND("]\n"); |
cristea | 0:741de6c95ae9 | 55 | _inUse = false; |
cristea | 0:741de6c95ae9 | 56 | return true; |
cristea | 0:741de6c95ae9 | 57 | } |