IRremote
IRremote.cpp@2:4f0d37840b59, 2013-12-23 (annotated)
- Committer:
- leejong87
- Date:
- Mon Dec 23 04:34:48 2013 +0000
- Revision:
- 2:4f0d37840b59
- Parent:
- 1:e12749a24efd
- Child:
- 3:1ec19fef8a30
IRremote 38Khz CustomCode 32bit DataCode 32bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leejong87 | 0:fe20e27c6295 | 1 | #include "IRremote.h" |
leejong87 | 0:fe20e27c6295 | 2 | #include "mbed.h" |
leejong87 | 0:fe20e27c6295 | 3 | |
leejong87 | 0:fe20e27c6295 | 4 | int IR_cnt, IR_state, IR_bit_cnt, IR_temp; |
leejong87 | 0:fe20e27c6295 | 5 | char IR_buf[4]; |
leejong87 | 2:4f0d37840b59 | 6 | //IR Timer |
leejong87 | 0:fe20e27c6295 | 7 | void IR_check() |
leejong87 | 0:fe20e27c6295 | 8 | { |
leejong87 | 0:fe20e27c6295 | 9 | if(IR_cnt<150) { |
leejong87 | 0:fe20e27c6295 | 10 | IR_cnt++; |
leejong87 | 0:fe20e27c6295 | 11 | } |
leejong87 | 0:fe20e27c6295 | 12 | if(IR_cnt>=150) { |
leejong87 | 0:fe20e27c6295 | 13 | IR_state=1; |
leejong87 | 0:fe20e27c6295 | 14 | } |
leejong87 | 0:fe20e27c6295 | 15 | } |
leejong87 | 0:fe20e27c6295 | 16 | |
leejong87 | 2:4f0d37840b59 | 17 | //IR Receive |
leejong87 | 0:fe20e27c6295 | 18 | void IR_start() |
leejong87 | 0:fe20e27c6295 | 19 | { |
leejong87 | 0:fe20e27c6295 | 20 | switch(IR_state) { |
leejong87 | 0:fe20e27c6295 | 21 | case 1: |
leejong87 | 0:fe20e27c6295 | 22 | if((IR_cnt>85)&&(IR_cnt<140)) { |
leejong87 | 0:fe20e27c6295 | 23 | IR_state=2; // Lead |
leejong87 | 0:fe20e27c6295 | 24 | IR_bit_cnt=IR_temp=0; |
leejong87 | 0:fe20e27c6295 | 25 | } |
leejong87 | 0:fe20e27c6295 | 26 | break; |
leejong87 | 0:fe20e27c6295 | 27 | case 2: |
leejong87 | 0:fe20e27c6295 | 28 | if(IR_cnt> 25) { |
leejong87 | 0:fe20e27c6295 | 29 | IR_state=1; // Error |
leejong87 | 0:fe20e27c6295 | 30 | break; |
leejong87 | 0:fe20e27c6295 | 31 | } else if(IR_cnt>=15) { |
leejong87 | 0:fe20e27c6295 | 32 | IR_temp|=0x80; // Data 1 : 2.250ms(22)(0.56ms+1.69ms ) / 2ms |
leejong87 | 0:fe20e27c6295 | 33 | } else if(IR_cnt>= 8) { |
leejong87 | 0:fe20e27c6295 | 34 | IR_temp|=0x00; // Data 0 : 1.125ms(12)(0.56ms+0.565ms) / 1ms |
leejong87 | 0:fe20e27c6295 | 35 | } else { |
leejong87 | 0:fe20e27c6295 | 36 | IR_state=1; // Error |
leejong87 | 0:fe20e27c6295 | 37 | break; |
leejong87 | 0:fe20e27c6295 | 38 | } |
leejong87 | 0:fe20e27c6295 | 39 | if((++IR_bit_cnt%8)==0) { |
leejong87 | 0:fe20e27c6295 | 40 | IR_buf[(IR_bit_cnt/8)-1]=IR_temp; |
leejong87 | 0:fe20e27c6295 | 41 | IR_temp=0; |
leejong87 | 0:fe20e27c6295 | 42 | if(IR_bit_cnt>=32) { |
leejong87 | 0:fe20e27c6295 | 43 | IR_state=1; |
leejong87 | 0:fe20e27c6295 | 44 | IR_bit_cnt=0; |
leejong87 | 0:fe20e27c6295 | 45 | } |
leejong87 | 0:fe20e27c6295 | 46 | } |
leejong87 | 0:fe20e27c6295 | 47 | IR_temp>>=1; |
leejong87 | 0:fe20e27c6295 | 48 | break; |
leejong87 | 0:fe20e27c6295 | 49 | } |
leejong87 | 0:fe20e27c6295 | 50 | IR_cnt=0; |
leejong87 | 0:fe20e27c6295 | 51 | } |
leejong87 | 0:fe20e27c6295 | 52 | |
leejong87 | 0:fe20e27c6295 | 53 | IRremote::IRremote(PinName pin) : _pin(pin) |
leejong87 | 0:fe20e27c6295 | 54 | { |
leejong87 | 0:fe20e27c6295 | 55 | _pin.mode(PullUp); |
leejong87 | 0:fe20e27c6295 | 56 | _pin.fall(&IR_start); |
leejong87 | 0:fe20e27c6295 | 57 | IR_timer.attach_us(&IR_check, 100.0); |
leejong87 | 0:fe20e27c6295 | 58 | } |
leejong87 | 0:fe20e27c6295 | 59 | |
leejong87 | 0:fe20e27c6295 | 60 | char IRremote::read(int ir_i) |
leejong87 | 0:fe20e27c6295 | 61 | { |
leejong87 | 0:fe20e27c6295 | 62 | char IR_rx; |
leejong87 | 0:fe20e27c6295 | 63 | if(ir_i<4) { |
leejong87 | 0:fe20e27c6295 | 64 | IR_rx=IR_buf[ir_i]; |
leejong87 | 1:e12749a24efd | 65 | IR_buf[ir_i]=0xFF; //init |
leejong87 | 0:fe20e27c6295 | 66 | } else { |
leejong87 | 0:fe20e27c6295 | 67 | IR_rx=0xFF;; |
leejong87 | 0:fe20e27c6295 | 68 | } |
leejong87 | 0:fe20e27c6295 | 69 | return IR_rx; |
leejong87 | 0:fe20e27c6295 | 70 | } |