Skip to content

Commit

Permalink
digital input for plus addon; refactoring of rollershutter code
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Jun 12, 2024
1 parent e50f471 commit 066ff0e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 126 deletions.
66 changes: 10 additions & 56 deletions src/Shelly25/shelly_init_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,18 @@ namespace shelly {
void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
HAPAccessoryServerRef *svr) {
// Roller-shutter mode.
if (mgos_sys_config_get_shelly_mode() == 1) {
const int id = 1;
auto *wc_cfg = (struct mgos_config_wc *) mgos_sys_config_get_wc1();
auto im = static_cast<hap::WindowCovering::InMode>(wc_cfg->in_mode);
Input *in1 = FindInput(1), *in2 = FindInput(2);
std::unique_ptr<hap::WindowCovering> wc(
new hap::WindowCovering(id, in1, in2, FindOutput(1), FindOutput(2),
FindPM(1), FindPM(2), wc_cfg));
if (wc == nullptr || !wc->Init().ok()) {
return;
}
wc->set_primary(true);
switch (im) {
case hap::WindowCovering::InMode::kSeparateMomentary:
case hap::WindowCovering::InMode::kSeparateToggle: {
// Single accessory with a single primary service.
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
pri_acc->SetCategory(kHAPAccessoryCategory_WindowCoverings);
pri_acc->AddService(wc.get());
break;
}
case hap::WindowCovering::InMode::kSingle:
case hap::WindowCovering::InMode::kDetached: {
std::unique_ptr<mgos::hap::Accessory> acc(
new mgos::hap::Accessory(SHELLY_HAP_AID_BASE_WINDOW_COVERING + id,
kHAPAccessoryCategory_BridgedAccessory,
wc_cfg->name, GetIdentifyCB(), svr));
acc->AddHAPService(&mgos_hap_accessory_information_service);
acc->AddService(wc.get());
accs->push_back(std::move(acc));
if (im == hap::WindowCovering::InMode::kDetached) {
hap::CreateHAPInput(1, mgos_sys_config_get_in1(), comps, accs, svr);
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
} else if (wc_cfg->swap_inputs) {
hap::CreateHAPInput(1, mgos_sys_config_get_in1(), comps, accs, svr);
} else {
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
}
break;
}
}
comps->emplace(comps->begin(), std::move(wc));
if (mgos_sys_config_get_shelly_mode() == (int) Mode::kRollerShutter) {
hap::CreateHAPWC(1, FindInput(1), FindInput(2), FindOutput(1),
FindOutput(2), FindPM(1), FindPM(2),
mgos_sys_config_get_wc1(), mgos_sys_config_get_in1(),
mgos_sys_config_get_in2(), comps, accs, svr);
return;
}
// Garage door opener mode.
if (mgos_sys_config_get_shelly_mode() == 2) {
auto *gdo_cfg = (struct mgos_config_gdo *) mgos_sys_config_get_gdo1();
std::unique_ptr<hap::GarageDoorOpener> gdo(new hap::GarageDoorOpener(
1, FindInput(1), FindInput(2), FindOutput(1), FindOutput(2), gdo_cfg));
if (gdo == nullptr || !gdo->Init().ok()) {
return;
}
gdo->set_primary(true);
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
pri_acc->SetCategory(kHAPAccessoryCategory_GarageDoorOpeners);
pri_acc->AddService(gdo.get());
comps->emplace_back(std::move(gdo));

if (mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor) {
hap::CreateHAPGDO(1, FindInput(1), FindInput(2), FindOutput(1),
FindOutput(2), mgos_sys_config_get_gdo1(), comps, accs,
svr, single_accessory);
return;
}
// Use legacy layout if upgraded from an older version (pre-2.1).
Expand Down
12 changes: 5 additions & 7 deletions src/ShellyPlus1/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
s_onewire.reset();
sensors = DiscoverDHTSensors(pin_in, pin_out);
}
if (sensors.empty()) {
// No sensors detected, we assume to use addon as input for switch or
// closed/open sensor
auto *in2 = new InputPin(2, pin_in, 0, MGOS_GPIO_PULL_NONE, false);
in2->Init();
inputs->emplace_back(in2);
}

auto *in2 = new InputPin(2, 19, 0, MGOS_GPIO_PULL_NONE, false);
in2->Init();
inputs->emplace_back(in2);

} else {
InitSysLED(LED_GPIO, LED_ON);
}
Expand Down
12 changes: 5 additions & 7 deletions src/ShellyPlus1PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
s_onewire.reset();
sensors = DiscoverDHTSensors(pin_in, pin_out);
}
if (sensors.empty()) {
// No sensors detected, we assume to use addon as input for switch or
// closed/open sensor
auto *in2 = new InputPin(2, pin_in, 0, MGOS_GPIO_PULL_NONE, false);
in2->Init();
inputs->emplace_back(in2);
}

