Skip to content

Commit f4f9a8d

Browse files
committed
add GPS blocks
1 parent a905d2f commit f4f9a8d

File tree

15 files changed

+360
-0
lines changed

15 files changed

+360
-0
lines changed

GPS-blockly.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
+(function (window, webduino) {
2+
3+
'use strict';
4+
5+
window.getGPS = function (board, rx, tx) {
6+
return new webduino.module.GPS(board, rx, tx);
7+
};
8+
9+
}(window, window.webduino));

GPS.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
+(function (factory) {
2+
if (typeof exports === 'undefined') {
3+
factory(webduino || {});
4+
} else {
5+
module.exports = factory;
6+
}
7+
}(function (scope) {
8+
'use strict';
9+
10+
var Module = scope.Module,
11+
BoardEvent = scope.BoardEvent,
12+
proto;
13+
14+
var GPS_MESSAGE = [0x04, 0x0C],
15+
MIN_READ_INTERVAL = 1000,
16+
MIN_RESPONSE_TIME = 30,
17+
RETRY_INTERVAL = 6000;
18+
19+
var GPSEvent = {
20+
READ: 'read',
21+
READ_ERROR: 'readError'
22+
};
23+
24+
function GPS(board, rx, tx) {
25+
Module.call(this);
26+
27+
this._type = 'GPS';
28+
this._board = board;
29+
this._rx = rx;
30+
this._tx = tx;
31+
this._longitude = null;
32+
this._latitude = null;
33+
this._time = null;
34+
this._lastRecv = null;
35+
this._readTimer = null;
36+
this._readCallback = function () {};
37+
38+
this._board.on(BoardEvent.BEFOREDISCONNECT, this.stopRead.bind(this));
39+
this._messageHandler = onMessage.bind(this);
40+
this._board.on(BoardEvent.ERROR, this.stopRead.bind(this));
41+
}
42+
43+
function onMessage(event) {
44+
var message = event.message;
45+
46+
if (message[0] !== GPS_MESSAGE[0] || message[1] !== GPS_MESSAGE[1]) {
47+
return;
48+
} else {
49+
processGPSData(this, message);
50+
}
51+
}
52+
53+
function processGPSData(self, data) {
54+
var str = '';
55+
for (var i = 4; i < data.length; i++) {
56+
str += String.fromCharCode(data[i]);
57+
}
58+
str = str.split(' ');
59+
var location = str[0].split(',');
60+
self._lastRecv = Date.now();
61+
self._longitude = location[0];
62+
self._latitude = location[1];
63+
self._time = str[1];
64+
self.emit(GPSEvent.READ, location[0], location[1], str[1]);
65+
}
66+
67+
GPS.prototype = proto = Object.create(Module.prototype, {
68+
constructor: {
69+
value: GPS
70+
},
71+
longitude: {
72+
get: function () {
73+
return this._longitude;
74+
}
75+
},
76+
latitude: {
77+
get: function () {
78+
return this._latitude;
79+
}
80+
},
81+
time: {
82+
get: function () {
83+
return this._time;
84+
}
85+
}
86+
});
87+
88+
proto.read = function (callback, interval) {
89+
var self = this,
90+
timer;
91+
92+
self.stopRead();
93+
94+
if (typeof callback === 'function') {
95+
self._readCallback = function (longitude, latitude, time) {
96+
self._location = location;
97+
self._time = time;
98+
callback({
99+
longitude: longitude,
100+
latitude: latitude,
101+
time: time
102+
});
103+
};
104+
self._board.on(BoardEvent.SYSEX_MESSAGE, self._messageHandler);
105+
self.on(GPSEvent.READ, self._readCallback);
106+
107+
timer = function () {
108+
self._board.sendSysex(GPS_MESSAGE[0], [GPS_MESSAGE[1]]);
109+
if (interval) {
110+
interval = Math.max(interval, MIN_READ_INTERVAL);
111+
if (self._lastRecv === null || Date.now() - self._lastRecv < 5 * interval) {
112+
self._readTimer = setTimeout(timer, interval);
113+
} else {
114+
self.stopRead();
115+
setTimeout(function () {
116+
self.read(callback, interval);
117+
}, RETRY_INTERVAL);
118+
}
119+
}
120+
};
121+
122+
timer();
123+
} else {
124+
return new Promise(function (resolve, reject) {
125+
self.read(function (data) {
126+
self._location = data.location;
127+
self._time = data.time;
128+
setTimeout(function () {
129+
resolve(data);
130+
}, MIN_RESPONSE_TIME);
131+
});
132+
});
133+
}
134+
};
135+
136+
proto.stopRead = function () {
137+
this.removeListener(GPSEvent.READ, this._readCallback);
138+
this._board.removeListener(BoardEvent.SYSEX_MESSAGE, this._messageHandler);
139+
this._lastRecv = null;
140+
141+
if (this._readTimer) {
142+
clearTimeout(this._readTimer);
143+
delete this._readTimer;
144+
}
145+
};
146+
147+
scope.module.GPSEvent = GPSEvent;
148+
scope.module.GPS = GPS;
149+
}));

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# webduino-module-gps
2+
3+
Webduino Module for GPS.
4+
5+
## Installation
6+
7+
```shell
8+
bower install https://github.com/webduinoio/webduino-module-gps.git
9+
```
10+
11+
## License
12+
13+
This project is licensed under the MIT license, see [LICENSE](LICENSE) for more information.

