Skip to content

Commit b148105

Browse files
authored
Auto detect obstruction sensors (#298)
* Auto detect obstruction sensors * Use toggle action instead of close if no sensors detected * Toggle for close only when door is open
1 parent c603f01 commit b148105

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

components/ratgdo/ratgdo.cpp

+13-19
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,9 @@ namespace ratgdo {
3838
this->input_gdo_pin_->setup();
3939
this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
4040

41-
if (this->input_obst_pin_ == nullptr) {
42-
// Our base.yaml is always going to set this so we check for 0
43-
// as well to avoid a breaking change.
44-
this->obstruction_from_status_ = true;
45-
} else {
46-
this->input_obst_pin_->setup();
47-
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
48-
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
49-
}
41+
this->input_obst_pin_->setup();
42+
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
43+
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
5044

5145
this->protocol_->setup(this, &App.scheduler, this->input_gdo_pin_, this->output_gdo_pin_);
5246

@@ -76,9 +70,7 @@ namespace ratgdo {
7670

7771
void RATGDOComponent::loop()
7872
{
79-
if (!this->obstruction_from_status_) {
80-
this->obstruction_loop();
81-
}
73+
this->obstruction_loop();
8274
this->protocol_->loop();
8375
}
8476

@@ -87,11 +79,7 @@ namespace ratgdo {
8779
ESP_LOGCONFIG(TAG, "Setting up RATGDO...");
8880
LOG_PIN(" Output GDO Pin: ", this->output_gdo_pin_);
8981
LOG_PIN(" Input GDO Pin: ", this->input_gdo_pin_);
90-
if (this->obstruction_from_status_) {
91-
ESP_LOGCONFIG(TAG, " Input Obstruction Pin: not used, will detect from GDO status");
92-
} else {
93-
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
94-
}
82+
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
9583
this->protocol_->dump_config();
9684
}
9785

@@ -218,7 +206,7 @@ namespace ratgdo {
218206

219207
void RATGDOComponent::received(const ObstructionState obstruction_state)
220208
{
221-
if (this->obstruction_from_status_) {
209+
if (!this->obstruction_sensor_detected_) {
222210
ESP_LOGD(TAG, "Obstruction: state=%s", ObstructionState_to_string(*this->obstruction_state));
223211

224212
this->obstruction_state = obstruction_state;
@@ -378,6 +366,7 @@ namespace ratgdo {
378366
// check to see if we got more then PULSES_LOWER_LIMIT pulses
379367
if (this->isr_store_.obstruction_low_count > PULSES_LOWER_LIMIT) {
380368
this->obstruction_state = ObstructionState::CLEAR;
369+
this->obstruction_sensor_detected_ = true;
381370
} else if (this->isr_store_.obstruction_low_count == 0) {
382371
// if there have been no pulses the line is steady high or low
383372
if (!this->input_obst_pin_->digital_read()) {
@@ -471,7 +460,12 @@ namespace ratgdo {
471460
return;
472461
}
473462

474-
this->door_action(DoorAction::CLOSE);
463+
if (this->obstruction_sensor_detected_) {
464+
this->door_action(DoorAction::CLOSE);
465+
} else if (*this->door_state == DoorState::OPEN) {
466+
ESP_LOGD(TAG, "No obstruction sensors detected. Close using TOGGLE.");
467+
this->door_action(DoorAction::TOGGLE);
468+
}
475469

476470
if (*this->closing_duration > 0) {
477471
// query state in case we don't get a status message

components/ratgdo/ratgdo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ namespace ratgdo {
176176
protected:
177177
RATGDOStore isr_store_ {};
178178
protocol::Protocol* protocol_;
179-
bool obstruction_from_status_ { false };
179+
bool obstruction_sensor_detected_ { false };
180180

181181
InternalGPIOPin* output_gdo_pin_;
182182
InternalGPIOPin* input_gdo_pin_;

0 commit comments

Comments
 (0)