auto *in2 = new InputPin(2, 19, 0, MGOS_GPIO_PULL_NONE, false);
in2->Init();
inputs->emplace_back(in2);

} else {
InitSysLED(LED_GPIO, LED_ON);
}
Expand Down
67 changes: 11 additions & 56 deletions src/ShellyPlus2PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,20 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
int pin_out = 0;
int pin_in = 1;

// TODO: this blocks UART as UART TX is GPIO1. Can we find out via
// ESP_UART_ENABLE (WHICH GPIO)?
// TODO: this blocks UART as UART TX is GPIO1. We should release if there is
// no
if (DetectAddon(pin_in, pin_out)) {
s_onewire.reset(new Onewire(pin_in, pin_out));
sensors = s_onewire->DiscoverAll();
if (sensors.empty()) {
s_onewire.reset();
sensors = DiscoverDHTSensors(pin_in, pin_out);
}
if (sensors.empty()) {
// No sensors detected, we assume to use addon as input for switch or
// closed/open sensor
auto *in2 = new InputPin(2, pin_in, 0, MGOS_GPIO_PULL_NONE, false);
in2->Init();
inputs->emplace_back(in2);
}

auto *in_digital = new InputPin(3, 19, 0, MGOS_GPIO_PULL_NONE, false);
in_digital->Init();
inputs->emplace_back(in_digital);

} else {
InitSysLED(LED_GPIO, LED_ON);
}
Expand All @@ -241,54 +239,11 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
HAPAccessoryServerRef *svr) {
bool single_accessory = sensors.empty();

// do not block uart when ESP_DBG_UART = 1
// this should be gpio19?
// mgos_gpio_setup_input(19, MGOS_GPIO_PULL_NONE); // pulldown?
// bool val = mgos_gpio_read(19);
// LOG(LL_INFO, ("gpio 19 is: %i", val ? 1 : 0));

if (mgos_sys_config_get_shelly_mode() == (int) Mode::kRollerShutter) {
const int id = 1;
auto *wc_cfg = (struct mgos_config_wc *) mgos_sys_config_get_wc1();
auto im = static_cast<hap::WindowCovering::InMode>(wc_cfg->in_mode);
Input *in1 = FindInput(1), *in2 = FindInput(2);
std::unique_ptr<hap::WindowCovering> wc(
new hap::WindowCovering(id, in1, in2, FindOutput(1), FindOutput(2),
FindPM(1), FindPM(2), wc_cfg));
if (wc == nullptr || !wc->Init().ok()) {
return;
}
wc->set_primary(true);
switch (im) {
case hap::WindowCovering::InMode::kSeparateMomentary:
case hap::WindowCovering::InMode::kSeparateToggle: {
// Single accessory with a single primary service.
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
pri_acc->SetCategory(kHAPAccessoryCategory_WindowCoverings);
pri_acc->AddService(wc.get());
break;
}
case hap::WindowCovering::InMode::kSingle:
case hap::WindowCovering::InMode::kDetached: {
std::unique_ptr<mgos::hap::Accessory> acc(
new mgos::hap::Accessory(SHELLY_HAP_AID_BASE_WINDOW_COVERING + id,
kHAPAccessoryCategory_BridgedAccessory,
wc_cfg->name, GetIdentifyCB(), svr));
acc->AddHAPService(&mgos_hap_accessory_information_service);
acc->AddService(wc.get());
accs->push_back(std::move(acc));
if (im == hap::WindowCovering::InMode::kDetached) {
hap::CreateHAPInput(1, mgos_sys_config_get_in1(), comps, accs, svr);
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
} else if (wc_cfg->swap_inputs) {
hap::CreateHAPInput(1, mgos_sys_config_get_in1(), comps, accs, svr);
} else {
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
}
break;
}
}
comps->emplace(comps->begin(), std::move(wc));
hap::CreateHAPWC(1, FindInput(1), FindInput(2), FindOutput(1),
FindOutput(2), FindPM(1), FindPM(2),
mgos_sys_config_get_wc1(), mgos_sys_config_get_in1(),
mgos_sys_config_get_in2(), comps, accs, svr);
return;
}

Expand Down
51 changes: 51 additions & 0 deletions src/shelly_hap_window_covering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "mgos.hpp"
#include "mgos_system.hpp"

#include "shelly_hap_input.hpp"
#include "shelly_main.hpp"

namespace shelly {
namespace hap {

Expand Down Expand Up @@ -730,5 +733,53 @@ void WindowCovering::HandleInputSingle(const char *src) {
}
}

void CreateHAPWC(int id, Input *in1, Input *in2, Output *out1, Output *out2,
PowerMeter *pm1, PowerMeter *pm2,
const struct mgos_config_wc *wc_cfg,
const struct mgos_config_in *in1_cfg,
const struct mgos_config_in *in2_cfg,
std::vector<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
HAPAccessoryServerRef *svr) {
auto im = static_cast<hap::WindowCovering::InMode>(wc_cfg->in_mode);
std::unique_ptr<hap::WindowCovering> wc(new hap::WindowCovering(
id, in1, in2, out1, out2, pm1, pm2, (struct mgos_config_wc *) wc_cfg));
if (wc == nullptr || !wc->Init().ok()) {
return;
}
wc->set_primary(true);
switch (im) {
case hap::WindowCovering::InMode::kSeparateMomentary:
case hap::WindowCovering::InMode::kSeparateToggle: {
// Single accessory with a single primary service.
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
pri_acc->SetCategory(kHAPAccessoryCategory_WindowCoverings);
pri_acc->AddService(wc.get());
break;
}
case hap::WindowCovering::InMode::kSingle:
case hap::WindowCovering::InMode::kDetached: {
// non primary
std::unique_ptr<mgos::hap::Accessory> acc(
new mgos::hap::Accessory(SHELLY_HAP_AID_BASE_WINDOW_COVERING + id,
kHAPAccessoryCategory_BridgedAccessory,
wc_cfg->name, GetIdentifyCB(), svr));
acc->AddHAPService(&mgos_hap_accessory_information_service);
acc->AddService(wc.get());
accs->push_back(std::move(acc));
if (im == hap::WindowCovering::InMode::kDetached) {
hap::CreateHAPInput(1, in1_cfg, comps, accs, svr);
hap::CreateHAPInput(2, in2_cfg, comps, accs, svr);
} else if (wc_cfg->swap_inputs) {
hap::CreateHAPInput(1, in1_cfg, comps, accs, svr);
} else {
hap::CreateHAPInput(2, in2_cfg, comps, accs, svr);
}
break;
}
}
comps->emplace(comps->begin(), std::move(wc));
}

} // namespace hap
} // namespace shelly
8 changes: 8 additions & 0 deletions src/shelly_hap_window_covering.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ class WindowCovering : public Component, public mgos::hap::Service {
Direction moving_dir_ = Direction::kNone;
Direction last_move_dir_ = Direction::kNone;
};
void CreateHAPWC(int id, Input *in1, Input *in2, Output *out1, Output *out2,
PowerMeter *pm1, PowerMeter *pm2,
const struct mgos_config_wc *wc_cfg,
const struct mgos_config_in *in1_cfg,
const struct mgos_config_in *in2_cfg,
std::vector<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
HAPAccessoryServerRef *svr);

} // namespace hap
} // namespace shelly

0 comments on commit 066ff0e

Please sign in to comment.