Skip to content

Commit 16b22c6

Browse files
committed
Match stock input mode settings
0 - Momentary, 1 - Toggle, 2 - Edge, 3 - Detached We don't have "Activation" yet. #1 #6
1 parent ac13767 commit 16b22c6

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

fs/index.html

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ <h1 class="" id="sw1_name">SW1 Name</h1>
6767
<div class="form">
6868
<div class="">
6969
<div class="form-control">
70-
<label>Push-button:</label>
71-
<input type="checkbox" id="sw1_pushbutton">
70+
<label>Mode:</label>
71+
<select id="sw1_in_mode">
72+
<option id="sw1_in_mode_0" value="0">Momentary</option>
73+
<option id="sw1_in_mode_1" value="1">Toggle</option>
74+
<option id="sw1_in_mode_2" value="2">Edge</option>
75+
<option id="sw1_in_mode_3" value="3">Detached</option>
76+
</select>
7277
</div>
7378
<div class="form-control">
7479
<label>Persist state:</label>
@@ -105,8 +110,13 @@ <h1 class="" id="sw2_name">SW2 Name</h1>
105110
<div class="form">
106111
<div class="">
107112
<div class="form-control">
108-
<label>Push-button:</label>
109-
<input type="checkbox" id="sw2_pushbutton">
113+
<label>Mode:</label>
114+
<select id="sw2_in_mode">
115+
<option id="sw2_in_mode_0" value="0">Momentary</option>
116+
<option id="sw2_in_mode_1" value="1">Toggle</option>
117+
<option id="sw2_in_mode_2" value="2">Edge</option>
118+
<option id="sw2_in_mode_3" value="3">Detached</option>
119+
</select>
110120
</div>
111121
<div class="form-control">
112122
<label>Persist state:</label>
@@ -325,15 +335,15 @@ <h1 class="">Firmware</h1>
325335
);
326336
}
327337

