[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / hardware / 35-arduino.js
1 /**
2  * Copyright 2013,2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 module.exports = function(RED) {
18     "use strict";
19     var util = require("util");
20     var ArduinoFirmata = require('arduino-firmata');
21     var fs = require('fs');
22     var plat = require('os').platform();
23     var portlist = ArduinoFirmata.list(function (err, ports) {
24         portlist = ports;
25     });
26
27     // The Board Definition - this opens (and closes) the connection
28     function ArduinoNode(n) {
29         RED.nodes.createNode(this,n);
30         this.device = n.device || null;
31         this.repeat = n.repeat||25;
32         //node.log("opening connection "+this.device);
33         var node = this;
34         node.board = new ArduinoFirmata();
35         if (portlist.indexOf(node.device) === -1) {
36             node.warn("Device "+node.device+" not found");
37         }
38         else {
39             node.board.connect(node.device);
40         }
41
42         node.board.on('boardReady', function(){
43             node.log("version "+node.board.boardVersion);
44         });
45
46         node.on('close', function() {
47             if (node.board) {
48                 try {
49                     node.board.close(function() {
50                         node.log("port closed");
51                     });
52                 } catch(e) { }
53             }
54         });
55     }
56     RED.nodes.registerType("arduino-board",ArduinoNode);
57
58
59     // The Input Node
60     function DuinoNodeIn(n) {
61         RED.nodes.createNode(this,n);
62         this.buttonState = -1;
63         this.pin = n.pin;
64         this.state = n.state;
65         this.arduino = n.arduino;
66         this.serverConfig = RED.nodes.getNode(this.arduino);
67         if (typeof this.serverConfig === "object") {
68             this.board = this.serverConfig.board;
69             //this.repeat = this.serverConfig.repeat;
70             var node = this;
71             node.status({fill:"red",shape:"ring",text:"connecting"});
72
73             node.board.on('connect', function() {
74                 node.status({fill:"green",shape:"dot",text:"connected"});
75                 //console.log("i",node.state,node.pin);
76                 if (node.state == "ANALOG") {
77                     node.board.on('analogChange', function(e) {
78                         if (e.pin == node.pin) {
79                             var msg = {payload:e.value, topic:"A"+e.pin};
80                             node.send(msg);
81                         }
82                     });
83
84                 }
85                 else {
86                     node.board.pinMode(node.pin, ArduinoFirmata.INPUT);
87                     node.board.on('digitalChange', function(e) {
88                         if (e.pin == node.pin) {
89                             var msg = {payload:e.value, topic:e.pin};
90                             node.send(msg);
91                         }
92                     });
93                 }
94             });
95         }
96         else {
97             util.log("[Firmata-arduino] port not configured");
98         }
99     }
100     RED.nodes.registerType("arduino in",DuinoNodeIn);
101
102
103     // The Output Node
104     function DuinoNodeOut(n) {
105         RED.nodes.createNode(this,n);
106         this.buttonState = -1;
107         this.pin = n.pin;
108         this.state = n.state;
109         this.arduino = n.arduino;
110         this.serverConfig = RED.nodes.getNode(this.arduino);
111         if (typeof this.serverConfig === "object") {
112             this.board = this.serverConfig.board;
113             var node = this;
114             node.status({fill:"red",shape:"ring",text:"connecting"});
115
116             node.board.on('connect', function() {
117                 node.status({fill:"green",shape:"dot",text:"connected"});
118                 //console.log("o",node.state,node.pin);
119                 node.board.pinMode(node.pin, node.state);
120                 node.on("input", function(msg) {
121                     if (node.state == "OUTPUT") {
122                         if ((msg.payload == true)||(msg.payload == 1)||(msg.payload.toString().toLowerCase() == "on")) {
123                             node.board.digitalWrite(node.pin, true);
124                         }
125                         if ((msg.payload == false)||(msg.payload == 0)||(msg.payload.toString().toLowerCase() == "off")) {
126                             node.board.digitalWrite(node.pin, false);
127                         }
128                     }
129                     if (node.state == "PWM") {
130                         msg.payload = msg.payload * 1;
131                         if ((msg.payload >= 0) && (msg.payload <= 255)) {
132                             //console.log(msg.payload, node.pin);
133                             node.board.servoWrite(node.pin, msg.payload);
134                         }
135                     }
136                     if (node.state == "SERVO") {
137                         msg.payload = msg.payload * 1;
138                         if ((msg.payload >= 0) && (msg.payload <= 180)) {
139                             //console.log(msg.payload, node.pin);
140                             node.board.servoWrite(node.pin, msg.payload);
141                         }
142                     }
143                 });
144             });
145         }
146         else {
147             util.log("[Firmata-arduino] port not configured");
148         }
149     }
150     RED.nodes.registerType("arduino out",DuinoNodeOut);
151
152     RED.httpAdmin.get("/arduinoports",function(req,res) {
153         ArduinoFirmata.list(function (err, ports) {
154             //console.log(JSON.stringify(ports));
155             res.writeHead(200, {'Content-Type': 'text/plain'});
156             res.write(JSON.stringify(ports));
157             res.end();
158         });
159     });
160 }