Skip to content

Commit

Permalink
Add push-button mode, expose state persistence setting
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
rojer committed Feb 6, 2020
1 parent 59d8c44 commit 3fa6854
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
91 changes: 87 additions & 4 deletions fs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
h1 { margin: 0; padding-top: 0.5em; text-align: center; }
.container { padding: 0 1em; margin: 1em auto; max-width: 640px; background: #fafafa; }
.form-control { margin: 0.5em 0; }
.form-control input, .form-control button { min-width: 15em; }
.form label { min-width: 8em; display: inline-block; }
.form-control input, .form-control button { Xmin-width: 15em; }
.form label { min-width: 10em; display: inline-block; }
.form { padding: 1em 0; }
.btn { background: #2079b0; }
.spin {
Expand Down Expand Up @@ -64,6 +64,25 @@ <h1 class="" id="sw1_name">SW1 Name</h1>
</div>
</div>
</div>
<div class="form">
<div class="">
<div class="form-control">
<label>Push-button:</label>
<input type="checkbox" id="sw1_pushbutton">
</div>
<div class="form-control">
<label>Persist state:</label>
<input type="checkbox" id="sw1_persist">
</div>
<div class="form-control">
<label></label>
<button class="btn" id="sw1_save_btn">
<span id="sw1_save_spinner"></span>
<label>Save</label>
</button>
</div>
</div>
</div>
</div>

<div class="container" id="sw2_container" style="display: none">
Expand All @@ -83,6 +102,25 @@ <h1 class="" id="sw2_name">SW2 Name</h1>
</div>
</div>
</div>
<div class="form">
<div class="">
<div class="form-control">
<label>Push-button:</label>
<input type="checkbox" id="sw2_pushbutton">
</div>
<div class="form-control">
<label>Persist state:</label>
<input type="checkbox" id="sw2_persist">
</div>
<div class="form-control">
<label></label>
<button class="btn" id="sw2_save_btn">
<span id="sw2_save_spinner"></span>
<label>Save</label>
</button>
</div>
</div>
</div>
</div>

<div class="container" id="homekit_container" style="visibility: hidden">
Expand Down Expand Up @@ -275,15 +313,56 @@ <h1 class="">Firmware</h1>
sw_set_common(
document.getElementById("sw1_container").sw_id,
!document.getElementById("sw1_container").sw_state,
!document.getElementById("sw1_set_spinner"),
document.getElementById("sw1_set_spinner"),
);
}

document.getElementById("sw2_set_btn").onclick = function() {
sw_set_common(
document.getElementById("sw2_container").sw_id,
!document.getElementById("sw2_container").sw_state,
!document.getElementById("sw2_set_spinner"),
document.getElementById("sw2_set_spinner"),
);
}

function sw_save_common(cfg_key, pb, persist, spinner) {
spinner.className = "spin";
var data = {
config: {},
save: true,
reboot: false,
};
data.config[cfg_key] = {
in_push_button: pb,
persist_state: persist,
};
axios.post(host + "/rpc/Config.Set", data).then(function(res) {
}).catch(function(err) {
if (err.response) {
err = err.response.data.message;
}
alert(err);
}).then(function() {
spinner.className = "";
getInfo();
});
}

document.getElementById("sw1_save_btn").onclick = function() {
sw_save_common(
"sw1",
document.getElementById("sw1_pushbutton").checked,
document.getElementById("sw1_persist").checked,
document.getElementById("sw1_save_spinner"),
);
}

document.getElementById("sw2_save_btn").onclick = function() {
sw_save_common(
"sw2",
document.getElementById("sw2_pushbutton").checked,
document.getElementById("sw2_persist").checked,
document.getElementById("sw2_save_spinner"),
);
}

Expand All @@ -309,6 +388,8 @@ <h1 class="">Firmware</h1>
document.getElementById("sw1_name").innerText = res.data.sw1.name;
document.getElementById("sw1_state").innerText = (res.data.sw1.state ? "on" : "off");
document.getElementById("sw1_btn_label").innerText = "Turn " + (res.data.sw1.state ? "Off" : "On");
document.getElementById("sw1_pushbutton").checked = res.data.sw1.pb;
document.getElementById("sw1_persist").checked = res.data.sw1.persist;
document.getElementById("sw1_container").sw_id = res.data.sw1.id;
document.getElementById("sw1_container").sw_state = res.data.sw1.state;
document.getElementById("sw1_container").style.display = "block";
Expand All @@ -317,6 +398,8 @@ <h1 class="">Firmware</h1>
document.getElementById("sw2_name").innerText = res.data.sw2.name;
document.getElementById("sw2_state").innerText = (res.data.sw2.state ? "on" : "off");
document.getElementById("sw2_btn_label").innerText = "Turn " + (res.data.sw2.state ? "Off" : "On");
document.getElementById("sw2_pushbutton").checked = res.data.sw2.pb;
document.getElementById("sw2_persist").checked = res.data.sw2.persist;
document.getElementById("sw2_container").sw_id = res.data.sw2.id;
document.getElementById("sw2_container").sw_state = res.data.sw2.state;
document.getElementById("sw2_container").style.display = "block";
Expand Down
5 changes: 3 additions & 2 deletions mos.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
author: 'Deomid "rojer" Ryabkov'
description: A HomeKit firmware for Shelly switches
version: 1.0.0
version: 1.1.0
platform: esp8266

libs_version: ${mos.version}
Expand All @@ -26,7 +26,7 @@ config_schema:
- ["sw.out_gpio", "i", -1, {"Output pin"}]
- ["sw.out_on_value", "i", 1, {"Active (ON) output for gpio, 1 or 0"}]
- ["sw.in_gpio", "i", -1, {"Input sensing pin"}]
- ["sw.in_on_value", "i", 1, {"Input pin active (ON) value, 1 or 0"}]
- ["sw.in_push_button", "b", false, {"If true, every transition causes output to be toggled, otherwise output = input"}]
- ["sw.state", "b", false, {"State of the switch"}]
- ["sw.persist_state", "b", false, {"Whether state of the switch should be persisted across reboots."}]

Expand Down Expand Up @@ -149,6 +149,7 @@ conds:
variant: ubuntu-nossl
config_schema:
- ["device.id", "ShellyU-????"]
- ["http.document_root", "."]
- ["sw1", "sw", {"title": "SW settings"}]
- ["sw1.id", 0]
- ["sw1.name", "SW"]
Expand Down
12 changes: 8 additions & 4 deletions src/shelly_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,24 @@ static void shelly_get_info_handler(struct mg_rpc_request_info *ri,
ri,
"{id: %Q, app: %Q, host: %Q, version: %Q, fw_build: %Q, "
#ifdef MGOS_CONFIG_HAVE_SW1
"sw1: {id: %d, name: %Q, state: %B},"
"sw1: {id: %d, name: %Q, pb: %B, persist: %B, state: %B},"
#endif
#ifdef MGOS_CONFIG_HAVE_SW2
"sw2: {id: %d, name: %Q, state: %B},"
"sw2: {id: %d, name: %Q, pb: %B, persist: %B, state: %B},"
#endif
"wifi_en: %B, wifi_ssid: %Q, wifi_pass: %Q, "
"hap_provisioned: %B, hap_paired: %B}",
mgos_sys_config_get_device_id(), MGOS_APP, mgos_dns_sd_get_host_name(),
mgos_sys_ro_vars_get_fw_version(), mgos_sys_ro_vars_get_fw_id(),
#ifdef MGOS_CONFIG_HAVE_SW1
mgos_sys_config_get_sw1_id(), mgos_sys_config_get_sw1_name(), sw1.state,
mgos_sys_config_get_sw1_id(), mgos_sys_config_get_sw1_name(),
mgos_sys_config_get_sw1_in_push_button(),
mgos_sys_config_get_sw1_persist_state(), sw1.state,
#endif
#ifdef MGOS_CONFIG_HAVE_SW2
mgos_sys_config_get_sw2_id(), mgos_sys_config_get_sw2_name(), sw2.state,
mgos_sys_config_get_sw2_id(), mgos_sys_config_get_sw2_name(),
mgos_sys_config_get_sw2_in_push_button(),
mgos_sys_config_get_sw2_persist_state(), sw2.state,
#endif
mgos_sys_config_get_wifi_sta_enable(), (ssid ? ssid : ""),
(pass ? pass : ""), hap_provisioned, hap_paired);
Expand Down
17 changes: 13 additions & 4 deletions src/shelly_sw_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*limitations under the License.
*/

#include "shelly_sw_service.h"
Expand All @@ -28,6 +28,7 @@ struct shelly_sw_service_ctx {
const HAPAccessory *hap_accessory;
const HAPService *hap_service;
bool state;
bool pb_state;
int change_cnt; // State change counter for reset.
double last_change_ts; // Timestamp of last change (uptime).
};
Expand Down Expand Up @@ -203,8 +204,14 @@ static const HAPCharacteristic *shelly_sw_on_char(uint16_t iid) {

static void shelly_sw_in_cb(int pin, void *arg) {
struct shelly_sw_service_ctx *ctx = arg;
bool in_state = (mgos_gpio_read(pin) == ctx->cfg->in_on_value);
shelly_sw_set_state_ctx(ctx, in_state, "input");
bool in_state = mgos_gpio_read(pin);
if (ctx->cfg->in_push_button) {
if (in_state) { // Only on 0 -> 1 transitions.
shelly_sw_set_state_ctx(ctx, !ctx->state, "button");
}
} else {
shelly_sw_set_state_ctx(ctx, in_state, "switch");
}
(void) pin;
}

Expand Down Expand Up @@ -241,6 +248,8 @@ HAPService *shelly_sw_service_create(const struct mgos_config_sw *cfg) {
mgos_gpio_set_button_handler(cfg->in_gpio, MGOS_GPIO_PULL_NONE,
MGOS_GPIO_INT_EDGE_ANY, 20, shelly_sw_in_cb,
ctx);
shelly_sw_in_cb(cfg->in_gpio, ctx);
if (!ctx->cfg->in_push_button) {
shelly_sw_in_cb(cfg->in_gpio, ctx);
}
return svc;
}

0 comments on commit 3fa6854

Please sign in to comment.