Skip to content

Commit

Permalink
Merge branch 'public' into ag-linux-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Jan 2, 2020
2 parents 87ce569 + e38e1c1 commit 9a49926
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 50 deletions.
12 changes: 6 additions & 6 deletions examples/network/http/httppost/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import {Request} from "http"

let request = new Request({ host: "httpbin.org",
path: "/post",
method: "POST",
body: JSON.stringify({name: "Moddable", value: 123}),
response: String
});
path: "/post",
method: "POST",
body: JSON.stringify({name: "Moddable", value: 123}),
response: String
});

request.callback = function(message, value)
{
if (Request.responseComplete == message) {
if (Request.responseComplete === message) {
value = JSON.parse(value);
value = JSON.parse(value.data);
trace(`name: ${value.name}\n`);
Expand Down
42 changes: 42 additions & 0 deletions examples/network/http/httppoststreaming/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2016-2020 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/

import {Request} from "http"

const kBodyPart = "Fragment...\n";
const kRepeat = 128;

let request = new Request({
host: "httpbin.org",
path: "/post",
method: "POST",
body: true, // callback is provided by Request.requestFragment callbacks
headers: ["Content-Length", kBodyPart.length * kRepeat, "Content-Type", "text/plain"],
response: String
});

request.bytesToSend = kBodyPart.length * kRepeat;

request.callback = function(message, value) {
if (Request.requestFragment === message) {
if (0 === this.bytesToSend)
return; // no more body
this.bytesToSend -= kBodyPart.length;
return kBodyPart; // fragment of body
}
else if (Request.responseComplete === message) {
value = JSON.parse(value);
trace(`Posted body: ${value.data}\n`);
}
}
15 changes: 15 additions & 0 deletions examples/network/http/httppoststreaming/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": [
"$(MODDABLE)/examples/manifest_base.json",
"$(MODDABLE)/examples/manifest_net.json",
],
"modules": {
"*": [
"./main",
"$(MODULES)/network/http/*"
]
},
"preload": [
"http"
]
}
32 changes: 32 additions & 0 deletions examples/network/wifi/wifiscancontinuous/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2016-2020 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/

import Scanner from "wifiscanner";
import WiFi from "wifi";

WiFi.mode = 1;

trace("Scan start\n");
const scanner = new Scanner({
onFound(ap) {
trace(`+ ${ap.ssid}\n`);
},
onLost(ssid) {
trace(`- ${ssid}\n`);
},
max: 64,
scanOptions: {
hidden: true,
},
});
35 changes: 35 additions & 0 deletions examples/network/wifi/wifiscancontinuous/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"include": "$(MODDABLE)/examples/manifest_base.json",
"modules": {
"*": [
"./main",
"./wifiscanner",
"$(MODULES)/network/wifi/*"
]
},
"preload": [
"wifiscanner",
"wifi"
],
"platforms": {
"esp": {
"modules": {
"*": "$(MODULES)/network/wifi/esp/*"
}
},
"esp32": {
"modules": {
"*": "$(MODULES)/network/wifi/esp32/*"
}
},
"qca4020": {
"modules": {
"*": "$(MODULES)/network/wifi/qca4020/*"
}
},
"...": {
"error": "WiFi module unsupported"
}
}
}

97 changes: 97 additions & 0 deletions examples/network/wifi/wifiscancontinuous/wifiscanner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2016-2020 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/

import WiFi from "wifi";
import Time from "time";
import Timer from "timer";

