Compare commits

..

No commits in common. "d0c3138c528aaf1e309e9d6f1173d13839c55a93" and "277985f79a97b1f6040e2a87ec42b0aef93bbc06" have entirely different histories.

2 changed files with 38 additions and 77 deletions

View File

@ -5,8 +5,11 @@
#define ISR_Out 10 #define ISR_Out 10
#define TestOut 13 #define TestOut 13
IR_Encoder *IR_Encoder::head = nullptr; std::list<IR_Encoder *> &IR_Encoder::get_enc_list() // определение функции
IR_Encoder *IR_Encoder::last = nullptr; {
static std::list<IR_Encoder *> 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, bool autoHandle)
{ {
@ -29,88 +32,45 @@ IR_Encoder::IR_Encoder(uint8_t pin, uint16_t addr, IR_DecoderRaw *decPair, bool
if (autoHandle) if (autoHandle)
{ {
if (IR_Encoder::head == nullptr) get_enc_list().push_back(this);
{
IR_Encoder::head = this;
}
if (last != nullptr)
{
last->next = this;
}
last = this;
} }
}; };
HardwareTimer* IR_Encoder::IR_Timer = nullptr; HardwareTimer IR_Encoder::IR_Timer;
inline HardwareTimer* IR_Encoder::get_IR_Timer(){return IR_Encoder::IR_Timer;} inline HardwareTimer* IR_Encoder::get_IR_Timer(){return &IR_Encoder::IR_Timer;}
// void Encoder_ISR(){
void IR_Encoder::begin(HardwareTimer* timer, uint8_t channel, IRQn_Type IRQn, uint8_t priority, void(*isrCallback)()){ // IR_Encoder::isr();
IR_Timer = timer; // }
if(IR_Timer == nullptr) return; void IR_Encoder::begin(HardwareTimer timer, uint8_t channel, IRQn_Type IRQn){
IR_Timer->setOverflow(carrierFrec * 2, HERTZ_FORMAT); //TODO: check std::bind isr func
IR_Timer->attachInterrupt(channel, (isrCallback == nullptr ? IR_Encoder::isr : isrCallback)); // IR_Timer = timer;
NVIC_SetPriority(IRQn, priority); // IR_Timer.setOverflow(carrierFrec * 2, HERTZ_FORMAT);
IR_Timer->resume(); // IR_Timer.attachInterrupt(channel, Encoder_ISR);
// NVIC_SetPriority(IRQn, 0);
// IR_Timer.resume();
} }
void IR_Encoder::enable() void IR_Encoder::enable()
{ {
bool exist = false; auto &enc_list = get_enc_list();
IR_Encoder *current = IR_Encoder::head; if (std::find(enc_list.begin(), enc_list.end(), this) == enc_list.end())
while (current != nullptr)
{ {
exist = (current == this); enc_list.push_back(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); pinMode(pin, OUTPUT);
} }
void IR_Encoder::disable() void IR_Encoder::disable()
{ {
IR_Encoder *current = IR_Encoder::head; auto &enc_list = get_enc_list();
IR_Encoder *prev = nullptr; auto it = std::find(enc_list.begin(), enc_list.end(), this);
if (it != enc_list.end())
while (current != nullptr)
{ {
if (current == this) break; enc_list.erase(it);
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); pinMode(pin, INPUT);
} }
@ -124,7 +84,10 @@ void IR_Encoder::setBlindDecoders(IR_DecoderRaw *decoders[], uint8_t count)
blindDecoders = decoders; blindDecoders = decoders;
} }
IR_Encoder::~IR_Encoder(){}; IR_Encoder::~IR_Encoder()
{
get_enc_list().remove(this);
};
void IR_Encoder::sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept) void IR_Encoder::sendData(uint16_t addrTo, uint8_t dataByte, bool needAccept)
{ {
@ -324,11 +287,10 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len)
void IR_Encoder::isr() void IR_Encoder::isr()
{ {
IR_Encoder *current = IR_Encoder::head; // Serial.println(get_enc_list().size());
while (current != nullptr) for (const auto &element : get_enc_list())
{ {
current->_isr(); element->_isr();
current = current->next;
} }
} }

View File

@ -7,11 +7,9 @@ class IR_DecoderRaw;
class IR_Encoder : public IR_FOX class IR_Encoder : public IR_FOX
{ {
friend IR_DecoderRaw; friend IR_DecoderRaw;
static IR_Encoder *head;
static IR_Encoder *last;
IR_Encoder *next;
public: public:
static HardwareTimer* IR_Timer; static HardwareTimer IR_Timer;
private: private:
// uint16_t id; /// @brief Адрес передатчика // uint16_t id; /// @brief Адрес передатчика
public: public:
@ -21,7 +19,7 @@ public:
/// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком /// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком
IR_Encoder(uint8_t pin, uint16_t addr = 0, IR_DecoderRaw *decPair = nullptr, bool autoHandle = true); IR_Encoder(uint8_t pin, uint16_t addr = 0, IR_DecoderRaw *decPair = nullptr, bool autoHandle = true);
static void isr(); static void isr();
static void begin(HardwareTimer* timer, uint8_t channel, IRQn_Type IRQn, uint8_t priority, void(*isrCallback)() = nullptr); static void begin(HardwareTimer IR_Timer, uint8_t channel, IRQn_Type IRQn);
static HardwareTimer* get_IR_Timer(); static HardwareTimer* get_IR_Timer();
void enable(); void enable();
@ -46,9 +44,10 @@ public:
~IR_Encoder(); ~IR_Encoder();
volatile bool ir_out_virtual; volatile bool ir_out_virtual;
void _isr();
private: private:
static std::list<IR_Encoder*>& get_enc_list();
void _sendBack(bool isAdressed, uint16_t addrTo, uint8_t *data, uint8_t len); void _sendBack(bool isAdressed, uint16_t addrTo, uint8_t *data, uint8_t len);
void _isr();
void setDecoder_isSending(); void setDecoder_isSending();
void sendByte(uint8_t byte, bool *prev, bool LOW_FIRST); void sendByte(uint8_t byte, bool *prev, bool LOW_FIRST);