Skip to content

Commit

Permalink
added a dedicated GPIO page
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyPork committed Aug 19, 2018
1 parent f729d91 commit 9aae724
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 78 deletions.
9 changes: 5 additions & 4 deletions _debug_replacements.php
Expand Up @@ -87,10 +87,6 @@

'sta_mac' => '5c:cf:7f:02:74:51',
'ap_mac' => '5e:cf:7f:02:74:51',

'gpio2_conf' => '0',
'gpio4_conf' => '1',
'gpio5_conf' => '1',

'width' => '80',
'height' => '25',
Expand All @@ -110,4 +106,9 @@
'access_name' => 'espterm',

'allow_decopt_12' => 0,

'gpio2_conf' => 0,
'gpio4_conf' => 1,
'gpio5_conf' => 2,
'gpio_initial' => '{"io2":0,"io4":0,"io5":1}',
];
1 change: 1 addition & 0 deletions _pages.php
Expand Up @@ -37,6 +37,7 @@ function pg($key, $bc, $icon, $path, $titleKey = null)
pg('restore_defaults', 'api', '', '/cfg/system/restore_defaults');
pg('restore_hard', 'api', '', '/cfg/system/restore_hard');

pg('cfg_gpio', 'cfg', 'gpio', '/cfg/gpio');
pg('help', 'cfg page-help', 'help', '/help');
pg('about', 'cfg page-about', 'about', '/about');
pg('term', 'term', '', '/', 'title.term');
Expand Down
6 changes: 5 additions & 1 deletion base.php
Expand Up @@ -97,7 +97,7 @@ function je($s)
}


function tr($key)
function tr($key, $subs=[])
{
global $_messages, $_messages_fallback, $_messages_common;

Expand All @@ -118,6 +118,10 @@ function tr($key)
// allow tildes in translation
$str = preg_replace('/(?<=[^ \\\\])~(?=[^ ])/', '&nbsp;', $str);
$str = str_replace('\~', '~', $str);

if ($subs) {
$str = str_replace(array_keys($subs), array_values($subs), $str);
}
return $str;
}

Expand Down
Binary file modified fontello/fontello.zip
Binary file not shown.
76 changes: 76 additions & 0 deletions js/gpio_control.js
@@ -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)
}
1 change: 1 addition & 0 deletions js/index.js
Expand Up @@ -4,6 +4,7 @@ require('./notif')
require('./appcommon')
try { require('./term/demo') } catch (err) {}
require('./wifi')
require('./gpio_control')

