Working IR send / Receive class

This commit is contained in:
Siwat Sirichai 2024-01-10 19:26:00 +07:00
parent 23a2f243c9
commit 618f0b51e8
3 changed files with 33 additions and 31 deletions

View File

@ -1,39 +1,43 @@
#include <IRBlaster.hpp> #include <IRBlaster.hpp>
#include <Arduino.h>
void IRBlaster::send(uint16_t *data, size_t size) void IRBlaster::send(uint16_t *data, size_t size)
{ {
rmt_item32_t *items = new rmt_item32_t[size]; // Send a raw IR signal
for (size_t i = 0; i < size; i++) 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)
{ {
items[i].duration0 = data[i] * 10; items[j].duration1 = data[i+1];
items[i].level0 = 1; } else {
items[i].duration1 = data[i] * 10; items[j].duration1 = 0;
items[i].level1 = 0;
} }
ESP_ERROR_CHECK(rmt_write_items(channel, items, size, true)); }
delete[] items; Serial.println("Sending IR data");
ESP_ERROR_CHECK(rmt_write_items(channel, items, size / 2 + size % 2, true));
delete[] items;
} }
IRBlaster::IRBlaster(uint8_t pin, rmt_channel_t channel) IRBlaster::IRBlaster(uint8_t pin, rmt_channel_t channel)
{ {
this->channel = channel; this->channel = channel;
rmt_config_t config; gpio_num_t gpio = gpio_num_t(pin);
config.rmt_mode = RMT_MODE_TX; rmt_config_t config = RMT_DEFAULT_CONFIG_TX(gpio, channel);
config.channel = channel; config.clk_div = 80;
config.gpio_num = gpio_num_t(pin); config.tx_config.carrier_en = true;
config.mem_block_num = 1; config.tx_config.carrier_freq_hz = 38000;
config.tx_config.loop_en = false; ESP_ERROR_CHECK(rmt_config(&config));
config.tx_config.carrier_en = false; ESP_ERROR_CHECK(rmt_driver_install(channel, 0, 0));
config.tx_config.idle_output_en = true;
config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
config.tx_config.carrier_freq_hz = 38000;
config.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
config.clk_div = 80;
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(channel, 0, 0));
} }
IRBlaster::IRBlaster(uint8_t pin) IRBlaster::IRBlaster(uint8_t pin)
{ {
IRBlaster(pin, RMT_CHANNEL_0); IRBlaster(pin, RMT_CHANNEL_0);
} }

View File

@ -20,17 +20,13 @@ ir_data_t IRReceiver::end_long_receive() {
// The data in the array is the time between each transition, so we need to convert it to the time of each transition // The data in the array is the time between each transition, so we need to convert it to the time of each transition
ir_data_t data; ir_data_t data;
const size_t size = irBufferPtr-1; const size_t size = irBufferPtr-1;
Serial.println("Allocating memory");
data.data = (unsigned int*)calloc(size, sizeof(unsigned int)); data.data = (unsigned int*)calloc(size, sizeof(unsigned int));
if (data.data == nullptr) { if (data.data == nullptr) {
Serial.println("Failed to allocate memory");
data.size = 0; data.size = 0;
return data; return data;
} }
Serial.println("Copying data");
// The data in the array is the time between each transition, so we need to convert it to the time of each transition // The data in the array is the time between each transition, so we need to convert it to the time of each transition
for (size_t i = 1; i <= size; i++) { for (size_t i = 1; i <= size; i++) {
Serial.printf("Copying data at index %d\n", i);
data.data[i-1] = irBuffer[i] - irBuffer[i-1]; data.data[i-1] = irBuffer[i] - irBuffer[i-1];
} }
//memcpy(data.data, (const unsigned int*)irBuffer, (size)*sizeof(unsigned int)); //memcpy(data.data, (const unsigned int*)irBuffer, (size)*sizeof(unsigned int));

View File

@ -3,13 +3,15 @@
#include <IRReceiver.hpp> #include <IRReceiver.hpp>
ESPMegaPRO espmega = ESPMegaPRO(); ESPMegaPRO espmega = ESPMegaPRO();
IRBlaster irBlaster = IRBlaster(14); IRBlaster irBlaster = IRBlaster(4);
uint16_t data[] = { 2441, 579, 621, 580, 620, 580, 620, 580, 621, 579, 621, 579, 1220, 580, 1220, 580, 1247, 554, 619, 580, 621, 580, 619, 581, 619, 26392, 2420, 580, 621, 580, 620, 579, 620, 581, 619, 581, 620, 580, 1219, 581, 1220, 580, 1220, 581, 619, 580, 620, 580, 620, 580, 621, 26385, 2421, 580, 621, 580, 620, 580, 620, 579, 621, 579, 621, 579, 1221, 579, 1221, 580, 1219, 580, 621, 580, 620, 580, 621, 579, 621, 26385, 2421, 580, 621, 579, 620, 580, 620, 580, 621, 579, 621, 579, 1221, 579, 1221, 580, 1219, 580, 621, 580, 620, 580, 620, 580, 621, 26384, 2422, 580, 621, 579, 621, 579, 620, 580, 620, 580, 621, 579, 1221, 579, 1221, 580, 1220, 580, 620, 580, 620, 579, 620, 581, 620};
void setup() { void setup() {
espmega.begin(); espmega.begin();
IRReceiver::begin(15); IRReceiver::begin(15);
uint16_t data[] = {100, 200, 300, 400, 500};
irBlaster.send(data, 5); irBlaster.send(data, sizeof(data)/sizeof(uint16_t));
} }
void loop() { void loop() {