-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (99 loc) · 1.74 KB
/
index.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
const SerialPort = require('serialport');
const setOptionsOnPort = options => port => port.set(options)
const flashOnPort = port => {
port.drain(() =>{
port.write(sendSyncCommand, 'buffer', () => {
console.log("sent");
})
})
}
const startReading = port => port.on('data', data => {
console.log(data.toString());
})
const queue = [
{timeout: 150, exec: startReading},
{timeout: 200, exec: flashOnPort},
{timeout: 250, exec: flashOnPort},
{timeout: 300, exec: flashOnPort},
]
/**
* Sync command can be sent multiple times before the ESP8266 acknowledges
*/
const sendSyncCommand = [
0xc0,
0x00,
0x08,
0x24,
0x00,
0x00,
0x00,
0x00,
0x00,
0x07,
0x07,
0x12,
0x20,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0xc0
];
/**
* Check if sync is acknowledged
*
* @param {boolean} response
*/
function isAcknowledged(response) {
const expectedResponse = [
0xc0,
0x01,
0x08,
0x02,
0x00,
0x07,
0x07,
0x12,
0x20,
0x00,
0x00,
0xc0
];
const deepEqual = (assert, currentValue, index) => assert && response[index] === currentValue
return expectedResponse.reduce(deepEqual, true);
}
const port = new SerialPort('COM12', {
baudRate: 115200,
})
port.on('open', () => {
queue.forEach(sequence =>
setTimeout(() => sequence.exec(port), sequence.timeout));
})