mirror of
				https://github.com/Show-maket/IR-protocol.git
				synced 2025-10-30 10:32:35 +00:00 
			
		
		
		
	Compare commits
	
		
			7 Commits
		
	
	
		
			d1cb167aaf
			...
			STM32-opti
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 2d839d3ff8 | |||
| 6ba8fdffe4 | |||
| aa0b478229 | |||
| 277985f79a | |||
| 444b84c313 | |||
| 2db1ef7805 | |||
| 1353ab6f75 | 
							
								
								
									
										4
									
								
								.vscode/arduino.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.vscode/arduino.json
									
									
									
									
										vendored
									
									
								
							| @ -1,7 +1,5 @@ | ||||
| { | ||||
|     "board": "STMicroelectronics:stm32:GenF4", | ||||
|     "port": "COM17", | ||||
|     "prebuild": "if exist bin rd /s /q bin", | ||||
|     "configuration": "clock=25MHz,pnum=MAKET_F401RETX,upload_method=swdMethod,xserial=generic,usb=CDCgen,xusb=FS,opt=osstd,dbg=none,rtlib=nano", | ||||
|     "sketch": "IR-protocol.ino" | ||||
|     "prebuild": "if exist bin rd /s /q bin" | ||||
| } | ||||
| @ -97,7 +97,7 @@ void IR_Decoder::_tick() | ||||
|         } | ||||
|         gotRaw.set(&packInfo, id); | ||||
|     } | ||||
|     if (isWaitingAcceptSend && millis() - acceptSendTimer > 75) | ||||
|     if (isWaitingAcceptSend && millis() - acceptSendTimer > acceptDelay) | ||||
|     { | ||||
|         encoder->sendAccept(addrAcceptSendTo, acceptCustomByte); | ||||
|         isWaitingAcceptSend = false; | ||||
|  | ||||
| @ -14,7 +14,7 @@ private: | ||||
|     bool isWaitingAcceptSend; | ||||
|     uint16_t addrAcceptSendTo; | ||||
|  | ||||
|     uint16_t acceptDelay = 75; | ||||
|     uint16_t acceptDelay = IR_ResponseDelay; | ||||
|     uint8_t acceptCustomByte; | ||||
|  | ||||
| public: | ||||
|  | ||||
| @ -23,6 +23,7 @@ | ||||
| #define riseTimeMin (riseTime - riseTolerance) | ||||
| #define aroundRise(t) (riseTimeMin < t && t < riseTimeMax) | ||||
| #define IR_timeout (riseTimeMax * (8 + syncBits + 1)) // us // таймаут в 8 data + 3 sync + 1 | ||||
| constexpr uint16_t IR_ResponseDelay = ((uint16_t)(((bitTime+riseTolerance) * (8 + syncBits + 1))*2.7735))/1000; | ||||
|  | ||||
| class IR_Encoder; | ||||
| class IR_DecoderRaw : virtual public IR_FOX | ||||
|  | ||||
							
								
								
									
										112
									
								
								IR_Encoder.cpp
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								IR_Encoder.cpp
									
									
									
									
									
								
							| @ -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,45 +29,88 @@ 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; | ||||
|     } | ||||
| }; | ||||
|   | ||||
| 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(){ | ||||
| //     IR_Encoder::isr(); | ||||
| // } | ||||
| void IR_Encoder::begin(HardwareTimer timer, uint8_t channel, IRQn_Type IRQn){ | ||||
|     //TODO: check std::bind isr func | ||||
|     // IR_Timer = timer; | ||||
|     // IR_Timer.setOverflow(carrierFrec * 2, HERTZ_FORMAT); | ||||
|     // IR_Timer.attachInterrupt(channel, Encoder_ISR); | ||||
|     // NVIC_SetPriority(IRQn, 0); | ||||
|     // IR_Timer.resume(); | ||||
| void IR_Encoder::begin(HardwareTimer* timer, uint8_t channel, IRQn_Type IRQn, uint8_t priority, void(*isrCallback)()){ | ||||
|     IR_Timer = timer; | ||||
|     if(IR_Timer == nullptr) return; | ||||
|     IR_Timer->setOverflow(carrierFrec * 2, HERTZ_FORMAT); | ||||
|     IR_Timer->attachInterrupt(channel, (isrCallback == nullptr ? IR_Encoder::isr : isrCallback)); | ||||
|     NVIC_SetPriority(IRQn, priority); | ||||
|     IR_Timer->resume(); | ||||
| } | ||||
|  | ||||
|  | ||||
| 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 +124,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) | ||||
| { | ||||
| @ -247,6 +284,10 @@ void IR_Encoder::setDecoder_isSending() | ||||
|         for (uint8_t i = 0; i < decodersCount; i++) | ||||
|         { | ||||
|             blindDecoders[i]->isPairSending ^= id; | ||||
|             // Serial.print("setDecoder_isSending()   id = "); | ||||
|             // Serial.print(id); | ||||
|             // Serial.print("   isPairSending = "); | ||||
|             // Serial.println(blindDecoders[i]->isPairSending); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -258,7 +299,7 @@ void IR_Encoder::rawSend(uint8_t *ptr, uint8_t len) | ||||
|         // TODO: Обработка повторной отправки | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     // Serial.println("START"); | ||||
|     setDecoder_isSending(); | ||||
|  | ||||
|     // noInterrupts(); | ||||
| @ -283,10 +324,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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -314,7 +356,9 @@ void IR_Encoder::_isr() | ||||
|             // сброс счетчиков | ||||
|             // ... | ||||
|             isSending = false; | ||||
|             // Serial.println("STOP"); | ||||
|             setDecoder_isSending(); | ||||
|             // Serial.println(); | ||||
|             return; | ||||
|             break; | ||||
|  | ||||
|  | ||||
							
								
								
									
										13
									
								
								IR_Encoder.h
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								IR_Encoder.h
									
									
									
									
									
								
							| @ -7,9 +7,11 @@ 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; | ||||
|     static HardwareTimer* IR_Timer; | ||||
| private: | ||||
|     // uint16_t id; /// @brief Адрес передатчика | ||||
| public: | ||||
| @ -19,7 +21,7 @@ public: | ||||
|     /// @param decPair Приёмник, для которого отключается приём в момент передачи передатчиком | ||||
|     IR_Encoder(uint8_t pin, uint16_t addr = 0, IR_DecoderRaw *decPair = nullptr, bool autoHandle = true); | ||||
|     static void isr(); | ||||
|     static void begin(HardwareTimer IR_Timer, uint8_t channel, IRQn_Type IRQn); | ||||
|     static void begin(HardwareTimer* timer, uint8_t channel, IRQn_Type IRQn, uint8_t priority, void(*isrCallback)() = nullptr); | ||||
|     static HardwareTimer* get_IR_Timer(); | ||||
|  | ||||
|     void enable(); | ||||
| @ -44,10 +46,9 @@ public: | ||||
|     ~IR_Encoder(); | ||||
|     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(); | ||||
| private: | ||||
|     void _sendBack(bool isAdressed, uint16_t addrTo, uint8_t *data, uint8_t len); | ||||
|  | ||||
|     void setDecoder_isSending(); | ||||
|     void sendByte(uint8_t byte, bool *prev, bool LOW_FIRST); | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
|     IR_MSG_ACCEPT с адреса 0 воспринимается всеми устройствами | ||||
| */  | ||||
| //**** Контрольные точки ****** | ||||
| #define IR_MAX_ADDR_CPU 64999 | ||||
| #define IR_MAX_ADDR_CPU 63999 | ||||
| #define IR_MIN_ADDR_CPU 32000 | ||||
|  | ||||
| // //***** Группы машинок ******** | ||||
| @ -28,11 +28,11 @@ | ||||
|  | ||||
| //********* Машинки *********** | ||||
| #define IR_MAX_CAR 31999 | ||||
| #define IR_MIN_CAR 100 | ||||
| #define IR_MIN_CAR 1 | ||||
|  | ||||
| //***** Пульты управления ***** | ||||
| #define IR_MAX_CONTROLLER 99 | ||||
| #define IR_MIN_CONTROLLER 0 | ||||
| #define IR_MAX_CONTROLLER 64999 | ||||
| #define IR_MIN_CONTROLLER 64000 | ||||
| /* | ||||
|  | ||||
| /```````````````````````````````````````````````` data pack `````````````````````````````````````````````\                                   | ||||
|  | ||||
		Reference in New Issue
	
	Block a user