class Scanner {
#items = [];
#interval;
#max;
#scanOptions;
#onLost;
#onFound;
#timer;

constructor(options) {
this.#onFound = options.onFound;
this.#onLost = options.onLost;
this.#max = options.max || 32;
this.#interval = options.interval || 1000;
this.#scanOptions = options.scanOptions || {};

this.#scan();
}
#scan() {
WiFi.scan(this.#scanOptions, item => {
if (!this.#items)
return;

if (item) {
const i = this.#items.find(i => i.ssid === item.ssid);
if (i)
i.ticks = Time.ticks;
else {
this.#items.push({ssid: item.ssid, ticks: Time.ticks});
if (this.#onFound)
this.#onFound(item);
}

if (this.#items.length > this.#max)
this.#purge();
}
else {
this.#purge();
this.#timer = Timer.set(() => {
this.#timer = undefined;
this.#scan();
}, this.#interval);
}
});
}
#purge() {
const items = this.#items;
let expire = Time.ticks - 60 * 1000; // haven't been seen in 60 seconds? gone.
if (expire < 0) expire = 0;
for (let i = 0; i < items.length; i++) {
if (items[i].ticks > expire)
continue;

if (this.#onLost)
this.#onLost(items[i].ssid);

items.splice(i, 1);
i -= 1;
}

if (items.length > this.#max) {
items.sort((a, b) => b.ticks - a.ticks); // most recently seen first
if (this.#onLost) {
for (let i = this.#max; i < items.length; i++)
this.#onLost(items[i].ssid)
}
items.length = this.#max;
}
}
close() {
if (this.#timer)
Timer.clear(this.#timer);
this.#timer = undefined;
this.#items = undefined;
}
}
Object.freeze(Scanner.prototype);

export default Scanner;
22 changes: 13 additions & 9 deletions modules/drivers/ili9341/modIli9341.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,20 @@ void xs_ILI9341(xsMachine *the)

sd->dispatch = (PixelsOutDispatch)&gPixelsOutDispatch;

SCREEN_CS_INIT;
SCREEN_DC_INIT;
modSPIInit(&sd->spiConfig);

#ifdef MODDEF_ILI9341_RST_PIN
SCREEN_RST_INIT;
#endif

ili9341Init(sd);

#ifdef MODDEF_ILI9341_BACKLIGHT_PIN
modGPIOInit(&sd->backlight, MODDEF_ILI9341_BACKLIGHT_PORT, MODDEF_ILI9341_BACKLIGHT_PIN, kModGPIOOutput);
modGPIOWrite(&sd->backlight, MODDEF_ILI9341_BACKLIGHT_OFF);
#endif
}

void xs_ILI9341_begin(xsMachine *the)
Expand Down Expand Up @@ -453,21 +466,12 @@ void ili9341Init(spiDisplay sd)
uint8_t data[16] __attribute__((aligned(4)));
const uint8_t *cmds;

SCREEN_CS_INIT;
SCREEN_DC_INIT;
modSPIInit(&sd->spiConfig);

#ifdef MODDEF_ILI9341_RST_PIN
SCREEN_RST_INIT;
SCREEN_RST_ACTIVE;
modDelayMilliseconds(10);
SCREEN_RST_DEACTIVE;
modDelayMilliseconds(1);
#endif
#ifdef MODDEF_ILI9341_BACKLIGHT_PIN
modGPIOInit(&sd->backlight, MODDEF_ILI9341_BACKLIGHT_PORT, MODDEF_ILI9341_BACKLIGHT_PIN, kModGPIOOutput);
modGPIOWrite(&sd->backlight, MODDEF_ILI9341_BACKLIGHT_OFF);
#endif

cmds = gInit;
while (true) {
Expand Down
30 changes: 13 additions & 17 deletions modules/network/ping/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,25 @@ class Ping extends Socket {
}
ping() {
this.icmp_seq++;
let address = this.address;
let packet = this.packet = new ArrayBuffer(56); // 8 for icmp header + 48 for icmp payload
let values = new Uint8Array(packet);
const values = new Uint8Array(56); // 8 for icmp header + 48 for icmp payload

// ICMP header
values[0] = 8; // type 8 (echo request)
values[1] = 0; // code 0
values[2] = values[3] = 0; // will be the checksum
values[4] = (this.id & 0xFF00) >> 8;
values[5] = this.id & 0x00FF;
values[6] = (this.icmp_seq & 0xFF00) >> 8;
values[7] = this.icmp_seq & 0x00FF;
// values[1] = 0; // code 0
// values[2] = values[3] = 0; // will be the checksum
values[4] = this.id >> 8;
values[5] = this.id;
values[6] = this.icmp_seq >> 8;
values[7] = this.icmp_seq;
for (let i=8, val=0x08; val<0x38; val+=1, i++) { // packet data
values[i] = val;
}
let cs = Ping.checksum(values);
values[2] = (cs & 0xFF00) >> 8;
values[3] = cs & 0x00FF;
const checksum = Ping.checksum(values);
values[2] = checksum >> 8;
values[3] = checksum;

this.reply = false;
this.write(address, packet);
this.write(this.address, values.buffer);
}
failed(message) {
this.client(-1, message);
Expand Down Expand Up @@ -103,15 +101,13 @@ class Ping extends Socket {
for (let i=0; i<values.length; i+=2) {
sum += (values[i] << 8) + values[i+1];
}
let carry = sum & 0xFFFF0000;
sum += (carry >> 16);
sum += (sum >> 16);
return ~sum & 0xFFFF;
}
static validate_checksum(identifier, seqNumber, checksum) {
let sum = 191232; // sum of packet bytes (0x08, 0x09...0x37)
sum += identifier + seqNumber;
let carry = sum & 0xFFFF0000;
sum += (carry >> 16);
sum += (sum >> 16);
return checksum == (~sum & 0xFFFF);
}
};
Expand Down
4 changes: 2 additions & 2 deletions modules/network/webthings/webthings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import Timer from "timer";
import {Server} from "http"

const ThingHeaders = [ "content-type", "application/json",
const ThingHeaders = Object.freeze([ "content-type", "application/json",
"access-control-allow-origin", "*",
"access-control-allow-methods", "PUT, GET"];
"access-control-allow-methods", "PUT, GET"], true);

class WebThing {
constructor(host) {
Expand Down
2 changes: 2 additions & 0 deletions xs/sources/xsGenerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void fxBuildGenerator(txMachine* the)
slot = fxNextStringXProperty(the, slot, "GeneratorFunction", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
mxGeneratorFunctionPrototype = *the->stack;
slot = fxBuildHostConstructor(the, mxCallback(fx_GeneratorFunction), 1, mxID(_GeneratorFunction));
slot->value.instance.prototype = mxFunctionConstructor.value.reference;
the->stack++;

slot = mxBehaviorGetProperty(the, mxGeneratorPrototype.value.reference, mxID(_constructor), XS_NO_ID, XS_OWN);
Expand All @@ -95,6 +96,7 @@ void fxBuildGenerator(txMachine* the)
slot = fxNextStringXProperty(the, slot, "AsyncGeneratorFunction", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
mxAsyncGeneratorFunctionPrototype = *the->stack;
slot = fxBuildHostConstructor(the, mxCallback(fx_AsyncGeneratorFunction), 1, mxID(_AsyncGeneratorFunction));
slot->value.instance.prototype = mxFunctionConstructor.value.reference;
the->stack++;

slot = mxBehaviorGetProperty(the, mxAsyncGeneratorPrototype.value.reference, mxID(_constructor), XS_NO_ID, XS_OWN);
Expand Down
Loading

0 comments on commit 9a49926

Please sign in to comment.