ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRBlaster.cpp

43 lines
1.3 KiB
C++
Raw Normal View History

2024-01-10 10:10:51 +00:00
#include <IRBlaster.hpp>
2024-01-10 12:26:00 +00:00
#include <Arduino.h>
2024-01-10 10:10:51 +00:00
void IRBlaster::send(uint16_t *data, size_t size)
{
2024-01-10 12:26:00 +00:00
// Send a raw IR signal
rmt_item32_t *items = new rmt_item32_t[size / 2 + size % 2];
Serial.println("Converting IR data");
// data is in microseconds, we need to convert it to ticks
// If the number of elements is odd, we need to add a 0 at the end
for (size_t i = 0, j = 0; i < size; i += 2, j++)
{
items[j].level0 = 1;
items[j].duration0 = data[i];
items[j].level1 = 0;
if (i + 1 < size)
2024-01-10 10:10:51 +00:00
{
2024-01-10 12:26:00 +00:00
items[j].duration1 = data[i+1];
} else {
items[j].duration1 = 0;
2024-01-10 10:10:51 +00:00
}
2024-01-10 12:26:00 +00:00
}
Serial.println("Sending IR data");
ESP_ERROR_CHECK(rmt_write_items(channel, items, size / 2 + size % 2, true));
delete[] items;
2024-01-10 10:10:51 +00:00
}
IRBlaster::IRBlaster(uint8_t pin, rmt_channel_t channel)
{
2024-01-10 12:26:00 +00:00
this->channel = channel;
gpio_num_t gpio = gpio_num_t(pin);
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(gpio, channel);
config.clk_div = 80;
config.tx_config.carrier_en = true;
config.tx_config.carrier_freq_hz = 38000;
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(channel, 0, 0));
2024-01-10 10:10:51 +00:00
}
IRBlaster::IRBlaster(uint8_t pin)
{
2024-01-10 12:26:00 +00:00
IRBlaster(pin, RMT_CHANNEL_0);
2024-01-10 10:10:51 +00:00
}