-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
180 lines (127 loc) · 3.1 KB
/
main.js
File metadata and controls
180 lines (127 loc) · 3.1 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
'use strict';
const async = require('async');
const SerialPort = require('serialport');
const stream = process.stdin;
const consoleClear = require('clear');
const fs = require('fs');
const musicPlayer = require('play-sound')({})
var serialPorts = [];
var selectedPort = '';
var selectedBaudRate = '';
const baudRates = ['1200', '2400', '4800', '9600', '14400', '19200', '28800', '38400', '57600', '115200', '230400'];
main(); return;
function main() {
consoleClear();
async.waterfall([
welcome,
getSerialPorts,
selectPort,
selectBaudrate,
startReading
],
function(err, result) {
console.log('end');
});
}
function welcome(callback) {
consolePrint('SerialTroz 1.0');
playSound();
printAlbatroz();
callback(null);
}
function getSerialPorts(callback) {
// console.log('getSerialPorts');
SerialPort.list().then(function(result) {
var ports = [];
for (var i = 0; i < result.length; i++) {
var port = result[i];
ports.push(port.comName);
}
serialPorts = ports;
callback(null);
});
}
function selectPort(callback) {
// console.log('selectPort');
showSelectMenu('Select USB Port:', serialPorts, function(value) {
// console.log('selected', value);
selectedPort = value;
callback(null);
});
}
function selectBaudrate(callback) {
consoleClear();
// console.log('selectPort');
showSelectMenu('Select Baudrate:', baudRates, function(value) {
// s
selectedBaudRate = Number(value);
callback(null);
});
}
function startReading(callback) {
consoleClear();
consolePrint('Start reading port: '+ selectedPort +' baudRate: '+ selectedBaudRate, true);
var port = new SerialPort(selectedPort, { baudRate: selectedBaudRate });
port.on('data', function (data) {
var data = bufferToString(data);
console.log(data);
});
callback(null);
}
function showSelectMenu(title, options, callback) {
// console.log('showSelectMenu', title, options);
consolePrint(title, true);
var list = require('select-shell')(
/* possible configs */
{
pointer: ' ▸ ',
pointerColor: 'yellow',
checked: ' ◉ ',
unchecked:' ◎ ',
checkedColor: 'blue',
msgCancel: 'No selected options!',
msgCancelColor: 'orange',
multiSelect: false,
inverse: true,
prepend: true,
disableInput: true
}
);
for (var i = 0; i < options.length; i++) {
var option = options[i];
list.option(option);
}
list.on('select', function(options) {
var option = options[0];
var value = option.value;
callback(value);
});
list.list();
}
function consolePrint(msg, breakLine=false) {
console.log(msg);
if(breakLine) {
console.log('\n');
}
}
function printAlbatroz() {
var px = fs.readFileSync('albatroz.px', 'utf8');
console.log(px);
}
function playSound() {
musicPlayer.play('music.mp3', function(err){
if (err) throw err
})
}
function stopSound() {
}
function bufferToString(buffer) {
var buff = new Buffer(buffer, 'utf8'); //no sure about this
var data = buff.toJSON().data;
var str = '';
for (var i = 0; i < data.length; i++) {
var char = data[i];
str += String.fromCharCode(char);
}
return str;
}