added Signada extension (for the Citilab ED1 board)

snap7
Bernat Romagosa 2021-08-05 13:05:45 +02:00
rodzic 0b3adc3198
commit 8457f8e2dd
3 zmienionych plików z 109 dodań i 0 usunięć

Wyświetl plik

@ -32,3 +32,7 @@ stream-tools.xml Streams (lazy lists) A variation on the list data type in which
bar-charts.xml Bar charts Takes a table (typically from a CSV data set) as input and reports a summary of the table grouped by the field in the specified column number. The remaining three inputs are used only if the field values are numbers, in which case they can be grouped into buckets (e.g., decades, centuries, etc.). Those three inputs specify the smallest and largest values of interest and, most importantly, the width of a bucket (10 for decades, 100 for centuries). If the field isn't numeric, leave these three inputs empty or set them to zero. In that case, each string value of the field is its own bucket, and they appear sorted alphabetically. The block reports a new table with three columns. The first column contains the bucket name or smallest number. The second column contains a nonnegative integer that says how many records in the input table fall into this bucket. The third column is a subtable containing the actual records from the original table that fall into the bucket. If your buckets aren't of constant width, or you want to group by some function of more than one field, load the "Frequency Distribution Analysis" library instead.
httpBlocks.xml Web services access (https) An extended version of the URL block that allows POST, PUT, and DELETE as well as GET requests, allows using the secure HTTPS protocol, and gives control over headers, etc. Also parses JSON data.
make-variables.xml Create variables Create and manage global/sprite/script variables in a script
~ ~
~ ~
~ ~
signada.xml Signada (Network remote control) Interact with MicroBlocks devices via WiFi. Requires the device to have a TFT display, two buttons and WiFi capability, as well as the Signada MicroBlocks project loaded. The Citilab ED1 and a bunch of the M5Stack boards are some of the devices that work with Signada.

Wyświetl plik

@ -0,0 +1,104 @@
/* Signada - a network remote control procotol
* ===========================================
* By Bernat Romagosa, August 2021
*
* Enables Snap! to talk to wifi-enabled
* MicroBlocks devices.
*
* This protocol was designed for the Citilab
* ED1 microcontroller board, but it can be
* used for any ESP32 board that has a display
* and two buttons, like the M5Stack.
*
* First, load the Signada example project
* from the MicroBlocks IDE and follow the
* instructions on the long comment in that
* project.
*
* Get MicroBlocks at https://microblocks.fun
*/
SnapExtensions.primitives.set(
'sgd_connect(ip)',
function (ip) {
if (location.protocol.indexOf('https') > -1) {
throw new Error(
'Signada requires an HTTP only instance of Snap!, like:\n' +
'http://extensions.snap.berkeley.edu'
);
}
var socket = new WebSocket('ws://' + ip + ':81'),
stage = this.parentThatIsA(StageMorph);
stage.signada = {};
stage.signada.ip = ip;
stage.signada.socket = socket;
stage.signada.responses = {};
stage.signada.lastID = 0;
stage.signada.eventListener = function(event) {
response = JSON.parse(event.data);
if (Array.isArray(response[1])) {
response[1] = new List(response[1]);
}
stage.signada.responses[response[0]] = response[1];
};
socket.addEventListener('message', stage.signada.eventListener);
}
);
SnapExtensions.primitives.set(
'sgd_disconnect()',
function () {
var stage = this.parentThatIsA(StageMorph);
if (SnapExtensions.primitives.get('sgd_connected()').call(this)) {
stage.signada.socket.removeEventListener(
'message',
stage.signada.eventListener
);
stage.signada.socket.close();
}
}
);
SnapExtensions.primitives.set(
'sgd_connected()',
function () {
var signada = this.parentThatIsA(StageMorph).signada;
return (signada !== undefined) && (signada.socket.readyState === 1);
}
);
SnapExtensions.primitives.set(
'sgd_call(blockname, params)',
function (blockname, params, proc) {
var signada = this.parentThatIsA(StageMorph).signada;
if (!SnapExtensions.primitives.get('sgd_connected()').call(this)) {
throw new Error(
'You are not connected to any device.\n' +
'Please use the connect block to establish a connection and' +
'try again.'
);
}
if (!proc.requestID) {
proc.startTime = new Date();
var signada = this.parentThatIsA(StageMorph).signada;
signada.socket.send(
JSON.stringify([signada.lastID, blockname, params.contents])
);
proc.requestID = signada.lastID;
// Last ID wraps at 1.000.000 to make sure it doesn't grow too much
// and it doesn't wrap too early, colliding with other requests.
signada.lastID = (signada.lastID + 1) % 1000000;
} else {
if (signada.responses[proc.requestID] !== undefined) {
return signada.responses[proc.requestID];
} else if ((new Date() - proc.startTime) > 1000) {
// Timeout after 1 second
return;
} else {
}
}
proc.pushContext('doYield');
proc.pushContext();
}
);

File diff suppressed because one or more lines are too long