-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart.js
258 lines (216 loc) · 6.49 KB
/
uart.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { EventEmitter } from 'events';
import audioCtx from './audioContext.js';
// encoder
export class UARTTransmitter {
/**
* @constructor
* @param keyer {Keyer} Keyer to be used for transmitting
* @param options {object} Options
* @param options.byteSize {number} Number of bits in byte
* @param options.bitSize {number} Length of bit in keyers time scale
* @param options.parityBits {number} Number of parity bits
* @param options.stopBits {number} Number of stop bits
*/
constructor(keyer, options) {
this._keyer = keyer;
this._byteSize = options.byteSize;
this._bitSize = options.bitSize;
this._parityBits = options.parityBits;
this._stopBits = options.stopBits;
}
/**
* Sends single byte
* @param byte {number} integer byte value
*/
send(byte) {
// eslint-disable-next-line no-unused-vars
const standbyTime = 0.1;
// eslint-disable-next-line no-unused-vars
const maxParity = (1 << this._parityBits) - 1;
const bits = [];
let parity = 0;
let time = 0;
// bits.push({ timestamp: time, value: 1 }) // standby value, high
// time += standbyTime;
bits.push({ timestamp: time, value: -1 });
time += this._bitSize;
console.log('Sending: %s', byte.toString(2));
// data bits
for (let i = 0; i < this._byteSize; i++) {
if (byte & 1) {
bits.push({ timestamp: time, value: 1 }); // mark, high
parity++;
} else {
bits.push({ timestamp: time, value: -1 }); // space, low
}
time += this._bitSize;
byte >>= 1;
}
// parity bits
for (let i = 0; i < this._parityBits; i++) {
if (parity & 1) {
bits.push({ timestamp: time, value: 1 }); // mark, high
} else {
bits.push({ timestamp: time, value: -1 }); // space, low
}
time += this._bitSize;
parity >>= 1;
}
bits.push({ timestamp: time, value: 1 }); // stop bits
bits.push({ timestamp: time + this._stopBits * this._bitSize, value: 0 });
return this._keyer.queue(bits);
}
}
// decoder
const States = {
WAIT_HIGH: 1,
WAIT_START: 2,
DECODING: 3
};
export class UARTReceiver extends EventEmitter {
/**
* @constructor
* @param dekeyer {Dekeyer} Dekeyer used for receiving
* @param options {object} Options
* @param options.byteSize {number} Number of bits in byte
* @param options.bitSize {number} Length of bit in keyers time scale
* @param options.parityBits {number} Number of parity bits
* @param options.stopBits {number} Number of stop bits
*/
constructor(dekeyer, options) {
super();
this._dekeyer = dekeyer;
this._byteSize = options.byteSize;
this._bitSize = options.bitSize;
this._parityBits = options.parityBits;
this._stopBits = options.stopBits;
if (dekeyer.currentValue !== -1) {
this._state = States.WAIT_HIGH;
} else {
this._state = States.WAIT_START;
}
this._dekeyer.on('change', this._change.bind(this));
}
_change(value, time, timeStamp) {
changes.push({
value, time: timeStamp, sample: time
});
switch (this._state) {
case States.DECODING:
break;
case States.WAIT_HIGH:
if (value === 1) { // Got high value
this._state = States.WAIT_START;
}
break;
case States.WAIT_START:
if (value === -1) { // Got start bit
this._state = States.DECODING;
this._byteStart = time;
this._decode();
} else if (value !== 1) {
console.log('Reverted to waiting high value');
this._state = States.WAIT_HIGH;
}
break;
}
}
_decode() {
// mask for start and stop bits
const ctrlMask = (((1 << this._stopBits) - 1) << (this._byteSize + this._parityBits + 1)) | 1;
const maxParity = (1 << this._parityBits) - 1; // max parity value
const parityMask = maxParity << (this._byteSize + 1); // mask for parity bits
const bitCount = this._byteSize + 1 + this._parityBits + this._stopBits;
let error = false;
let byteValue = 0;
let bitIndex = 0;
let parity = 0;
const callback = value => {
if (error) {
return;
}
if (value === 0) {
error = true;
console.log('ERROR: Got zero value');
return;
}
value = value === 1 ? 1 : 0;
if (value && bitIndex > 0 && bitIndex <= this._byteSize) {
parity++;
}
byteValue |= value << bitIndex++;
if (bitIndex >= bitCount) {
if ((byteValue & ctrlMask) === ctrlMask - 1) {
console.log('Start and stop bits OK');
} else {
console.log('Start and stop bits NOT OK');
}
if ((byteValue & parityMask) >> (this._byteSize + 1) === (parity & maxParity)) {
console.log('Parity bits OK');
} else {
console.log('Parity bits NOT OK: parity bit: %d; byte parity: %d', (byteValue & parityMask) >> (this._byteSize + 1), parity);
}
byteValue >>= 1;
byteValue &= (1 << this._byteSize) - 1;
console.log('Got byte: %s', byteValue);
this._state = this._dekeyer.currentValue !== 1 ? States.WAIT_HIGH : States.WAIT_START;
}
};
for (let i = 0; i < bitCount; i++) {
const sampleOffset = this._byteStart + i * this._bitSize + this._bitSize / 2;
samplings.push({ sample: sampleOffset });
this._dekeyer.once(`${sampleOffset}`, callback);
}
}
}
// debug
const changes = [];
const samplings = [];
function draw() {
requestAnimationFrame(draw);
const period = 8000; // in milliseconds
const time = performance.now() - period;
const e = document.getElementById('fsk-input');
const ctx = e.getContext('2d');
ctx.strokeStyle = 'blue';
while (changes[1] && changes[1].time < time - period) {
changes.shift();
}
let changeIndex = 0;
let currentValue = 0;
let nextChange = changes[changeIndex];
ctx.clearRect(0, 0, e.width, e.height);
ctx.beginPath();
for (let i = 0; i < e.width; i++) {
const timeScale = time + period * i / e.width;
while (nextChange && nextChange.time <= timeScale) {
currentValue = nextChange.value;
nextChange = changes[changeIndex++];
}
if (currentValue) {
ctx.moveTo(i, e.height / 2);
ctx.lineTo(i, currentValue > 0 ? 0 : e.height);
}
}
ctx.stroke();
if (changes[0]) {
const referenceSample = changes[0].sample;
const referenceTime = changes[0].time;
const referenceTimeOffset = referenceTime - time;
const referenceSampleOffset = referenceTimeOffset * audioCtx.sampleRate / 1000;
const firstSample = referenceSample - referenceSampleOffset;
const totalSamples = period * audioCtx.sampleRate / 1000;
ctx.strokeStyle = 'red';
ctx.beginPath();
for (const sampling of samplings) {
const x = e.width * (sampling.sample - firstSample) / totalSamples;
ctx.moveTo(x, 0);
ctx.lineTo(x, e.height);
}
ctx.stroke();
while (samplings[0] && samplings[0].sample < firstSample) {
samplings.shift();
}
}
}
draw();