
Arduino 上手教程
Chain Captain 红外发送与接收相关 API 与案例程序。
3.3.10-aM5ChainCaptain0.3.00.3.01.0.94.7.1Chain Captain 红外电源由 M5IOE1 IO2 控制,使用前必须将其配置为高电平。
#include <IRremote.hpp>
#include <M5Unified.h>
#include <M5IOE1.h>
#define IR_SEND_PIN 48
#define IR_RECEIVE_PIN 47
uint16_t address = 0x0000;
uint8_t command = 0x10;
uint8_t repeats = 0;
M5IOE1 ioExpander;
void setup()
{
delay(500);
M5.begin();
Serial.begin(115200);
delay(200);
M5.Display.setTextColor(TFT_BLACK);
M5.Display.setTextFont(&fonts::FreeMonoBoldOblique9pt7b);
M5.Display.clear(TFT_WHITE);
M5.Display.setCursor(0, 0);
M5.Display.printf("Chain Captain IR");
Serial.println("Chain Captain IR TX/RX");
if (ioExpander.begin(&M5.In_I2C, M5IOE1_DEFAULT_ADDR_2,
M5IOE1_I2C_FREQ_100K) != M5IOE1_OK) {
Serial.println("M5IOE1 init failed");
while (true) {
delay(1000);
}
}
// IO2 powers the IR transmitter and receiver.
ioExpander.pinMode(M5IOE1_PIN_2, OUTPUT);
ioExpander.digitalWrite(M5IOE1_PIN_2, HIGH);
IrReceiver.begin(IR_RECEIVE_PIN);
IrSender.begin(DISABLE_LED_FEEDBACK);
IrSender.setSendPin(IR_SEND_PIN);
M5.Display.setCursor(0, 20);
M5.Display.println("A: send & receive");
Serial.printf("IR Send Pin: %d, IR Recv Pin: %d\n", IR_SEND_PIN, IR_RECEIVE_PIN);
delay(500);
}
void loop()
{
M5.update();
if (!M5.BtnA.wasClicked()) {
delay(10);
return;
}
// 1. Send one NEC frame
Serial.printf("Send NEC: addr=0x%04x, cmd=0x%02x\n", address, command);
IrSender.sendNEC(address, command, repeats);
M5.Display.fillRect(0, 20, 240, 70, TFT_WHITE);
M5.Display.setCursor(0, 24);
M5.Display.printf("Send NEC:\n addr=0x%04x\n cmd=0x%02x\n", address, command);
M5.Display.drawFastHLine(0, 86, 240, TFT_BLACK);
IrReceiver.restartAfterSend();
// 2. Decode the reflected frame.
delay(20);
if (IrReceiver.decode()) {
Serial.printf("Received: protocol=%s, addr=0x%04x, cmd=0x%02x, raw=0x%08lx\n",
getProtocolString(IrReceiver.decodedIRData.protocol),
IrReceiver.decodedIRData.address,
IrReceiver.decodedIRData.command,
(unsigned long)IrReceiver.decodedIRData.decodedRawData);
M5.Display.fillRect(0, 92, 240, 148, TFT_WHITE);
M5.Display.setCursor(0, 96);
M5.Display.printf("Received:\n protocol=%s\n addr=0x%04x\n cmd=0x%02x\n raw=0x%08lx\n",
getProtocolString(IrReceiver.decodedIRData.protocol),
IrReceiver.decodedIRData.address,
IrReceiver.decodedIRData.command,
(unsigned long)IrReceiver.decodedIRData.decodedRawData);
IrReceiver.resume();
} else {
Serial.println("No IR received.");
M5.Display.fillRect(0, 92, 240, 148, TFT_WHITE);
M5.Display.setCursor(0, 96);
M5.Display.println("No IR received.");
}
address += 0x0001;
command += 0x01;
repeats = 0;
delay(200);
}按动 KEYA 发送一次 NEC 编码,随后重新开启接收头,解码自身发出的信号(近距离反射/耦合)。屏幕上方显示发送信息,下方显示解析结果。
Chain Captain 红外收发使用 IRremote 库,更多相关 API 可以参考下方文档: