-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (107 loc) · 3.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const ev = require('./lib/event');
const modules = {
backlight: function (tingbot) {
const backlight = require('./hardware/backlight');
//convenience
//
/**
* Sets a new brightness value, tweens if do_tween is true. Alias for {@link tingbot.backlight.set_backlight}
* @function
* @memberof tingbot
* @fires tingbot#backlight
* @alias tingbot.backlight.set_backlight
* @example <caption>set backlight value</caption>
* tingbot.backlight.set_backlight(99999, function(val){
* console.log('done', val);
* });
* @name set_backlight
* @param {number} num the new brightness value
* @param {tingbot.backlight~setBrightnessCallback} [cb] callback when ready, gets value as argument
*/
tingbot.set_backlight = function(num, cb) {
return backlight.set_backlight(num, cb);
};
return backlight;
},
buttons: function (tingbot) {
const buttons = require('./hardware/buttons');
return buttons;
}
};
/**
* tingbot-node itself. exposes optional tingbot submodules ({@link tingbot.backlight}, {@link tingbot.buttons}), event listeners and convenience functions
* @namespace tingbot
* @example <caption>create a tingbot-node instance called tingbot</caption>
* var tb = require('tingbot-node'), tingbot = new tb();
* //use tingbot.on or tingbot.set_backlight
*/
/**
* Not really a Module, but the main constructor. Creates a {@link tingbot} instance and loads optional tingbot submodules ({@link tingbot.backlight}, {@link tingbot.buttons}).
*
* @constructor
* @function
* @exports tingbot
* @example <caption>create a tingbot-node instance called tingbot</caption>
* var tb = require('tingbot-node'), tingbot = new tb();
* @param {Object} [options]
* @param {Array.<String>} options.modules (['buttons', 'backlight']) If you only want do set the backlight, you don't need to load wiring-pi
*/
const TingbotNode = module.exports = function tingbot(options) {
this.options = options || {};
//start those modules
this.options.modules = this.options.modules || ['buttons', 'backlight'];
this.init();
};
/**
* Loads modules set in this.options.modules once internally and self-destructs. Don't use.
*
* @function
*/
TingbotNode.prototype.init = function () {
//load modules
for (let i = 0; i < this.options.modules.length; i++) {
console.log('module start:', this.options.modules[i]);
this[this.options.modules[i]] = modules[this.options.modules[i]](this);
}
this.init = function () {};
};
/**
* This callback is displayed as a global member. Should, among optional event specific properties, always have 'type' and 'value'. Look at the specific Events for more Detail.
* @callback tingbot~onCallback
* @param {object} tingbot_event
* @param {string} tingbot_event.type Type of Event, eg 'button'
* @param {number} tingbot_event.value value of Event, eg brightness
*/
/**
* Attaches Event listener to tingbot-node events.
*
* @function
* @name on
* @memberof tingbot
* @example <caption>listen to a release button event</caption>
* tingbot.on('button-left:up',
* function(data) {
* console.log('button left released', data);
* });
* @param {string} evname Event name, like 'button'
* @param {tingbot~onCallback} cb callback function to be called with event specific argument payload
*/
TingbotNode.prototype.on = function (evname, cb) {
ev.on(evname, cb);
};
/**
* Attaches Event listener to tingbot-node events for only one event.
*
* @function
* @name once
* @memberof tingbot
* @example <caption>listen to a press button event once</caption>
* tingbot.once('button#3:down', function(data) {
* console.log('button right pressed', data);
* });
* @param {string} evname Event name, like 'button'
* @param {tingbot~onCallback} cb callback function to be called with event specific argument payload
*/
TingbotNode.prototype.once = function (evname, cb) {
ev.once(evname, cb);
};