-
Notifications
You must be signed in to change notification settings - Fork 105
/
NecIR.cpp
131 lines (122 loc) · 3.94 KB
/
NecIR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "pxt.h"
//% color=50 weight=80
//% icon="\uf1eb"
namespace maqueenIRV2 {
int ir_code = 0x00;
int ir_addr = 0x00;
int data;
int logic_value(){//判断逻辑值"0"和"1"子函数
uint32_t lasttime = system_timer_current_time_us();
uint32_t nowtime;
while(!uBit.io.P16.getDigitalValue());//低等待
nowtime = system_timer_current_time_us();
if((nowtime - lasttime) > 400 && (nowtime - lasttime) < 700){//低电平560us
while(uBit.io.P16.getDigitalValue());//是高就等待
lasttime = system_timer_current_time_us();
if((lasttime - nowtime)>400 && (lasttime - nowtime) < 700){//接着高电平560us
return 0;
}else if((lasttime - nowtime)>1500 && (lasttime - nowtime) < 1800){//接着高电平1.7ms
return 1;
}
}
uBit.serial.printf("error\r\n");
return -1;
}
void pulse_deal(){
int i;
ir_addr=0x00;//清零
for(i=0; i<16;i++ )
{
if(logic_value() == 1)
{
ir_addr |=(1<<i);
}
}
//解析遥控器编码中的command指令
ir_code=0x00;//清零
for(i=0; i<16;i++ )
{
if(logic_value() == 1)
{
ir_code |=(1<<i);
}
}
}
void remote_decode(void){
data = 0x00;
uint32_t lasttime = system_timer_current_time_us();
uint32_t nowtime;
while(uBit.io.P16.getDigitalValue()){//高电平等待
nowtime = system_timer_current_time_us();
if((nowtime - lasttime) > 100000){//超过100 ms,表明此时没有按键按下
ir_code = 0xff00;
return;
}
}
//如果高电平持续时间不超过100ms
lasttime = system_timer_current_time_us();
while(!uBit.io.P16.getDigitalValue());//低等待
nowtime = system_timer_current_time_us();
if((nowtime - lasttime) < 10000 && (nowtime - lasttime) > 8000){//9ms
while(uBit.io.P16.getDigitalValue());//高等待
lasttime = system_timer_current_time_us();
if((lasttime - nowtime) > 4000 && (lasttime - nowtime) < 5000){//4.5ms,接收到了红外协议头且是新发送的数据。开始解析逻辑0和1
pulse_deal();
//uBit.serial.printf("addr=0x%X,code = 0x%X\r\n",ir_addr,ir_code);
data = ir_code;
return;//ir_code;
}else if((lasttime - nowtime) > 2000 && (lasttime - nowtime) < 2500){//2.25ms,表示发的跟上一个包一致
while(!uBit.io.P16.getDigitalValue());//低等待
nowtime = system_timer_current_time_us();
if((nowtime - lasttime) > 500 && (nowtime - lasttime) < 700){//560us
//uBit.serial.printf("addr=0x%X,code = 0x%X\r\n",ir_addr,ir_code);
data = ir_code;
return;//ir_code;
}
}
}
}
//%
int irCode(){
remote_decode();
return data;
}
//%
int readPulseIn(int status){
uint32_t lasttime,nowtime,temp;
if(status == 1){//HIGH
lasttime = system_timer_current_time_us();
while(!uBit.io.P2.getDigitalValue()){
temp = system_timer_current_time_us();
if((temp - lasttime) > 70000){
//uBit.serial.printf("time out 0 %d\r\n",(temp-lasttime));
return -1;
}
}
lasttime = system_timer_current_time_us();
while(uBit.io.P2.getDigitalValue()){
if((system_timer_current_time_us() - lasttime) > 70000){
//uBit.serial.printf("time out 1");
return -1;
}
}
nowtime = system_timer_current_time_us();
}else{//LOW
while(uBit.io.P2.getDigitalValue()){
if((system_timer_current_time_us() - lasttime) > 70000){
//uBit.serial.printf("time out 3");
return -1;
}
}
lasttime = system_timer_current_time_us();
while(!uBit.io.P2.getDigitalValue()){
if((system_timer_current_time_us() - lasttime) > 70000){
//uBit.serial.printf("time out 4");
return -1;
}
}
nowtime = system_timer_current_time_us();
}
return (nowtime - lasttime);
}
}