Skip to content

Commit b2267a3

Browse files
author
Ke, Mingze
committed
fix fish and Safari
1 parent 941838e commit b2267a3

5 files changed

Lines changed: 60 additions & 89 deletions

File tree

blocks/webduino.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ Blockly.Blocks['fish_move'] = {
246246
this.appendValueInput("secs_")
247247
.appendField(new Blockly.FieldVariable(" "), "fish_")
248248
.appendField(new Blockly.FieldDropdown([
249-
["↑ " + Blockly.Msg.WEBDUINO_FISH_MOVE, "move"],
250-
["↺ " + Blockly.Msg.WEBDUINO_FISH_LEFT, "left"],
251-
["↻ " + Blockly.Msg.WEBDUINO_FISH_RIGHT, "right"]
252-
]), "move_")
249+
["↑ " + Blockly.Msg.WEBDUINO_FISH_MOVE, ""],
250+
["↺ " + Blockly.Msg.WEBDUINO_FISH_LEFT, "1"],
251+
["↻ " + Blockly.Msg.WEBDUINO_FISH_RIGHT, "2"]
252+
]), "direction_")
253253
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_FOR);
254254
this.appendDummyInput()
255255
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_SEC);
@@ -259,7 +259,9 @@ Blockly.Blocks['fish_move'] = {
259259
["1", "1"],
260260
["2", "2"],
261261
["3", "3"],
262-
["4", "4"]
262+
["4", "4"],
263+
["5", "5"],
264+
["6", "6"]
263265
]), "speed_");
264266
this.setInputsInline(true);
265267
this.setPreviousStatement(true);

