赤外線リモコンの受信をパルス幅にて行います。 Displays the pulse width of the IR control.
Dependents: IRLED_SendReceveDemo IRreceiver_PulseWidth
IRRcevPulseWidth.cpp@3:b2a1f396ee27, 2017-02-16 (annotated)
- Committer:
- nameless129
- Date:
- Thu Feb 16 02:07:26 2017 +0000
- Revision:
- 3:b2a1f396ee27
- Parent:
- 2:674f9ea95a5a
input Pin:enable pullup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nameless129 | 0:612202986301 | 1 | #include "mbed.h" |
nameless129 | 0:612202986301 | 2 | #include "IRRcevPulseWidth.h" |
nameless129 | 0:612202986301 | 3 | |
nameless129 | 0:612202986301 | 4 | IRRcevPulseWidth::IRRcevPulseWidth(PinName InputPin) : _inputPin(InputPin) |
nameless129 | 0:612202986301 | 5 | { |
nameless129 | 0:612202986301 | 6 | fRcevStart = 0; |
nameless129 | 0:612202986301 | 7 | cIRRcev = 0; |
nameless129 | 0:612202986301 | 8 | cIRRceved = 0; |
nameless129 | 0:612202986301 | 9 | IRRcevState_prev = 1; |
nameless129 | 2:674f9ea95a5a | 10 | filterN = 0; |
nameless129 | 3:b2a1f396ee27 | 11 | _inputPin.mode(PullUp); |
nameless129 | 0:612202986301 | 12 | } |
nameless129 | 0:612202986301 | 13 | |
nameless129 | 0:612202986301 | 14 | void IRRcevPulseWidth::init(uint16_t *dataPtr,uint16_t dataLim,uint32_t timeout_us) |
nameless129 | 0:612202986301 | 15 | { |
nameless129 | 0:612202986301 | 16 | IRRcevTimeOut = timeout_us; |
nameless129 | 0:612202986301 | 17 | limitUpper_dataN = dataLim; |
nameless129 | 0:612202986301 | 18 | p_data = dataPtr; |
nameless129 | 0:612202986301 | 19 | _timer.start(); |
nameless129 | 0:612202986301 | 20 | IRRcevMicroSec_prev = _timer.read_us(); |
nameless129 | 0:612202986301 | 21 | } |
nameless129 | 2:674f9ea95a5a | 22 | |
nameless129 | 2:674f9ea95a5a | 23 | void IRRcevPulseWidth::setPulseNumFilter(uint16_t n) |
nameless129 | 2:674f9ea95a5a | 24 | { |
nameless129 | 2:674f9ea95a5a | 25 | filterN = n; |
nameless129 | 2:674f9ea95a5a | 26 | } |
nameless129 | 2:674f9ea95a5a | 27 | |
nameless129 | 0:612202986301 | 28 | uint16_t IRRcevPulseWidth::getData_N(void) |
nameless129 | 0:612202986301 | 29 | { |
nameless129 | 0:612202986301 | 30 | return cIRRceved; |
nameless129 | 0:612202986301 | 31 | } |
nameless129 | 0:612202986301 | 32 | |
nameless129 | 0:612202986301 | 33 | int8_t IRRcevPulseWidth::status(void) |
nameless129 | 0:612202986301 | 34 | { |
nameless129 | 0:612202986301 | 35 | int8_t ret=0; |
nameless129 | 0:612202986301 | 36 | IRRcevMicroSec = _timer.read_us(); |
nameless129 | 0:612202986301 | 37 | IRRcevMicroSec_diff = IRRcevMicroSec - IRRcevMicroSec_prev; |
nameless129 | 0:612202986301 | 38 | |
nameless129 | 0:612202986301 | 39 | IRRcevState = _inputPin; |
nameless129 | 0:612202986301 | 40 | if( IRRcevState != IRRcevState_prev ) |
nameless129 | 0:612202986301 | 41 | { |
nameless129 | 0:612202986301 | 42 | if(fRcevStart == 0) |
nameless129 | 0:612202986301 | 43 | { |
nameless129 | 0:612202986301 | 44 | fRcevStart = 1; |
nameless129 | 0:612202986301 | 45 | } |
nameless129 | 0:612202986301 | 46 | else |
nameless129 | 0:612202986301 | 47 | { |
nameless129 | 0:612202986301 | 48 | *(p_data+cIRRcev) = IRRcevMicroSec_diff; |
nameless129 | 0:612202986301 | 49 | cIRRcev++; |
nameless129 | 0:612202986301 | 50 | if(cIRRcev >= limitUpper_dataN) |
nameless129 | 0:612202986301 | 51 | { |
nameless129 | 0:612202986301 | 52 | cIRRcev = 0; |
nameless129 | 0:612202986301 | 53 | ret = -1; |
nameless129 | 0:612202986301 | 54 | } |
nameless129 | 0:612202986301 | 55 | } |
nameless129 | 0:612202986301 | 56 | IRRcevState_prev = IRRcevState; |
nameless129 | 0:612202986301 | 57 | IRRcevMicroSec_prev = IRRcevMicroSec; |
nameless129 | 0:612202986301 | 58 | } |
nameless129 | 0:612202986301 | 59 | else if( (IRRcevMicroSec_diff >= IRRcevTimeOut) && (cIRRcev != 0) ) |
nameless129 | 0:612202986301 | 60 | { |
nameless129 | 0:612202986301 | 61 | #if IR_RCEV_DEBUGOUT_ENABLE |
nameless129 | 0:612202986301 | 62 | DBG("Rceved\r\n"); |
nameless129 | 0:612202986301 | 63 | DBG("RcevCnt:%d\r\n",cIRRcev); |
nameless129 | 0:612202986301 | 64 | for(i=0;i<cIRRcev;i++) |
nameless129 | 0:612202986301 | 65 | { |
nameless129 | 0:612202986301 | 66 | DBG("%d,",*(p_data+i)); |
nameless129 | 0:612202986301 | 67 | } |
nameless129 | 0:612202986301 | 68 | DBG("\r\n"); |
nameless129 | 0:612202986301 | 69 | #endif |
nameless129 | 2:674f9ea95a5a | 70 | if(cIRRcev >= filterN) |
nameless129 | 2:674f9ea95a5a | 71 | { |
nameless129 | 2:674f9ea95a5a | 72 | cIRRceved = cIRRcev; |
nameless129 | 2:674f9ea95a5a | 73 | cIRRcev = 0; |
nameless129 | 2:674f9ea95a5a | 74 | fRcevStart = 0; |
nameless129 | 2:674f9ea95a5a | 75 | ret = 1; |
nameless129 | 2:674f9ea95a5a | 76 | } |
nameless129 | 2:674f9ea95a5a | 77 | else |
nameless129 | 2:674f9ea95a5a | 78 | { |
nameless129 | 2:674f9ea95a5a | 79 | cIRRcev = 0; |
nameless129 | 2:674f9ea95a5a | 80 | fRcevStart = 0; |
nameless129 | 2:674f9ea95a5a | 81 | ret = 0; |
nameless129 | 2:674f9ea95a5a | 82 | } |
nameless129 | 0:612202986301 | 83 | } |
nameless129 | 0:612202986301 | 84 | return ret; |
nameless129 | 0:612202986301 | 85 | } |