From 618f0b51e8ec800bd66ad86e62726dbe4658f147 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 10 Jan 2024 19:26:00 +0700 Subject: [PATCH] Working IR send / Receive class --- .../lib/ESPMegaPRO/IRBlaster.cpp | 52 ++++++++++--------- .../lib/ESPMegaPRO/IRReceiver.cpp | 4 -- ESPMegaPRO-OS-SDK/src/rmt_demo.cpp | 8 +-- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRBlaster.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRBlaster.cpp index 35848c1..c38bdae 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRBlaster.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRBlaster.cpp @@ -1,39 +1,43 @@ #include +#include void IRBlaster::send(uint16_t *data, size_t size) { - rmt_item32_t *items = new rmt_item32_t[size]; - for (size_t i = 0; i < size; i++) + // 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) { - items[i].duration0 = data[i] * 10; - items[i].level0 = 1; - items[i].duration1 = data[i] * 10; - items[i].level1 = 0; + items[j].duration1 = data[i+1]; + } else { + items[j].duration1 = 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) { - this->channel = channel; - rmt_config_t config; - config.rmt_mode = RMT_MODE_TX; - config.channel = channel; - config.gpio_num = gpio_num_t(pin); - config.mem_block_num = 1; - config.tx_config.loop_en = false; - config.tx_config.carrier_en = false; - 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)); + 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)); } IRBlaster::IRBlaster(uint8_t pin) { - IRBlaster(pin, RMT_CHANNEL_0); + IRBlaster(pin, RMT_CHANNEL_0); } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRReceiver.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRReceiver.cpp index 8d0d688..be1da79 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRReceiver.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/IRReceiver.cpp @@ -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 ir_data_t data; const size_t size = irBufferPtr-1; - Serial.println("Allocating memory"); data.data = (unsigned int*)calloc(size, sizeof(unsigned int)); if (data.data == nullptr) { - Serial.println("Failed to allocate memory"); data.size = 0; 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 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]; } //memcpy(data.data, (const unsigned int*)irBuffer, (size)*sizeof(unsigned int)); diff --git a/ESPMegaPRO-OS-SDK/src/rmt_demo.cpp b/ESPMegaPRO-OS-SDK/src/rmt_demo.cpp index 463f84c..bbea1f0 100644 --- a/ESPMegaPRO-OS-SDK/src/rmt_demo.cpp +++ b/ESPMegaPRO-OS-SDK/src/rmt_demo.cpp @@ -3,13 +3,15 @@ #include 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() { espmega.begin(); 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() {