Skip to content

Commit 5787a58

Browse files
committed
init
1 parent dd84963 commit 5787a58

15 files changed

Lines changed: 318 additions & 0 deletions

MAX44009-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.getMAX44009 = function (board, sda, scl) {
6+
return new webduino.module.MAX44009(board, sda, scl);
7+
};
8+
9+
}(window, window.webduino));

MAX44009.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 MAX44009_MESSAGE = [0x04, 0x42];
15+
16+
var MAX44009Event = {
17+
MESSAGE: 'message'
18+
};
19+
20+
function MAX44009(board, sda, scl) {
21+
Module.call(this);
22+
this.board = board;
23+
this.init = false;
24+
this.lux = 0;
25+
this._sda = sda;
26+
this._scl = scl;
27+
this.callback = function() {};
28+
this.messageHandler = onMessage.bind(this);
29+
this.board.send([0xf0, 0x04, 0x42, 0x00, this._sda, this._scl,0xf7]);
30+
}
31+
32+
function onMessage(event) {
33+
var msg = event.message;
34+
if (msg[0] == MAX44009_MESSAGE[0] && msg[1] == MAX44009_MESSAGE[1]) {
35+
this.emit(MAX44009Event.MESSAGE, msg.slice(2));
36+
}
37+
}
38+
39+
MAX44009.prototype = proto = Object.create(Module.prototype, {
40+
// constructor: {
41+
// value: MAX44009
42+
// },
43+
// state: {
44+
// get: function() {
45+
// return this.state;
46+
// },
47+
// set: function(val) {
48+
// this.state = val;
49+
// }
50+
// }
51+
});
52+
53+
proto.on = function(callback) {
54+
this.board.send([0xf0, 0x04, 0x42, 0x01, 0xf7]);
55+
if (typeof callback !== 'function') {
56+
callback = function() {};
57+
}
58+
this.callback = function(rawData) { // rawData為array
59+
var val = '';
60+
console.log(rawData);
61+
for (var i = 0; i < rawData.length; i++) {
62+
val += String.fromCharCode(rawData[i]);
63+
}
64+
var StrAra = '[' + val + ']';
65+
var info = eval(StrAra);
66+
this.lux = info[0];
67+
callback(this.lux);
68+
};
69+
this.state = 'on';
70+
this.board.on(BoardEvent.SYSEX_MESSAGE, this.messageHandler);
71+
this.addListener(MAX44009Event.MESSAGE, this.callback);
72+
};
73+
74+
proto.off = function() {
75+
this.state = 'off';
76+
this.board.send([0xf0, 0x04, 0x42, 0x02, 0xf7]);
77+
this.board.removeListener(BoardEvent.SYSEX_MESSAGE, this.messageHandler);
78+
this.removeListener(MAX44009Event.MESSAGE, this.callback);
79+
this.callback = null;
80+
};
81+
82+
scope.module.MAX44009 = MAX44009;
83+
}));

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# webduino-module-max44009
2+
3+
Webduino Module for MAX44009 sensor.
4+
5+
## Installation
6+
7+
```shell
8+
bower install https://github.com/webduinoio/webduino-module-max44009.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": ["max44009_new", "max44009_on", "max44009_get_value", "max44009_off"],
3+
"category": "catMenu3",
4+
"scripts": [
5+
"blockly/blocks.js",
6+
"blockly/javascript.js"
7+
],
8+
"dependencies": [
9+
"MAX44009.js",
10+
"MAX44009-blockly.js"
11+
],
12+
"msg": "blockly/msg",
13+
"blocksMsg": "blockly/msg/blocks",
14+
"toolbox": "blockly/toolbox.xml"
15+
}

blockly/blocks.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Blockly.Blocks['max44009_new'] = {
2+
init: function() {
3+
this.appendDummyInput()
4+
.appendField(Blockly.Msg.WEBDUINO_MAX44009_SDA, "MAX44009,SDA")
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+
["14", "14"],
19+
["15", "15"],
20+
["16", "16"],
21+
["17", "17"],
22+
["18", "18"],
23+
["19", "19"]
24+
]), "sda")
25+
.appendField(Blockly.Msg.WEBDUINO_MAX44009_SCL, " SCL")
26+
.appendField(new Blockly.FieldDropdown([
27+
["2", "2"],
28+
["3", "3"],
29+
["4", "4"],
30+
["5", "5"],
31+
["6", "6"],
32+
["7", "7"],
33+
["8", "8"],
34+
["9", "9"],
35+
["10", "10"],
36+
["11", "11"],
37+
["12", "12"],
38+
["13", "13"],
39+
["14", "14"],
40+
["15", "15"],
41+
["16", "16"],
42+
["17", "17"],
43+
["18", "18"],
44+
["19", "19"]
45+
]), "scl");
46+
this.setOutput(true, null);
47+
this.setColour(230);
48+
this.setTooltip('');
49+
this.setHelpUrl('http://www.example.com/');
50+
}
51+
};
52+
53+
Blockly.Blocks['max44009_on'] = {
54+
init: function() {
55+
this.appendDummyInput()
56+
.appendField(new Blockly.FieldVariable("max44009"), "var")
57+
.appendField(Blockly.Msg.WEBDUINO_MAX44009_START, "開始偵測");
58+
this.appendStatementInput("do")
59+
.setCheck(null)
60+
.appendField(Blockly.Msg.WEBDUINO_MAX44009_DO, "執行");
61+
this.setPreviousStatement(true, null);
62+
this.setNextStatement(true, null);
63+
this.setColour(65);
64+
this.setTooltip('');
65+
this.setHelpUrl('http://www.example.com/');
66+
}
67+
};
68+
69+
Blockly.Blocks['max44009_off'] = {
70+
init: function() {
71+
this.appendDummyInput()
72+
.appendField(new Blockly.FieldVariable("max44009"), "var")
73+
.appendField(Blockly.Msg.WEBDUINO_MAX44009_STOP, "停止偵測");
74+
this.setPreviousStatement(true, null);
75+
this.setNextStatement(true, null);
76+
this.setColour(65);
77+
this.setTooltip('');
78+
this.setHelpUrl('http://www.example.com/');
79+
}
80+
};
81+
82+
Blockly.Blocks['max44009_val'] = {
83+
init: function() {
84+
this.appendDummyInput()
85+
.appendField(new Blockly.FieldVariable("max44009"), "var")
86+
.appendField(Blockly.Msg.WEBDUINO_MAX44009_VAL, "數值");
87+
this.setOutput(true, null);
88+
this.setColour(35);
89+
this.setTooltip('');
90+
this.setHelpUrl('http://www.example.com/');
91+
}
92+
};

blockly/javascript.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Blockly.JavaScript['max44009_new'] = function(block) {
2+
var dropdown_sda = block.getFieldValue('sda');
3+
var dropdown_scl = block.getFieldValue('scl');
4+
var code = 'getMAX44009(board,'+ dropdown_sda + ',' + dropdown_scl+ ')';
5+
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
6+
};
7+
8+
Blockly.JavaScript['max44009_on'] = function(block) {
9+
var variable_var = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('var'), Blockly.Variables.NAME_TYPE);
10+
var statements_do = Blockly.JavaScript.statementToCode(block, 'do');
11+
var code = variable_var + '.on(async function(_lux){\n'+
12+
' ' + variable_var + '._lux = _lux;\n' +
13+
statements_do + '});\n';
14+
return code;
15+
};
16+
17+
Blockly.JavaScript['max44009_off'] = function(block) {
18+
var variable_var = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('var'), Blockly.Variables.NAME_TYPE);
19+
var code = variable_var + '.off();\n';
20+
return code;
21+
};
22+
23+
Blockly.JavaScript['max44009_val'] = function(block) {
24+
var variable_var = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('var'), Blockly.Variables.NAME_TYPE);
25+
var dropdown_info = block.getFieldValue('info');
26+
var code = variable_var + '._lux';
27+
return [code, Blockly.JavaScript.ORDER_ATOMIC];
28+
};

blockly/msg/blocks/en.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// MAX44009
2+
Blockly.Msg.WEBDUINO_MAX44009_SDA = "MAX44009, SDA";
3+
Blockly.Msg.WEBDUINO_MAX44009_SCL = " SCL";
4+
Blockly.Msg.WEBDUINO_MAX44009_START = "start Detection";
5+
Blockly.Msg.WEBDUINO_MAX44009_DO = "execute";
6+
Blockly.Msg.WEBDUINO_MAX44009_STOP = "stop detection";
7+
Blockly.Msg.WEBDUINO_MAX44009_VAL = "value";

blockly/msg/blocks/zh-hans.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// MAX44009
2+
Blockly.Msg.WEBDUINO_MAX44009_SDA = "MAX44009,SDA";
3+
Blockly.Msg.WEBDUINO_MAX44009_SCL = " SCL";
4+
Blockly.Msg.WEBDUINO_MAX44009_START = "开始侦测";
5+
Blockly.Msg.WEBDUINO_MAX44009_DO = "执行";
6+
Blockly.Msg.WEBDUINO_MAX44009_STOP = "停止侦测";
7+
Blockly.Msg.WEBDUINO_MAX44009_VAL = "数值";

blockly/msg/blocks/zh-hant.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// MAX44009
2+
Blockly.Msg.WEBDUINO_MAX44009_SDA = "MAX44009,SDA";
3+
Blockly.Msg.WEBDUINO_MAX44009_SCL = " SCL";
4+
Blockly.Msg.WEBDUINO_MAX44009_START = "開始偵測";
5+
Blockly.Msg.WEBDUINO_MAX44009_DO = "執行";
6+
Blockly.Msg.WEBDUINO_MAX44009_STOP = "停止偵測";
7+
Blockly.Msg.WEBDUINO_MAX44009_VAL = "數值";

blockly/msg/en.js

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

0 commit comments

Comments
 (0)