APIs and example programs related to IR signal transmission, reception, raw waveform recording, and replay of CoreP4X.
3.3.10-aM5CoreP4X0.3.10.3.14.7.1The IR emitter of CoreP4X is driven through a transistor from G49, and the IR receiver output is connected to G13. The IR circuit uses the 3V3_L3B power domain, which is controlled by M5IOE1 IO12 (PYB_L3B_EN); M5Unified sets IO12 high during initialization to enable the shared L3B power.
#include <IRremote.hpp>
#include <M5Unified.h>
static constexpr uint8_t IR_TX_PIN = 49;
static constexpr uint8_t IR_RX_PIN = 13;
M5Canvas canvas(&M5.Display);
uint16_t nextAddress = 0x0000;
uint8_t nextCommand = 0x10;
bool hasSent = false;
uint16_t sentAddress = 0;
uint8_t sentCommand = 0;
bool hasReceived = false;
char rxProtocol[16] = "--";
uint32_t rxAddress = 0;
uint8_t rxCommand = 0;
uint32_t rxRaw = 0;
static void drawPanel(int y, const char* title, uint16_t accent,
const char* line1, const char* line2)
{
constexpr int x = 24;
constexpr int w = 432;
constexpr int h = 148;
canvas.fillRect(x, y, w, h, TFT_WHITE);
canvas.drawRect(x, y, w, h, TFT_DARKGREY);
canvas.fillRect(x, y, w, 8, accent);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString(title, x + w / 2, y + 34);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.drawString(line1, x + w / 2, y + 82);
canvas.drawString(line2, x + w / 2, y + 118);
}
static void drawScreen()
{
char txLine[48];
char rxLine[48];
char rxDetail[48];
if (hasSent) {
snprintf(txLine, sizeof(txLine), "Addr 0x%04lX Cmd 0x%02X",
static_cast<unsigned long>(sentAddress), sentCommand);
} else {
snprintf(txLine, sizeof(txLine), "Ready to send");
}
if (hasReceived) {
snprintf(rxLine, sizeof(rxLine), "%s Addr 0x%04lX Cmd 0x%02X",
rxProtocol, static_cast<unsigned long>(rxAddress), rxCommand);
snprintf(rxDetail, sizeof(rxDetail), "Raw 0x%08lX",
static_cast<unsigned long>(rxRaw));
} else {
snprintf(rxLine, sizeof(rxLine), "No signal received");
snprintf(rxDetail, sizeof(rxDetail), "Waiting for IR");
}
canvas.fillSprite(TFT_WHITE);
canvas.drawRect(8, 8, 464, 464, TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold24pt7b);
canvas.drawString("CoreP4X IR", 240, 36);
canvas.setFont(&fonts::FreeSans9pt7b);
canvas.drawString("Tap screen to send NEC", 240, 80);
drawPanel(104, "SEND", TFT_BLUE, txLine,
hasSent ? "NEC frame sent" : "Tap anywhere");
drawPanel(270, "RECEIVE", TFT_DARKGREEN, rxLine, rxDetail);
canvas.setFont(&fonts::FreeSans9pt7b);
canvas.drawString("Point an IR remote at the receiver at any time", 240, 448);
canvas.pushSprite(0, 0);
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setRotation(0);
M5.Display.setBrightness(160);
canvas.createSprite(M5.Display.width(), M5.Display.height());
IrReceiver.begin(IR_RX_PIN, DISABLE_LED_FEEDBACK);
IrSender.begin(DISABLE_LED_FEEDBACK);
IrSender.setSendPin(IR_TX_PIN);
Serial.printf("IR TX pin: %u, RX pin: %u\n", IR_TX_PIN, IR_RX_PIN);
drawScreen();
}
void loop()
{
M5.update();
if (M5.Touch.getDetail().wasClicked()) {
sentAddress = nextAddress;
sentCommand = nextCommand;
hasSent = true;
Serial.printf("Send NEC: addr=0x%04X, cmd=0x%02X\n",
sentAddress, sentCommand);
IrSender.sendNEC(sentAddress, sentCommand, 0);
++nextAddress;
++nextCommand;
IrReceiver.restartAfterSend();
delay(20);
drawScreen();
}
if (IrReceiver.decode()) {
const auto& data = IrReceiver.decodedIRData;
snprintf(rxProtocol, sizeof(rxProtocol), "%s",
getProtocolString(data.protocol));
rxAddress = data.address;
rxCommand = data.command;
rxRaw = data.decodedRawData;
hasReceived = true;
Serial.printf("Receive: protocol=%s, addr=0x%04lX, cmd=0x%02X, raw=0x%08lX\n",
rxProtocol, static_cast<unsigned long>(rxAddress),
rxCommand, static_cast<unsigned long>(rxRaw));
IrReceiver.resume();
drawScreen();
}
delay(5);
}After tapping the screen, the program sends one NEC frame and resumes reception with IrReceiver.restartAfterSend(). The upper part of the screen shows the address and command of the most recent transmission, and the lower part shows the received protocol, address, command, and raw data; the serial monitor also outputs the transmit and receive results.
This example saves any complete raw mark/space timing frame received by the receiver, and displays the protocol name, bit count, timing entry count, frame duration, and the first few tick values on the screen; then it retransmits with IrSender.sendRaw() using the original timing. Even if the protocol is not in the IRremote decode table (displayed as UNKNOWN), it can still be recorded and replayed.
The receiver output is an envelope signal with the carrier removed, so the original carrier frequency cannot be recovered. Therefore, the example always retransmits at 38 kHz, while the high/low timing comes from the actually received signal.
#define RAW_BUFFER_LENGTH 750
#include <IRremote.hpp>
#include <M5Unified.h>
static constexpr uint8_t IR_TX_PIN = 49;
static constexpr uint8_t IR_RX_PIN = 13;
static constexpr uint8_t IR_FREQUENCY_KHZ = 38;
static constexpr uint32_t REPLAY_DELAY_MS = 1000;
static constexpr uint8_t RAW_HEAD_COUNT = 6;
M5Canvas canvas(&M5.Display);
static uint8_t replayData[RAW_BUFFER_LENGTH];
static uint16_t replayLength = 0;
static bool replayPending = false;
static uint32_t replayAtMs = 0;
static bool hasReceived = false;
static bool hasSent = false;
static char rxProtocol[16] = "--";
static uint16_t rxBits = 0;
static uint32_t rxDurationMs = 0;
static uint8_t rxHead[RAW_HEAD_COUNT] = {};
static uint8_t rxHeadCount = 0;
static void drawPanel(int y, int h, const char* title, uint16_t accent,
const char* line1, const char* line2,
const char* line3 = nullptr)
{
constexpr int x = 24;
constexpr int w = 432;
canvas.fillRect(x, y, w, h, TFT_BLACK);
canvas.drawRect(x, y, w, h, TFT_DARKGREY);
canvas.fillRect(x, y, 6, h, accent);
canvas.setTextDatum(middle_center);
canvas.setTextColor(accent, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString(title, x + w / 2, y + 32);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.drawString(line1, x + w / 2, y + 72);
canvas.drawString(line2, x + w / 2, y + 101);
if (line3) canvas.drawString(line3, x + w / 2, y + 130);
}
static void drawScreen()
{
char rxLine1[64];
char rxLine2[64];
char rxLine3[72];
char txLine[48];
char txDetail[48];
char stateLine[48];
if (hasReceived) {
snprintf(rxLine1, sizeof(rxLine1), "Protocol: %s Bits: %u",
rxProtocol, rxBits);
snprintf(rxLine2, sizeof(rxLine2), "Raw: %u Time: %lu ms",
replayLength, static_cast<unsigned long>(rxDurationMs));
int offset = snprintf(rxLine3, sizeof(rxLine3), "Ticks:");
for (uint8_t i = 0; i < rxHeadCount &&
offset < static_cast<int>(sizeof(rxLine3)); ++i) {
offset += snprintf(rxLine3 + offset, sizeof(rxLine3) - offset,
" %u", rxHead[i]);
}
} else {
snprintf(rxLine1, sizeof(rxLine1), "Waiting for any IR frame");
snprintf(rxLine2, sizeof(rxLine2), "IR signals accepted");
snprintf(rxLine3, sizeof(rxLine3), "Raw timing will be captured");
}
if (replayPending) {
const uint32_t now = millis();
const uint32_t remaining =
replayAtMs > now ? replayAtMs - now : 0;
snprintf(stateLine, sizeof(stateLine), "REPLAY IN %lu.%01lu s",
static_cast<unsigned long>(remaining / 1000),
static_cast<unsigned long>((remaining % 1000) / 100));
snprintf(txLine, sizeof(txLine), "Raw frame queued");
snprintf(txDetail, sizeof(txDetail), "Protocol-independent replay");
} else if (hasSent) {
snprintf(stateLine, sizeof(stateLine), "REPLAY COMPLETE");
snprintf(txLine, sizeof(txLine), "Last frame replayed");
snprintf(txDetail, sizeof(txDetail), "Listening for next signal");
} else {
snprintf(stateLine, sizeof(stateLine), "LISTENING");
snprintf(txLine, sizeof(txLine), "No frame queued");
snprintf(txDetail, sizeof(txDetail), "Receive -> wait 1 s -> send");
}
const uint16_t stateColor =
replayPending ? TFT_CYAN : (hasSent ? TFT_GREEN : TFT_YELLOW);
canvas.fillSprite(TFT_BLACK);
canvas.drawRect(8, 8, 464, 464, TFT_DARKGREY);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold24pt7b);
canvas.drawString("IR REPLAY", 240, 46);
canvas.setTextColor(stateColor, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold12pt7b);
canvas.drawString(stateLine, 240, 94);
drawPanel(126, 176, "CAPTURED", TFT_CYAN,
rxLine1, rxLine2, rxLine3);
drawPanel(318, 124, "NEXT SEND", TFT_DARKGREEN,
txLine, txDetail);
canvas.setTextColor(TFT_DARKGREY, TFT_BLACK);
canvas.setFont(&fonts::FreeSans9pt7b);
canvas.drawString("Received carrier is unavailable; replay uses 38 kHz",
240, 458);
canvas.pushSprite(0, 0);
}
static bool isReplayable(const IRData& data)
{
// Any completed frame is valid as long as the raw buffer did not overflow.
if (data.flags & IRDATA_FLAGS_WAS_OVERFLOW) return false;
return data.rawlen >= 4 && data.rawlen <= RAW_BUFFER_LENGTH;
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setBrightness(160);
canvas.createSprite(M5.Display.width(), M5.Display.height());
IrReceiver.begin(IR_RX_PIN, DISABLE_LED_FEEDBACK);
IrSender.begin(IR_TX_PIN);
Serial.printf("IR replay ready: TX=%u, RX=%u, delay=%lu ms\n",
IR_TX_PIN, IR_RX_PIN,
static_cast<unsigned long>(REPLAY_DELAY_MS));
drawScreen();
}
void loop()
{
M5.update();
if (!replayPending && IrReceiver.decode()) {
const auto& data = IrReceiver.decodedIRData;
if (isReplayable(data)) {
replayLength = data.rawlen - 1;
IrReceiver.compensateAndStoreIRResultInArray(replayData);
snprintf(rxProtocol, sizeof(rxProtocol), "%s",
getProtocolString(data.protocol));
rxBits = data.numberOfBits;
uint32_t totalTicks = 0;
for (uint16_t i = 0; i < replayLength; ++i) {
totalTicks += replayData[i];
}
rxDurationMs = totalTicks * MICROS_PER_TICK / 1000;
rxHeadCount = replayLength < RAW_HEAD_COUNT
? replayLength : RAW_HEAD_COUNT;
memcpy(rxHead, replayData, rxHeadCount);
hasReceived = true;
hasSent = false;
replayPending = true;
replayAtMs = millis() + REPLAY_DELAY_MS;
// Freeze the raw buffer until the captured frame is retransmitted.
IrReceiver.stop();
Serial.printf("Queued: protocol=%s, bits=%u, raw=%u, time=%lu ms, flags=0x%02X\n",
rxProtocol, rxBits, replayLength,
static_cast<unsigned long>(rxDurationMs), data.flags);
drawScreen();
} else {
Serial.printf("Ignored overflow frame: rawlen=%u\n", data.rawlen);
IrReceiver.resume();
}
}
if (replayPending) {
const uint32_t now = millis();
if (static_cast<int32_t>(now - replayAtMs) >= 0) {
IrSender.sendRaw(replayData, replayLength, IR_FREQUENCY_KHZ);
replayPending = false;
hasSent = true;
// Start reception only after transmission is complete.
IrReceiver.start();
Serial.printf("Replayed %u raw timing entries at %u kHz\n",
replayLength, IR_FREQUENCY_KHZ);
drawScreen();
} else {
static uint32_t lastRefresh = 0;
if (now - lastRefresh >= 100) {
lastRefresh = now;
drawScreen();
}
}
}
delay(1);
}After uploading, point any IR remote at the IR receiver of CoreP4X and send a signal once; the screen shows the capture result. After the 1-second countdown, the IR emitter automatically retransmits once with the saved timing.
Serial output example:
Queued: protocol=NEC, bits=32, raw=67, time=68 ms, flags=0x00
Replayed 67 raw timing entries at 38 kHz
Queued: protocol=UNKNOWN, bits=0, raw=53, time=72 ms, flags=0x00
Replayed 53 raw timing entries at 38 kHz
The IR transmit and receive of CoreP4X uses the IRremote library. The examples on this page mainly use the following interfaces:
IrReceiver.decode(): completes the reception of an IR frame and parses the protocol.IrReceiver.compensateAndStoreIRResultInArray(): saves the received mark/space timing.IrSender.sendRaw(): retransmits the IR signal according to the saved timing.IrReceiver.stop() / IrReceiver.start(): pauses reception during the delay to prevent the buffer from being overwritten by the next frame, and resumes after transmission is complete.For more related APIs, refer to the documentation below: