This is a port of the Adafruit Trinket (Arduino) universal Infrared receiver. The should produce a unique code for every key on virtually any Infrared Remote control
main.cpp@0:0124991faa9d, 2015-05-19 (annotated)
- Committer:
- djbottrill
- Date:
- Tue May 19 09:43:35 2015 +0000
- Revision:
- 0:0124991faa9d
It works!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
djbottrill | 0:0124991faa9d | 1 | |
djbottrill | 0:0124991faa9d | 2 | /* Trinket/Gemma compatible Raw IR decoder sketch |
djbottrill | 0:0124991faa9d | 3 | This sketch/program uses an Adafruit Trinket or Gemma |
djbottrill | 0:0124991faa9d | 4 | ATTiny85 based mini microcontroller and a PNA4602 to |
djbottrill | 0:0124991faa9d | 5 | decode IR received. This can be used to make a IR receiver |
djbottrill | 0:0124991faa9d | 6 | (by looking for a particular code) or transmitter |
djbottrill | 0:0124991faa9d | 7 | (by pulsing an IR LED at ~38KHz for the durations detected |
djbottrill | 0:0124991faa9d | 8 | |
djbottrill | 0:0124991faa9d | 9 | Based on Adafruit tutorial http://learn.adafruit.com/ir-sensor/using-an-ir-sensor |
djbottrill | 0:0124991faa9d | 10 | |
djbottrill | 0:0124991faa9d | 11 | and ATTiny program by TinyPCRemote Nathan Chantrell http://nathan.chantrell.net |
djbottrill | 0:0124991faa9d | 12 | under Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license |
djbottrill | 0:0124991faa9d | 13 | |
djbottrill | 0:0124991faa9d | 14 | Modified to work on MBed by David Bottrill May 2015 |
djbottrill | 0:0124991faa9d | 15 | This has been tested on a KL25z using a Grove IR sensor with a number of different |
djbottrill | 0:0124991faa9d | 16 | Infrared Remote controls. |
djbottrill | 0:0124991faa9d | 17 | |
djbottrill | 0:0124991faa9d | 18 | */ |
djbottrill | 0:0124991faa9d | 19 | #include "mbed.h" |
djbottrill | 0:0124991faa9d | 20 | |
djbottrill | 0:0124991faa9d | 21 | #define MAXPULSE 10000 // the maximum pulse we'll listen for - 10 milliseconds |
djbottrill | 0:0124991faa9d | 22 | #define NUMPULSES 200 // max IR pulse pairs to sample |
djbottrill | 0:0124991faa9d | 23 | #define RESOLUTION 1 // time between IR measurements ~1uS |
djbottrill | 0:0124991faa9d | 24 | |
djbottrill | 0:0124991faa9d | 25 | // we will store up to 200 pulse pairs (this is -a lot-) |
djbottrill | 0:0124991faa9d | 26 | uint16_t pulses[200][2]; // pair is high and low pulse |
djbottrill | 0:0124991faa9d | 27 | uint16_t currentpulse = 0; // index for pulses we're storing |
djbottrill | 0:0124991faa9d | 28 | uint32_t irCode = 0; |
djbottrill | 0:0124991faa9d | 29 | Serial pc(USBTX, USBRX); // tx, rx |
djbottrill | 0:0124991faa9d | 30 | DigitalIn IRpin (D2); // Listen to IR receiver on Trinket/Gemma pin D2 |
djbottrill | 0:0124991faa9d | 31 | |
djbottrill | 0:0124991faa9d | 32 | int listenForIR(void); // IR receive code |
djbottrill | 0:0124991faa9d | 33 | void printcode(void); // Print IR code in HEX |
djbottrill | 0:0124991faa9d | 34 | |
djbottrill | 0:0124991faa9d | 35 | int main(void) |
djbottrill | 0:0124991faa9d | 36 | { |
djbottrill | 0:0124991faa9d | 37 | pc.printf("Ready to decode IR!\n"); |
djbottrill | 0:0124991faa9d | 38 | |
djbottrill | 0:0124991faa9d | 39 | while (1) { |
djbottrill | 0:0124991faa9d | 40 | |
djbottrill | 0:0124991faa9d | 41 | int numpulse=listenForIR(); // Wait for an IR Code |
djbottrill | 0:0124991faa9d | 42 | |
djbottrill | 0:0124991faa9d | 43 | // Process the pulses to get a single number representing code |
djbottrill | 0:0124991faa9d | 44 | for (int i = 0; i < 32; i++) { |
djbottrill | 0:0124991faa9d | 45 | irCode=irCode<<1; |
djbottrill | 0:0124991faa9d | 46 | if((pulses[i][0] * RESOLUTION)>0&&(pulses[i][0] * RESOLUTION)<500) { |
djbottrill | 0:0124991faa9d | 47 | irCode|=0; |
djbottrill | 0:0124991faa9d | 48 | } else { |
djbottrill | 0:0124991faa9d | 49 | irCode|=1; |
djbottrill | 0:0124991faa9d | 50 | } |
djbottrill | 0:0124991faa9d | 51 | } |
djbottrill | 0:0124991faa9d | 52 | |
djbottrill | 0:0124991faa9d | 53 | printcode(); // Print IR code |
djbottrill | 0:0124991faa9d | 54 | wait_ms(100); |
djbottrill | 0:0124991faa9d | 55 | |
djbottrill | 0:0124991faa9d | 56 | } //end of main loop |
djbottrill | 0:0124991faa9d | 57 | } |
djbottrill | 0:0124991faa9d | 58 | |
djbottrill | 0:0124991faa9d | 59 | |
djbottrill | 0:0124991faa9d | 60 | int listenForIR(void){ // IR receive code |
djbottrill | 0:0124991faa9d | 61 | currentpulse = 0; |
djbottrill | 0:0124991faa9d | 62 | while (1) { |
djbottrill | 0:0124991faa9d | 63 | unsigned int highpulse, lowpulse; // temporary storage timing |
djbottrill | 0:0124991faa9d | 64 | highpulse = lowpulse = 0; // start out with no pulse length |
djbottrill | 0:0124991faa9d | 65 | |
djbottrill | 0:0124991faa9d | 66 | while (IRpin==1) { // got a high pulse |
djbottrill | 0:0124991faa9d | 67 | highpulse++; |
djbottrill | 0:0124991faa9d | 68 | wait_us(RESOLUTION); |
djbottrill | 0:0124991faa9d | 69 | if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) { |
djbottrill | 0:0124991faa9d | 70 | return currentpulse; |
djbottrill | 0:0124991faa9d | 71 | } |
djbottrill | 0:0124991faa9d | 72 | } |
djbottrill | 0:0124991faa9d | 73 | pulses[currentpulse][0] = highpulse; |
djbottrill | 0:0124991faa9d | 74 | |
djbottrill | 0:0124991faa9d | 75 | while (IRpin==0) { // got a low pulse |
djbottrill | 0:0124991faa9d | 76 | lowpulse++; |
djbottrill | 0:0124991faa9d | 77 | wait_us(RESOLUTION); |
djbottrill | 0:0124991faa9d | 78 | if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) { |
djbottrill | 0:0124991faa9d | 79 | return currentpulse; |
djbottrill | 0:0124991faa9d | 80 | } |
djbottrill | 0:0124991faa9d | 81 | } |
djbottrill | 0:0124991faa9d | 82 | pulses[currentpulse][1] = lowpulse; |
djbottrill | 0:0124991faa9d | 83 | currentpulse++; |
djbottrill | 0:0124991faa9d | 84 | } |
djbottrill | 0:0124991faa9d | 85 | } |
djbottrill | 0:0124991faa9d | 86 | |
djbottrill | 0:0124991faa9d | 87 | void printcode(void) |
djbottrill | 0:0124991faa9d | 88 | { |
djbottrill | 0:0124991faa9d | 89 | uint16_t half; |
djbottrill | 0:0124991faa9d | 90 | half=irCode>>16; // Get first 16 bits of code |
djbottrill | 0:0124991faa9d | 91 | pc.printf("0x%x",half); // Print upper 16 bits in hex |
djbottrill | 0:0124991faa9d | 92 | pc.printf("%x\n",(irCode & 0xFFFF)); // print lower 16 bits in hex |
djbottrill | 0:0124991faa9d | 93 | } |
djbottrill | 0:0124991faa9d | 94 |