Skip to content

Commit

Permalink
integrade shelly plus addon and fix compatibility with newer stock
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kirberg committed May 17, 2024
1 parent 46a3432 commit efbe209
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
4 changes: 3 additions & 1 deletion mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,14 @@ conds:
includes:
- src/ADE7953
libs:
- location: https://github.com/mongoose-os-libs/onewire
- location: https://github.com/mongoose-os-libs/dht
- location: https://github.com/mongoose-os-libs/ade7953
build_vars:
MGOS_ROOT_FS_TYPE: LFS
MGOS_ROOT_FS_SIZE: 458752
ESP_IDF_EXTRA_PARTITION: "aux,0x55,0x00,0x3f0000,48K"
ESP_IDF_EXTRA_PARTITION_2: "shelly,data,nvs,0x3fc000,16K"
ESP_IDF_EXTRA_PARTITION_2: "shelly,data,0x88,0x3fc000,16K"
ESP_IDF_SDKCONFIG_OPTS: >
${build_vars.ESP_IDF_SDKCONFIG_OPTS}
CONFIG_FREERTOS_UNICORE=y
Expand Down
47 changes: 34 additions & 13 deletions src/ShellyPlus2PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

#include "mgos_ade7953.h"

#include "shelly_dht_sensor.hpp"
#include "shelly_input_pin.hpp"
#include "shelly_main.hpp"
#include "shelly_pm.hpp"
#include "shelly_pm_ade7953.hpp"
#include "shelly_sys_led_btn.hpp"
#include "shelly_temp_sensor_ntc.hpp"
#include "shelly_temp_sensor_ow.hpp"

#include <algorithm>

Expand All @@ -33,6 +35,9 @@

namespace shelly {

static std::unique_ptr<Onewire> s_onewire;
static std::vector<std::unique_ptr<TempSensor>> sensors;

static struct mgos_ade7953 *s_ade7953 = NULL;

static Status PowerMeterInit(std::vector<std::unique_ptr<PowerMeter>> *pms) {
Expand All @@ -59,7 +64,6 @@ static Status PowerMeterInit(std::vector<std::unique_ptr<PowerMeter>> *pms) {
mgos_gpio_write(reset_pin, 1);
mgos_gpio_set_mode(reset_pin, MGOS_GPIO_MODE_INPUT);


s_ade7953 = mgos_ade7953_create(mgos_i2c_get_global(), &ade7953_cfg);

if (s_ade7953 == nullptr) {
Expand Down Expand Up @@ -100,11 +104,32 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
}

sys_temp->reset(new TempSensorSDNT1608X103F3950(35, 3.3f, 10000.0f));

int pin_out = 0;
int pin_in = 1;

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);
}
}
}

void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
HAPAccessoryServerRef *svr) {
bool single_accessory = sensors.empty();

// Roller-shutter mode.
if (mgos_sys_config_get_shelly_mode() == 1) {
const int id = 1;
Expand Down Expand Up @@ -151,25 +176,21 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
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;
}

CreateHAPSwitch(1, mgos_sys_config_get_sw1(), mgos_sys_config_get_in1(),
comps, accs, svr, false /* to_pri_acc */);
CreateHAPSwitch(2, mgos_sys_config_get_sw2(), mgos_sys_config_get_in2(),
comps, accs, svr, false /* to_pri_acc */);

if (!sensors.empty()) {
CreateHAPSensors(&sensors, comps, accs, svr);
}
}

} // namespace shelly

0 comments on commit efbe209

Please sign in to comment.