migrate IRRemote to RMT

This commit is contained in:
Siwat Sirichai 2023-12-30 15:39:16 +07:00
parent 5fd8bdf73c
commit d0e4825c2d
8 changed files with 77 additions and 17 deletions

View File

@ -1,9 +1,8 @@
#include <ClimateCard.hpp> #include <ClimateCard.hpp>
ClimateCard::ClimateCard(uint8_t ir_pin) : irsender(ir_pin) ClimateCard::ClimateCard(uint8_t ir_pin)
{ {
this->ir_pin = ir_pin; this->ir_pin = ir_pin;
irsender.begin(ir_pin);
// Initialize Pointers // Initialize Pointers
this->dht = nullptr; this->dht = nullptr;
this->ds18b20 = nullptr; this->ds18b20 = nullptr;
@ -20,12 +19,19 @@ ClimateCard::ClimateCard(uint8_t ir_pin) : irsender(ir_pin)
this->state.ac_temperature = 25; this->state.ac_temperature = 25;
this->state.ac_mode = 0; this->state.ac_mode = 0;
this->state.ac_fan_speed = 0; this->state.ac_fan_speed = 0;
// Initialize RMT
gpio_num_t gpio_num = gpio_num_t(ir_pin);
rmt_config_t rmt_tx = RMT_DEFAULT_CONFIG_TX(gpio_num, RMT_TX_CHANNEL);
rmt_tx.clk_div = 80; // 1MHz clock
rmt_config(&rmt_tx);
rmt_driver_install(rmt_tx.channel, 0, 0);
} }
ClimateCard::~ClimateCard() ClimateCard::~ClimateCard()
{ {
delete dht; delete dht;
delete ds18b20; delete ds18b20;
rmt_driver_uninstall(RMT_TX_CHANNEL);
} }
bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin) bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin)
@ -151,9 +157,20 @@ void ClimateCard::updateSensor()
void ClimateCard::updateAirConditioner() void ClimateCard::updateAirConditioner()
{ {
irsender.sendRaw(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature], size_t itemCount = sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature]) / sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature][0]);
sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature]) / sizeof(uint16_t), rmt_item32_t items[itemCount];
NEC_KHZ); // Convert IR timing array to RMT items
for (size_t i = 0; i < itemCount; i++)
{
items[i].level0 = 1;
items[i].duration0 = ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature][i];
items[i].level1 = 0;
items[i].duration1 = ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature][i];
}
// Send IR signal
rmt_write_items(RMT_TX_CHANNEL, items, itemCount, true);
rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);
// Publish state
for (uint8_t i = 0; i < callbacks.size(); i++) for (uint8_t i = 0; i < callbacks.size(); i++)
{ {
callbacks[i](this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature); callbacks[i](this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);

View File

@ -1,12 +1,14 @@
#pragma once #pragma once
#include <ExpansionCard.hpp> #include <ExpansionCard.hpp>
#include <IRremote.h> #include <driver/rmt.h>
#include <FRAM.h> #include <FRAM.h>
#include <OneWire.h> #include <OneWire.h>
#include <DS18B20.h> #include <DS18B20.h>
#include <dhtnew.h> #include <dhtnew.h>
#include <vector> #include <vector>
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define CARD_TYPE_CLIMATE 0x03 #define CARD_TYPE_CLIMATE 0x03
#define AC_SENSOR_TYPE_NONE 0x00 #define AC_SENSOR_TYPE_NONE 0x00
@ -68,7 +70,6 @@ class ClimateCard : public ExpansionCard {
void updateSensor(); void updateSensor();
void updateAirConditioner(); void updateAirConditioner();
// IR variables // IR variables
IRsend irsender;
uint8_t ir_pin; uint8_t ir_pin;
// Air conditioner variables // Air conditioner variables
AirConditioner ac; AirConditioner ac;

View File

@ -129,4 +129,12 @@ bool ClimateIoT::processRequestStateMessage(char *topic, char *payload, uint8_t
return true; return true;
} }
return false; return false;
}
void ClimateIoT::handleSensorUpdate(float temperature, float humidity) {
this->publishSensor();
}
void ClimateIoT::handleAirConditionerUpdate(uint8_t mode, uint8_t fan_speed, uint8_t temperature) {
this->publishClimate();
} }

View File

@ -115,15 +115,15 @@ void ESPMegaIoT::registerCard(uint8_t card_id)
components[card_id]->publishReport(); components[card_id]->publishReport();
} }
break; break;
// case CARD_TYPE_CLIMATE: case CARD_TYPE_CLIMATE:
// components[card_id] = new ClimateIoT(); components[card_id] = new ClimateIoT();
// components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic); components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);
// if (mqtt_connected) if (mqtt_connected)
// { {
// components[card_id]->subscribe(); components[card_id]->subscribe();
// components[card_id]->publishReport(); components[card_id]->publishReport();
// } }
// break; break;
default: default:
Serial.println("Unsupported card type"); Serial.println("Unsupported card type");
return; return;

View File

@ -6,6 +6,8 @@
#include <DigitalInputIoT.hpp> #include <DigitalInputIoT.hpp>
#include <DigitalOutputCard.hpp> #include <DigitalOutputCard.hpp>
#include <DigitalOutputIoT.hpp> #include <DigitalOutputIoT.hpp>
#include <ClimateCard.hpp>
#include <ClimateIoT.hpp>
#include <IoTComponent.hpp> #include <IoTComponent.hpp>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <ETH.h> #include <ETH.h>

View File

@ -0,0 +1,32 @@
#include "driver/rmt.h"
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define RMT_TX_GPIO_NUM GPIO_NUM_4
void app_main() {
// Configure RMT transmitter
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(RMT_TX_GPIO_NUM, RMT_TX_CHANNEL);
config.clk_div = 80; // Set clock divider for desired frequency (80MHz / 80 = 1MHz)
rmt_config(&config);
rmt_driver_install(config.channel, 0, 0);
// Define NEC IR protocol parameters
uint16_t irTimingArray[] = { /* Your IR timing array here */ };
size_t itemCount = sizeof(irTimingArray) / sizeof(irTimingArray[0]);
rmt_item32_t items[itemCount];
// Convert IR timing array to RMT items
for (size_t i = 0; i < itemCount; i++) {
items[i].level0 = 1;
items[i].duration0 = irTimingArray[i];
items[i].level1 = 0;
items[i].duration1 = irTimingArray[i];
}
// Send IR signal
rmt_write_items(config.channel, items, itemCount, true);
rmt_wait_tx_done(config.channel, portMAX_DELAY);
// Cleanup RMT driver
rmt_driver_uninstall(config.channel);
}

View File

@ -25,7 +25,6 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033 paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033
knolleary/pubsubclient@^2.8.0 knolleary/pubsubclient@^2.8.0
seithan/Easy Nextion Library@^1.0.6 seithan/Easy Nextion Library@^1.0.6
z3t0/IRremote@^4.2.0
bblanchon/ArduinoJson@^6.21.4 bblanchon/ArduinoJson@^6.21.4
robtillaart/DS18B20@^0.2.1 robtillaart/DS18B20@^0.2.1
robtillaart/DHTNEW@^0.4.18 robtillaart/DHTNEW@^0.4.18

View File

@ -1,6 +1,7 @@
#include <ESPMegaPRO_OOP.hpp> #include <ESPMegaPRO_OOP.hpp>
#include <InternalDisplay.hpp> #include <InternalDisplay.hpp>
#include <ETH.h> #include <ETH.h>
#include <ClimateCard.hpp>
ESPMegaPRO espmega = ESPMegaPRO(); ESPMegaPRO espmega = ESPMegaPRO();