enable-disable func

This commit is contained in:
2024-08-29 16:46:40 +03:00
parent d4dd0e95fd
commit 7c9529d42f
4 changed files with 73 additions and 20 deletions

View File

@ -7,12 +7,37 @@ std::list<IR_Decoder *> &IR_Decoder::get_dec_list() // определение ф
} }
// IR_Decoder::IR_Decoder() {}; // 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) : IR_DecoderRaw(pin, addr, encPair)
{ {
get_dec_list().push_back(this); 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<void()> IR_Decoder::operator()() std::function<void()> IR_Decoder::operator()()
{ {
return std::bind(&IR_Decoder::isr, this); return std::bind(&IR_Decoder::isr, this);

View File

@ -25,10 +25,13 @@ public:
PacketTypes::BasePack gotRaw; PacketTypes::BasePack gotRaw;
// IR_Decoder(); // 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<void()> operator()(); std::function<void()> operator()();
void enable();
void disable();
~IR_Decoder(); ~IR_Decoder();
static void tick(); static void tick();

View File

@ -5,14 +5,13 @@
#define ISR_Out 10 #define ISR_Out 10
#define TestOut 13 #define TestOut 13
std::list<IR_Encoder*>& IR_Encoder::get_enc_list() // определение функции std::list<IR_Encoder *> &IR_Encoder::get_enc_list() // определение функции
{ {
static std::list<IR_Encoder*> dec_list; // статическая локальная переменная static std::list<IR_Encoder *> dec_list; // статическая локальная переменная
return dec_list; // возвращается ссылка на переменную return dec_list; // возвращается ссылка на переменную
} }
IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool autoHandle)
IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair)
{ {
setPin(pin); setPin(pin);
id = addr; id = addr;
@ -22,8 +21,7 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair)
#if disablePairDec #if disablePairDec
if (decPair != nullptr) if (decPair != nullptr)
{ {
blindDecoders = new IR_DecoderRaw *[1] blindDecoders = new IR_DecoderRaw *[1]{decPair};
{ decPair };
decodersCount = 1; decodersCount = 1;
} }
#endif #endif
@ -31,10 +29,33 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair)
{ {
decPair->encoder = this; decPair->encoder = this;
} }
pinMode(pin,OUTPUT); pinMode(pin, OUTPUT);
get_enc_list().push_back(this);
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) void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count)
{ {
#if disablePairDec #if disablePairDec
@ -149,7 +170,7 @@ void IR_Encoder::sendBack(uint8_t data)
_sendBack(false, 0, &data, 1); _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); _sendBack(false, 0, data, len);
} }
@ -238,9 +259,11 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len)
// interrupts(); // interrupts();
} }
void IR_Encoder::isr(){ void IR_Encoder::isr()
{
// Serial.println(get_enc_list().size()); // Serial.println(get_enc_list().size());
for(const auto &element : get_enc_list()){ for (const auto &element : get_enc_list())
{
element->_isr(); element->_isr();
} }
} }
@ -254,7 +277,6 @@ void IR_Encoder::_isr()
port->ODR &= ~(mask); port->ODR &= ~(mask);
port->ODR |= mask & (ir_out_virtual ? (uint16_t)0xFFFF : (uint16_t)0x0000); port->ODR |= mask & (ir_out_virtual ? (uint16_t)0xFFFF : (uint16_t)0x0000);
if (toggleCounter) if (toggleCounter)
{ {
@ -389,11 +411,11 @@ void IR_Encoder::addSync(bool *prev, bool *next)
} }
uint8_t IR_Encoder::bitHigh[2] = { uint8_t IR_Encoder::bitHigh[2] = {
(bitPauseTakts) * 2 - 1, (bitPauseTakts) * 2 - 1,
(bitActiveTakts) * 2 - 1}; (bitActiveTakts) * 2 - 1};
uint8_t IR_Encoder::bitLow[2] = { uint8_t IR_Encoder::bitLow[2] = {
(bitPauseTakts/2 + bitActiveTakts) * 2 - 1, (bitPauseTakts / 2 + bitActiveTakts) * 2 - 1,
(bitPauseTakts) - 1}; (bitPauseTakts)-1};
// uint8_t* IR_Encoder::bitHigh = new uint8_t[2]{ // uint8_t* IR_Encoder::bitHigh = new uint8_t[2]{
// (bitPauseTakts) * 2 - 0, // (bitPauseTakts) * 2 - 0,

View File

@ -16,9 +16,12 @@ public:
/// @param addr Адрес передатчика /// @param addr Адрес передатчика
/// @param pin Вывод передатчика /// @param pin Вывод передатчика
/// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком /// @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(); static void isr();
void enable();
void disable();
void setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count); void setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count);
void rawSend(uint8_t *ptr, uint8_t len); void rawSend(uint8_t *ptr, uint8_t len);