Skip to content

Commit

Permalink
Fixed generic input/output functionality (unhandled class methods) in…
Browse files Browse the repository at this point in the history
… html5ui. Fixed nano and neopixel input validation
  • Loading branch information
hwinther committed Oct 4, 2018
1 parent f024e82 commit 73ca281
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 32 deletions.
2 changes: 1 addition & 1 deletion deploy/micropython/esp8266/deploy-dev.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
rem python -m esptool --port %1 erase_flash
python -m esptool --chip esp8266 --port %1 --baud 921600 --before default_reset --after hard_reset write_flash --verify --flash_size=detect --flash_mode=qio 0 v1.9.4-568-g4df194394-dirty-2018-09-28.bin
python -m esptool --chip esp8266 --port %1 --baud 921600 --before default_reset --after hard_reset write_flash --verify --flash_size=detect --flash_mode=qio 0 v1.9.4-568-g4df194394-dirty-2018-10-04.bin
10 changes: 9 additions & 1 deletion src/core/prometheus/pnano.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=utf-8
import prometheus
import prometheus.logging as logging
import time
import gc

Expand Down Expand Up @@ -51,5 +52,12 @@ def infrain(self, **kwargs):
return '%s %s %s' % (_type, code, bitlength)

@prometheus.Registry.register('NanoI2C', 'io')
def infraout(self, device, code, bitlength, **kwargs):
def infraout(self, device=None, code=0, bitlength=20, **kwargs):
if device is None:
logging.notice('infraout did not get device')
return
if not isinstance(code, int):
code = int(code)
if not isinstance(bitlength, int):
bitlength = int(bitlength)
self.i2c.writeto(8, b'%s %x %d' % (device, code, bitlength))
4 changes: 2 additions & 2 deletions src/core/prometheus/pneopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, pin, number):
self.n = number

@prometheus.Registry.register('NeoPixel', 'sp')
def set_pixel(self, pixel, r, g, b, **kwargs):
def set_pixel(self, pixel=0, r=0, g=0, b=0, **kwargs):
# combination of the two methods below
if not isinstance(pixel, int):
pixel = int(pixel)
Expand All @@ -40,7 +40,7 @@ def set_pixel(self, pixel, r, g, b, **kwargs):
self.np.write()

@prometheus.Registry.register('NeoPixel', 's')
def set(self, pixel, r, g, b, **kwargs):
def set(self, pixel=0, r=0, g=0, b=0, **kwargs):
if not isinstance(pixel, int):
pixel = int(pixel)
if not isinstance(r, int):
Expand Down
16 changes: 9 additions & 7 deletions src/core/prometheus/server/socketserver/jsonrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ def handle_request(self, path, query, sock):
if path in self.instance.cached_urls.keys():
logging.notice('found matching command_key')
value = self.instance.cached_urls[path] # type: prometheus.RegisteredMethod
self.handle_data(value.command_key, source=sock, context=query)
if value.return_type != 'str':
# give default empty response
self.reply(return_value=None, source=sock, context=query)
if not self.handle_data(value.command_key, source=sock, context=query):
if value.return_type != 'str':
# give default empty response
self.reply(return_value=None, source=sock, context=query)
return True
elif path == b'schema':
schema = dict()
Expand All @@ -151,9 +151,9 @@ def handle_request(self, path, query, sock):
elif path == b'favicon.ico':
self.reply(return_value=prometheus.server.favicon,
source=sock,
context=query,
contenttype='image/x-icon')
return True

else:
return self.handle_data(path, source=sock, context=query)

