mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Added support for sending and receiving strings
This commit is contained in:
parent
ea9356a3cc
commit
57445cdfd0
@ -27,6 +27,7 @@
|
||||
<select type="text" id="node-input-state" style="width: 150px;">
|
||||
<option value="INPUT" data-i18n="arduino.state.in.digital"></option>
|
||||
<option value="ANALOG" data-i18n="arduino.state.in.analogue"></option>
|
||||
<option value="STRING" data-i18n="arduino.state.in.string"></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
@ -83,6 +84,7 @@
|
||||
<option value="OUTPUT" data-i18n="arduino.state.out.digital"></option>
|
||||
<option value="PWM" data-i18n="arduino.state.out.analogue"></option>
|
||||
<option value="SERVO" data-i18n="arduino.state.out.servo"></option>
|
||||
<option value="STRING" data-i18n="arduino.state.out.string"></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
@ -106,7 +108,7 @@
|
||||
color:"#3fadb5",
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
pin: {value:"",required:true},
|
||||
pin: {value:"",required:false},
|
||||
state: {value:"",required:true},
|
||||
arduino: {type:"arduino-board"}
|
||||
},
|
||||
|
@ -102,6 +102,13 @@ module.exports = function(RED) {
|
||||
node.send(msg);
|
||||
});
|
||||
}
|
||||
if (node.state == "STRING") {
|
||||
node.board.on('sysex', function(e) {
|
||||
var string = new Buffer(e.data.slice(0, -1)).toString("utf8").replace(/\0/g, "");
|
||||
var msg = {payload:string, topic:"string"};
|
||||
node.send(msg);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
@ -127,9 +134,10 @@ module.exports = function(RED) {
|
||||
node.board.on('connect', function() {
|
||||
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
||||
//console.log("o",node.state,node.pin);
|
||||
node.board.pinMode(node.pin, node.state);
|
||||
|
||||
node.on("input", function(msg) {
|
||||
if (node.state === "OUTPUT") {
|
||||
node.board.pinMode(node.pin, node.state);
|
||||
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
||||
node.board.digitalWrite(node.pin, true);
|
||||
}
|
||||
@ -138,12 +146,14 @@ module.exports = function(RED) {
|
||||
}
|
||||
}
|
||||
if (node.state === "PWM") {
|
||||
node.board.pinMode(node.pin, node.state);
|
||||
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
||||
if ((msg.payload >= 0) && (msg.payload <= 255)) {
|
||||
node.board.analogWrite(node.pin, msg.payload);
|
||||
}
|
||||
}
|
||||
if (node.state === "SERVO") {
|
||||
node.board.pinMode(node.pin, node.state);
|
||||
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
||||
if ((msg.payload >= 0) && (msg.payload <= 180)) {
|
||||
node.board.servoWrite(node.pin, msg.payload);
|
||||
@ -152,6 +162,16 @@ module.exports = function(RED) {
|
||||
if (node.state === "SYSEX") {
|
||||
node.board.sysex(msg.payload);
|
||||
}
|
||||
if (node.state === "STRING") {
|
||||
var bytes = new Buffer(msg.payload.toString(), "utf8");
|
||||
var data = [];
|
||||
for (var i = 0, length = bytes.length; i < length; i++) {
|
||||
data.push(bytes[i] & 0x7F);
|
||||
data.push((bytes[i] >> 7) & 0x7F);
|
||||
}
|
||||
data.push(0);
|
||||
node.board.sysex(0x71, data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -18,12 +18,14 @@
|
||||
"state": {
|
||||
"in": {
|
||||
"digital": "Digital pin",
|
||||
"analogue": "Analogue pin"
|
||||
"analogue": "Analogue pin",
|
||||
"string": "String"
|
||||
},
|
||||
"out": {
|
||||
"digital": "Digital (0/1)",
|
||||
"analogue": "Analogue (0-255)",
|
||||
"servo": "Servo (0-180)"
|
||||
"servo": "Servo (0-180)",
|
||||
"string": "String"
|
||||
}
|
||||
},
|
||||
"tip": {
|
||||
@ -35,4 +37,4 @@
|
||||
"devnotfound": "device __dev__ not found. Trying to find board."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user