mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Merge branch 'binary_sensor_gpio_polling' into integration
This commit is contained in:
		| @@ -70,7 +70,12 @@ void GPIOBinarySensor::dump_config() { | |||||||
|  |  | ||||||
| void GPIOBinarySensor::loop() { | void GPIOBinarySensor::loop() { | ||||||
|   if (this->use_interrupt_) { |   if (this->use_interrupt_) { | ||||||
|     if (this->store_.has_changed()) { |     if (this->store_.is_changed()) { | ||||||
|  |       // Clear the flag immediately to minimize the window where we might miss changes | ||||||
|  |       this->store_.clear_changed(); | ||||||
|  |       // Read the state and publish it | ||||||
|  |       // Note: If the ISR fires between clear_changed() and get_state(), that's fine - | ||||||
|  |       // we'll process the new change on the next loop iteration | ||||||
|       bool state = this->store_.get_state(); |       bool state = this->store_.get_state(); | ||||||
|       this->publish_state(state); |       this->publish_state(state); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -21,21 +21,14 @@ class GPIOBinarySensorStore { | |||||||
|     return this->state_; |     return this->state_; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   bool has_changed() { |   bool is_changed() const { | ||||||
|     // No lock needed: single writer (ISR) / single reader (main loop) pattern |     // Simple read of volatile bool - no clearing here | ||||||
|     // Volatile bool operations are atomic on all ESPHome-supported platforms |     return this->changed_; | ||||||
|     // |   } | ||||||
|     // Note: There's a benign race where ISR could set changed_ = true between |  | ||||||
|     // our read and clear. This is intentional and causes no issues because: |   void clear_changed() { | ||||||
|     // 1. We'll process the state change on the next loop iteration |     // Separate method to clear the flag | ||||||
|     // 2. Multiple rapid changes between loop iterations would only result in |  | ||||||
|     //    one update anyway (we only care about the final state) |  | ||||||
|     // 3. This avoids the overhead of atomic operations in the ISR |  | ||||||
|     if (!this->changed_) { |  | ||||||
|       return false; |  | ||||||
|     } |  | ||||||
|     this->changed_ = false; |     this->changed_ = false; | ||||||
|     return true; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user