Expand All @@ -173,11 +173,13 @@ def reply(self, return_value, source=None, context=None, contenttype=None, **kwa
if isinstance(return_value, (dict, list)):
msg = json.dumps(return_value)
else:
if isinstance(return_value, bytes):
return_value = return_value.decode('utf-8')
print('ret: %s' % repr(return_value))
msg = json.dumps({'value': return_value})

if context is not None and b'callback' in context.keys():
callback = context[b'callback'].decode('utf-8')
if context is not None and 'callback' in context.keys():
callback = context['callback'] # .decode('utf-8')
msg = '%s(%s)' % (callback, msg)

# convert to bytes
Expand Down
9 changes: 7 additions & 2 deletions src/devices/cpython-local-test/localtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ def __init__(self):
self.hygrometer = prometheus.Adc(0)
self.register(prefix='h', hygrometer=self.hygrometer)

self.ssd = prometheus.pssd1306.SSD1306(None)
self.register(prefix='ss', ssd=self.ssd)
# self.i2c = machine.I2C(scl=machine.Pin(0), sda=machine.Pin(4), freq=400000)
# logging.info('i2c: %s' % self.i2c.scan())

# self.ssd = prometheus.pssd1306.SSD1306(self.i2c)
# self.register(prefix='ss', ssd=self.ssd)

# self.ssd.text('init', 0, 0)

@prometheus.Registry.register('LocalTest', '1', 'OUT')
def test1(self, **kwargs):
Expand Down
74 changes: 56 additions & 18 deletions src/gui/html5ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,25 @@
else
{
console.log('Unhandled class: ' + obj.class + ' from host: ' + lanotHostInner.baseUri + ' showing methods as buttons.');
var lanotHost = this.lanotHost;
jQuery.each(obj.methods, function(method_name, method_uri) {
// console.log('method_name=' + method_name + ' method_uri=' + method_uri);

for (var method_name in obj.methods) {
if (!obj.methods.hasOwnProperty(method_name)) {
continue;
}
var method_uri = obj.methods[method_name];

var method_friendly_name = method_name.split('_').join(' ');
method_friendly_name = method_friendly_name.charAt(0).toUpperCase() + method_friendly_name.slice(1);
var btnElementId = 'btn_' + idPostfix + '_' + method_friendly_name.replace(' ', '_').toLowerCase();
console.log(btnElementId);
lanotHostContainerInner.panelBody.append(
'<div class="input-group input-group-sm"><button type="button" class="btn btn-default btn-sm btnmethod" data-name="' + method_friendly_name + '" data-host="' + lanotHostInner.name + '" data-hostname="' + lanotHostInner.identifier + '" data-uri="' + method_uri + '" id="' + btnElementId + '">\n' +
method_friendly_name + '\n</button></div>\n');
$('#' + btnElementId).data('lanotHost', lanotHost);
});
btnElementId = 'btn_' + idPostfix + '_' + method_friendly_name.replace(' ', '_').toLowerCase();

var gi = new GenericInput(lanotHostContainerInner, lanotHostInner, idPostfix, btnElementId, obj, nodePath, friendlyName, method_friendly_name, method_uri);
}
}
});

// TODO: if update is called more than once, we will be adding additional events to the existing elements (if not in this particular scope, then the other, non updated containers will get duplicate events
// perhaps limit the event attachment to this particular container, and wipe the container as part of the update process
this.lanotHostContainer.attachEvents();

$('.btnmethod').click(function() {
this.lanotHost = $(this).data('lanotHost');
var uri = this.lanotHost.baseUri + '/' + $(this).data('uri');
$.ajax({
url: uri,
dataType: 'jsonp'});
});
}

function dht11ClickEvent() {
Expand Down Expand Up @@ -324,6 +317,51 @@
});
}

function genericInputClickEvent() {
var genericInput = $(this).data('genericinput');
var requestArgs = genericInput.textBoxInput.val();
var uri = genericInput.lanotHost.baseUri + '/' + $(this).data('uri');
if (requestArgs !== '') {
uri = genericInput.lanotHost.baseUri + '/' + $(this).data('uri') + '?' + requestArgs;
}
console.log(uri);
$.ajax({
url: uri,
dataType: 'jsonp',
indexValue: genericInput,
success: function (data) {
var value = data['value'];
if (value === null) {
value = 'No value returned';
}
genericInput.textBoxOutput.val(value);
}
});
}
function GenericInput(lanotHostContainer, lanotHost, idPostfix, btnElementId, obj, nodePath, friendlyName, methodFriendlyName, methodUri) {
this.lanotHost = lanotHost;
var completeFriendlyName = methodFriendlyName.replace(' ', '_').toLowerCase();
var textBoxInputId = 'gi_input_' + completeFriendlyName + idPostfix;
var textBoxOutputId = 'gi_output_' + completeFriendlyName + idPostfix;

lanotHostContainer.panelBody.append(
'<div class="input-group input-group-sm">\n' +
' <button type="button" class="btn btn-default btn-sm btngi" data-uri="' + methodUri + '" id="' + btnElementId + '">' +
' ' + friendlyName + ' send ' + methodFriendlyName +
' </button>\n' +
' <input class="form-control" type="textbox" id="' + textBoxInputId + '" />\n' +
' <input class="form-control" type="textbox" id="' + textBoxOutputId + '" />\n' +
'</div>\n');

this.textBoxInput = $('#' + textBoxInputId);
this.textBoxOutput = $('#' + textBoxOutputId);
$('#' + btnElementId).data('genericinput', this);

lanotHostContainer.eventAttachers.push(function(container) {
container.find('#' + btnElementId).click(genericInputClickEvent);
});
}

function digitalClickEvent() {
var digital = $(this).data('digital');
var uri = digital.lanotHost.baseUri + '/' + $(this).data('uri');
Expand Down
18 changes: 17 additions & 1 deletion tools/micropython-mockup/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,25 @@ def __init__(self, freq, scl, sda):
self.freq = freq
self.scl = scl
self.sda = sda
import smbus
self.bus = smbus.SMBus(1)

def scan(self):
return list()
addresses = list()
for device in range(128):
try:
self.bus.read_byte(device)
addresses.append(device)
except IOError as io_error:
if io_error.errno is not 121:
raise
return addresses

def readfrom(self, addr, nbytes, stop=True):
pass

def writeto(self, addr, buf, stop=True):
pass


class SPI(object):
Expand Down

0 comments on commit 73ca281

Please sign in to comment.