Skip to content

Commit

Permalink
fix logic and restore uart
Browse files Browse the repository at this point in the history
  • Loading branch information
markirb committed Jun 14, 2024
1 parent 32071b9 commit d6af072
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/ShellyPlus1/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
inputs->emplace_back(in2);

} else {
RestoreUART();
InitSysLED(LED_GPIO, LED_ON);
}
InitSysBtn(BTN_GPIO, BTN_DOWN);
Expand All @@ -67,7 +68,7 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
bool gdo_mode = mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor;
bool ext_sensor_switch = (FindInput(2) != nullptr);
bool detatched_sensor =
(mgos_sys_config_get_sw1_in_mode() != (int) InMode::kDetached) &&
(mgos_sys_config_get_sw1_in_mode() == (int) InMode::kDetached) &&
!gdo_mode && ext_sensor_switch;
bool single_accessory = sensors.empty() && !detatched_sensor;
if (gdo_mode) {
Expand Down
3 changes: 2 additions & 1 deletion src/ShellyPlus1PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
inputs->emplace_back(in2);

} else {
RestoreUART();
InitSysLED(LED_GPIO, LED_ON);
}
InitSysBtn(BTN_GPIO, BTN_DOWN);
Expand All @@ -129,7 +130,7 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
bool gdo_mode = mgos_sys_config_get_shelly_mode() == (int) Mode::kGarageDoor;
bool ext_sensor_switch = (FindInput(2) != nullptr);
bool detatched_sensor =
(mgos_sys_config_get_sw1_in_mode() != (int) InMode::kDetached) &&
(mgos_sys_config_get_sw1_in_mode() == (int) InMode::kDetached) &&
!gdo_mode && ext_sensor_switch;
bool single_accessory = sensors.empty() && !detatched_sensor;
if (gdo_mode) {
Expand Down
7 changes: 3 additions & 4 deletions src/ShellyPlus2PM/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ 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. 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();
Expand All @@ -228,6 +226,7 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
inputs->emplace_back(in_digital);

} else {
RestoreUART();
InitSysLED(LED_GPIO, LED_ON);
}

Expand All @@ -237,8 +236,6 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
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();

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),
Expand All @@ -247,6 +244,8 @@ void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
return;
}

bool single_accessory = sensors.empty();

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,
Expand Down
1 change: 1 addition & 0 deletions src/ShellyPlusI4/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
sensors = DiscoverDHTSensors(pin_in, pin_out);
}
} else {
RestoreUART();
InitSysLED(LED_GPIO, LED_ON);
}
InitSysBtn(BTN_GPIO, BTN_DOWN);
Expand Down
10 changes: 10 additions & 0 deletions src/shelly_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ static std::vector<const HAPAccessory *> s_hap_accs;
static std::unique_ptr<TempSensor> s_sys_temp_sensor;
std::vector<std::unique_ptr<Component>> g_comps;

void RestoreUART() {
struct mgos_uart_config ucfg;
int uart_no = 0;
mgos_uart_config_set_defaults(uart_no, &ucfg);

if (!mgos_uart_configure(uart_no, &ucfg)) {
LOG(LL_ERROR, ("Failed to configure UART%d", uart_no));
}
}

bool DetectAddon(int pin_in, int pin_out) {
// case 1: input with pull up
mgos_gpio_setup_input(pin_in, MGOS_GPIO_PULL_UP);
Expand Down
1 change: 1 addition & 0 deletions src/shelly_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace shelly {
extern std::vector<std::unique_ptr<Component>> g_comps;

bool DetectAddon(int pin_in, int pin_out);
void RestoreUART();
Input *FindInput(int id);
Output *FindOutput(int id);
PowerMeter *FindPM(int id);
Expand Down

0 comments on commit d6af072

Please sign in to comment.