const $ = require('./lib/chibi')
const { qs } = require('./utils')
Expand Down
2 changes: 1 addition & 1 deletion js/utils.js
Expand Up @@ -21,7 +21,7 @@ exports.qsa = function qsa (s) {
exports.cr = function cr (hdl) {
return function (e) {
if (e.which === 10 || e.which === 13 || e.which === 32) {
hdl()
hdl.call(this, e)
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions lang/cs.php
Expand Up @@ -10,6 +10,7 @@
'menu.cfg_system' => 'Nastavení systému',
'menu.cfg_wifi_conn' => 'Připojování',
'menu.settings' => 'Nastavení',
'menu.cfg_gpio' => 'Ovládání GPIO',

// Terminal page

Expand Down Expand Up @@ -265,16 +266,18 @@
obrazovky a stránky se budou načítat rychleji. Nevýhodou je vyšší spotřeba a citlivost k~rušení.
',
'hwtuning.overclock' => 'Přetaktovat na 160~MHz',

'gpio2_config' => 'Funkce GPIO2',
'gpio4_config' => 'Funkce GPIO4',
'gpio5_config' => 'Funkce GPIO5',

'gpiox_config' => 'Funkce GPIO:x',
'gpiox_level' => 'Stav GPIO:x',
'gpio_config.off' => 'Vypnuto',
'gpio_config.off_2' => 'Debug UART Tx',
'gpio_config.out_initial0' => 'Výstup (výchozí stav 0)',
'gpio_config.out_initial1' => 'Výstup (výchozí stav 1)',
'gpio_config.in_pull' => 'Vstup (s pull-upem)',
'gpio_config.in_pull' => 'Vstup (pull-up)',
'gpio_config.in_nopull' => 'Vstup (plovoucí)',
'gpio_config.explain' =>
'Klikněte na indikátory k přepnutí stavu pinu.
HTTP API pro ovládání pinů viz nápověda.',

// Generic button / dialog labels

Expand Down
11 changes: 8 additions & 3 deletions lang/de.php
Expand Up @@ -10,6 +10,7 @@
'menu.cfg_system' => 'Systemeinstellungen',
'menu.cfg_wifi_conn' => 'Verbinden mit dem Netzwerk',
'menu.settings' => 'Einstellungen',
'menu.cfg_gpio' => 'Steuerung von GPIO',

// Terminal page

Expand Down Expand Up @@ -266,15 +267,19 @@
',
'hwtuning.overclock' => 'Auf 160MHz übertakten',

'gpio2_config' => 'GPIO2 Funktion',
'gpio4_config' => 'GPIO4 Funktion',
'gpio5_config' => 'GPIO5 Funktion',
'gpiox_config' => 'GPIO:x Funktion',
'gpiox_level' => 'GPIO:x Level',
'gpio_config.title_config' => 'GPIO-Einstellungen',
'gpio_config.title_control' => 'GPIO-Status',
'gpio_config.off' => 'Deaktiviert',
'gpio_config.off_2' => 'UART Tx Debuggen',
'gpio_config.out_initial0' => 'Output (Anfangslevel 0)',
'gpio_config.out_initial1' => 'Output (Anfangslevel 1)',
'gpio_config.in_pull' => 'Input (pull-up)',
'gpio_config.in_nopull' => 'Input (floating)',
'gpio_config.explain' => // FIXME google translate
'Klicken Sie auf die Anzeigen, um die Ausgangspin-Ebenen umzuschalten.
Informationen zur GPIO-API finden Sie auf der Hilfeseite.',

// Generic button / dialog labels

Expand Down
11 changes: 8 additions & 3 deletions lang/en.php
Expand Up @@ -10,6 +10,7 @@
'menu.cfg_system' => 'System Settings',
'menu.cfg_wifi_conn' => 'Connecting to Network',
'menu.settings' => 'Settings',
'menu.cfg_gpio' => 'GPIO Control',

// Terminal page

Expand Down Expand Up @@ -266,15 +267,19 @@
',
'hwtuning.overclock' => 'Overclock to 160MHz',

'gpio2_config' => 'GPIO2 function',
'gpio4_config' => 'GPIO4 function',
'gpio5_config' => 'GPIO5 function',
'gpiox_config' => 'GPIO:x function',
'gpiox_level' => 'GPIO:x level',
'gpio_config.title_config' => 'Pin Config',
'gpio_config.title_control' => 'Pin State',
'gpio_config.off' => 'Disabled',
'gpio_config.off_2' => 'Debug UART Tx',
'gpio_config.out_initial0' => 'Output (initial 0)',
'gpio_config.out_initial1' => 'Output (initial 1)',
'gpio_config.in_pull' => 'Input (pull-up)',
'gpio_config.in_nopull' => 'Input (floating)',
'gpio_config.explain' =>
'Click state indicators to toggle output levels.
See the help page for information about the GPIO access HTTP API.',

// Generic button / dialog labels

Expand Down
23 changes: 15 additions & 8 deletions lang/hu.php
Expand Up @@ -10,6 +10,7 @@
'menu.cfg_system' => 'Rendszer beállítások',
'menu.cfg_wifi_conn' => 'Csatlakozás a hálózathoz',
'menu.settings' => 'Beállítások',
'menu.cfg_gpio' => 'GPIO vezérlése',

// Terminal page

Expand Down Expand Up @@ -267,15 +268,21 @@
',
'hwtuning.overclock' => 'Órajel emelése 160MHz-re',

'gpio2_config' => 'GPIO2 function', // TODO translate
'gpio4_config' => 'GPIO4 function',
'gpio5_config' => 'GPIO5 function',
'gpio_config.off' => 'Disabled',
// FIXME google translate
'gpiox_config' => 'GPIO:x funkciója',
'gpiox_level' => 'GPIO:x értéket',
'gpio_config.title_config' => 'GPIO beállítások',
'gpio_config.title_control' => 'GPIO állapot',
'gpio_config.off' => 'Nincs funkció',
'gpio_config.off_2' => 'Debug UART Tx',
'gpio_config.out_initial0' => 'Output (initial 0)',
'gpio_config.out_initial1' => 'Output (initial 1)',
'gpio_config.in_pull' => 'Input (pull-up)',
'gpio_config.in_nopull' => 'Input (floating)',
'gpio_config.out_initial0' => 'Kimenet (kezdeti 0)',
'gpio_config.out_initial1' => 'Kimenet (kezdeti 1)',
'gpio_config.in_pull' => 'Bemenet (pull-up)',
'gpio_config.in_nopull' => 'Bemenet (úszó)',
'gpio_config.explain' =>
'Kattintson a mutatókra a kimeneti tűszintek váltásához.
Lásd a súgó oldalt a GPIO API-ról.
',

// Generic button / dialog labels

Expand Down
52 changes: 52 additions & 0 deletions pages/cfg_gpio.php
@@ -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>
36 changes: 0 additions & 36 deletions pages/cfg_system.php
Expand Up @@ -63,39 +63,6 @@
--><span class="box" tabindex=0 role=checkbox></span>
<input type="hidden" id="overclock" name="overclock" value="%overclock%">
</div>

<div class="Row">
<label for="gpio2_conf"><?= tr("gpio2_config") ?></label>
<select name="gpio2_conf" id="gpio2_conf">
<option value="0"><?= tr("gpio_config.off_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>

<div class="Row">
<label for="gpio4_conf"><?= tr("gpio4_config") ?></label>
<select name="gpio4_conf" id="gpio4_conf">
<option value="0"><?= tr("gpio_config.off") ?></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>

<div class="Row">
<label for="gpio5_conf"><?= tr("gpio5_config") ?></label>
<select name="gpio5_conf" id="gpio5_conf">
<option value="0"><?= tr("gpio_config.off") ?></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>

<div class="Row buttons">
<a class="button icn-ok" href="#" onclick="qs('#form-hw').submit()"><?= tr('apply') ?></a>
Expand Down Expand Up @@ -187,7 +154,4 @@ function writeDefaults() {
}

$('#pwlock').val(%pwlock%);
$('#gpio2_conf').val(%gpio2_conf%);
$('#gpio4_conf').val(%gpio4_conf%);
$('#gpio5_conf').val(%gpio5_conf%);
</script>

0 comments on commit 9aae724

Please sign in to comment.