Skip to content

Commit

Permalink
allow inputs to be inverted (#38)
Browse files Browse the repository at this point in the history
* allow inputs to be inverted

* add inverted and active to the module reference
  • Loading branch information
pascalzauberzeug committed May 28, 2024
1 parent dcb6f54 commit 5d0f99d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions docs/module_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ The input module is associated with a digital input pin that is be connected to
| -------------------- | -------------------------------------- | --------- |
| `input = Input(pin)` | `pin` is the corresponding GPIO number | `int` |

| Properties | Description | Data type |
| -------------- | ------------------------------------- | --------- |
| `input.level` | Current signal level (0 or 1) | `int` |
| `input.change` | Level change since last cycle (-1..1) | `int` |
| Properties | Description | Data type |
| ---------------- | ------------------------------------- | --------- |
| `input.level` | Current signal level (0 or 1) | `int` |
| `input.change` | Level change since last cycle (-1..1) | `int` |
| `input.inverted` | Inverts the active property if true | `bool` |
| `input.active` | Current active state of the input | `bool` |

| Methods | Description |
| ------------------ | ---------------------------------- |
Expand Down
3 changes: 3 additions & 0 deletions main/modules/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
Input::Input(const std::string name) : Module(input, name) {
this->properties["level"] = std::make_shared<IntegerVariable>();
this->properties["change"] = std::make_shared<IntegerVariable>();
this->properties["inverted"] = std::make_shared<BooleanVariable>();
this->properties["active"] = std::make_shared<BooleanVariable>();
}

void Input::step() {
const int new_level = this->get_level();
this->properties.at("change")->integer_value = new_level - this->properties.at("level")->integer_value;
this->properties.at("level")->integer_value = new_level;
this->properties.at("active")->boolean_value = this->properties.at("inverted")->boolean_value ? !new_level : new_level;
Module::step();
}

Expand Down

0 comments on commit 5d0f99d

Please sign in to comment.