-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
355 lines (282 loc) · 9.28 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
var pinoccio = require('pinoccio')
var Emitter = require("events").EventEmitter;
var util = require('util');
// there are more pins but they are already connected to a bunch of cool things.
// im not sure the right way to manage it. io.led?
// https://github.com/Pinoccio/core-pinoccio/blob/master/avr/variants/pinoccio/pins_arduino.h
var pins = [
{ id: "D0", modes: [-2] }, //always reserved
{ id: "D1", modes: [-2] }, //always reserved
{ id: "D2", modes: [0, 1, 3, 4] },
{ id: "D3", modes: [0, 1, 3, 4] },
{ id: "D4", modes: [0, 1, 3, 4] },
{ id: "D5", modes: [0, 1, 3, 4] },
{ id: "D6", modes: [0, 1] }, // reserved on lead scout. sdcard
{ id: "D7", modes: [0, 1] }, // reserved on lead scout. wifi
{ id: "D8", modes: [0, 1] }, // reserved on lead scout. wifi
{ id: "A0", modes: [0, 1, 2] },
{ id: "A1", modes: [0, 1, 2] },
{ id: "A2", modes: [0, 1, 2] },
{ id: "A3", modes: [0, 1, 2] },
{ id: "A4", modes: [0, 1, 2] },
{ id: "A5", modes: [0, 1, 2] },
{ id: "A6", modes: [0, 1, 2] },
{ id: "A7", modes: [0, 1, 2] }
];
//
// -2 -1 0 1 2 3
//var pinModes = ['reserved', 'disabled', 'float', 'output', 'input', 'pwm'];
var publicModes = {
INPUT: 0,
OUTPUT: 1,
ANALOG: 2,
PWM: 3,
SERVO: 4
}
var internalModes = {
RESERVED:-2, // pinoccio
DISABLED:-1, // pinoccio
FLOAT:0, // pinoccio
INPUT: 2,
OUTPUT: 1,
ANALOG:0,
PWM: 3,
SERVO: 4 // todo
};
var modeMap = translate(publicModes,internalModes)
var internalModeMap = translate(internalModes,publicModes)
module.exports = PinoccioIO;
function PinoccioIO(opts){
if (!(this instanceof PinoccioIO)) {
return new PinoccioIO(opts);
}
var z = this;
Emitter.call(z);
z._api = pinoccio(opts);
z.troop = opts.troop;
z.scout = opts.scout;
z._api.rest({url:'v1/'+opts.troop+'/'+opts.scout},function(err,data){
if(err) {
console.error(err);
return z.emit('error',err);
}
if(!data) {
console.error(err);
return z.emit('error',new Error('unknown troop or scout'));
}
z.sync = z._api.sync({stale:1});
// make sure we get a fresh pin report. edge case where a report may be missing.
z.command("pin.digital.report;pin.analog.report;",function(err,res){
if(err) console.error('error sending pin report command ',err);
})
z.data = {};// sync data object.
// board is ready after i get available && digital && analog events.
// TODO FIND GREAT Way to message when a scout may be off / unavailable
var isReady = function(){
return !z.isReady && (z.data.available && z.data.available.available) && z.data.digital && z.data.analog;
};
z.emit('connect');
var delay;
z.sync.on('data',function(data){
// i care about 3 api events
//
// available: {"scout":1,"available":1,"reply":"11\n","_t":1399594464252,"type":"available"}
// digital: {"type":"digital","mode":[-1,-1,-1,-1,2,-1,-1],"state":[-1,-1,-1,-1,0,-1,-1],"_t":1396672122237}
// analog: {"type":"analog","mode":[-1,-1,-1,-1,-1,-1,-1,-1],"state":[-1,-1,-1,-1,-1,-1,-1,-1],"_t":1396651237836}
//
data = data.data;
if(data.account && data.troop == z.troop && data.scout == z.scout && data.type) {
clearTimeout(delay);
var key = data.type
z.data[key] = data.value||data;
if(key == 'digital' || key == 'analog') {
var offset = key == 'analog'?9:2;
var report = data.value;
var skew = key == 'digital'?2:0;
//console.log('report',report);
report.mode.forEach(function(mode,i){
// mode may be undefined if the pin becomes disabled because this is not a state understood by the j5 interface
mode = internalModeMap[mode];//
var value = report.state[i];
var pin = z.pins[offset+i];
var change = false;
if(mode != pin.mode) {
change = true;
pin.mode = mode;
}
if(value != pin.value) {
change = true;
pin.value = value;
}
if(z.isReady && change) {
z.emit(key+'-pin-'+(i+offset),value);
}
});
}
if(isReady()) {
// completely ready...
z.isReady = true;
z.emit('ready');
}
}
}).on('error',function(err){
z.emit('error',err);
});
});
z.pins = pins.map(function(pin,i) {
return {
id:pin.id,
supportedModes: pin.modes,
mode: -1, // disabled. waiting for push from api.
value: 0,
report:1,// all pins report
analogChannel:i>=9?i-9:127
};
});
this.analogPins = this.pins.slice(9).map(function(pin, i) {
return i+9;
});
}
util.inherits(PinoccioIO.prototype,Emitter);
PinoccioIO.prototype = new Emitter;
mix(PinoccioIO.prototype,{
name:"pinoccio-io",
isReady:false,
HIGH:1,
LOW:0,
MODES:publicModes,
pins:[],
// handle to api.
_api:false,
// placeholder for setSamplingInterval
_interval:19,
defaultLed:'@led',
pinMode:function(pin,mode){
var p = pinType(pin,'digital');
// short circuit for psuedo pins
if(p.type === '@') return this;
if(!this.pins[p.i]) return false;// throw?
if(this.pins[p.i].mode === mode) return this;
this.pins[p.i].mode = mode;
mode = modeMap[mode];
this.command('pin.setmode("'+p.pin+'",'+mode+')',function(err){
if(err) console.log('pin setmode error ',p.pin,mode,err);
});
return this;
},
digitalWrite:function(pin,value){
var p = pinType(pin,'digital');
if(p.type === '@') {
if(p.pin === '@led'){
this.command('led.'+(value == this.LOW?'on':'off'),function(err){
if(err) console.error('error setting led');
})
}
return this;
}
return this._pinWrite(p,value);
},
analogWrite:function(pin,value){
var p = pinType(pin,'analog');
if(p.type === '@') return this;
// based on http://arduino.cc/en/Reference/AnalogWrite shouldn't the pin default to digital?
// just copying the default behavior from spark-io. TODO check j5 examples
return this._pinWrite(p,value);
},
digitalRead:function(pin,handler){
var p = pinType(pin,'digital');
if(p.type === '@') return this;
return this._pinRead(p,handler);
},
analogRead:function(pin,handler){
var p = pinType(pin,'analog');
if(p.type === '@') return this;
return this._pinRead(p,handler);
},
setSamplingInterval:function(analogInterval,digitalInterval,peripheralInterval,cb) {
// this sets the analog sampling interval.
// right now it also resets the digital and peripheral sampling intervals.
analogInterval = safeInt(analogInterval||1000);
digitalInterval = safeInt(digitalInterval||50,50);
peripheralInterval = safeInt(peripheralInterval||60000);
// polling reporting interval.
// events.setCycle(digtialEvents (default is 50ms),analogEvents (default is 1000ms),peripheral sampling interval (temp battery etc default 60000ms))
var z = this;
z.command("events.setCycle("+digitalInterval+","+analogInterval+","+peripheralInterval+");",function(err,data){
if(err) z.emit('command-error',err)
if(cb) cb(err,data);
else console.error('error setting sampling interval. ',err);
});
return this;
},
reset:function() {
// whats this supposed to do.
return this;
},
close:function() {
if(this.sync) this.sync.end();
// it seems like i should do these things
//this.isReady = false;
//this.emit('close');
},
// its kinda a noop right now. just return the pin int which gets changed into the pin str again before being added to scout script
// add normalize re https://github.com/rwaldron/johnny-five/issues/345
normalize:function(pin){
return pinType(pin).i;
},
_pinWrite:function(data,value){
value = +value;
if(isNaN(value)) return false;
this.command('pin.write("'+data.pin+'",'+value+')',function(err,data){
if(err) console.error('error writing pin ',data,value,err);
});
},
_pinRead:function(data,handler){
// expect pin as string
this.on(data.name+'-pin-'+data.i,handler);
return this;
},
// pinoccio only.
// send scout script command directly to the scout.
command:function(command,cb){
var z = this;
z._api.rest({url:'/v1/'+this.troop+'/'+this.scout+'/command',data:{command:command}},function(err,data){
cb(err,data);
});
},
});
PinoccioIO.prototype.servoWrite = PinoccioIO.prototype.analogWrite;
function safeInt(interval,min,max){
min = min||100;
max = max||65535;
return interval < min ?
min : (interval > max ? max : interval);
}
function pinType(pin,type){
var t = type == 'analog'?'a':'d';
if(typeof pin == 'number'){
if(pin >= 9){
t = 'a';
pin -= 9;
}
pin = t+pin;
}
if(pin.indexOf('@') == 0){
//special @led etc
return {pin:pin.toLowerCase(),type:'@',i:-1};
}
pinInt = (pin.replace(/A|D/i, "") | 0) + (t == 'a' ? 9 : 0);
return {pin:pin.toLowerCase(),type:t,name:type,i:pinInt};
}
function mix(o1,o2){
for(var i in o2){
if(o2.hasOwnProperty(i)) o1[i] = o2[i];
}
}
function translate(o,o2){
var out = {};
Object.keys(o).forEach(function(k){
out[o[k]] = o2[k];
});
return out;
}