code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Code.promisifyBlock = function (block, code) {
518518
block.promise = true;
519519
type = 1;
520520
} else {
521-
if (block.promise && code.endsWith(';\n') && next !== null) {
521+
if (block.promise && code.substring(code.length - 2) === ';\n' && next !== null) {
522522
code = code.substr(0, code.length - 2);
523523
type = 2;
524524
}

generators/webduino.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ Blockly.JavaScript['fish_angle'] = function (block) {
9292

9393
Blockly.JavaScript['fish_move'] = function (block) {
9494
var variable_fish_ = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('fish_'), Blockly.Variables.NAME_TYPE);
95-
var dropdown_move_ = block.getFieldValue('move_');
95+
var dropdown_direction_ = block.getFieldValue('direction_');
9696
var value_secs_ = Blockly.JavaScript.valueToCode(block, 'secs_', Blockly.JavaScript.ORDER_NONE);
9797
var dropdown_speed_ = block.getFieldValue('speed_');
98-
var code = variable_fish_ + '.' + dropdown_move_ + '(' + value_secs_ + ', ' + dropdown_speed_ + ');\n';
98+
var code = variable_fish_ + ".flap(" + value_secs_ + ", " + dropdown_speed_ + (dropdown_direction_.length ? ", " + dropdown_direction_ : '') + ");\n";
9999
block.setPromise(true);
100100
return code;
101101
};
@@ -189,9 +189,9 @@ Blockly.JavaScript['sound_status'] = function (block) {
189189
var dropdown_status_ = block.getFieldValue('status_');
190190
var statements_var_ = Blockly.JavaScript.statementToCode(block, 'var_');
191191
var code = variable_item_ + '.on("' + dropdown_status_ + '",function(){\n' +
192-
' setTimeout(function(){\n'+
193-
' '+statements_var_ + '\n' +
194-
' },300);\n'+
192+
' setTimeout(function(){\n' +
193+
' ' + statements_var_ + '\n' +
194+
' },300);\n' +
195195
'});\n';
196196
return code;
197197
};

views/index.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@
440440
</block>
441441
<block type="fish_move">
442442
<field name="fish_">fish</field>
443-
<field name="move_">move</field>
443+
<field name="direction_"></field>
444444
<value name="secs_">
445445
<block type="math_number">
446446
<field name="NUM">1</field>

webduino-fish.js

Lines changed: 46 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ function getFish(board) {
55
function Fish(board) {
66
this._board = board;
77
this._pins = [];
8-
this._timer = null;
98
var self = this;
109
[0, 2, 4, 5, 12, 13, 14, 15].forEach(function (val) {
1110
var pin = new webduino.module.Led(board, board.getDigitalPin(val));
@@ -47,66 +46,19 @@ Fish.prototype.sink = function (secs) {
4746
});
4847
};
4948

50-
Fish.prototype.left = function (secs, speed) {
51-
var self = this;
52-
return new Promise(function (resolve, reject) {
53-
if (self._timer) {
54-
self.stop();
55-
}
56-
57-
self.flap('left', speed);
58-
setTimeout(function () {
59-
self.stop();
60-
resolve();
61-
}, secs * 1000);
62-
});
63-
};
64-
65-
Fish.prototype.right = function (secs, speed) {
66-
var self = this;
67-
return new Promise(function (resolve, reject) {
68-
if (self._timer) {
69-
self.stop();
70-
}
71-
72-
self.flap('right', speed);
73-
setTimeout(function () {
74-
self.stop();
75-
resolve();
76-
}, secs * 1000);
77-
});
78-
};
79-
80-
Fish.prototype.move = function (secs, speed) {
81-
var self = this;
82-
return new Promise(function (resolve, reject) {
83-
if (self._timer) {
84-
self.stop();
85-
}
86-
87-
self.flap(speed);
88-
setTimeout(function () {
89-
self.stop();
90-
resolve();
91-
}, secs * 1000);
92-
});
93-
};
94-
95-
Fish.prototype.flap = function (direction, speed) {
49+
Fish.prototype.flap = function (secs, speed, direction) {
9650
var lSpeed, rSpeed, self = this;
9751

9852
if (isNaN(speed = parseInt(speed))) {
99-
if (isNaN(speed = parseInt(direction))) {
100-
speed = 1;
101-
}
53+
speed = 1;
10254
}
10355

10456
switch (direction) {
105-
case 'left':
57+
case 1:
10658
lSpeed = 2 * speed;
10759
rSpeed = speed;
10860
break;
109-
case 'right':
61+
case 2:
11062
lSpeed = speed;
11163
rSpeed = 2 * speed;
11264
break;
@@ -115,30 +67,47 @@ Fish.prototype.flap = function (direction, speed) {
11567
break;
11668
}
11769

118-
self._pins[4].on();
119-
self._pins[5].on();
120-
self._pins[6].off();
121-
self._pins[7].off();
122-
self._timer = setTimeout(function () {
123-
self._pins[4].off();
124-
self._pins[5].off();
125-
self._pins[6].on();
126-
self._pins[7].on();
127-
self._timer = setTimeout(function () {
128-
if (self._timer) {
129-
self.flap(direction, speed);
130-
}
131-
}, 1000 / lSpeed);
132-
}, 1000 / rSpeed);
133-
};
70+
return new Promise(function (resolve, reject) {
71+
var flapping = true;
13472

135-
Fish.prototype.stop = function () {
136-
if (this._timer) {
137-
clearTimeout(this._timer);
138-
delete this._timer;
139-
this._pins[4].off();
140-
this._pins[5].off();
141-
this._pins[6].off();
142-
this._pins[7].off();
143-
}
73+
_flap(function () {
74+
resolve();
75+
});
76+
77+
setTimeout(function () {
78+
flapping = false;
79+
}, 1000 * secs);
80+
81+
function _flap(callback) {
82+
if (flapping) {
83+
self._pins[4].on();
84+
self._pins[5].on();
85+
self._pins[6].off();
86+
self._pins[7].off();
87+
setTimeout(function () {
88+
if (flapping) {
89+
self._pins[4].off();
90+
self._pins[5].off();
91+
self._pins[6].on();
92+
self._pins[7].on();
93+
setTimeout(function () {
94+
_flap(callback);
95+
}, 1000 / rSpeed);
96+
} else {
97+
self._pins[4].off();
98+
self._pins[5].off();
99+
self._pins[6].off();
100+
self._pins[7].off();
101+
callback();
102+
}
103+
}, 1000 / lSpeed);
104+
} else {
105+
self._pins[4].off();
106+
self._pins[5].off();
107+
self._pins[6].off();
108+
self._pins[7].off();
109+
callback();
110+
}
111+
}
112+
});
144113
};

0 commit comments

Comments
 (0)