-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeblock.js
423 lines (371 loc) · 13.2 KB
/
makeblock.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// m4s.js
// yzj June 1st 2014
// Makeblock for Scratch Extension
//
(function(ext) {
var device = null
var rxbuff = null
var slot1 = 1
var slot2 = 2
var axisX = 0
var axisY = 1
var axisZ = 2
var port1 = 0x10
var port2 = 0x20
var port3 = 0x30
var port4 = 0x40
var port5 = 0x50
var port6 = 0x60
var port7 = 0x70
var port8 = 0x80
var m1 = 0x90
var m2 = 0xA0
var I2C = 0xB0
var DIGIPORT = 0xC0
var ALOGPORT = 0xD0
var portEnum = {"Port1":port1,"Port2":port2,"Port3":port3,"Port4":port4,"Port5":port5,"Port6":port6,"Port7":port7,"Port8":port8,"M1":m1,"M2":m2,"I2C":I2C}
var slotEnum = {"Slot1":slot1,"Slot2":slot2}
var axisEnum = {"X-Axis":axisX,"Y-Axis":axisY,"Z-Axis":axisZ}
var dpinEnum = {"D2":0,"D3":1,"D4":2,"D5":3,"D6":4,"D7":5,"D8":6,"D9":7,"D10":8,"D11":9,"D12":10,"D13":11}
var apinEnum = {"A0":0,"A1":1,"A2":2,"A3":3,"A4":4,"A5":5}
var pinmodeEnum = {"Input":1,"Output":0}
var levelEnum = {"Low":0,"High":1,"Off":0,"On":1}
var firmVersion = 0;
var VERSION = 0
var ULTRASONIC_SENSOR = 1
var TEMPERATURE_SENSOR = 2
var LIGHT_SENSOR = 3
var POTENTIONMETER = 4
var JOYSTICK = 5
var GYRO = 6
var RGBLED = 8
var SEVSEG = 9
var MOTOR = 10
var SERVO = 11
var ENCODER = 12
var INFRARED = 16
var LINEFOLLOWER = 17
var DIGITAL_INPUT = 30
var ANALOG_INPUT = 31
var DIGITAL_OUTPUT = 32
var ANALOG_OUTPUT = 33
var PWM_OUTPUT = 34
moduleList = [] //{port:port2,slot:slot1,module:module}
function appendModule(module,portstr,slotstr,pin){
var port = portEnum[portstr]
var slot = slotEnum[slotstr]
var value = 0;
for(var i=0;i<moduleList.length;i++){
mod = moduleList[i];
if(mod.port == port && mod.slot == slot){
// module at this port & slot changed
if(module != mod.module){
mod.module = module
mod.value = [] // reset the value to 0
}
return i
}
}
moduleList.push(constructModule(module,portstr,slotstr,pin,0));
return moduleList.length-1
}
function constructModule(module,portstr,slotstr,pin,value){
var port = portEnum[portstr]
var slot = slotEnum[slotstr]
return {port:port,slot:slot,module:module,pin:pin,value:[value]}
}
function sendModuleList(){
if(!device) return;
len = moduleList.length
// ff 55 1 numdev dev1 port|slot
var buff = new Uint8Array(4+len*2)
buff[0]=0xff
buff[1]=0x55
buff[2]=0x01
buff[3]=len*2
for(var i=0;i<moduleList.length;i++){
mod = moduleList[i]
buff[4+i*2] = mod.module
buff[4+i*2+1] = mod.module>=DIGITAL_INPUT?mod.pin:(mod.port+mod.slot)
}
console.log("sendModuleList:",buff)
device.send(buff.buffer);
}
function b2f(s,pos_start)
{
var d =new Uint8Array(s.subarray(pos_start,pos_start+4))
var floatarray = new Float32Array(d.buffer, 0);
return floatarray[0]
}
function appendBuffer( buffer1, buffer2 ) {
var tmp = new Uint8Array( buffer1.byteLength + buffer2.byteLength );
tmp.set( new Uint8Array( buffer1 ), 0 );
tmp.set( new Uint8Array( buffer2 ), buffer1.byteLength );
return tmp;
}
function parsePackage(s){
console.log("parsePack:",s);
if(s[0]==0xff && s[1]==0x55){
// ff 55 1 dev0[4] .... \r \n
if(s[2]==0x01){
var dataLen = (s.length-3-2)/4;
var moduleIndex = 0
if(dataLen==0){
return;
}
for(var i=0;i<dataLen;i++){
if(moduleIndex>=moduleList.length){
continue;
}
// some special module may take multiple reply
if(moduleList[moduleIndex].module == JOYSTICK){
value = b2f(s,3+i*4).toFixed(0);
i++;
value2 = b2f(s,3+i*4).toFixed(0);
moduleList[moduleIndex].value = [value,value2]
}else if(moduleList[moduleIndex].module == GYRO){
value = b2f(s,3+i*4);
i++;
value2 = b2f(s,3+i*4);
i++;
value3 = b2f(s,3+i*4);
moduleList[moduleIndex].value = [value,value2,value3];
}else{
value = b2f(s,3+i*4);
moduleList[moduleIndex].value = [value];
}
moduleIndex+=1;
}
}
}
}
function deviceRun(mod){
if(!device) return;
// ff 55 2 dev port|slot value[4]
var cc = new Uint8Array(10);
cc[0]=0xff;
cc[1]=0x55;
cc[2]=0x02;
cc[3]=0x06; // the len of one device description
cc[4]=mod.module
cc[5]=mod.module>=DIGITAL_INPUT?mod.pin:(mod.port+mod.slot)
if(mod.value.length==1){
var floatarray = new Float32Array(1)
floatarray[0] = mod.value[0]
var s = new Uint8Array(floatarray.buffer)
cc.set(s,6)
}else if(mod.value.length == 4){
cc.set(mod.value,6)
}
console.log("run>",cc)
device.send(cc.buffer);
}
ext.doMotorRun = function(port,speed) {
mod=constructModule(MOTOR,port,"Slot1",0,speed)
deviceRun(mod)
};
ext.doServoRun = function(port,slot,speed){
mod=constructModule(SERVO,port,slot,0,speed)
deviceRun(mod)
};
ext.doUltrasonic = function(port){
index = appendModule(ULTRASONIC_SENSOR,port,"Slot1",0)
sendModuleList()
return moduleList[index].value[0]
};
ext.doLinefollow = function(port){
index = appendModule(LINEFOLLOWER,port,"Slot1",0)
sendModuleList()
return moduleList[index].value[0]
};
ext.doLimitSwitch = function(port){
};
ext.doTemperature = function(port,slot){
index = appendModule(TEMPERATURE_SENSOR,port,slot,0)
sendModuleList()
return moduleList[index].value[0]
};
ext.doLightSensor = function(port){
index = appendModule(LIGHT_SENSOR,port,"Slot1",0)
sendModuleList()
return moduleList[index].value[0]
};
ext.doRunLightSensor = function(port,level){
value = levelEnum[level]
mod=constructModule(LIGHT_SENSOR,port,"Slot1",0,value)
deviceRun(mod)
}
ext.doSoundSensor = function(port){
};
ext.doJoystick = function(port,axis){
index = appendModule(JOYSTICK,port,"Slot1",0)
sendModuleList()
axis = axisEnum[axis]
switch(axis){
case axisX:
return moduleList[index].value[0]
case axisY:
return moduleList[index].value[1]
}
};
ext.doGyro = function(axis){
index = appendModule(GYRO,"I2C","Slot1",0)
sendModuleList()
axis = axisEnum[axis]
switch(axis){
case axisX:
return moduleList[index].value[0]
case axisY:
return moduleList[index].value[1]
case axisZ:
return moduleList[index].value[2]
}
}
ext.doPotentialMeter = function(port){
index = appendModule(POTENTIONMETER,port,"Slot2",0)
sendModuleList()
return moduleList[index].value[0]
}
ext.doInfrared = function(port){
index = appendModule(INFRARED,port,"Slot1",0)
sendModuleList()
return moduleList[index].value[0]
}
ext.doVersion = function(){
index = appendModule(VERSION,0,0,0)
sendModuleList();
return moduleList[index].value[0].toFixed(4);
};
ext.doButton = function(port){
}
ext.doRunSeg = function(port,num){
mod=constructModule(SEVSEG,port,"Slot1",0,num)
deviceRun(mod)
}
ext.doRunRgb = function(port,pixal,r,g,b){
mod=constructModule(RGBLED,port,"Slot1",0,0)
mod.value = [pixal,r,g,b]
deviceRun(mod)
}
ext.doDWrite = function(pinstr,level){
pin = dpinEnum[pinstr]
value = levelEnum[level]
mod = constructModule(DIGITAL_OUTPUT,"DIGIPORT","Slot1",0,0)
mod.pin = pin+2 // +2 be compatable to arduino code
mod.value = [value]
deviceRun(mod)
}
ext.doAWrite = function(pinstr,value){
pin = dpinEnum[pinstr]
mod = constructModule(ANALOG_OUTPUT,"DIGIPORT","Slot1",0,value)
mod.pin = pin+2 // +2 be compatable to arduino code
mod.value = [value]
deviceRun(mod)
}
ext.doDRead = function(pin){
index = appendModule(DIGITAL_INPUT,"DIGIPORT","Slot1",pin)
sendModuleList()
pinvalue = moduleList[index].value[0];
return pinvalue
}
ext.doARead = function(pin){
index = appendModule(ANALOG_INPUT,"ALOGPORT","Slot1",pin);
sendModuleList();
pinvalue = moduleList[index].value[0];
return pinvalue;
}
ext.resetAll = function(){
console.log("resetAll")
var cc = new Uint8Array(4);
cc[0]=0xff;
cc[1]=0x55;
cc[2]=0x04;
cc[3]=0x0;
device.send(cc.buffer);
};
ext.setSerialPort = function(port){
ext._deviceConnected(port);
}
ext._deviceConnected = function(dev) {
console.log("_deviceConnected:",device);
if(device) return;
device = dev;
var ser = prompt("check serial port",dev.id);
device.id = ser
device.open({ stopBits: 0, bitRate: 115200, ctsFlowControl: 0 });
device.set_receive_handler(function(data) {
console.log("rx:",new Uint8Array(data))
if(!rxbuff){
rxbuff = new Uint8Array(data)
}else{
var s = new Uint8Array(data)
rxbuff = appendBuffer(rxbuff,s)
}
if(rxbuff!=null){
var bufflen = rxbuff.length
for(var i=0;i<bufflen-1;i++){
if(rxbuff[i] == 0xD && rxbuff[i+1]==0xA){ // trace to \n\r
parsePackage(rxbuff)
rxbuff = null;
}
}}
});
};
ext._shutdown = function() {
console.log("_shutdown");
if(device) device.close();
device = null;
};
ext._deviceRemoved = function(dev) {
console.log("_deviceRemoved");
if(device != dev) return;
device = null;
};
ext._getStatus = function() {
//console.log("_getStatus:",device)
if(!device) return {status: 1, msg: 'Makeblock disconnected'};
return {status: 2, msg: 'Makeblock connected'};
}
var descriptor = {
blocks:[
["r", "Version","doVersion"],
["", "run motor %m.motorPort speed %n", "doMotorRun", "M1", 50],
["", "run servo %m.servoPort %m.slot angle %n", "doServoRun", "Port1", "Slot1", 90],
["", "7-segments display %m.normalPort number %n", "doRunSeg", "Port3", 100],
["", "set led %m.normalPort at %n red %n green %n blue %n", "doRunRgb", "Port3", 0, 0, 0, 0],
["r", "ultrasonic sensor %m.normalPort distance", "doUltrasonic", "Port3"],
["r", "light sensor %m.normalPort", "doLightSensor", "Port3"],
["", "set light sensor %m.normalPort state %m.switch", "doRunLightSensor", "Port3", "On"],
["r", "line follower %m.normalPort", "doLinefollow", "Port3"],
["r", "potentiometer %m.normalPort", "doPotentialMeter", "Port7"],
["r", "gyro %m.GyroAxis angle", "doGyro", "X-Axis"],
["r", "infrared receiver %m.normalPort", "doInfrared", "Port6"],
["r", "temperature %m.normalPort%m.slot", "doTemperature", "Port3", "Slot1"],
["r", "joystick %m.normalPort %m.Axis", "doJoystick", "Port3", "X-Axis"],
["b", "digital pin %n ", "doDRead", "13"],
["r", "analog pin %n ", "doARead", "0"],
["", "set digital pin %n output %n", "doDWrite", "13", "High"],
["", "set analog pin %n output %n", "doAWrite", "0", 512],
["", "set serial port %m.serialport","setSerialPort","/dev/cu.wchusbserial1410"]
],
menus: {
"normalPort":["Port3","Port4","Port5","Port6","Port7","Port8"],
"digitalPin":["D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13"],
"analogPin":["A0","A1","A2","A3","A4","A5"],
"motorPort":["M1","M2","Port1","Port2"],
"servoPort":["Port1","Port2","Port3","Port4","Port5","Port6","Port7","Port8"],
"slot":["Slot1","Slot2"],
"device":["Ultrasonic","Line Finder","Light Sensor","Sound Sensor","Joystick","Button"],
"exdevice":["LimitSwitch","Temperature"],
"mode":["Input","Output"],
"type":["Digital","Analog"],
"Axis":["X-Axis","Y-Axis"],
"GyroAxis":["X-Axis","Y-Axis","Z-Axis"],
"digital":["Low","High"],
"switch":["Off","On"],
"serialport":["/dev/cu.wchusbserial1410"]
},
url: 'http://www.makeblock.cc'
};
ScratchExtensions.register('makeblock', descriptor, ext, {type: 'serial'});
})({});