-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminitel.js
122 lines (102 loc) · 2.87 KB
/
minitel.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
/*
* minitel.js a dream come true in one color only (unless you have a minitel 2 !!!)
*/
var SerialPort = require('serialport').SerialPort;
var lineReader = require('line-reader');
var assert = require('assert');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
// minitel codes
// TODO implement them ...
var ESC = '\x1b';
// Control sequence introducer
var CSI = ESC+'[';
// constructor func
function Minitel(options) {
if (!(this instanceof Minitel)) return new Minitel();
EventEmitter.call(this);
var opts = {};
var self = this;
// default options
if (!options){
console.log('you should at least provide a port !!!');
}else {
opts = options;
if (!opts.txtFile){
opts.txtFile = './ascii/j5.txt';
}
if(!opts.speed){
opts.speed = 1200;
}
}
// serial com conf (DO NOT TOUCH THIS)
var serialPort = new SerialPort(opts.port, {
baudrate: opts.speed,
databits:7,
parity:'even'
});
/* minitel functions */
// write a line to the minitel
self.wrChars = function (str) {
assert(typeof str==='string');
serialPort.write(str);
};
// write a line to the minitel
self.wrLn = function (line) {
var lnLnth = line.length;
var curLine ='';
assert(typeof(line)==='string');
assert(isNumber(lnLnth));
// if the line is less thant 40 char
// we fill it with white space till 40;
if(lnLnth < 41){
var fillCount = 40 - line.length + 1;
var fill = Array(fillCount).join(' ');
curLine = line+fill;
}else{ // else we trim it to 40 char
var sF = (lnLnth -40) *-1;
line = line.slice(0, sF);
curLine = line;
}
// check that the line is exactly 40 char long
assert(curLine.length===40);
// write the line to the minitel
serialPort.write(curLine);
};
// clear the minitel output
self.clear = function(){
serialPort.write('\x0c');
};
self.beep = function () {
serialPort.write('\x07'); // da noiz
};
// read a textfile clear the screen and send its content line by line to the minitel
self.wrFile = function (file) {
self.clear();
lineReader.eachLine(file, function(line, last) {
self.wrLn(line);
if (last === true) {
return false; // stop reading
}
});
};
// when the minitel serial com is open
serialPort.on('open', function () {
console.log('Minitel is online');
self.clear();
// logs input of the minitel keyboard
serialPort.on('data', function(data) {
self.emit('keyPress', data);
});
// display the default image
self.emit('ready', 'ready');
});
// check if n is a number
function isNumber (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
// TODO understand why i can't pass the emmiter AND the proto functions
//return minitel;
}
util.inherits(Minitel, EventEmitter);
module.exports = Minitel;