328-
function sw_save_common(cfg_key, pb, persist, spinner) {
338+
function sw_save_common(cfg_key, in_mode, persist, spinner) {
329339
spinner.className = "spin";
330340
var data = {
331341
config: {},
332342
save: true,
333343
reboot: false,
334344
};
335345
data.config[cfg_key] = {
336-
in_push_button: pb,
346+
in_mode: parseInt(in_mode),
337347
persist_state: persist,
338348
};
339349
axios.post(host + "/rpc/Config.Set", data).then(function(res) {
@@ -351,7 +361,7 @@ <h1 class="">Firmware</h1>
351361
document.getElementById("sw1_save_btn").onclick = function() {
352362
sw_save_common(
353363
"sw1",
354-
document.getElementById("sw1_pushbutton").checked,
364+
document.getElementById("sw1_in_mode").value,
355365
document.getElementById("sw1_persist").checked,
356366
document.getElementById("sw1_save_spinner"),
357367
);
@@ -360,7 +370,7 @@ <h1 class="">Firmware</h1>
360370
document.getElementById("sw2_save_btn").onclick = function() {
361371
sw_save_common(
362372
"sw2",
363-
document.getElementById("sw2_pushbutton").checked,
373+
document.getElementById("sw2_in_mode").value,
364374
document.getElementById("sw2_persist").checked,
365375
document.getElementById("sw2_save_spinner"),
366376
);
@@ -388,7 +398,7 @@ <h1 class="">Firmware</h1>
388398
document.getElementById("sw1_name").innerText = res.data.sw1.name;
389399
document.getElementById("sw1_state").innerText = (res.data.sw1.state ? "on" : "off");
390400
document.getElementById("sw1_btn_label").innerText = "Turn " + (res.data.sw1.state ? "Off" : "On");
391-
document.getElementById("sw1_pushbutton").checked = res.data.sw1.pb;
401+
document.getElementById("sw1_in_mode_" + res.data.sw1.in_mode).selected = true;
392402
document.getElementById("sw1_persist").checked = res.data.sw1.persist;
393403
document.getElementById("sw1_container").sw_id = res.data.sw1.id;
394404
document.getElementById("sw1_container").sw_state = res.data.sw1.state;
@@ -398,7 +408,7 @@ <h1 class="">Firmware</h1>
398408
document.getElementById("sw2_name").innerText = res.data.sw2.name;
399409
document.getElementById("sw2_state").innerText = (res.data.sw2.state ? "on" : "off");
400410
document.getElementById("sw2_btn_label").innerText = "Turn " + (res.data.sw2.state ? "Off" : "On");
401-
document.getElementById("sw2_pushbutton").checked = res.data.sw2.pb;
411+
document.getElementById("sw2_in_mode_" + res.data.sw2.in_mode).selected = true;
402412
document.getElementById("sw2_persist").checked = res.data.sw2.persist;
403413
document.getElementById("sw2_container").sw_id = res.data.sw2.id;
404414
document.getElementById("sw2_container").sw_state = res.data.sw2.state;

mos.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
author: 'Deomid "rojer" Ryabkov'
22
description: A HomeKit firmware for Shelly switches
3-
version: 1.1.1
3+
version: 1.2.0
44
platform: esp8266
55

66
libs_version: ${mos.version}
@@ -26,7 +26,7 @@ config_schema:
2626
- ["sw.out_gpio", "i", -1, {"Output pin"}]
2727
- ["sw.out_on_value", "i", 1, {"Active (ON) output for gpio, 1 or 0"}]
2828
- ["sw.in_gpio", "i", -1, {"Input sensing pin"}]
29-
- ["sw.in_push_button", "b", false, {"If true, every transition causes output to be toggled, otherwise output = input"}]
29+
- ["sw.in_mode", "i", 1, {"0 - Momentary, 1 - Toggle, 2 - Edge, 3 - Detached"}]
3030
- ["sw.state", "b", false, {"State of the switch"}]
3131
- ["sw.persist_state", "b", false, {"Whether state of the switch should be persisted across reboots."}]
3232

src/shelly_main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,23 +285,23 @@ static void shelly_get_info_handler(struct mg_rpc_request_info *ri,
285285
ri,
286286
"{id: %Q, app: %Q, host: %Q, version: %Q, fw_build: %Q, "
287287
#ifdef MGOS_CONFIG_HAVE_SW1
288-
"sw1: {id: %d, name: %Q, pb: %B, persist: %B, state: %B},"
288+
"sw1: {id: %d, name: %Q, in_mode: %d, persist: %B, state: %B},"
289289
#endif
290290
#ifdef MGOS_CONFIG_HAVE_SW2
291-
"sw2: {id: %d, name: %Q, pb: %B, persist: %B, state: %B},"
291+
"sw2: {id: %d, name: %Q, in_mode: %d, persist: %B, state: %B},"
292292
#endif
293293
"wifi_en: %B, wifi_ssid: %Q, wifi_pass: %Q, "
294294
"hap_provisioned: %B, hap_paired: %B}",
295295
mgos_sys_config_get_device_id(), MGOS_APP, mgos_dns_sd_get_host_name(),
296296
mgos_sys_ro_vars_get_fw_version(), mgos_sys_ro_vars_get_fw_id(),
297297
#ifdef MGOS_CONFIG_HAVE_SW1
298298
mgos_sys_config_get_sw1_id(), mgos_sys_config_get_sw1_name(),
299-
mgos_sys_config_get_sw1_in_push_button(),
299+
mgos_sys_config_get_sw1_in_mode(),
300300
mgos_sys_config_get_sw1_persist_state(), sw1.state,
301301
#endif
302302
#ifdef MGOS_CONFIG_HAVE_SW2
303303
mgos_sys_config_get_sw2_id(), mgos_sys_config_get_sw2_name(),
304-
mgos_sys_config_get_sw2_in_push_button(),
304+
mgos_sys_config_get_sw2_in_mode(),
305305
mgos_sys_config_get_sw2_persist_state(), sw2.state,
306306
#endif
307307
mgos_sys_config_get_wifi_sta_enable(), (ssid ? ssid : ""),

src/shelly_sw_service.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
#define IID_BASE 0x100
2323
#define IID_STEP 4
2424

25+
enum shelly_sw_in_mode {
26+
SHELLY_SW_IN_MODE_MOMENTARY = 0,
27+
SHELLY_SW_IN_MODE_TOGGLE = 1,
28+
SHELLY_SW_IN_MODE_EDGE = 2,
29+
SHELLY_SW_IN_MODE_DETACHED = 3,
30+
};
31+
2532
struct shelly_sw_service_ctx {
2633
const struct mgos_config_sw *cfg;
2734
HAPAccessoryServerRef *hap_server;
@@ -205,12 +212,21 @@ static const HAPCharacteristic *shelly_sw_on_char(uint16_t iid) {
205212
static void shelly_sw_in_cb(int pin, void *arg) {
206213
struct shelly_sw_service_ctx *ctx = arg;
207214
bool in_state = mgos_gpio_read(pin);
208-
if (ctx->cfg->in_push_button) {
209-
if (in_state) { // Only on 0 -> 1 transitions.
215+
switch ((enum shelly_sw_in_mode) ctx->cfg->in_mode) {
216+
case SHELLY_SW_IN_MODE_MOMENTARY:
217+
if (in_state) { // Only on 0 -> 1 transitions.
218+
shelly_sw_set_state_ctx(ctx, !ctx->state, "button");
219+
}
220+
break;
221+
case SHELLY_SW_IN_MODE_TOGGLE:
222+
shelly_sw_set_state_ctx(ctx, in_state, "switch");
223+
break;
224+
case SHELLY_SW_IN_MODE_EDGE:
210225
shelly_sw_set_state_ctx(ctx, !ctx->state, "button");
211-
}
212-
} else {
213-
shelly_sw_set_state_ctx(ctx, in_state, "switch");
226+
break;
227+
case SHELLY_SW_IN_MODE_DETACHED:
228+
// Nothing to do
229+
break;
214230
}
215231
(void) pin;
216232
}
@@ -248,7 +264,7 @@ HAPService *shelly_sw_service_create(const struct mgos_config_sw *cfg) {
248264
mgos_gpio_set_button_handler(cfg->in_gpio, MGOS_GPIO_PULL_NONE,
249265
MGOS_GPIO_INT_EDGE_ANY, 20, shelly_sw_in_cb,
250266
ctx);
251-
if (!ctx->cfg->in_push_button) {
267+
if (ctx->cfg->in_mode == SHELLY_SW_IN_MODE_TOGGLE) {
252268
shelly_sw_in_cb(cfg->in_gpio, ctx);
253269
}
254270
return svc;

0 commit comments

Comments
 (0)