Skip to content

Commit a1a4fe2

Browse files
committed
add deviceOrientation listener
1 parent 4423c58 commit a1a4fe2

8 files changed

Lines changed: 187 additions & 22 deletions

File tree

blocks/msg/en.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ Blockly.Msg.DEMO_CONTROLLER_SCREENSHOW = "Controller screen";
9292
Blockly.Msg.DEMO_CONTROLLER_SCREENBG = "background";
9393
Blockly.Msg.DEMO_CONTROLLER_SCREENTEXT = "text";
9494
Blockly.Msg.DEMO_CONTROLLER_SCREENCOLOR = "'s color'";
95+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION = "Mobile orientation";
96+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_DO = "do";
97+
Blockly.Msg.DEMO_CONTROLLER_MOBILE = "Orientation";
98+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_ALPHA = "alpha";
99+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_BETA = "beta";
100+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_GAMMA = "gamma";
101+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_VAL1 = "'s value, round:'";
102+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_VAL2 = "";
103+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_REMOVE = "Stop orientation";
95104

96105
Blockly.Msg.CUSTOM_JS_CONSOLE = "Console:";
97106
Blockly.Msg.CUSTOM_JS_Date = "Date:";

blocks/msg/zh-hant.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ Blockly.Msg.DEMO_CONTROLLER_SCREENSHOW = "遙控器螢幕";
9898
Blockly.Msg.DEMO_CONTROLLER_SCREENBG = "背景";
9999
Blockly.Msg.DEMO_CONTROLLER_SCREENTEXT = "文字";
100100
Blockly.Msg.DEMO_CONTROLLER_SCREENCOLOR = "顏色";
101+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION = "行動裝置,偵測旋轉與翻轉";
102+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_DO = "執行";
103+
Blockly.Msg.DEMO_CONTROLLER_MOBILE = "行動裝置";
104+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_ALPHA = "水平旋轉";
105+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_BETA = "前後翻轉";
106+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_GAMMA = "左右翻轉";
107+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_VAL1 = "的數值 ( 小數點";
108+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_VAL2 = "位 )";
109+
Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_REMOVE = "停止偵測旋轉與翻轉";
101110

102111

103112
// Console

blocks/webduino.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,68 @@ Blockly.Blocks['demo_controller_showcolor'] = {
617617
}
618618
};
619619

