First Commit IR remote class This class constructer needs (PwmOutPin,DigitalInPin,data array pointer,data array length,Serial instance pointer)
IRremote.cpp@0:fc05497b2a23, 2014-04-06 (annotated)
- Committer:
- lelect
- Date:
- Sun Apr 06 09:12:22 2014 +0000
- Revision:
- 0:fc05497b2a23
IRremote class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lelect | 0:fc05497b2a23 | 1 | #include "mbed.h" |
lelect | 0:fc05497b2a23 | 2 | #include "IRremote.h" |
lelect | 0:fc05497b2a23 | 3 | #include "debug.h" |
lelect | 0:fc05497b2a23 | 4 | IRremote::IRremote(PinName trans,PinName receive,int* point,int length,Serial* device) |
lelect | 0:fc05497b2a23 | 5 | :_trans(trans) |
lelect | 0:fc05497b2a23 | 6 | ,_receive(receive) |
lelect | 0:fc05497b2a23 | 7 | { |
lelect | 0:fc05497b2a23 | 8 | _device=device; |
lelect | 0:fc05497b2a23 | 9 | _point=point; |
lelect | 0:fc05497b2a23 | 10 | _length=length; |
lelect | 0:fc05497b2a23 | 11 | _trans.period_us(26); |
lelect | 0:fc05497b2a23 | 12 | _trans.pulsewidth_us(13); |
lelect | 0:fc05497b2a23 | 13 | } |
lelect | 0:fc05497b2a23 | 14 | void IRremote::setArray(int* point,int length) |
lelect | 0:fc05497b2a23 | 15 | { |
lelect | 0:fc05497b2a23 | 16 | _point=point; |
lelect | 0:fc05497b2a23 | 17 | _length=length; |
lelect | 0:fc05497b2a23 | 18 | } |
lelect | 0:fc05497b2a23 | 19 | void IRremote::showArray() |
lelect | 0:fc05497b2a23 | 20 | { |
lelect | 0:fc05497b2a23 | 21 | for(int i=0; i<_length; i++) { |
lelect | 0:fc05497b2a23 | 22 | if(_point[i]==0)break; |
lelect | 0:fc05497b2a23 | 23 | log_info("%03d:%d\r\n",i,_point[i]); |
lelect | 0:fc05497b2a23 | 24 | } |
lelect | 0:fc05497b2a23 | 25 | } |
lelect | 0:fc05497b2a23 | 26 | int IRremote::sum(void) |
lelect | 0:fc05497b2a23 | 27 | { |
lelect | 0:fc05497b2a23 | 28 | if(_length!=0) { |
lelect | 0:fc05497b2a23 | 29 | if(_sum==0) { |
lelect | 0:fc05497b2a23 | 30 | for(int i=0; i<_length; i++) { |
lelect | 0:fc05497b2a23 | 31 | _sum += _point[i]; |
lelect | 0:fc05497b2a23 | 32 | } |
lelect | 0:fc05497b2a23 | 33 | } |
lelect | 0:fc05497b2a23 | 34 | return _sum; |
lelect | 0:fc05497b2a23 | 35 | } else { |
lelect | 0:fc05497b2a23 | 36 | return -1; |
lelect | 0:fc05497b2a23 | 37 | } |
lelect | 0:fc05497b2a23 | 38 | } |
lelect | 0:fc05497b2a23 | 39 | float IRremote::average(void) |
lelect | 0:fc05497b2a23 | 40 | { |
lelect | 0:fc05497b2a23 | 41 | if(_length!=0) { |
lelect | 0:fc05497b2a23 | 42 | if(_sum==0) { |
lelect | 0:fc05497b2a23 | 43 | for(int i=0; i<_length; i++) { |
lelect | 0:fc05497b2a23 | 44 | _sum += _point[i]; |
lelect | 0:fc05497b2a23 | 45 | } |
lelect | 0:fc05497b2a23 | 46 | } |
lelect | 0:fc05497b2a23 | 47 | if(_ave==0.0) { |
lelect | 0:fc05497b2a23 | 48 | _ave=(float)_sum/(float)_length; |
lelect | 0:fc05497b2a23 | 49 | } |
lelect | 0:fc05497b2a23 | 50 | return _ave; |
lelect | 0:fc05497b2a23 | 51 | } else { |
lelect | 0:fc05497b2a23 | 52 | return -1.0; |
lelect | 0:fc05497b2a23 | 53 | } |
lelect | 0:fc05497b2a23 | 54 | } |
lelect | 0:fc05497b2a23 | 55 | void IRremote::trans(void (*afunc)(void),void (*bfunc)(void)) |
lelect | 0:fc05497b2a23 | 56 | { |
lelect | 0:fc05497b2a23 | 57 | afunc(); |
lelect | 0:fc05497b2a23 | 58 | _timer.start(); |
lelect | 0:fc05497b2a23 | 59 | _trans = 0; |
lelect | 0:fc05497b2a23 | 60 | if (_point[0] == 0) { |
lelect | 0:fc05497b2a23 | 61 | _timer.stop(); |
lelect | 0:fc05497b2a23 | 62 | _trans=0; |
lelect | 0:fc05497b2a23 | 63 | return; |
lelect | 0:fc05497b2a23 | 64 | } |
lelect | 0:fc05497b2a23 | 65 | for (int i = 0; i < _length; i++) { |
lelect | 0:fc05497b2a23 | 66 | if (_point[i] == 0) { |
lelect | 0:fc05497b2a23 | 67 | _timer.stop(); |
lelect | 0:fc05497b2a23 | 68 | _trans=0.0f; |
lelect | 0:fc05497b2a23 | 69 | bfunc(); |
lelect | 0:fc05497b2a23 | 70 | break; |
lelect | 0:fc05497b2a23 | 71 | } |
lelect | 0:fc05497b2a23 | 72 | if (i&1) { //odd |
lelect | 0:fc05497b2a23 | 73 | _timer.reset(); |
lelect | 0:fc05497b2a23 | 74 | while (_timer.read_us() <= _point[i]) { |
lelect | 0:fc05497b2a23 | 75 | _trans=0.0f; |
lelect | 0:fc05497b2a23 | 76 | } |
lelect | 0:fc05497b2a23 | 77 | } else { //even |
lelect | 0:fc05497b2a23 | 78 | _timer.reset(); |
lelect | 0:fc05497b2a23 | 79 | while (_timer.read_us() <= _point[i]) { |
lelect | 0:fc05497b2a23 | 80 | _trans=0.5f; |
lelect | 0:fc05497b2a23 | 81 | } |
lelect | 0:fc05497b2a23 | 82 | } |
lelect | 0:fc05497b2a23 | 83 | } |
lelect | 0:fc05497b2a23 | 84 | _timer.stop(); |
lelect | 0:fc05497b2a23 | 85 | _trans=0; |
lelect | 0:fc05497b2a23 | 86 | } |
lelect | 0:fc05497b2a23 | 87 | void IRremote::receive(void (*afunc)(void),void (*bfunc)(void)) |
lelect | 0:fc05497b2a23 | 88 | { |
lelect | 0:fc05497b2a23 | 89 | afunc(); |
lelect | 0:fc05497b2a23 | 90 | for (int i = 0; i < _length; i++) { |
lelect | 0:fc05497b2a23 | 91 | _point[i] = 0; |
lelect | 0:fc05497b2a23 | 92 | } |
lelect | 0:fc05497b2a23 | 93 | int lastStatus=0; |
lelect | 0:fc05497b2a23 | 94 | log_info("Common!\r\n"); |
lelect | 0:fc05497b2a23 | 95 | _timer.start(); |
lelect | 0:fc05497b2a23 | 96 | _timer.reset(); |
lelect | 0:fc05497b2a23 | 97 | while (_receive) { |
lelect | 0:fc05497b2a23 | 98 | if (_timer.read_us()>=5000000) { |
lelect | 0:fc05497b2a23 | 99 | _timer.stop(); |
lelect | 0:fc05497b2a23 | 100 | log_info("TimeOut..\r\n"); |
lelect | 0:fc05497b2a23 | 101 | return; |
lelect | 0:fc05497b2a23 | 102 | } |
lelect | 0:fc05497b2a23 | 103 | } |
lelect | 0:fc05497b2a23 | 104 | _timer.reset(); |
lelect | 0:fc05497b2a23 | 105 | for (int i =0; i < _length; i++) { |
lelect | 0:fc05497b2a23 | 106 | while (lastStatus == _receive) { |
lelect | 0:fc05497b2a23 | 107 | if (_timer.read_us()>=1000000) { |
lelect | 0:fc05497b2a23 | 108 | log_info("less than %d..Done!\r\n",_length); |
lelect | 0:fc05497b2a23 | 109 | _timer.stop(); |
lelect | 0:fc05497b2a23 | 110 | bfunc(); |
lelect | 0:fc05497b2a23 | 111 | return; |
lelect | 0:fc05497b2a23 | 112 | } |
lelect | 0:fc05497b2a23 | 113 | } |
lelect | 0:fc05497b2a23 | 114 | _point[i] = _timer.read_us(); |
lelect | 0:fc05497b2a23 | 115 | lastStatus = !lastStatus; |
lelect | 0:fc05497b2a23 | 116 | _timer.reset(); |
lelect | 0:fc05497b2a23 | 117 | } |
lelect | 0:fc05497b2a23 | 118 | log_info("over %d...\r\n",_length); |
lelect | 0:fc05497b2a23 | 119 | } |
lelect | 0:fc05497b2a23 | 120 | |
lelect | 0:fc05497b2a23 | 121 | void IRremote::init(void) |
lelect | 0:fc05497b2a23 | 122 | { |
lelect | 0:fc05497b2a23 | 123 | _length=0; |
lelect | 0:fc05497b2a23 | 124 | } |