Skip to content

Commit 264f18e

Browse files
author
Ke, Mingze
committed
add flying-fish
1 parent f14587d commit 264f18e

9 files changed

Lines changed: 242 additions & 4 deletions

File tree

blocks/msg/en.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Blockly.Msg.WEBDUINO_CAR_R = "Right";
9191
Blockly.Msg.WEBDUINO_CAR_MOVE_FOR = "for";
9292
Blockly.Msg.WEBDUINO_CAR_MOVE_SEC = "secs";
9393

94+
Blockly.Msg.WEBDUINO_FISH = "Fish";
95+
Blockly.Msg.WEBDUINO_FISH_MOVE_FOR = "for";
96+
Blockly.Msg.WEBDUINO_FISH_MOVE_SEC = "secs";
97+
9498
Blockly.Msg.WEBDUINO_BUZZER = "Buzzer , pin:";
9599
Blockly.Msg.WEBDUINO_BUZZER_MUSIC1 = "Add Music, name:";
96100
Blockly.Msg.WEBDUINO_BUZZER_MUSIC1_EDIT = "Notes and Tempos:";

blocks/msg/zh-hant.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ Blockly.Msg.WEBDUINO_CAR_R = "右轉";
118118
Blockly.Msg.WEBDUINO_CAR_MOVE_FOR = "持續";
119119
Blockly.Msg.WEBDUINO_CAR_MOVE_SEC = "秒";
120120

121+
Blockly.Msg.WEBDUINO_FISH = "飛天魚";
122+
Blockly.Msg.WEBDUINO_FISH_MOVE_FOR = "持續";
123+
Blockly.Msg.WEBDUINO_FISH_MOVE_SEC = "秒";
124+
121125
Blockly.Msg.WEBDUINO_FIREBASE_NAME = "< Firebase > 名稱:";
122126
Blockly.Msg.WEBDUINO_FIREBASE_URL = "網址:";
123127
Blockly.Msg.WEBDUINO_FIREBASE_DO = "執行:";

blocks/webduino.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,40 @@ Blockly.Blocks['car_move'] = {
210210
}
211211
};
212212

213+
Blockly.Blocks['fish_new'] = {
214+
init: function () {
215+
this.appendDummyInput()
216+
.appendField(Blockly.Msg.WEBDUINO_FISH);
217+
this.setOutput(true);
218+
this.setColour(230);
219+
this.setTooltip('');
220+
this.setHelpUrl('http://www.example.com/');
221+
}
222+
};
223+
224+
Blockly.Blocks['fish_move'] = {
225+
init: function () {
226+
this.appendValueInput("secs_")
227+
.appendField(new Blockly.FieldVariable(" "), "fish_")
228+
.appendField(new Blockly.FieldDropdown([
229+
["↑", "soar"],
230+
["↓", "sink"],
231+
["move", "move"],
232+
["left", "left"],
233+
["right", "right"]
234+
]), "move_")
235+
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_FOR);
236+
this.appendDummyInput()
237+
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_SEC);
238+
this.setInputsInline(true);
239+
this.setPreviousStatement(true);
240+
this.setNextStatement(true);
241+
this.setColour(65);
242+
this.setTooltip('');
243+
this.setHelpUrl('http://www.example.com/');
244+
}
245+
};
246+
213247
Blockly.Blocks['timer'] = {
214248
init: function () {
215249
this.appendValueInput("secs_")
@@ -886,4 +920,4 @@ Blockly.Blocks['data_firebase_clear'] = {
886920
this.setColour(160);
887921
this.setHelpUrl('http://www.example.com/');
888922
}
889-
};
923+
};

generators/webduino.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ Blockly.JavaScript['car_move'] = function (block) {
7676
return code;
7777
};
7878

