mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[pipsolar] fix faults_present, fix update interval (#10289)
This commit is contained in:
		| @@ -23,20 +23,18 @@ void Pipsolar::loop() { | |||||||
|   // Read message |   // Read message | ||||||
|   if (this->state_ == STATE_IDLE) { |   if (this->state_ == STATE_IDLE) { | ||||||
|     this->empty_uart_buffer_(); |     this->empty_uart_buffer_(); | ||||||
|     switch (this->send_next_command_()) { |  | ||||||
|       case 0: |     if (this->send_next_command_()) { | ||||||
|         // no command send (empty queue) time to poll |       // command sent | ||||||
|         if (millis() - this->last_poll_ > this->update_interval_) { |  | ||||||
|           this->send_next_poll_(); |  | ||||||
|           this->last_poll_ = millis(); |  | ||||||
|         } |  | ||||||
|       return; |       return; | ||||||
|         break; |  | ||||||
|       case 1: |  | ||||||
|         // command send |  | ||||||
|         return; |  | ||||||
|         break; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     if (this->send_next_poll_()) { | ||||||
|  |       // poll sent | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return; | ||||||
|   } |   } | ||||||
|   if (this->state_ == STATE_COMMAND_COMPLETE) { |   if (this->state_ == STATE_COMMAND_COMPLETE) { | ||||||
|     if (this->check_incoming_length_(4)) { |     if (this->check_incoming_length_(4)) { | ||||||
| @@ -530,7 +528,7 @@ void Pipsolar::loop() { | |||||||
|         // '(00000000000000000000000000000000' |         // '(00000000000000000000000000000000' | ||||||
|         // iterate over all available flag (as not all models have all flags, but at least in the same order) |         // iterate over all available flag (as not all models have all flags, but at least in the same order) | ||||||
|         this->value_warnings_present_ = false; |         this->value_warnings_present_ = false; | ||||||
|         this->value_faults_present_ = true; |         this->value_faults_present_ = false; | ||||||
|  |  | ||||||
|         for (size_t i = 1; i < strlen(tmp); i++) { |         for (size_t i = 1; i < strlen(tmp); i++) { | ||||||
|           enabled = tmp[i] == '1'; |           enabled = tmp[i] == '1'; | ||||||
| @@ -708,6 +706,7 @@ void Pipsolar::loop() { | |||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       // crc ok |       // crc ok | ||||||
|  |       this->used_polling_commands_[this->last_polling_command_].needs_update = false; | ||||||
|       this->state_ = STATE_POLL_CHECKED; |       this->state_ = STATE_POLL_CHECKED; | ||||||
|       return; |       return; | ||||||
|     } else { |     } else { | ||||||
| @@ -788,7 +787,7 @@ uint8_t Pipsolar::check_incoming_crc_() { | |||||||
| } | } | ||||||
|  |  | ||||||
| // send next command used | // send next command used | ||||||
| uint8_t Pipsolar::send_next_command_() { | bool Pipsolar::send_next_command_() { | ||||||
|   uint16_t crc16; |   uint16_t crc16; | ||||||
|   if (!this->command_queue_[this->command_queue_position_].empty()) { |   if (!this->command_queue_[this->command_queue_position_].empty()) { | ||||||
|     const char *command = this->command_queue_[this->command_queue_position_].c_str(); |     const char *command = this->command_queue_[this->command_queue_position_].c_str(); | ||||||
| @@ -809,20 +808,23 @@ uint8_t Pipsolar::send_next_command_() { | |||||||
|     // end Byte |     // end Byte | ||||||
|     this->write(0x0D); |     this->write(0x0D); | ||||||
|     ESP_LOGD(TAG, "Sending command from queue: %s with length %d", command, length); |     ESP_LOGD(TAG, "Sending command from queue: %s with length %d", command, length); | ||||||
|     return 1; |     return true; | ||||||
|   } |   } | ||||||
|   return 0; |   return false; | ||||||
| } | } | ||||||
|  |  | ||||||
| void Pipsolar::send_next_poll_() { | bool Pipsolar::send_next_poll_() { | ||||||
|   uint16_t crc16; |   uint16_t crc16; | ||||||
|   this->last_polling_command_ = (this->last_polling_command_ + 1) % 15; |  | ||||||
|  |   for (uint8_t i = 0; i < POLLING_COMMANDS_MAX; i++) { | ||||||
|  |     this->last_polling_command_ = (this->last_polling_command_ + 1) % POLLING_COMMANDS_MAX; | ||||||
|     if (this->used_polling_commands_[this->last_polling_command_].length == 0) { |     if (this->used_polling_commands_[this->last_polling_command_].length == 0) { | ||||||
|     this->last_polling_command_ = 0; |       // not enabled | ||||||
|  |       continue; | ||||||
|     } |     } | ||||||
|   if (this->used_polling_commands_[this->last_polling_command_].length == 0) { |     if (!this->used_polling_commands_[this->last_polling_command_].needs_update) { | ||||||
|     // no command specified |       // no update requested | ||||||
|     return; |       continue; | ||||||
|     } |     } | ||||||
|     this->state_ = STATE_POLL; |     this->state_ = STATE_POLL; | ||||||
|     this->command_start_millis_ = millis(); |     this->command_start_millis_ = millis(); | ||||||
| @@ -840,6 +842,9 @@ void Pipsolar::send_next_poll_() { | |||||||
|     ESP_LOGD(TAG, "Sending polling command : %s with length %d", |     ESP_LOGD(TAG, "Sending polling command : %s with length %d", | ||||||
|              this->used_polling_commands_[this->last_polling_command_].command, |              this->used_polling_commands_[this->last_polling_command_].command, | ||||||
|              this->used_polling_commands_[this->last_polling_command_].length); |              this->used_polling_commands_[this->last_polling_command_].length); | ||||||
|  |     return true; | ||||||
|  |   } | ||||||
|  |   return false; | ||||||
| } | } | ||||||
|  |  | ||||||
| void Pipsolar::queue_command_(const char *command, uint8_t length) { | void Pipsolar::queue_command_(const char *command, uint8_t length) { | ||||||
| @@ -869,7 +874,13 @@ void Pipsolar::dump_config() { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
| void Pipsolar::update() {} | void Pipsolar::update() { | ||||||
|  |   for (auto &used_polling_command : this->used_polling_commands_) { | ||||||
|  |     if (used_polling_command.length != 0) { | ||||||
|  |       used_polling_command.needs_update = true; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
| void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand polling_command) { | void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand polling_command) { | ||||||
|   for (auto &used_polling_command : this->used_polling_commands_) { |   for (auto &used_polling_command : this->used_polling_commands_) { | ||||||
| @@ -891,6 +902,7 @@ void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand poll | |||||||
|       used_polling_command.errors = 0; |       used_polling_command.errors = 0; | ||||||
|       used_polling_command.identifier = polling_command; |       used_polling_command.identifier = polling_command; | ||||||
|       used_polling_command.length = length - 1; |       used_polling_command.length = length - 1; | ||||||
|  |       used_polling_command.needs_update = true; | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -25,6 +25,7 @@ struct PollingCommand { | |||||||
|   uint8_t length = 0; |   uint8_t length = 0; | ||||||
|   uint8_t errors; |   uint8_t errors; | ||||||
|   ENUMPollingCommand identifier; |   ENUMPollingCommand identifier; | ||||||
|  |   bool needs_update; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #define PIPSOLAR_VALUED_ENTITY_(type, name, polling_command, value_type) \ | #define PIPSOLAR_VALUED_ENTITY_(type, name, polling_command, value_type) \ | ||||||
| @@ -189,14 +190,14 @@ class Pipsolar : public uart::UARTDevice, public PollingComponent { | |||||||
|   static const size_t PIPSOLAR_READ_BUFFER_LENGTH = 110;  // maximum supported answer length |   static const size_t PIPSOLAR_READ_BUFFER_LENGTH = 110;  // maximum supported answer length | ||||||
|   static const size_t COMMAND_QUEUE_LENGTH = 10; |   static const size_t COMMAND_QUEUE_LENGTH = 10; | ||||||
|   static const size_t COMMAND_TIMEOUT = 5000; |   static const size_t COMMAND_TIMEOUT = 5000; | ||||||
|   uint32_t last_poll_ = 0; |   static const size_t POLLING_COMMANDS_MAX = 15; | ||||||
|   void add_polling_command_(const char *command, ENUMPollingCommand polling_command); |   void add_polling_command_(const char *command, ENUMPollingCommand polling_command); | ||||||
|   void empty_uart_buffer_(); |   void empty_uart_buffer_(); | ||||||
|   uint8_t check_incoming_crc_(); |   uint8_t check_incoming_crc_(); | ||||||
|   uint8_t check_incoming_length_(uint8_t length); |   uint8_t check_incoming_length_(uint8_t length); | ||||||
|   uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len); |   uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len); | ||||||
|   uint8_t send_next_command_(); |   bool send_next_command_(); | ||||||
|   void send_next_poll_(); |   bool send_next_poll_(); | ||||||
|   void queue_command_(const char *command, uint8_t length); |   void queue_command_(const char *command, uint8_t length); | ||||||
|   std::string command_queue_[COMMAND_QUEUE_LENGTH]; |   std::string command_queue_[COMMAND_QUEUE_LENGTH]; | ||||||
|   uint8_t command_queue_position_ = 0; |   uint8_t command_queue_position_ = 0; | ||||||
| @@ -216,7 +217,7 @@ class Pipsolar : public uart::UARTDevice, public PollingComponent { | |||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   uint8_t last_polling_command_ = 0; |   uint8_t last_polling_command_ = 0; | ||||||
|   PollingCommand used_polling_commands_[15]; |   PollingCommand used_polling_commands_[POLLING_COMMANDS_MAX]; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace pipsolar | }  // namespace pipsolar | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user