faster for each

This commit is contained in:
DashyFox 2025-02-25 15:17:09 +03:00
parent 277985f79a
commit aa0b478229
2 changed files with 64 additions and 23 deletions

View File

@ -5,11 +5,8 @@
#define ISR_Out 10
#define TestOut 13
std::list<IR_Encoder *> &IR_Encoder::get_enc_list() // определение функции
{
static std::list<IR_Encoder *> dec_list; // статическая локальная переменная
return dec_list; // возвращается ссылка на переменную
}
IR_Encoder *IR_Encoder::head = nullptr;
IR_Encoder *IR_Encoder::last = nullptr;
IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool autoHandle)
{
@ -32,7 +29,15 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool
if (autoHandle)
{
get_enc_list().push_back(this);
if (IR_Encoder::head == nullptr)
{
IR_Encoder::head = this;
}
if (last != nullptr)
{
last->next = this;
}
last = this;
}
};
@ -54,23 +59,60 @@ void IR_Encoder::begin(HardwareTimer timer, uint8_t channel, IRQn_Type IRQn){
void IR_Encoder::enable()
{
auto &enc_list = get_enc_list();
if (std::find(enc_list.begin(), enc_list.end(), this) == enc_list.end())
bool exist = false;
IR_Encoder *current = IR_Encoder::head;
while (current != nullptr)
{
enc_list.push_back(this);
exist = (current == this);
if (exist) break;
current = current->next;
}
if (!exist)
{
if (IR_Encoder::head == nullptr)
{
IR_Encoder::head = this;
last = this;
}
else
{
last->next = this;
last = this;
}
this->next = nullptr; // Указываем, что следующий за этим элементом — nullptr
}
pinMode(pin, OUTPUT);
}
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())
IR_Encoder *current = IR_Encoder::head;
IR_Encoder *prev = nullptr;
while (current != nullptr)
{
enc_list.erase(it);
if (current == this) break;
prev = current;
current = current->next;
}
if (current != nullptr) // Элемент найден в списке
{
if (prev != nullptr)
{
prev->next = current->next; // Убираем текущий элемент из списка
}
else
{
IR_Encoder::head = current->next; // Удаляемый элемент был первым
}
if (current == last)
{
last = prev; // Если удаляется последний элемент, обновляем last
}
}
pinMode(pin, INPUT);
}
@ -84,10 +126,7 @@ void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count)
blindDecoders = decoders;
}
IR_Encoder::~IR_Encoder()
{
get_enc_list().remove(this);
};
IR_Encoder::~IR_Encoder(){};
void IR_Encoder::sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept)
{
@ -287,10 +326,11 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len)
void IR_Encoder::isr()
{
// Serial.println(get_enc_list().size());
for (const auto &element : get_enc_list())
IR_Encoder *current = IR_Encoder::head;
while (current != nullptr)
{
element->_isr();
current->_isr();
current = current->next;
}
}

View File

@ -7,7 +7,9 @@ class IR_DecoderRaw;
class IR_Encoder : public IR_FOX
{
friend IR_DecoderRaw;
static IR_Encoder *head;
static IR_Encoder *last;
IR_Encoder *next;
public:
static HardwareTimer IR_Timer;
private:
@ -45,7 +47,6 @@ public:
volatile bool ir_out_virtual;
private:
static std::list<IR_Encoder*>& get_enc_list();
void _sendBack(bool isAdressed, uint16_t addrTo, uint8_t *data, uint8_t len);
void _isr();