79+
Blockly.JavaScript['fish_new'] = function (block) {
80+
var code = 'getFish(board)';
81+
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
82+
};
83+
84+
Blockly.JavaScript['fish_move'] = function (block) {
85+
var variable_fish_ = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('fish_'), Blockly.Variables.NAME_TYPE);
86+
var dropdown_move_ = block.getFieldValue('move_');
87+
var value_secs_ = Blockly.JavaScript.valueToCode(block, 'secs_', Blockly.JavaScript.ORDER_NONE);
88+
var code = variable_fish_ + '.' + dropdown_move_ + '(' + value_secs_ + ');\n';
89+
block.setPromise(true);
90+
return code;
91+
};
92+
7993
Blockly.JavaScript['timer'] = function (block) {
8094
var value_secs_ = Blockly.JavaScript.valueToCode(block, 'secs_', Blockly.JavaScript.ORDER_NONE);
8195
var statements_do_ = Blockly.JavaScript.statementToCode(block, 'do_');
@@ -440,4 +454,3 @@ Blockly.JavaScript['data_firebase_clear'] = function (block) {
440454
'console.log("clear ok");\n';;
441455
return code;
442456
};
443-

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<script src="https://webduino.io/components/webduino-js/dist/webduino-all.min.js"></script>
1616
<script src="code.js"></script>
1717
<script src="webduino-blockly.js"></script>
18+
<script src="webduino-fish.js"></script>
1819
<script src="blocks/webduino.js"></script>
1920
<script src="generators/webduino.js"></script>
2021
<script src='https://cdn.firebase.com/js/client/2.2.9/firebase.js'></script>

msg/en.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ var MSG = {
2727
catShock: "Shock",
2828
catPir: "PIR Detect",
2929
catSound: "Sound Detect",
30+
catBuzzer: "Buzzer",
3031
catServo: "Servo",
3132
catCar: "Car",
32-
catBuzzer: "Buzzer",
33+
catFish: "Fish",
3334
listVariable: "list",
3435
textVariable: "text",
3536
httpRequestError: "There was a problem with the request.",

msg/zh-hant.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ var MSG = {
2222
catRelay: "繼電器",
2323
catRGBLed: "三色 LED",
2424
catUltraSonic: "超音波傳感器",
25+
catDht: "溫濕度傳感器",
2526
catButton: "按鈕開關",
2627
catShock: "震動開關",
2728
catPir: "人體紅外線偵測",
2829
catSound: "聲音偵測",
29-
catDht: "溫濕度傳感器",
3030
catBuzzer: "蜂鳴器",
3131
catServo: "伺服馬達",
3232
catCar: "遙控車",
33+
catFish: "飛天魚",
3334
listVariable: "列表",
3435
textVariable: "文字",
3536
httpRequestError: "命令出現錯誤。",

views/index.handlebars

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,23 @@
422422
</value>
423423
</block>
424424
</category>
425+
<category id="catFish">
426+
<block type="variables_set">
427+
<field name="VAR">fish</field>
428+
<value name="VALUE">
429+
<block type="fish_new"></block>
430+
</value>
431+
</block>
432+
<block type="fish_move">
433+
<field name="fish_">fish</field>
434+
<field name="move_">soar</field>
435+
<value name="secs_">
436+
<block type="math_number">
437+
<field name="NUM">1</field>
438+
</block>
439+
</value>
440+
</block>
441+
</category>
425442
<sep></sep>
426443
<category id="catButton">
427444
<block type="variables_set">

webduino-fish.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
function getFish(board) {
2+
return new Fish(board);
3+
}
4+
5+
function Fish(board) {
6+
this._board = board;
7+
this._pins = [];
8+
this._speed = 2;
9+
this._timer = null;
10+
var self = this;
11+
[0, 2, 4, 5, 12, 13, 14, 15].forEach(function (val) {
12+
var pin = new webduino.module.Led(board, board.getDigitalPin(val));
13+
self._pins.push(pin);
14+
});
15+
}
16+
17+
Fish.prototype.soar = function (secs) {
18+
var self = this;
19+
return new Promise(function (resolve, reject) {
20+
self._pins[0].off();
21+
self._pins[1].off();
22+
self._pins[2].on();
23+
self._pins[3].on();
24+
setTimeout(function () {
25+
self._pins[0].off();
26+
self._pins[1].off();
27+
self._pins[2].off();
28+
self._pins[3].off();
29+
resolve();
30+
}, secs * 1000);
31+
});
32+
};
33+
34+
Fish.prototype.sink = function (secs) {
35+
var self = this;
36+
return new Promise(function (resolve, reject) {
37+
self._pins[0].on();
38+
self._pins[1].on();
39+
self._pins[2].off();
40+
self._pins[3].off();
41+
setTimeout(function () {
42+
self._pins[0].off();
43+
self._pins[1].off();
44+
self._pins[2].off();
45+
self._pins[3].off();
46+
resolve();
47+
}, secs * 1000);
48+
});
49+
};
50+
51+
Fish.prototype.left = function (secs, speed) {
52+
var self = this;
53+
return new Promise(function (resolve, reject) {
54+
speed = speed || self._speed;
55+
self._speed = speed;
56+
self.flapLeft();
57+
setTimeout(function () {
58+
self.stopFlap();
59+
resolve();
60+
}, secs * 1000);
61+
});
62+
};
63+
64+
Fish.prototype.right = function (secs, speed) {
65+
var self = this;
66+
return new Promise(function (resolve, reject) {
67+
speed = speed || self._speed;
68+
self._speed = speed;
69+
self.flapRight();
70+
setTimeout(function () {
71+
self.stopFlap();
72+
resolve();
73+
}, secs * 1000);
74+
});
75+
};
76+
77+
Fish.prototype.move = function (secs, speed) {
78+
var self = this;
79+
return new Promise(function (resolve, reject) {
80+
speed = speed || self._speed;
81+
self._speed = speed;
82+
self.flap();
83+
setTimeout(function () {
84+
self.stopFlap();
85+
resolve();
86+
}, secs * 1000);
87+
});
88+
};
89+
90+
Fish.prototype.flap = function () {
91+
var self = this;
92+
if (self._timer) {
93+
self.stopFlap();
94+
}
95+
self._pins[4].on();
96+
self._pins[5].on();
97+
self._pins[6].off();
98+
self._pins[7].off();
99+
self._timer = setTimeout(function () {
100+
self._pins[4].off();
101+
self._pins[5].off();
102+
self._pins[6].on();
103+
self._pins[7].on();
104+
self._timer = setTimeout(function () {
105+
if (self._timer) {
106+
self.flap();
107+
}
108+
}, Fish.SPEED[self._speed]);
109+
}, Fish.SPEED[self._speed]);
110+
};
111+
112+
Fish.prototype.flapRight = function () {
113+
var self = this;
114+
if (self._timer) {
115+
self.stopFlap();
116+
}
117+
self._pins[4].on();
118+
self._pins[5].on();
119+
self._pins[6].off();
120+
self._pins[7].off();
121+
self._timer = setTimeout(function () {
122+
self._pins[4].off();
123+
self._pins[5].off();
124+
self._pins[6].off();
125+
self._pins[7].off();
126+
self._timer = setTimeout(function () {
127+
if (self._timer) {
128+
self.flapRight();
129+
}
130+
}, Fish.SPEED[self._speed]);
131+
}, Fish.SPEED[self._speed]);
132+
};
133+
134+
Fish.prototype.flapLeft = function () {
135+
var self = this;
136+
if (self._timer) {
137+
self.stopFlap();
138+
}
139+
self._pins[4].off();
140+
self._pins[5].off();
141+
self._pins[6].on();
142+
self._pins[7].on();
143+
self._timer = setTimeout(function () {
144+
self._pins[4].off();
145+
self._pins[5].off();
146+
self._pins[6].off();
147+
self._pins[7].off();
148+
self._timer = setTimeout(function () {
149+
if (self._timer) {
150+
self.flapLeft();
151+
}
152+
}, Fish.SPEED[self._speed]);
153+
}, Fish.SPEED[self._speed]);
154+
};
155+
156+
Fish.prototype.stopFlap = function () {
157+
if (this._timer) {
158+
clearTimeout(this._timer);
159+
delete this._timer;
160+
}
161+
};
162+
163+
Fish.SPEED = [1500, 900, 300];

0 commit comments

Comments
 (0)