blockly.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"types": ["gps_new","gps_read","gps_get_number"],
3+
"category": "catMenu2",
4+
"scripts": [
5+
"blockly/blocks.js",
6+
"blockly/javascript.js"
7+
],
8+
"dependencies": [
9+
"GPS.js",
10+
"GPS-blockly.js"
11+
],
12+
"msg": "blockly/msg",
13+
"blocksMsg": "blockly/msg/blocks",
14+
"toolbox": "blockly/toolbox.xml"
15+
}

blockly/blocks.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Blockly.Blocks['gps_new'] = {
2+
init: function () {
3+
this.appendDummyInput()
4+
.appendField(Blockly.Msg.WEBDUINO_GPS_RX, "GPS定位,rx")
5+
.appendField(new Blockly.FieldDropdown([
6+
["2", "2"],
7+
["3", "3"],
8+
["4", "4"],
9+
["5", "5"],
10+
["6", "6"],
11+
["7", "7"],
12+
["8", "8"],
13+
["9", "9"],
14+
["10", "10"],
15+
["11", "11"],
16+
["12", "12"],
17+
["13", "13"]
18+
]), "rx_")
19+
.appendField(Blockly.Msg.WEBDUINO_GPS_TX, " tx")
20+
.appendField(new Blockly.FieldDropdown([
21+
["2", "2"],
22+
["3", "3"],
23+
["4", "4"],
24+
["5", "5"],
25+
["6", "6"],
26+
["7", "7"],
27+
["8", "8"],
28+
["9", "9"],
29+
["10", "10"],
30+
["11", "11"],
31+
["12", "12"],
32+
["13", "13"]
33+
]), "tx_");
34+
this.setOutput(true);
35+
this.setColour(230);
36+
this.setTooltip('');
37+
this.setHelpUrl('https://webduino.io');
38+
}
39+
};
40+
41+
//https://blockly-demo.appspot.com/static/demos/blockfactory_old/index.html#ohq8mv
42+
Blockly.Blocks['gps_read'] = {
43+
init: function () {
44+
this.appendValueInput("qryTime")
45+
.setCheck(null)
46+
.appendField(new Blockly.FieldVariable("gps"), "gps")
47+
.appendField("取得經緯度和時間,每");
48+
this.appendDummyInput()
49+
.appendField("毫秒執行一次");
50+
this.appendStatementInput("callback")
51+
.setCheck(null)
52+
.appendField("執行");
53+
this.setInputsInline(true);
54+
this.setPreviousStatement(true, null);
55+
this.setNextStatement(true, null);
56+
this.setColour(65);
57+
this.setTooltip('');
58+
this.setHelpUrl('http://www.example.com/');
59+
}
60+
};
61+
62+
63+
Blockly.Blocks['gps_get_data'] = {
64+
init: function() {
65+
this.appendDummyInput()
66+
.appendField(new Blockly.FieldVariable("gps"), "gps")
67+
.appendField("所測得目前的")
68+
.appendField(new Blockly.FieldDropdown([["經度", "longitude"], ["緯度", "latitude"], ["時間", "time"]]), "dataType");
69+
this.setOutput(true, null);
70+
this.setColour(20);
71+
this.setTooltip('');
72+
this.setHelpUrl('http://www.example.com/');
73+
}
74+
};

