From 7c9529d42f58227b2c774b9ab8e59df461465342 Mon Sep 17 00:00:00 2001 From: DashyFox Date: Thu, 29 Aug 2024 16:46:40 +0300 Subject: [PATCH] enable-disable func --- IR_Decoder.cpp | 27 +++++++++++++++++++++++- IR_Decoder.h | 5 ++++- IR_Encoder.cpp | 56 +++++++++++++++++++++++++++++++++++--------------- IR_Encoder.h | 5 ++++- 4 files changed, 73 insertions(+), 20 deletions(-) diff --git a/IR_Decoder.cpp b/IR_Decoder.cpp index 41afe3c..63d4d7a 100644 --- a/IR_Decoder.cpp +++ b/IR_Decoder.cpp @@ -7,12 +7,37 @@ std::list &IR_Decoder::get_dec_list() // определение ф } // IR_Decoder::IR_Decoder() {}; -IR_Decoder::IR_Decoder(const uint8_t pin, uint16_t addr, IR_Encoder *encPair) +IR_Decoder::IR_Decoder(const uint8_t pin, uint16_t addr, IR_Encoder *encPair, bool autoHandle) : IR_DecoderRaw(pin, addr, encPair) { get_dec_list().push_back(this); + if(autoHandle){ + enable(); + } }; +void IR_Decoder::enable() +{ + auto &dec_list = get_dec_list(); + if (std::find(dec_list.begin(), dec_list.end(), this) == dec_list.end()) + { + dec_list.push_back(this); + } + attachInterrupt(pin, (*this)(), CHANGE); +} + +void IR_Decoder::disable() +{ + detachInterrupt(pin); + auto &dec_list = get_dec_list(); + auto it = std::find(dec_list.begin(), dec_list.end(), this); + if (it != dec_list.end()) + { + dec_list.erase(it); + } +} + + std::function IR_Decoder::operator()() { return std::bind(&IR_Decoder::isr, this); diff --git a/IR_Decoder.h b/IR_Decoder.h index bdd9df3..27dca9f 100644 --- a/IR_Decoder.h +++ b/IR_Decoder.h @@ -25,10 +25,13 @@ public: PacketTypes::BasePack gotRaw; // IR_Decoder(); - IR_Decoder(const uint8_t pin, uint16_t addr, IR_Encoder *encPair = nullptr); + IR_Decoder(const uint8_t pin, uint16_t addr, IR_Encoder *encPair = nullptr, bool autoHandle = true); std::function operator()(); + void enable(); + void disable(); + ~IR_Decoder(); static void tick(); diff --git a/IR_Encoder.cpp b/IR_Encoder.cpp index 5c29fdf..48dcb28 100644 --- a/IR_Encoder.cpp +++ b/IR_Encoder.cpp @@ -5,14 +5,13 @@ #define ISR_Out 10 #define TestOut 13 -std::list& IR_Encoder::get_enc_list() // определение функции +std::list &IR_Encoder::get_enc_list() // определение функции { - static std::list dec_list; // статическая локальная переменная - return dec_list; // возвращается ссылка на переменную + static std::list dec_list; // статическая локальная переменная + return dec_list; // возвращается ссылка на переменную } - -IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair) +IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool autoHandle) { setPin(pin); id = addr; @@ -22,8 +21,7 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair) #if disablePairDec if (decPair != nullptr) { - blindDecoders = new IR_DecoderRaw *[1] - { decPair }; + blindDecoders = new IR_DecoderRaw *[1]{decPair}; decodersCount = 1; } #endif @@ -31,10 +29,33 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair) { decPair->encoder = this; } - pinMode(pin,OUTPUT); - get_enc_list().push_back(this); + pinMode(pin, OUTPUT); + + if (autoHandle) + { + get_enc_list().push_back(this); + } }; +void IR_Encoder::enable() +{ + auto &enc_list = get_enc_list(); + if (std::find(enc_list.begin(), enc_list.end(), this) == enc_list.end()) + { + enc_list.push_back(this); + } +} + +void IR_Encoder::disable() +{ + auto &enc_list = get_enc_list(); + auto it = std::find(enc_list.begin(), enc_list.end(), this); + if (it != enc_list.end()) + { + enc_list.erase(it); + } +} + void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count) { #if disablePairDec @@ -149,7 +170,7 @@ void IR_Encoder::sendBack(uint8_t data) _sendBack(false, 0, &data, 1); } -void IR_Encoder::sendBack(uint8_t *data , uint8_t len) +void IR_Encoder::sendBack(uint8_t *data, uint8_t len) { _sendBack(false, 0, data, len); } @@ -238,9 +259,11 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len) // interrupts(); } -void IR_Encoder::isr(){ +void IR_Encoder::isr() +{ // Serial.println(get_enc_list().size()); - for(const auto &element : get_enc_list()){ + for (const auto &element : get_enc_list()) + { element->_isr(); } } @@ -254,7 +277,6 @@ void IR_Encoder::_isr() port->ODR &= ~(mask); port->ODR |= mask & (ir_out_virtual ? (uint16_t)0xFFFF : (uint16_t)0x0000); - if (toggleCounter) { @@ -389,11 +411,11 @@ void IR_Encoder::addSync(bool *prev, bool *next) } uint8_t IR_Encoder::bitHigh[2] = { - (bitPauseTakts) * 2 - 1, - (bitActiveTakts) * 2 - 1}; + (bitPauseTakts) * 2 - 1, + (bitActiveTakts) * 2 - 1}; uint8_t IR_Encoder::bitLow[2] = { - (bitPauseTakts/2 + bitActiveTakts) * 2 - 1, - (bitPauseTakts) - 1}; + (bitPauseTakts / 2 + bitActiveTakts) * 2 - 1, + (bitPauseTakts)-1}; // uint8_t* IR_Encoder::bitHigh = new uint8_t[2]{ // (bitPauseTakts) * 2 - 0, diff --git a/IR_Encoder.h b/IR_Encoder.h index b312415..eed42be 100644 --- a/IR_Encoder.h +++ b/IR_Encoder.h @@ -16,9 +16,12 @@ public: /// @param addr Адрес передатчика /// @param pin Вывод передатчика /// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком - IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair = nullptr); + IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair = nullptr, bool autoHandle = true); static void isr(); + void enable(); + void disable(); + void setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count); void rawSend(uint8_t *ptr, uint8_t len);