-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
229 lines (216 loc) · 6.05 KB
/
index.js
File metadata and controls
229 lines (216 loc) · 6.05 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @file
* HTTP server to get images from a LinkSprite TTL Serial Camera.
*/
var SerialPort = require('serialport').SerialPort;
var serialPort = new SerialPort('/dev/ttyAMA0', {
baudrate: 38400
});
var fs = require('fs');
var http = require('http');
// Command Variables
var x = 00;
var y = 00;
var resetCommand = new Buffer([86, 00, 38, 00]);
var takePic = new Buffer([86, 00, 54, 01, 00]);
var readSize = new Buffer([86, 00, 52, 01, 00]);
var readPic = new Buffer([86, 00, 50, 12, 00, 10, 00, 00, 00, 00, 00, 00, x, y, 00, 10]);
// State flags
var resetState = true;
var takeState = false;
var sizeState = false;
var readState = false;
var serialReady = false;
var takingPic = false;
var lastPic = 0;
// Return data expectations for comparision.
var takeReturn = new Buffer([118, 00, 54, 00, 00]);
var sizeReturn = new Buffer([118, 00, 52, 00, 04, 00, 00]);
var readReturn = new Buffer([118, 00, 50, 00, 00]);
var resetReturn = [118, 00, 38, 00];
var returnedReset = new Array();
var returnedSize = new Array();
var returnedImage = new Array();
// Prepare the server on 8881.
http.createServer(function(req, res) {
var action = req.url;
console.log(action);
// Give the image upon request, strip off anthing after the filename since we
// add cache-breakers to the request on the client side.
if (action.substr(0, 10) == '/image.jpg') {
console.log(action);
var img = fs.readFileSync('./image.jpg');
// Don't let the client's browser store the image. We need constantly
// updated data.
res.writeHead(200, {'Content-Type': 'image/jpeg', 'Cache-Control': 'no-cache, must-revalidate'});
res.end(img, 'binary');
// We set a timeout variable. Sometimes the program flow gets stuck. If this
// has been the case for more than 7 seconds then reset the state.
var time = new Date().getTime();
if (time - lastPic > 7000) {
reload();
takingPic = false;
}
// Don't start a new flow if we are already taking a picture.
if (!takingPic) {
reload();
snapIt();
}
}
if (action == '/') {
var indexPage = fs.readFileSync('./index.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(indexPage);
}
}).listen(8881);
serialPort.open(function () {
serialReady = true;
});
// On the data receive event decide what is happening based on the returned byte
// array and then more to next state as appropriate.
serialPort.on('data', function(data) {
var newData = data.toJSON();
if (resetState) {
returnedReset = returnedReset.concat(newData);
if (
returnedReset[0] == 118 &&
returnedReset[1] == 0 &&
returnedReset[2] == 38 &&
returnedReset[3] == 0 &&
returnedReset.length == 71
) {
triggerShutter();
}
}
if (sizeState) {
returnedSize = returnedSize.concat(newData);
if (returnedSize.length == 9) {
var mem1 = returnedSize[7];
var mem2 = returnedSize[8];
getImageData(mem1, mem2);
}
}
if (readState) {
returnedImage = returnedImage.concat(newData);
if (
returnedImage[returnedImage.length - 7] == 255 &&
returnedImage[returnedImage.length - 6] == 217 &&
returnedImage[returnedImage.length - 5] == 118 &&
returnedImage[returnedImage.length - 4] == 0 &&
returnedImage[returnedImage.length - 3] == 50 &&
returnedImage[returnedImage.length - 2] == 0 &&
returnedImage[returnedImage.length - 1] == 0
) {
console.log('Writing image');
var begin = returnedImage.indexOf(255);
console.log(begin);
var end = returnedImage.lastIndexOf(255);
console.log(end);
returnedImage = returnedImage.slice(begin, end);
var image = new Buffer(returnedImage);
// Clear the previous image by filling the file with zero bytes.
var buf = new Buffer(0);
fs.writeFile('image.jpg', buf, function (err) {
if (err) throw err;
});
// Write the new image to the file.
fs.appendFile('image.jpg', image, function (err) {
if (err) throw err;
});
// Reset the global state flag.
takingPic = false;
}
}
if (data.toString() == takeReturn.toString() && takeState) {
getSize();
}
});
/**
* Start the process of taking a picture.
*/
function snapIt() {
// Prevent interfereince by setting the global state flag.
takingPic = true;
console.log('reset');
serialPort.write(resetCommand, function(err, results) {
if (err) {
console.log('err ' + err);
}
else {
console.log(results + ' bytes sent');
}
});
resetState = true;
takeState = false;
sizeState = false;
readState = false;
}
/**
* Trigger the actual command to get a new image.
*/
function triggerShutter() {
console.log('take picture');
serialPort.write(takePic, function(err, results) {
if (err) {
console.log('err ' + err);
}
else {
console.log(results + ' bytes sent');
}
});
resetState = false;
takeState = true;
sizeState = false;
readState = false;
}
/**
* Find the size of the taken image. We use this to read the memory cells.
*/
function getSize() {
console.log('getting size');
serialPort.write(readSize, function(err, results) {
if (err) {
console.log('err ' + err);
}
else {
console.log(results + ' bytes sent');
}
});
resetState = false;
takeState = false;
sizeState = true;
readState = false;
}
/**
* Get the camera to return an image as data from memory space 1 through memory
* space 2.
*/
function getImageData(m1, m2) {
console.log('reading image');
readPic[12] = m1;
readPic[13] = m2;
serialPort.write(readPic, function(err, results) {
if (err) {
console.log('err ' + err);
}
else {
console.log(results + ' bytes sent');
}
});
resetState = false;
takeState = false;
sizeState = false;
readState = true;
}
/**
* Return everything to the default state.
*/
function reload() {
resetState = true;
takeState = false;
sizeState = false;
readState = false;
returnedSize = new Array();
returnedImage = new Array();
returnedReset = new Array();
}