Skip to content

Commit 3790a94

Browse files
markirbMarkus Kirberg
authored and
Markus Kirberg
committed
addon refactoring
1 parent 430f93f commit 3790a94

17 files changed

+316
-292
lines changed

mos.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,10 @@ conds:
576576
name: Plus1
577577
sources:
578578
- src/DS18XXX
579+
- src/DHT
579580
libs:
580581
- location: https://github.com/mongoose-os-libs/onewire
582+
- location: https://github.com/mongoose-os-libs/dht
581583
- location: https://github.com/mongoose-os-libs/mongoose
582584
build_vars:
583585
MGOS_ROOT_FS_TYPE: LFS
@@ -605,6 +607,9 @@ conds:
605607
- ["in1", "in", {title: "Input 1 settings"}]
606608
- ["in1.ssw.name", "Shelly SSW1"]
607609
- ["in1.sensor.name", "Shelly S1"]
610+
- ["in2", "in", {title: "Input 2 settings"}]
611+
- ["in2.ssw.name", "Shelly SSW2"]
612+
- ["in2.sensor.name", "Shelly S2"]
608613
- ["gdo1", "gdo", {title: "GDO1 settings"}]
609614
- ["gdo1.name", "Garage Door"]
610615
- ["gdo1.open_sensor_mode", 2]
@@ -623,8 +628,10 @@ conds:
623628
name: Plus1PM
624629
sources:
625630
- src/DS18XXX
631+
- src/DHT
626632
libs:
627633
- location: https://github.com/mongoose-os-libs/onewire
634+
- location: https://github.com/mongoose-os-libs/dht
628635
- location: https://github.com/mongoose-os-libs/mongoose
629636
build_vars:
630637
MGOS_ROOT_FS_TYPE: LFS
@@ -652,6 +659,9 @@ conds:
652659
- ["in1", "in", {title: "Input 1 settings"}]
653660
- ["in1.ssw.name", "Shelly SSW1"]
654661
- ["in1.sensor.name", "Shelly S1"]
662+
- ["in2", "in", {title: "Input 2 settings"}]
663+
- ["in2.ssw.name", "Shelly SSW2"]
664+
- ["in2.sensor.name", "Shelly S2"]
655665
- ["gdo1", "gdo", {title: "GDO1 settings"}]
656666
- ["gdo1.name", "Garage Door"]
657667
- ["gdo1.open_sensor_mode", -1]

src/DHT/shelly_dht_sensor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121

