-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskullExtension.js
138 lines (113 loc) · 4.16 KB
/
skullExtension.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
(function(ext) {
//load MQTT library
function loadMQTT() {
$.getScript('http://sisomm.github.io/scratch-skull-extension/mqttws31.js')
.done(function(script, textStatus) {
console.log('Loaded MQTT');
})
.fail(function(jqxhr, settings, exception) {
console.log('Error loading MQTT');
});
}
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
// Status reporting code
// Use this to report missing hardware, plugin or unsupported browser
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
// Functions for block with type 'w' will get a callback function as the
// final argument. This should be called to indicate that the block can
// stop waiting.
ext.blink_eyes = function() {
if(client.isConnected()) client.send(mqqtDefaultTopic,'BLINK,1');
}
ext.talk = function(times) {
if(client.isConnected()) client.send(mqqtDefaultTopic,'JAW_MOTION,'+times+',1');
}
ext.head = function(x,y) {
if(client.isConnected()) client.send(mqqtDefaultTopic,'SCRATCH_MOVE,'+Math.floor(x)+','+Math.floor(y));
}
ext.send_mqtt = function(topic,msg) {
if(client.isConnected()) client.send(topic,msg);
}
ext.isConnected = function(){
if(client.isConnected()){
return 1;
} else {
return 0;
}
}
ext.eyes = function(eye,onoff){
if(!client.isConnected()) return;
var level=(onoff=="On" ? 1 :0);
if(eye=="Both"){
client.send(mqqtDefaultTopic,'LED,1,'+level);
client.send(mqqtDefaultTopic,'LED,0,'+level);
} else {
var whichEye=(eye=="Left"?0:1);
client.send(mqqtDefaultTopic,'LED,'+whichEye+','+level);
}
}
ext.mouth = function(position,callback){
var cmd =0;
if(position=="Open") {
cmd=1;
}
if(client.isConnected()) client.send(mqqtDefaultTopic,'JAW_POSITION,'+cmd);
//Allow the movement to finish.. //todo add delay in the arduino code
window.setTimeout(function() {
callback();
}, 500);
}
// Block and block menu descriptions
var descriptor = {
blocks: [
[' ', 'Blink Eyes', 'blink_eyes'],
['w', '%m.openClose Mouth', 'mouth','Close'],
[' ', 'Move Head to %n,%n', 'head',300,300],
[' ', 'Talk %n times', 'talk',5],
[' ', '%m.whichEye Eye(s) %m.onOff', 'eyes','Both','On'],
[' ', 'MQTT topic %s message %s','send_mqtt','/scratch/sisomm','Hello, World'],
['r', 'Connected','isConnected'],
],
menus: {
openClose:['Open','Close'],
whichEye:['Both','Left','Right'],
onOff:['On','Off']
}
};
// Register the extension
ScratchExtensions.register('Keith Richards', descriptor, ext);
loadMQTT();
var wsbroker = "test.mosquitto.org";
//mqtt websocket enabled broker
var wsport = 8080;
// port for above
var client = new Paho.MQTT.Client(wsbroker, wsport,
"myclientid_" + parseInt(Math.random() * 100, 10));
var mqqtDefaultTopic="/scratch/sisomm";
client.onConnectionLost = function (responseObject) {
console.log("connection lost: " + responseObject.errorMessage);
setTimeout(client.connect(),100);
};
client.onMessageArrived = function (message) {
console.log(message.destinationName, ' -- ', message.payloadString);
};
var options = {
timeout: 3,
onSuccess: function () {
console.log("mqtt connected");
// Connection succeeded; subscribe to our topic, you can add multile lines of these
client.subscribe("/scratch/sisomm/#", {qos: 1});
//use the below if you want to publish to a topic on connect
message = new Paho.MQTT.Message('Hello');
message.destinationName = "/scratch/sisomm";
client.send(message);
},
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
}
};
client.connect(options);
})({});