620+
/*
621+
.88b d88. .d88b. d8888b. d888888b db d88888b
622+
88'YbdP`88 .8P Y8. 88 `8D `88' 88 88'
623+
88 88 88 88 88 88oooY' 88 88 88ooooo
624+
88 88 88 88 88 88~~~b. 88 88 88~~~~~
625+
88 88 88 `8b d8' 88 8D .88. 88booo. 88.
626+
YP YP YP `Y88P' Y8888P' Y888888P Y88888P Y88888P
627+
*/
628+
629+
630+
Blockly.Blocks['mobile_deviceorientation_event'] = {
631+
init: function () {
632+
this.appendDummyInput()
633+
.appendField(Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION, "行動裝置,偵測旋轉與翻轉");
634+
this.appendStatementInput("do_")
635+
.setCheck(null)
636+
.appendField(Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_DO, "執行");
637+
this.setPreviousStatement(true, null);
638+
this.setNextStatement(true, null);
639+
this.setTooltip('');
640+
this.setColour(190);
641+
this.setHelpUrl('http://www.example.com/');
642+
}
643+
};
644+
645+
Blockly.Blocks['mobile_deviceorientation_event_val'] = {
646+
init: function () {
647+
this.appendDummyInput()
648+
.appendField(Blockly.Msg.DEMO_CONTROLLER_MOBILE, "行動裝置")
649+
.appendField(new Blockly.FieldDropdown([
650+
[Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_ALPHA, "水平旋轉", "alpha"],
651+
[Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_BETA, "前後翻轉", "beta"],
652+
[Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_GAMMA, "左右翻轉", "gamma"]
653+
]), "val_")
654+
.appendField(Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_VAL1, "的數值 ( 小數點")
655+
.appendField(new Blockly.FieldDropdown([
656+
["0", '1'],
657+
["1", '10'],
658+
["2", '100']
659+
]), "round_")
660+
.appendField(Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_VAL2, "位 )");
661+
this.setOutput(true, null);
662+
this.setTooltip('');
663+
this.setColour(210);
664+
this.setHelpUrl('http://www.example.com/');
665+
}
666+
};
667+
668+
669+
Blockly.Blocks['mobile_deviceorientation_event_remove'] = {
670+
init: function () {
671+
this.appendDummyInput()
672+
.appendField(Blockly.Msg.DEMO_CONTROLLER_MOBILE_DEVICEORIENTATION_REMOVE, "停止偵測旋轉與翻轉");
673+
this.setPreviousStatement(true, null);
674+
this.setNextStatement(true, null);
675+
this.setTooltip('');
676+
this.setColour(190);
677+
this.setHelpUrl('http://www.example.com/');
678+
}
679+
};
680+
681+
620682
/*
621683
. oooo
622684
.o8 `888

generators/webduino.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,46 @@ Blockly.JavaScript['demo_controller_showcolor'] = function (block) {
387387
return code;
388388
};
389389

390+
/*
391+
.88b d88. .d88b. d8888b. d888888b db d88888b
392+
88'YbdP`88 .8P Y8. 88 `8D `88' 88 88'
393+
88 88 88 88 88 88oooY' 88 88 88ooooo
394+
88 88 88 88 88 88~~~b. 88 88 88~~~~~
395+
88 88 88 `8b d8' 88 8D .88. 88booo. 88.
396+
YP YP YP `Y88P' Y8888P' Y888888P Y88888P Y88888P
397+
*/
398+
399+
400+
// https://blockly-demo.appspot.com/static/demos/blockfactory/index.html#yztfg4
401+
Blockly.JavaScript['mobile_deviceorientation_event'] = function (block) {
402+
var statements_do_ = Blockly.JavaScript.statementToCode(block, 'do_');
403+
var code = 'setDeviceOrientationListener(function(alpha, beta, gamma){\n'+
404+
statements_do_ +
405+
'});\n';
406+
return code;
407+
};
408+
409+
410+
Blockly.JavaScript['mobile_deviceorientation_event_val'] = function (block) {
411+
var dropdown_val_ = block.getFieldValue('val_');
412+
var dropdown_round_ = block.getFieldValue('round_');
413+
var code;
414+
if(dropdown_round_==1){
415+
code = 'Math.round(' + dropdown_val_ + ')';
416+
}else{
417+
code = 'Math.round(' + dropdown_val_ + '*' + dropdown_round_ + ')/' + dropdown_round_;
418+
}
419+
420+
return [code, Blockly.JavaScript.ORDER_NONE];
421+
};
422+
423+
Blockly.JavaScript['mobile_deviceorientation_event_remove'] = function(block) {
424+
var code = 'removeDeviceOrientationListener();\n';
425+
return code;
426+
};
427+
428+
429+
390430
/*
391431
. oooo
392432
.o8 `888
@@ -637,9 +677,9 @@ Blockly.JavaScript['board_ready'] = function (block) {
637677
var statements_callbacks_ = Blockly.JavaScript.statementToCode(block, 'callbacks_');
638678
var type;
639679
if (checkbox_type_ == '1') {
640-
if(checkbox_multi_=='TRUE'){
680+
if (checkbox_multi_ == 'TRUE') {
641681
type = 'boardReady({device:' + value_device_ + ' , multi:true}, async function (board) {\n';
642-
}else{
682+
} else {
643683
type = 'boardReady(' + value_device_ + ', async function (board) {\n';
644684
}
645685
} else if (checkbox_type_ == '2') {
@@ -1320,18 +1360,18 @@ Blockly.JavaScript['data_firebase_read'] = function (block) {
13201360
code = variable_name_ + '.on("value", function(snapshot) {\n' +
13211361
' ' + value_read_ + '=[];\n' +
13221362
' snapshot.forEach(function(data) {\n' +
1323-
' if(data.val().'+text_attr_+'){\n'+
1363+
' if(data.val().' + text_attr_ + '){\n' +
13241364
' ' + value_read_ + '.push(data.val().' + text_attr_ + ');\n' +
1325-
' }\n'+
1365+
' }\n' +
13261366
' });\n' +
13271367
statements_do_ +
13281368
' }, function (errorObject) {\n' +
13291369
' console.log("The read failed: " + errorObject.code);\n' +
13301370
'});\n';
13311371
} else {
13321372
code = variable_name_ + '.limitToLast(1).on("child_added",function(snapshot){\n' +
1333-
' ' + value_read_ + ' = snapshot.val().'+text_attr_+';\n' +
1334-
statements_do_ +
1373+
' ' + value_read_ + ' = snapshot.val().' + text_attr_ + ';\n' +
1374+
statements_do_ +
13351375
'});\n';
13361376
}
13371377
return code;

msg/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var MSG = {
6767
catMenu2: "Sensors",
6868
catMenu3: "Toies",
6969
catSpeech: "Speech",
70+
catMobile: "Mobild",
7071
catTracking: "Tracking",
7172
catKeyCode: "Keyboard",
7273
catPlus: "Advanced",

msg/zh-hant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var MSG = {
6767
catMenu2: "傳感器",
6868
catMenu3: "玩具應用",
6969
catSpeech: "語音聲控",
70+
catMobile: "行動裝置",
7071
catTracking: "影像辨識",
7172
catKeyCode: "鍵盤行為",
7273
catPlus: "進階功能",

views/index.handlebars

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,6 @@
409409
</value>
410410
</block>
411411
</category>
412-
<category id="catTextLogic">
413-
<block type="text_indexof">
414-
<value name="indexof_">
415-
<block type="text">
416-
<field name="TEXT"></field>
417-
</block>
418-
</value>
419-
</block>
420-
</category>
421412
<category id="catMathOther">
422413
<block type="math_round_digit"></block>
423414
<block type="math_value_conversion">
@@ -449,6 +440,13 @@
449440
</block>
450441
</value>
451442
</block>
443+
<block type="text_indexof">
444+
<value name="indexof_">
445+
<block type="text">
446+
<field name="TEXT"></field>
447+
</block>
448+
</value>
449+
</block>
452450
</category>
453451
<category id="catRepeat">
454452
<block type="status_repeat">
@@ -483,13 +481,11 @@
483481
</block>
484482
<block type="sound_recognition_text"></block>
485483
<block type="sound_recognition_stop"></block>
486-
<block type="translate_speech">
487-
<value name="speech_">
488-
<block type="text">
489-
<field name="TEXT"></field>
490-
</block>
491-
</value>
492-
</block>
484+
</category>
485+
<category id="catMobile">
486+
<block type="mobile_deviceorientation_event"></block>
487+
<block type="mobile_deviceorientation_event_val"></block>
488+
<block type="mobile_deviceorientation_event_remove"></block>
493489
</category>
494490
<sep></sep>
495491
</category>

webduino-blockly.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
var boards = [],
1212
undef = void 0;
1313

14+
var orientationEventListener = function () {};
15+
1416
function boardReady(options, callback) {
1517
var board;
1618

@@ -87,6 +89,48 @@
8789
return candidate;
8890
}
8991

92+
function deviceOrientation(event) {
93+
var alpha, beta, gamma;
94+
if(event.webkitCompassHeading){
95+
alpha = event.webkitCompassHeading;
96+
}else{
97+
alpha = event.alpha;
98+
if(!window.chrome){
99+
alpha = alpha - 270;
100+
}
101+
}
102+
beta = event.beta;
103+
gamma = event.gamma;
104+
return [alpha,beta,gamma];
105+
}
106+
107+
function setDeviceOrientationListener(listener) {
108+
removeDeviceOrientationListener();
109+
110+
if (typeof listener === 'function') {
111+
orientationEventListener = function (event) {
112+
var alpha, beta, gamma;
113+
if (event.webkitCompassHeading) {
114+
alpha = event.webkitCompassHeading;
115+
} else {
116+
alpha = event.alpha;
117+
if (!window.chrome) {
118+
alpha = alpha - 270;
119+
}
120+
}
121+
beta = event.beta;
122+
gamma = event.gamma;
123+
listener.apply(undef, [alpha, beta, gamma]);
124+
};
125+
126+
window.addEventListener('deviceorientation', orientationEventListener);
127+
}
128+
}
129+
130+
function removeDeviceOrientationListener() {
131+
window.removeEventListener('deviceorientation', orientationEventListener);
132+
}
133+
90134
function getLed(board, pin) {
91135
return new webduino.module.Led(board, board.getDigitalPin(pin));
92136
}
@@ -382,5 +426,8 @@
382426
scope.getJoystick = getJoystick;
383427
scope.getRFID = getRFID;
384428
scope.getToyCar = getToyCar;
429+
scope.deviceOrientation = deviceOrientation;
430+
scope.setDeviceOrientationListener = setDeviceOrientationListener;
431+
scope.removeDeviceOrientationListener = removeDeviceOrientationListener;
385432

386433
}));

0 commit comments

Comments
 (0)