Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f729d91
commit 9aae724
Showing
15 changed files
with
226 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import {cr} from './utils' | ||
|
||
const $ = require('./lib/chibi') | ||
|
||
const HTTPS = window.location.protocol.match(/s:/) | ||
|
||
const PERIOD_NORMAL = 500 | ||
const PERIOD_FAIL = 2500 | ||
|
||
const w = window.GPIO_Ctl = {} | ||
|
||
let scheduledSampleTimeout = null | ||
let lastState = {'io2': -1, 'io4': -1, 'io5': -1} | ||
|
||
let $in2, $in4, $in5 | ||
|
||
let enablePolling | ||
|
||
function scheduleSample (time) { | ||
if (!enablePolling || window._demo) return // no scanning in demo mode | ||
|
||
clearTimeout(scheduledSampleTimeout) | ||
scheduledSampleTimeout = setTimeout(sampleGPIOs, time) | ||
} | ||
|
||
function onSampled (resp, status) { | ||
if (status !== 200) { | ||
// bad response | ||
scheduleSample(PERIOD_FAIL) // wait 5s then retry | ||
return | ||
} | ||
|
||
try { | ||
if (typeof resp !== 'object') resp = JSON.parse(resp) | ||
|
||
if (resp.io2 !== lastState.io2) $in2.toggleClass('active', resp.io2) | ||
if (resp.io4 !== lastState.io4) $in4.toggleClass('active', resp.io4) | ||
if (resp.io5 !== lastState.io5) $in5.toggleClass('active', resp.io5) | ||
} catch (e) { | ||
console.log(e) | ||
scheduleSample(PERIOD_FAIL) | ||
return | ||
} | ||
|
||
lastState = resp | ||
|
||
scheduleSample(PERIOD_NORMAL) | ||
} | ||
|
||
function sampleGPIOs (getargs = '') { | ||
clearTimeout(scheduledSampleTimeout) | ||
$.get(`${HTTPS ? 'https' : 'http'}://${window._root}/api/v1/gpio${getargs}`, onSampled, {loader: getargs !== ''}) | ||
} | ||
|
||
w.init = function (state, doPoll) { | ||
|
||
$in2 = $('.x-in2') | ||
$in4 = $('.x-in4') | ||
$in5 = $('.x-in5') | ||
|
||
enablePolling = doPoll | ||
|
||
$('.gpio-indicator, .x-in-handle') | ||
.on('click', function (e) { | ||
let num = $(this).data('num') | ||
sampleGPIOs(`?do${num}=${1 - lastState['io' + num]}`) | ||
}) | ||
|
||
$('.x-in-handle') | ||
.on('keypress', cr(function (e) { | ||
let num = $(this).data('num') | ||
sampleGPIOs(`?do${num}=${1 - lastState['io' + num]}`) | ||
})) | ||
|
||
onSampled(state, 200) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<form class="Box str mobcol" action="<?= e(url('system_set')) ?>" method="GET" id="form-gpio"> | ||
<input type="hidden" name="redir" value="/cfg/gpio"> | ||
<h2><?= tr('gpio_config.title_config') ?></h2> | ||
|
||
<?php foreach([2,4,5] as $n): ?> | ||
<div class="Row"> | ||
<label for="gpio<?=$n?>_conf"><?= tr("gpiox_config", [':x' => $n]) ?></label> | ||
<select name="gpio<?=$n?>_conf" id="gpio<?=$n?>_conf"> | ||
<option value="0"><?= tr("gpio_config.off" . ($n==2?'_2':'')) ?></option> | ||
<option value="1"><?= tr("gpio_config.out_initial0") ?></option> | ||
<option value="2"><?= tr("gpio_config.out_initial1") ?></option> | ||
<option value="3"><?= tr("gpio_config.in_pull") ?></option> | ||
<option value="4"><?= tr("gpio_config.in_nopull") ?></option> | ||
</select> | ||
</div> | ||
<?php endforeach; ?> | ||
|
||
<div class="Row buttons"> | ||
<a class="button icn-ok" href="#" onclick="qs('#form-gpio').submit()"><?= tr('apply') ?></a> | ||
</div> | ||
</form> | ||
|
||
<div class="Box str"> | ||
<h2><?= tr('gpio_config.title_control') ?></h2> | ||
|
||
<div class="Row explain nomargintop"> | ||
<?= tr('gpio_config.explain') ?> | ||
</div> | ||
|
||
<?php foreach([2,4,5] as $n): ?> | ||
<div class="Row x-iorow-<?=$n?>"> | ||
<label class="x-in-handle" data-num="<?=$n?>" tabindex="0"> | ||
<?= tr("gpiox_level", [':x' => $n]) ?> | ||
|
||
<span class="gpio-indicator x-in<?=$n?>" data-num="<?=$n?>"></span> | ||
</label> | ||
</div> | ||
<?php endforeach; ?> | ||
</div> | ||
|
||
<script> | ||
GPIO_Ctl.init(%gpio_initial%, %gpio2_conf%>2 || %gpio4_conf%>2 || %gpio5_conf%>2) | ||
|
||
$('#gpio2_conf').val(%gpio2_conf%); | ||
if (%gpio2_conf% === 0) $('.x-iorow-2').addClass('hidden'); | ||
|
||
$('#gpio4_conf').val(%gpio4_conf%); | ||
if (%gpio4_conf% === 0) $('.x-iorow-4').addClass('hidden'); | ||
|
||
$('#gpio5_conf').val(%gpio5_conf%); | ||
if (%gpio5_conf% === 0) $('.x-iorow-5').addClass('hidden'); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.