haas-python-irremote 0.0.7
This is irremote driver for HaaS Python. HaaS Python is forked from micropython.
License: Apache License, Version 2.0


## HaaS Python介绍
HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架，兼容MicroPython编程规范，依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力，从而解决了物联网应用开发难的问题。有了Python轻应用框架，物联网编程不再局限于专业软件开发人员，一般的技术员也可以快速实现复杂的物联网需求。
更多HaaS Python介绍和开发资料见[HaaS Python官网](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b16145223.0.0.595660b1dZDX71#/?from=corp),[创意案例](https://haas.iot.aliyun.com/solution?from=crop), [硬件积木](https://haas.iot.aliyun.com/solution/hardware?from=corp)

HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements.
For more HaaS Python introduction and development materials, see [HaaS Python official website](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b16145223.0.0.595660b1dZDX71#/?from=corp), [Creative Case](https://haas.iot.aliyun.com/solution?from=crop), [Hardware Building Blocks](https://haas.iot.aliyun.com/solution/hardware?from=corp)

## 安装方法
* 在[HaaS Python官网](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b50127119.0.0.4e1260b1ebMIf1#/)下载开发板对应的固件，并完成固件烧录

Download the firmware corresponding to the development board at [HaaS Python website](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b50127119.0.0.4e1260b1ebMIf1#/) and complete the firmware burning

* 在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题，欢迎在[github上向我们提交issue](https://github.com/alibaba/AliOS-Things/issues)，我们的工程师会及时解答

Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us on [github](https://github.com/alibaba/AliOS-Things/issues), Our engineers will answer in time
```
import upip
upip.install("haas-python-irremote")
import haas-python-irremote
```
# 红外接收传感器

## 一、产品简介
&emsp;&emsp;
一般家庭环境里面会有很多的遥控器，比如空调、电视机、机顶盒等等，这些都是通过红外遥控器发出的信号控制，红外遥控器发出的就是一连串的二进制脉冲码。红外接收传感器可以将这一连串的二进制脉冲码接收过来传递给MCU解析控制对应的设备。
<br>

<div align="center">
<img src=./../../docs/images/ext_红外接收_定义尺寸.png  width=30%/>
</div>

引脚定义
* -：地<br>
* S：输出信号<br>
* +：DC-3.3V<br>

## 二、技术参数
* 工作电压：2.7-5.5V<br>
* 低功耗：宽角度及长距离接收<br>
* 输入频率：38Khz<br>
* 存储温度：-40°C-125°C<br>
* 抗干扰：抗干扰能力强<br>
* 输出电平：TTL<br>

## 三、软件接口

红外接收传感器HaaS Python驱动：[下载地址](https://github.com/alibaba/AliOS-Things/tree/master/haas_lib_bundles/python/libraries/irremote)
<br>

### IRREMOTE(gpioObj) - 创建红外接收对象
<br>

* 函数原型

> irrecvObj = IRREMOTE(gpioObj)

* 参数说明

参数|类型|必选参数？|说明|
|:-:|:-:|:-:|:-|
|gpioObj|GPIO|是|调用此函数前需确保gpioObj对象已经处于open状态|

* 返回值

> IRREMOTE对象成功，返回IRREMOTE对象；IRREMOTE对象创建失败，抛出Exception

* 示例代码
```python
import irremote
from driver import GPIO

print("Testing IRremote ...")
irrecvDev = GPIO()
irrecvDev.open("irrecv")
irrecvObj = irremote.IRREMOTE(irrecvDev)

```

* 输出
```python
Testing IRremote ...
```
</br>

### getRemoteChar() - 获取遥控器对应的按键状态值
<br>

* 函数功能：

> 获取遥控器对应的按键状态值

* 函数原型：

> IRREMOTE.getRemoteChar()

* 参数说明：
> 无

* 返回值：
> changed，data；
</br>
其中changed为True，说明有数据值更新，False，说明没有数据值更新
</br>
data如下所示：

data返回值|类型|说明|
|:-:|:-:|:-|
|0|string|获取遥控器按键0值|
|1|string|获取遥控器按键1值|
|2|string|获取遥控器按键2值|
|3|string|获取遥控器按键3值|
|4|string|获取遥控器按键4值|
|5|string|获取遥控器按键5值|
|6|string|获取遥控器按键6值|
|7|string|获取遥控器按键7值|
|8|string|获取遥控器按键8值|
|9|string|获取遥控器按键9值|
|PREV|string|获取遥控器按键上一步|
|NEXT|string|获取遥控器按键下一步|
|VOL+|string|获取遥控器按键音量增|
|VOL-|string|获取遥控器按键音量减|
|START/STOP|string|获取遥控器按键启动或暂停|
|100+|string|获取遥控器按键 step100增量值|
|200+|string|获取遥控器按键 step200增量值|

* 示例：

```python
import irremote
from driver import GPIO
import utime

print("Testing IRremote ...")
irrecvDev = GPIO()
irrecvDev.open("irrecv")
irrecvObj = irremote.IRREMOTE(irrecvDev)
while True:             # 无限循环
    changed, data = irrecvObj.getRemoteChar()
    if changed == True and data != None:
        print('char %s'%data)
    utime.sleep(0.2)      # 打印完之后休眠1秒
```

* 输出
```python
Testing IRremote ...
char VOL-
char VOL+
```

</br>

## 四、接口案例

此使用实例在board.json中定义了名为IRREMOTE的GPIO类型的对象。在Python脚本中获取红外遥控器按键状态值并打印在日志中。

* 案例代码
```python
{
  "name": "board-name",
  "version": "1.0.0",
  "io": {
    "irrecv": {
      "type": "GPIO",
      "port": 34,
      "dir": "irq",
      "pull": "pullup",
      "intMode": "both"
    }
  }
}
```

```python
import irremote
from driver import GPIO
import utime

print("Testing IRremote ...")
irrecvDev = GPIO()
irrecvDev.open("irrecv")
irrecvObj = irremote.IRREMOTE(irrecvDev)
while True:             # 无限循环
    changed, data = irrecvObj.getRemoteChar()
    if changed == True and data != None:
        print('char %s'%data)
    utime.sleep(0.2)      # 打印完之后休眠1秒
```

* 输出
```python
Testing IRremote ...
char VOL-
char VOL+
```

## 五、工作原理
### 1、红外收发概述
<div align="center">
<img src=./../../docs/images/ext_红外接收_硬件示意图.png  width=30%/>
</div>

<br>
&emsp;&emsp;
红外遥控器发出的信号是一连串的二进制脉冲码。<br>
&emsp;&emsp;
为了使其在无线传输过程中免受其他红外信号的干扰,通常都是先将其调制在特定的载波频率上, 然后再经红外发射二极管发射出去,而红外线接收装置则要滤除其他杂波,只接收该特定频率的信号并将其还原成二进制脉冲码,也就是解调，接收头的信号输出脚输入到MCU进行解码控制的过程。

<br>

### 光电参数（T=35°C,Vcc=5V,f=38Khz）
<div align="center">
<img src=./../../docs/images/ext_红外接收_光电参数.png  width=50%/>
</div>

### 测试波形
<div align="center">
<img src=./../../docs/images/ext_红外接收_测试波形.png  width=50%/>
</div>

## 参考文献及购买链接
[1] [红外接收传感器](https://item.taobao.com/item.htm?spm=a230r.1.14.170.16d05050X8h59m&id=524001649988&ns=1&abbucket=17#detail)