-
Notifications
You must be signed in to change notification settings - Fork 0
/
setport.js~
206 lines (153 loc) · 4.21 KB
/
setport.js~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var gpio = require("pi-gpio");
var sys = require('sys');
var http = require('http');
var express = require('express');
var app = express();
// app route mapping
app.map = function(a, route){
route = route || '';
for (var key in a) {
switch (typeof a[key]) {
// { '/path': { ... }}
case 'object':
app.map(a[key], route + key);
break;
// get: function(){ ... }
case 'function':
console.log('%s %s', key, route);
app[key](route, a[key]);
break;
}
}
};
var ports = {
list: function(req, res){
res.send('port list');
},
get: function(req, res){
res.send('port ' + req.params.portid);
},
index: function(req, res){
res.render('index.html');
},
set: function(req, res){
res.send('Going to set port ' + req.params.portid + ' to ' + req.params.state);
setGPIO(req.params.portid,states[req.params.state], function(){
sys.puts("Setted ");
});
},
toggle: function(req, res){
res.send('toggle port ' + req.params.portid );
toggleStateFromGPIO(req.params.portid, function(){
sys.puts("Setted ");
});
},
readDirection: function(req, res){
res.send('Going to get port ' + req.params.portid );
getDirectionFromGPIO(req.params.portid, function(){
sys.puts("getted ");
});
},
readState: function(req, res){
res.send('Going to get port ' + req.params.portid );
getStateFromGPIO(req.params.portid, function(){
sys.puts("getted ");
});
},
};
// app structure
app.map({
'/':{
get: ports.index
},
'/port': {
get: ports.list,
'/:portid': {
get: ports.get,
'/set': {
'/:state': {
get: ports.set
}},
'/toggle':
{
get: ports.toggle
},
'/getdirection': {
get: ports.readDirection
},
'/getstate': {
get: ports.readState
}
}
}
});
// simple logger
app.use(function(req, res, next){
console.log('%s %s', req.method, req.url);
// console.log('%s %s', req.method, req.params.name);
next();
});
// Listen on port 80, IP defaults to 127.0.0.1
app.listen(80);
var ports = new Array(); //the libery is doing a mapping that is obsolete and is complicating everything.. now its already to late
// https://github.com/rakeshpai/pi-gpio/blob/master/pi-gpio.js
//
ports[1] = 16; // raspberry pi
ports[2] = 15; // GP22 usb 3.0 external HDD
ports[3] = 22; // GP25 Dell Monitor
ports[4] = 7; // GP4 stereo
ports[5] = 12; // GP18 hornbach lamp
ports[6] = 11; // GP17 ikea lamp
ports[7] = 18; // GP24 samsung monitor
var states = new Array(); //not used now
states[0] = 0;
states[1] = 1;
function setGPIO(pin, state, callback) {
sys.puts("Setting Pin: " + pin + " to " + state);
gpio.write(ports[pin], state);
}
function initGPIO(pin, callback) {
gpio.open(ports[pin], "output", function(err) {
gpio.read(ports[pin], function(err, value) {
sys.puts("Getting state of Pin: " + ports[pin] );
console.log('The value is ' + value);
port_states[pin] = value;
})
});
}
function getStateFromGPIO(pin, callback) {
gpio.read(ports[pin], function(err, value) {
if (err) throw err;
sys.puts("Getting state of Pin: " + ports[pin] );
console.log('The value is ' + value);
port_states[pin] = value;
})
}
function toggleStateFromGPIO(pin, callback) {
gpio.read(ports[pin], function(err, value) {
if (err){throw err}
sys.puts("Getting state of Pin: " + ports[pin] );
console.log('The value is ' + value);
if (value == 0){
console.log('The new value is 1');
gpio.write(ports[pin], 1);
}
else if(value == 1){
console.log('The new value is 0');
gpio.write(ports[pin], 0);
}
port_states[pin] = value;
});
}
function getDirectionFromGPIO(pin, callback) {
gpio.getDirection(ports[pin], function(err, value) {
sys.puts("Getting direction of Pin: " + ports[pin] );
console.log('The value is ' + value);
port_states[pin] = value;
})
}
port_states = new Array();
for( var i = 1; i <= 7; i++ ) {
//getDirectionFromGPIO(i);
initGPIO(i);
}