2222
namespace shelly {
2323

24+
std::vector<std::unique_ptr<TempSensor>> DiscoverDHTSensors(int in, int out) {
25+
std::vector<std::unique_ptr<TempSensor>> sensors;
26+
27+
std::unique_ptr<DHTSensor> dht(new DHTSensor(in, out));
28+
auto status = dht->Init();
29+
if (status == Status::OK()) {
30+
sensors.push_back(std::move(dht));
31+
} else {
32+
LOG(LL_ERROR, ("dht init failed: %s", status.ToString().c_str()));
33+
}
34+
return sensors;
35+
}
36+
2437
DHTSensor::DHTSensor(uint8_t pin_in, uint8_t pin_out)
2538
: pin_in_(pin_in),
2639
pin_out_(pin_out),

src/DHT/shelly_dht_sensor.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323

2424
#include <mgos_dht.h>
2525

26+
#include <vector>
27+
2628
namespace shelly {
2729

30+
std::vector<std::unique_ptr<TempSensor>> DiscoverDHTSensors(int in, int out);
31+
2832
class DHTSensor : public HumidityTempSensor {
2933
public:
3034
DHTSensor(uint8_t pin_in, uint8_t pin_out);

src/Shelly1/shelly_init.cpp

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
#include "shelly_dht_sensor.hpp"
2121
#include "shelly_hap_garage_door_opener.hpp"
2222
#include "shelly_hap_input.hpp"
23-
#include "shelly_hap_temperature_sensor.hpp"
2423
#include "shelly_input_pin.hpp"
2524
#include "shelly_main.hpp"
2625
#include "shelly_temp_sensor_ow.hpp"
2726

28-
#define MAX_TS_NUM 3
29-
3027
namespace shelly {
3128

3229
static std::unique_ptr<Onewire> s_onewire;
@@ -42,15 +39,20 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
4239
in1->Init();
4340
inputs->emplace_back(in1);
4441

45-
bool addon_detected = DetectAddon(3, 0);
42+
int pin_in = 3;
43+
int pin_out = 0;
4644

47-
if (addon_detected) {
48-
s_onewire.reset(new Onewire(3, 0));
49-
if (s_onewire->DiscoverAll().empty()) {
45+
if (DetectAddon(pin_in, pin_out)) {
46+
s_onewire.reset(new Onewire(pin_in, pin_out));
47+
sensors = s_onewire->DiscoverAll();
48+
if (sensors.empty()) {
5049
s_onewire.reset();
51-
52-
// No sensor detected, we assume we have a switch attached
53-
auto *in2 = new InputPin(2, 3, 0, MGOS_GPIO_PULL_NONE, false);
50+
sensors = DiscoverDHTSensors(pin_in, pin_out);
51+
}
52+
if (sensors.empty()) {
53+
// No sensors detected, we assume to use addon as input for switch or
54+
// closed/open sensor
55+
auto *in2 = new InputPin(2, pin_in, 0, MGOS_GPIO_PULL_NONE, false);
5456
in2->Init();
5557
inputs->emplace_back(in2);
5658
}
@@ -60,70 +62,24 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
6062
void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
6163
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
6264
HAPAccessoryServerRef *svr) {
63-
if (mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor) {
64-
auto *gdo_cfg = (struct mgos_config_gdo *) mgos_sys_config_get_gdo1();
65-
66-
std::unique_ptr<hap::GarageDoorOpener> gdo(new hap::GarageDoorOpener(
67-
1, FindInput(1), FindInput(2), FindOutput(1), FindOutput(1), gdo_cfg));
68-
if (gdo == nullptr) return;
69-
auto st = gdo->Init();
70-
if (!st.ok()) {
71-
LOG(LL_ERROR, ("GDO init failed: %s", st.ToString().c_str()));
72-
return;
73-
}
74-
gdo->set_primary(true);
75-
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
76-
pri_acc->SetCategory(kHAPAccessoryCategory_GarageDoorOpeners);
77-
pri_acc->AddService(gdo.get());
78-
comps->emplace_back(std::move(gdo));
79-
return;
80-
}
81-
82-
// Sensor Discovery
83-
std::unique_ptr<DHTSensor> dht;
84-
bool dht_found = false;
85-
sensors.clear();
86-
if (s_onewire != nullptr) {
87-
sensors = s_onewire->DiscoverAll();
65+
bool gdo_mode = mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor;
66+
bool ext_sensor_switch = (FindInput(2) != nullptr);
67+
bool detatched_sensor =
68+
(mgos_sys_config_get_sw1_in_mode() != (int) InMode::kDetached) &&
69+
!gdo_mode && ext_sensor_switch;
70+
bool single_accessory = sensors.empty() && !detatched_sensor;
71+
if (gdo_mode) {
72+
hap::CreateHAPGDO(1, FindInput(1), FindInput(2), FindOutput(1),
73+
FindOutput(1), mgos_sys_config_get_gdo1(), comps, accs,
74+
svr, single_accessory);
8875
} else {
89-
// Try DHT, only works on second boot
90-
dht.reset(new DHTSensor(3, 0));
91-
auto status = dht->Init();
92-
if (status == Status::OK()) {
93-
sensors.push_back(std::move(dht));
94-
dht_found = true;
95-
} else {
96-
LOG(LL_ERROR, ("dht init failed: %s", status.ToString().c_str()));
97-
}
76+
CreateHAPSwitch(1, mgos_sys_config_get_sw1(), mgos_sys_config_get_in1(),
77+
comps, accs, svr, single_accessory);
9878
}
9979

100-
bool ext_sensor_switch = (FindInput(2) != nullptr);
101-
102-
// Single switch with non-detached input and no sensors = only one accessory.
103-
bool to_pri_acc = (sensors.empty() && (mgos_sys_config_get_sw1_in_mode() !=
104-
(int) InMode::kDetached)) &&
105-
!ext_sensor_switch;
106-
CreateHAPSwitch(1, mgos_sys_config_get_sw1(), mgos_sys_config_get_in1(),
107-
comps, accs, svr, to_pri_acc);
108-
10980
if (!sensors.empty()) {
110-
struct mgos_config_ts *ts_cfgs[MAX_TS_NUM] = {
111-
(struct mgos_config_ts *) mgos_sys_config_get_ts1(),
112-
(struct mgos_config_ts *) mgos_sys_config_get_ts2(),
113-
(struct mgos_config_ts *) mgos_sys_config_get_ts3(),
114-
};
115-
116-
for (size_t i = 0; i < std::min((size_t) MAX_TS_NUM, sensors.size()); i++) {
117-
auto *ts_cfg = ts_cfgs[i];
118-
CreateHAPTemperatureSensor(i + 1, sensors[i].get(), ts_cfg, comps, accs,
119-
svr);
120-
HumidityTempSensor *hum = (HumidityTempSensor *) sensors[i].get();
121-
if (dht_found) { // can only be one shares config, as same update
122-
// interval but no unit settable
123-
CreateHAPHumiditySensor(i + 2, hum, ts_cfg, comps, accs, svr);
124-
}
125-
}
126-
} else if (ext_sensor_switch) {
81+
CreateHAPSensors(&sensors, comps, accs, svr);
82+
} else if (detatched_sensor) {
12783
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
12884
}
12985
}

src/Shelly1PM/shelly_init.cpp

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include "shelly_temp_sensor_ntc.hpp"
2626
#include "shelly_temp_sensor_ow.hpp"
2727

28-
#define MAX_TS_NUM 3
29-
3028
namespace shelly {
3129

3230
static std::unique_ptr<Onewire> s_onewire;
@@ -54,14 +52,20 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
5452
}
5553
sys_temp->reset(new TempSensorSDNT1608X103F3950(0, 3.3f, 33000.0f));
5654

57-
bool addon_detected = DetectAddon(3, 0);
55+
int pin_in = 3;
56+
int pin_out = LED_GPIO;
5857

59-
if (addon_detected) {
60-
s_onewire.reset(new Onewire(3, 0));
61-
if (s_onewire->DiscoverAll().empty()) {
58+
if (DetectAddon(pin_in, pin_out)) {
59+
s_onewire.reset(new Onewire(pin_in, pin_out));
60+
sensors = s_onewire->DiscoverAll();
61+
if (sensors.empty()) {
6262
s_onewire.reset();
63-
64-
auto *in2 = new InputPin(2, 3, 0, MGOS_GPIO_PULL_NONE, false);
63+
sensors = DiscoverDHTSensors(pin_in, pin_out);
64+
}
65+
if (sensors.empty()) {
66+
// No sensors detected, we assume to use addon as input for switch or
67+
// closed/open sensor
68+
auto *in2 = new InputPin(2, pin_in, 0, MGOS_GPIO_PULL_NONE, false);
6569
in2->Init();
6670
inputs->emplace_back(in2);
6771
}
@@ -75,68 +79,24 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
7579
void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
7680
std::vector<std::unique_ptr<mgos::hap::Accessory>> *accs,
7781
HAPAccessoryServerRef *svr) {
78-
if (mgos_sys_config_get_shelly_mode() == 2) {
79-
// Garage door opener mode.
80-
auto *gdo_cfg = (struct mgos_config_gdo *) mgos_sys_config_get_gdo1();
81-
std::unique_ptr<hap::GarageDoorOpener> gdo(new hap::GarageDoorOpener(
82-
1, FindInput(1), FindInput(2), FindOutput(1), FindOutput(1), gdo_cfg));
83-
if (gdo == nullptr) return;
84-
auto st = gdo->Init();
85-
if (!st.ok()) {
86-
LOG(LL_ERROR, ("GDO init failed: %s", st.ToString().c_str()));
87-
return;
88-
}
89-
gdo->set_primary(true);
90-
mgos::hap::Accessory *pri_acc = (*accs)[0].get();
91-
pri_acc->SetCategory(kHAPAccessoryCategory_GarageDoorOpeners);
92-
pri_acc->AddService(gdo.get());
93-
comps->emplace_back(std::move(gdo));
94-
return;
95-
}
96-
97-
// Sensor Discovery
98-
std::unique_ptr<DHTSensor> dht;
99-
bool dht_found = false;
100-
sensors.clear();
101-
if (s_onewire != nullptr) {
102-
sensors = s_onewire->DiscoverAll();
82+
bool gdo_mode = mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor;
83+
bool ext_sensor_switch = (FindInput(2) != nullptr);
84+
bool detatched_sensor =
85+
(mgos_sys_config_get_sw1_in_mode() != (int) InMode::kDetached) &&
86+
!gdo_mode && ext_sensor_switch;
87+
bool single_accessory = sensors.empty() && !detatched_sensor;
88+
if (gdo_mode) {
89+
hap::CreateHAPGDO(1, FindInput(1), FindInput(2), FindOutput(1),
90+
FindOutput(1), mgos_sys_config_get_gdo1(), comps, accs,
91+
svr, single_accessory);
10392
} else {
104-
// Try DHT, only works on second boot
105-
dht.reset(new DHTSensor(3, 0));
106-
auto status = dht->Init();
107-
if (status == Status::OK()) {
108-
sensors.push_back(std::move(dht));
109-
dht_found = true;
110-
}
93+
CreateHAPSwitch(1, mgos_sys_config_get_sw1(), mgos_sys_config_get_in1(),
94+
comps, accs, svr, single_accessory);
11195
}
11296

113-
bool ext_sensor_switch = (FindInput(2) != nullptr);
114-
115-
// Single switch with non-detached input and no sensors = only one accessory.
116-
bool to_pri_acc = (sensors.empty() && (mgos_sys_config_get_sw1_in_mode() !=
117-
(int) InMode::kDetached)) &&
118-
!ext_sensor_switch;
119-
CreateHAPSwitch(1, mgos_sys_config_get_sw1(), mgos_sys_config_get_in1(),
120-
comps, accs, svr, to_pri_acc);
121-
12297
if (!sensors.empty()) {
123-
struct mgos_config_ts *ts_cfgs[MAX_TS_NUM] = {
124-
(struct mgos_config_ts *) mgos_sys_config_get_ts1(),
125-
(struct mgos_config_ts *) mgos_sys_config_get_ts2(),
126-
(struct mgos_config_ts *) mgos_sys_config_get_ts3(),
127-
};
128-
129-
for (size_t i = 0; i < std::min((size_t) MAX_TS_NUM, sensors.size()); i++) {
130-
auto *ts_cfg = ts_cfgs[i];
131-
CreateHAPTemperatureSensor(i + 1, sensors[i].get(), ts_cfg, comps, accs,
132-
svr);
133-
HumidityTempSensor *hum = (HumidityTempSensor *) sensors[i].get();
134-
if (dht_found) { // can only be one shares config, as same update
135-
// interval but no unit settable
136-
CreateHAPHumiditySensor(i + 2, hum, ts_cfg, comps, accs, svr);
137-
}
138-
}
139-
} else if (ext_sensor_switch) {
98+
CreateHAPSensors(&sensors, comps, accs, svr);
99+
} else if (detatched_sensor) {
140100
hap::CreateHAPInput(2, mgos_sys_config_get_in2(), comps, accs, svr);
141101
}
142102
}

0 commit comments

Comments
 (0)