-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.js
140 lines (106 loc) · 2.93 KB
/
serial.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
const chalk = require('chalk')
const SerialPort = require('serialport')
const SerialCommandProtocol = require('./SerialCommandProtocol')
const ReconnectableSerialPort = require('./ReconnectableSerialPort')
const sleep = ms => new Promise((res, rej) => setTimeout(res, ms))
require('draftlog').into(console)
// SerialPort.list(console.log)
let arduinoPort = new ReconnectableSerialPort({ serialNumber: 'AH03B7U8' })
let arduino = new SerialCommandProtocol(arduinoPort)
let robotPort = new ReconnectableSerialPort({
comName: '/dev/tty.JOAO_S2_IVAN-DevB'
}, {
baudRate: 115200,
})
let meudraft = console.draft('Ola, tudo bem?')
let colors = [
['Black', 'Black'],
['White', 'White'],
['Green', 'Red'],
['Blue', 'Blue'],
['Yellow', 'Blue'],
['Yellow', 'Blue'],
['Yellow', 'White'],
['White', 'White'],
['Black', 'Black'],
]
let lines = []
for (let color of colors) {
let line = chalk['bg'+color[0]](' s ') + chalk['bg'+color[1]](' ')
// line = chalk.bgWhite(' '+line+' ')
let draft = console.draft(line)
draft.original = line
lines.push(draft)
}
;(async () => {
let test = console.draft('hmmm')
let i = 0
// for (let i = 0; i < lines.length; i++) {
while(1){
await sleep(50)
test(chalk.bgRgb((i+=2) % 255, 100, 150)(' '))
// let draft = lines[i]
// restore()
// draft(draft.original + ' <')
}
// meudraft('Atualizei!')
})()
function restore() {
for (let line of lines) {
line(line.original)
}
}
async function runArduino() {
console.log('arduino connection')
while(1) {
let now = Date.now()
let exp = now % 10
let raw = await arduino.execute(''+exp)
let res = raw.split(':')[1]
let out = `res: ${res} | ${Date.now() - now}ms`
console.log(exp == res ? chalk.green(out) : chalk.red(out))
// await arduino.execute('l')
// await sleep(100)
// await arduino.execute('h')
// await sleep(100)
}
}
async function runRobot() {
console.log('robot connection')
while(1) {
await robot.execute('r')
console.log('ended!')
await robot.execute('b')
await sleep(1000)
}
}
console.log('init')
robotPort.on('open', () => runRobot())
arduinoPort.on('ready', () => runArduino())
// arduino.on('data', (data) => {
// console.log(data)
// })
// setTimeout(() => {
// arduino.write('lol')
// }, 4000)
// const SerialPort = require('serialport')
// const Readline = SerialPort.parsers.Readline;
// const parser = new Readline();
// ;(async () => {
// let ports = await SerialPort.list()
// console.log(ports)
// let port = await new SerialPort('/dev/tty.usbserial-AH03B7U8')
// console.log('Listening...')
// port.pipe(parser)
// parser.on('data', (data) => {
// console.log('got:', data.toString())
// })
// setInterval(() => {
// port.write('ola, ' + Date.now())
// }, 500)
// port.on('close', () => {
// console.log('closed connection')
// })
// })()
// // async read() {
// // }