-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlittlebits_extension_test.js
189 lines (161 loc) · 4.71 KB
/
littlebits_extension_test.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
(function(ext) {
var START_MSG = 0xF0,
END_MSG = 0xF7;
var parsingMsg = false;
var msgBytesRead = 0;
var storedMsg = new Uint8Array(1024);
var connected = false;
var device = null;
var poller = null;
var rawData = null;
/* TEMPORARY WORKAROUND
this is needed since the _deviceRemoved method
is not called when serial devices are unplugged*/
var sendAttempts = 0;
var pingCmd = new Uint8Array(1);
pingCmd[0] = 1;
var inputVals = { d0: 0, a0: 0, a1: 0 };
var outputPins = { d1: 1, d5: 5, d9: 9 };
function processMsg() {
inputVals.d0 = storedMsg[0] | (storedMsg[1] << 0x08);
inputVals.a0 = storedMsg[2] | (storedMsg[3] << 0x08);
inputVals.a1 = storedMsg[4] | (storedMsg[5] << 0x08);
}
function processInput(data) {
for (var i=0; i < data.length; i++) {
if (parsingMsg) {
if (data[i] == END_MSG) {
parsingMsg = false;
processMsg();
} else {
storedMsg[msgBytesRead++] = data[i];
}
} else {
if (data[i] == START_MSG) {
parsingMsg = true;
msgBytesRead = 0;
}
}
}
}
ext.analogRead = function(pin) {
return inputVals[pin];
};
ext.digitalRead = function(pin) {
if (inputVals[pin] > 0) return true;
return false;
};
ext.analogWrite = function(pin, val) {
var output = new Uint8Array(3);
output[0] = 2;
output[1] = outputPins[pin];
output[2] = val;
device.send(output.buffer);
};
ext.digitalWrite = function(pin, val) {
var output = new Uint8Array(3);
output[0] = 3;
output[1] = outputPins[pin];
if (val === 'on')
output[2] = 1;
else
output[2] = 0;
device.send(output.buffer);
};
ext.whenAnalogRead = function(pin, op, val) {
if (op === '>')
return inputVals[pin] > val;
else if (op === '<')
return inputVals[pin] < val;
else if (op === '=')
return inputVals[pin] === val;
else
return false;
};
ext.whenDigitalRead = function(pin, val) {
if (val === 'on')
return ext.digitalRead(pin);
else
return ext.digitalRead(pin) === false;
};
ext.mapValues = function(val, aMin, aMax, bMin, bMax) {
var output = (((bMax - bMin) * (val - aMin)) / (aMax - aMin)) + bMin;
return Math.round(output);
};
ext._getStatus = function() {
if (!connected)
return { status:1, msg:'Disconnected' };
else
return { status:2, msg:'Connected' };
};
ext._deviceRemoved = function(dev) {
// Not currently implemented with serial devices
};
var poller = null;
ext._deviceConnected = function(dev) {
sendAttempts = 0;
connected = true;
if (device) return;
device = dev;
console.log("Waiting 5 seconds before opening");
setTimeout(function() {
console.log("Pre-open");
device.open({ stopBits: 0, bitRate: 38400, ctsFlowControl: 0 });
console.log("Post-open");
}, 5000);
device.set_receive_handler(function(data) {
sendAttempts = 0;
var inputData = new Uint8Array(data);
processInput(inputData);
});
poller = setInterval(function() {
/* TEMPORARY WORKAROUND
Since _deviceRemoved is not
called while using serial devices */
if (sendAttempts >= 10) {
connected = false;
device.close();
device = null;
rawData = null;
clearInterval(poller);
return;
}
device.send(pingCmd.buffer);
sendAttempts++;
}, 50);
};
ext._shutdown = function() {
ext.digitalWrite(d1, 'off');
ext.digitalWrite(d5, 'off');
ext.digitalWrite(d9, 'off');
if (device) device.close();
if (poller) clearInterval(poller);
device = null;
};
var descriptor = {
blocks: [
[' ', 'set %m.outDPins %m.dOutp', 'digitalWrite', 'd1', 'on'],
[' ', 'set %m.outAPins to %n', 'analogWrite', 'd5', '255'],
['b', 'read %m.inDPins', 'digitalRead', 'd0'],
['r', 'read %m.inAPins', 'analogRead', 'a0'],
['h', 'when %m.inDPins is %m.dOutp', 'whenDigitalRead', 'd0', 'on'],
['h', 'when %m.inAPins is %m.ops %n', 'whenAnalogRead', 'a0', '>', '100'],
['r', 'map %n from %n %n to %n %n', 'mapValues', 500, 0, 1023, 0, 255]
],
menus: {
outDPins: ['d1', 'd5', 'd9'],
outAPins: ['d5', 'd9'],
inDPins: ['d0', 'a0', 'a1'],
inAPins: ['a0', 'a1'],
dOutp: ['on', 'off'],
ops: ['>', '=', '<']
},
url: 'http://khanning.github.io/scratch-littlebits-extension'
};
console.log("Waiting 5 seconds to register");
setTimeout(function() {
console.log("Pre-register");
ScratchExtensions.register('littleBits', descriptor, ext, {type:'serial'});
console.log("Post-register");
}, 5000);
})({});