Create and Publish your customized and Parts.
Required function and variables
- keys variable: object key names used at wired().
- requiredKeys variable: required object key names used at wired().
- info() class function: parts information like name
- wired() function: It is called from obniz.wired(). parts initial code must be written in here.
Minimum Codes
Minimum model of parts is below.
// Javascript Example
class LED {
constructor() {
this.keys = ['anode', 'cathode'];
this.requiredKeys = ['anode'];
}
static info() {
return {
name: 'LED',
};
}
wired(obniz) {
}
}
if (typeof module === 'object') {
module.exports = LED;
}
Below is necessary variables
// Javascript Example
this.keys = ['anode', 'cathode'];
When you write requiredKeys, obniz.js will throw error when key does not exist at wired()
// Javascript Example
this.requiredKeys = ['anode'];
Object returned by info() tell obniz.js parts name. This is independent from parts class name.
// Javascript Example
static info() {
return {
name: 'LED',
};
}
wired() function will be called from obniz.wired(). object passed from obniz.wired() can be access by calling this.params in wired() function.
Last parts is not necessary but strongly recommended. It makes your code useful from both browser Node.js.
// Javascript Example
if (typeof module === 'object') {
module.exports = LED;
}
Example
For example, Below is custom LED class that having new function onesec().
// Javascript Example
class MYLED {
constructor() {
this.keys = ['anode', 'cathode'];
this.requiredKeys = ['anode', 'cathode'];
}
static info() {
return {
name: 'MYLED',
};
}
wired(obniz) {
this.obniz = obniz;
this.io_anode = obniz.getIO(this.params.anode);
this.io_anode.output(false);
this.io_cathode = obniz.getIO(this.params.cathode);
this.io_cathode.output(false);
}
onesec() {
this.io_anode.output(true);
obniz.wait(1000);
this.io_anode.output(false);
}
}
if (typeof module === 'object') {
module.exports = MYLED;
}
Publish
How to publish
Use obnizRepository or other services like GitHub.
How to use it
Include it by using URL on browser.
// Javascript Example
<script src="公開されているURL"></script>
For Node.js You can use npm to include codes from github.
Use a class
1. register as obniz parts
See This page.
// Javascript Full Example
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
Obniz.PartsRegistrate(MYLED);
var led = obniz.wired("MYLED", { anode:0, cathode:1 } );
led.onesec();
}
2. Use as standard class
You can use without using obniz.js parts registration.
// Javascript Full Example
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
var led = new MYLED();
led.params = { anode:0, cathode:1 };
led.wired(obniz);
led.onesec();
}