blockly/javascript.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Blockly.JavaScript['gps_new'] = function (block) {
2+
var dropdown_rx_ = block.getFieldValue('rx_');
3+
var dropdown_tx_ = block.getFieldValue('tx_');
4+
var code = 'getGPS(board,' + dropdown_rx_ + ',' + dropdown_tx_ + ')';
5+
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
6+
};
7+
8+
Blockly.JavaScript['gps_read'] = function (block) {
9+
var variable_gps = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('gps'), Blockly.Variables.NAME_TYPE);
10+
var value_qryTime = Blockly.JavaScript.valueToCode(block, 'qryTime', Blockly.JavaScript.ORDER_ATOMIC);
11+
var statements_callback = Blockly.JavaScript.statementToCode(block, 'callback');
12+
var code = variable_gps + '.read(async function(evt){\n' +
13+
statements_callback + '}, ' + value_qryTime + ');\n';
14+
return code;
15+
};
16+
17+
Blockly.JavaScript['gps_get_data'] = function(block) {
18+
var variable_gps = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('gps'), Blockly.Variables.NAME_TYPE);
19+
var dropdown_datatype = block.getFieldValue('dataType');
20+
var code = variable_gps + '.' + dropdown_datatype;
21+
return [code, Blockly.JavaScript.ORDER_NONE];
22+
};

blockly/msg/blocks/en.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// GPS
2+
Blockly.Msg.WEBDUINO_GPS_RX = "GPS (TinyGPS),RX ";
3+
Blockly.Msg.WEBDUINO_GPS_TX = " TX";
4+
Blockly.Msg.WEBDUINO_GPS_LOCATE = "Location:";
5+
Blockly.Msg.WEBDUINO_GPS_TIME = "Time:";
6+
Blockly.Msg.WEBDUINO_GPS_GET_NOW = "read Info:";

blockly/msg/blocks/zh-hans.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// GPS
2+
Blockly.Msg.WEBDUINO_GPS_RX = "GPS (TinyGPS),RX ";
3+
Blockly.Msg.WEBDUINO_GPS_TX = " TX";
4+
Blockly.Msg.WEBDUINO_GPS_LOCATE = "Location:";
5+
Blockly.Msg.WEBDUINO_GPS_TIME = "Time:";
6+
Blockly.Msg.WEBDUINO_GPS_GET_NOW = "read Info:";

blockly/msg/blocks/zh-hant.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// GPS
2+
Blockly.Msg.WEBDUINO_GPS_RX = "GPS 定位(TinyGPS),RX ";
3+
Blockly.Msg.WEBDUINO_GPS_TX = " TX";
4+
Blockly.Msg.WEBDUINO_GPS_LOCATE = "經緯度:";
5+
Blockly.Msg.WEBDUINO_GPS_TIME = "目前時間:";
6+
Blockly.Msg.WEBDUINO_GPS_GET_NOW = "取得資訊:";

blockly/msg/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MSG.catGPS = "GPS";

blockly/msg/zh-hans.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MSG.catGPS = "GPS";

blockly/msg/zh-hant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MSG.catGPS = "GPS";

blockly/toolbox.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<category id="catGPS" tags="fly,mark1">
2+
<block type="variables_set">
3+
<field name="VAR">gps</field>
4+
<value name="VALUE">
5+
<block type="gps_new">
6+
<field name="rx_">10</field>
7+
<field name="tx_">11</field>
8+
</block>
9+
</value>
10+
</block>
11+
<block type="gps_read">
12+
<value name="qryTime">
13+
<block type="math_number">
14+
<field name="NUM">1000</field>
15+
</block>
16+
</value>
17+
</block>
18+
<block type="gps_get_data"></block>
19+
</category>

bower.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "webduino-module-gps",
3+
"description": "GPS Module for Webduino",
4+
"main": "GPS.js",
5+
"authors": "",
6+
"license": "ISC",
7+
"keywords": [
8+
"arduino",
9+
"webduino",
10+
"tinygps"
11+
],
12+
"ignore": [
13+
"**/.*",
14+
"node_modules",
15+
"bower_components",
16+
"test",
17+
"tests"
18+
]
19+
}

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "webduino-module-lcd1602",
3+
"version": "1.0.0",
4+
"description": "LCD1602 Module for Webduino",
5+
"main": "LCD1602.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"keywords": [
12+
"arduino",
13+
"webduino",
14+
"lcd1602"
15+
],
16+
"dependencies": {
17+
"webduino-js": "0.x"
18+
}
19+
}

0 commit comments

Comments
 (0)