IRremoteIDF
Infrared send & receive for ESP-IDF
Loading...
Searching...
No Matches
IRplatform.h
Go to the documentation of this file.
1// Copyright 2025 IRremoteIDF contributors
2//
3// ESP-IDF platform layer for the IR library.
4// Everything the protocol code needs from the underlying framework lives here,
5// so the ~120 protocol implementations stay framework agnostic.
6
7#ifndef IRPLATFORM_H_
8#define IRPLATFORM_H_
9
10#define __STDC_LIMIT_MACROS
11#include <stdint.h>
12#include <stddef.h>
13#include <string>
14#include <type_traits>
15#ifdef UNIT_TEST
16#include <iostream> // Debug output goes to cout when off-target.
17#endif // UNIT_TEST
18
21typedef std::string String;
22
23// The library uses Arduino's F()/PROGMEM flash-string markers in a few places.
24// ESP-IDF maps constants into flash transparently, so they are no-ops.
25#ifndef F
26#define F(x) x
27#endif // F
28#ifndef PROGMEM
29#define PROGMEM
30#endif // PROGMEM
31#ifndef FPSTR
32#define FPSTR(x) x
33#endif // FPSTR
34
35#ifndef UNIT_TEST
36
37#include "sdkconfig.h"
38
40const uint8_t kIrLow = 0;
41const uint8_t kIrHigh = 1;
42#ifndef LOW
43#define LOW kIrLow
44#endif // LOW
45#ifndef HIGH
46#define HIGH kIrHigh
47#endif // HIGH
48
50uint32_t irMillis(void);
52uint32_t irMicros(void);
54void irDelayMs(uint32_t msec);
56void irDelayUs(uint32_t usec);
58void irYield(void);
59
62bool irGpioOutput(uint16_t pin);
65bool irGpioInput(uint16_t pin, bool pullup);
67void irGpioWrite(uint16_t pin, uint8_t level);
69uint8_t irGpioRead(uint16_t pin);
70
72void irRestart(void);
73
74#ifdef CONFIG_IRREMOTE_TX_HW_CARRIER
75// Optional LEDC-generated carrier. The CPU then only has to time the
76// mark/space boundaries instead of every single carrier pulse.
77
80int8_t irCarrierAttach(uint16_t pin, bool inverted);
82void irCarrierDetach(int8_t channel, uint16_t pin, bool inverted);
84void irCarrierConfig(int8_t channel, uint32_t freq, uint8_t duty);
86void irCarrierOn(int8_t channel);
88void irCarrierOff(int8_t channel);
89#endif // CONFIG_IRREMOTE_TX_HW_CARRIER
90
91#endif // UNIT_TEST
92
93// Debug output. Enable with CONFIG_IRREMOTE_DEBUG or by defining DEBUG
94// (e.g. -DDEBUG=1). IRconfig.h maps the former onto the latter, so it has to
95// be seen before the test below.
96#include "IRconfig.h"
97#ifdef DEBUG
98#ifdef UNIT_TEST
99#define DPRINT(x) do { \
100 std::cout << x; \
101 } while (0)
102#define DPRINTLN(x) do { \
103 std::cout << x << std::endl; \
104 } while (0)
105#else // UNIT_TEST
106void irDebugPrint(const char *str);
107void irDebugPrint(const String &str);
108void irDebugPrint(char c);
109void irDebugPrint(uint64_t value);
110void irDebugPrint(int64_t value);
113template <typename T>
114inline typename std::enable_if<std::is_integral<T>::value>::type
115irDebugPrint(T value) {
116 if (std::is_signed<T>::value)
117 irDebugPrint(static_cast<int64_t>(value));
118 else
119 irDebugPrint(static_cast<uint64_t>(value));
120}
121#define DPRINT(x) do { \
122 irDebugPrint(x); \
123 } while (0)
124#define DPRINTLN(x) do { \
125 irDebugPrint(x); \
126 irDebugPrint("\n"); \
127 } while (0)
128#endif // UNIT_TEST
129#else // DEBUG
130#define DPRINT(x)
131#define DPRINTLN(x)
132#endif // DEBUG
133
134#endif // IRPLATFORM_H_
void irRestart(void)
Reboot the chip. Used when a buffer allocation fails.
Definition IRplatform.cpp:93
bool irGpioInput(uint16_t pin, bool pullup)
Configure a GPIO as an input, optionally with the internal pull-up.
Definition IRplatform.cpp:71
void irYield(void)
Feed the task watchdog without giving up the CPU for long.
Definition IRplatform.cpp:55
void irCarrierOff(int8_t channel)
Stop emitting the carrier, leaving the pin at its idle level.
Definition IRcarrier.cpp:178
void irCarrierOn(int8_t channel)
Start emitting the carrier.
Definition IRcarrier.cpp:171
void irCarrierDetach(int8_t channel, uint16_t pin, bool inverted)
Release a previously claimed channel.
Definition IRcarrier.cpp:129
uint32_t irMicros(void)
Microseconds since boot. Wraps every ~71.6 minutes.
Definition IRplatform.cpp:23
uint8_t irGpioRead(uint16_t pin)
Read the level of a GPIO.
Definition IRplatform.cpp:89
int8_t irCarrierAttach(uint16_t pin, bool inverted)
Claim an LEDC channel and bind it to pin.
Definition IRcarrier.cpp:74
void irGpioWrite(uint16_t pin, uint8_t level)
Set the level of an output GPIO.
Definition IRplatform.cpp:85
void irDelayUs(uint32_t usec)
Busy-wait. Accurate, but blocks the core it runs on.
Definition IRplatform.cpp:31
void irDelayMs(uint32_t msec)
Sleep, yielding the CPU to other FreeRTOS tasks.
Definition IRplatform.cpp:35
const uint8_t kIrLow
Logical pin levels.
Definition IRplatform.h:40
uint32_t irMillis(void)
Milliseconds since boot. Wraps every ~49.7 days, like Arduino's millis().
Definition IRplatform.cpp:27
void irDebugPrint(const char *str)
Definition IRplatform.cpp:96
const uint8_t kIrHigh
Definition IRplatform.h:41
void irCarrierConfig(int8_t channel, uint32_t freq, uint8_t duty)
Set the carrier frequency (Hz) and duty cycle (percent).
Definition IRcarrier.cpp:147
bool irGpioOutput(uint16_t pin)
Configure a GPIO as a push-pull output.
Definition IRplatform.cpp:57
std::string String
The protocol code builds human readable output with String. On ESP-IDF that is just std::string.
Definition IRplatform.h:21