
Arduino Quick Start
Chain Captain infrared transmission and reception related APIs and sample programs.
3.3.10-aM5ChainCaptain0.3.00.3.01.0.94.7.1The Chain Captain IR power is controlled by M5IOE1 IO2, which must be set to a high level before use.
#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);
}Press KEYA to send one NEC frame. The receiver is then re-enabled to decode the signal from the transmitter itself (near-field reflection/coupling). The transmission information is shown at the top of the screen, and the decoded result is shown below.
Chain Captain IR transmission and reception uses the IRremote library. For more related APIs, refer to the following document: